1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//! Vortex Indicator.
use std::collections::VecDeque;
use crate::error::{Error, Result};
use crate::ohlcv::Candle;
use crate::traits::Indicator;
/// Vortex Indicator output: the two directional movement lines.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct VortexOutput {
/// `VI+` — strength of upward (positive) vortex movement.
pub plus: f64,
/// `VI−` — strength of downward (negative) vortex movement.
pub minus: f64,
}
/// Vortex Indicator — Botes & Siepman's pair of oscillators (`VI+`, `VI−`) that
/// capture the relationship between two consecutive bars.
///
/// Two "vortex movements" measure how far price travelled against the opposite
/// extreme of the previous bar; each is normalised by the summed true range:
///
/// ```text
/// VM+_t = |high_t − low_{t−1}|
/// VM−_t = |low_t − high_{t−1}|
/// VI+ = Σ VM+ over n / Σ TR over n
/// VI− = Σ VM− over n / Σ TR over n
/// ```
///
/// `VI+` crossing above `VI−` is a bullish signal, the reverse a bearish one;
/// the wider the gap, the stronger the trend. A fully flat window (zero true
/// range) reports `(0, 0)`.
///
/// # Example
///
/// ```
/// use wickra_core::{Candle, Indicator, Vortex};
///
/// let mut indicator = Vortex::new(14).unwrap();
/// let mut last = None;
/// for i in 0..80 {
/// let base = 100.0 + i as f64;
/// let candle =
/// Candle::new(base, base + 2.0, base - 2.0, base + 1.0, 10.0, i64::from(i)).unwrap();
/// last = indicator.update(candle);
/// }
/// assert!(last.is_some());
/// ```
#[derive(Debug, Clone)]
pub struct Vortex {
period: usize,
prev: Option<Candle>,
/// Rolling window of `(VM+, VM−, TR)` triples.
window: VecDeque<(f64, f64, f64)>,
sum_vm_plus: f64,
sum_vm_minus: f64,
sum_tr: f64,
last: Option<VortexOutput>,
}
impl Vortex {
/// Construct a new Vortex Indicator with the given period.
///
/// # Errors
///
/// Returns [`Error::PeriodZero`] if `period == 0`.
pub fn new(period: usize) -> Result<Self> {
if period == 0 {
return Err(Error::PeriodZero);
}
Ok(Self {
period,
prev: None,
window: VecDeque::with_capacity(period),
sum_vm_plus: 0.0,
sum_vm_minus: 0.0,
sum_tr: 0.0,
last: None,
})
}
/// Configured period.
pub const fn period(&self) -> usize {
self.period
}
/// Current value if available.
pub const fn value(&self) -> Option<VortexOutput> {
self.last
}
}
impl Indicator for Vortex {
type Input = Candle;
type Output = VortexOutput;
fn update(&mut self, candle: Candle) -> Option<VortexOutput> {
let Some(prev) = self.prev else {
// The first bar has no predecessor to measure against.
self.prev = Some(candle);
return None;
};
let vm_plus = (candle.high - prev.low).abs();
let vm_minus = (candle.low - prev.high).abs();
let tr = candle.true_range(Some(prev.close));
self.prev = Some(candle);
if self.window.len() == self.period {
let (old_p, old_m, old_tr) = self.window.pop_front().expect("window is non-empty");
self.sum_vm_plus -= old_p;
self.sum_vm_minus -= old_m;
self.sum_tr -= old_tr;
}
self.window.push_back((vm_plus, vm_minus, tr));
self.sum_vm_plus += vm_plus;
self.sum_vm_minus += vm_minus;
self.sum_tr += tr;
if self.window.len() < self.period {
return None;
}
let out = if self.sum_tr == 0.0 {
// A perfectly flat window has no range to normalise against.
VortexOutput {
plus: 0.0,
minus: 0.0,
}
} else {
VortexOutput {
plus: self.sum_vm_plus / self.sum_tr,
minus: self.sum_vm_minus / self.sum_tr,
}
};
self.last = Some(out);
Some(out)
}
fn reset(&mut self) {
self.prev = None;
self.window.clear();
self.sum_vm_plus = 0.0;
self.sum_vm_minus = 0.0;
self.sum_tr = 0.0;
self.last = None;
}
fn warmup_period(&self) -> usize {
// The first VM/TR triple needs a previous bar, then the window fills.
self.period + 1
}
fn is_ready(&self) -> bool {
self.last.is_some()
}
fn name(&self) -> &'static str {
"Vortex"
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::traits::BatchExt;
use approx::assert_relative_eq;
fn candle(open: f64, high: f64, low: f64, close: f64, ts: i64) -> Candle {
Candle::new(open, high, low, close, 1.0, ts).unwrap()
}
#[test]
fn new_rejects_zero_period() {
assert!(matches!(Vortex::new(0), Err(Error::PeriodZero)));
}
/// Cover the const accessors `period` / `value` (84-91) and the
/// Indicator-impl `name` body (157-159). `warmup_period` is covered
/// elsewhere.
#[test]
fn accessors_and_metadata() {
let mut v = Vortex::new(14).unwrap();
assert_eq!(v.period(), 14);
assert_eq!(v.name(), "Vortex");
assert!(v.value().is_none());
let warmup = i64::try_from(v.warmup_period()).unwrap();
let candles: Vec<Candle> = (0..warmup)
.map(|i| {
let p = 100.0 + (i as f64 * 0.3).sin() * 5.0;
Candle::new(p, p + 1.0, p - 1.0, p, 1.0, i).unwrap()
})
.collect();
for c in &candles {
v.update(*c);
}
assert!(v.value().is_some());
}
#[test]
fn reference_values() {
// Vortex(2) over three explicit candles (high, low, close):
// c1 = (10, 8, 9), c2 = (12, 9, 11), c3 = (13, 11, 12).
// bar 2: VM+ = |12-8| = 4, VM- = |9-10| = 1, TR = 3.
// bar 3: VM+ = |13-9| = 4, VM- = |11-12| = 1, TR = 2.
// window sums: VM+ = 8, VM- = 2, TR = 5 -> VI+ = 1.6, VI- = 0.4.
let candles = [
candle(9.0, 10.0, 8.0, 9.0, 0),
candle(10.0, 12.0, 9.0, 11.0, 1),
candle(12.0, 13.0, 11.0, 12.0, 2),
];
let mut v = Vortex::new(2).unwrap();
let out = v.batch(&candles);
assert_eq!(v.warmup_period(), 3);
assert_eq!(out[0], None);
assert_eq!(out[1], None);
let o = out[2].unwrap();
assert_relative_eq!(o.plus, 1.6, epsilon = 1e-12);
assert_relative_eq!(o.minus, 0.4, epsilon = 1e-12);
}
#[test]
fn perfectly_flat_market_yields_zero() {
let mut v = Vortex::new(5).unwrap();
let candles: Vec<Candle> = (0..20).map(|i| candle(10.0, 10.0, 10.0, 10.0, i)).collect();
for o in v.batch(&candles).into_iter().flatten() {
assert_relative_eq!(o.plus, 0.0, epsilon = 1e-12);
assert_relative_eq!(o.minus, 0.0, epsilon = 1e-12);
}
}
#[test]
fn outputs_are_non_negative() {
let mut v = Vortex::new(14).unwrap();
let candles: Vec<Candle> = (0..120)
.map(|i| {
let mid = 100.0 + (i as f64 * 0.3).sin() * 10.0;
candle(mid, mid + 3.0, mid - 3.0, mid + 1.0, i)
})
.collect();
for o in v.batch(&candles).into_iter().flatten() {
assert!(o.plus >= 0.0 && o.minus >= 0.0, "negative VI: {o:?}");
}
}
#[test]
fn reset_clears_state() {
let mut v = Vortex::new(5).unwrap();
let candles: Vec<Candle> = (0..20)
.map(|i| candle(100.0, 102.0, 98.0, 101.0, i))
.collect();
v.batch(&candles);
assert!(v.is_ready());
v.reset();
assert!(!v.is_ready());
assert_eq!(v.update(candles[0]), None);
}
#[test]
fn batch_equals_streaming() {
let candles: Vec<Candle> = (0..80)
.map(|i| {
let mid = 100.0 + (i as f64 * 0.35).sin() * 9.0;
candle(mid, mid + 2.5, mid - 2.5, mid + 0.5, i)
})
.collect();
let batch = Vortex::new(14).unwrap().batch(&candles);
let mut b = Vortex::new(14).unwrap();
let streamed: Vec<_> = candles.iter().map(|c| b.update(*c)).collect();
assert_eq!(batch, streamed);
}
}