Skip to main content

wickra_core/indicators/
rwi.rs

1//! Random Walk Index (RWI).
2
3use std::collections::VecDeque;
4
5use crate::error::{Error, Result};
6use crate::ohlcv::Candle;
7use crate::traits::Indicator;
8
9/// Random Walk Index output: the bullish (high) and bearish (low) lines.
10#[derive(Debug, Clone, Copy, PartialEq)]
11pub struct RwiOutput {
12    /// `RWI_High` — strength of the trend up vs. a random walk.
13    pub high: f64,
14    /// `RWI_Low` — strength of the trend down vs. a random walk.
15    pub low: f64,
16}
17
18/// Mike Poulos' Random Walk Index — a trend-vs.-random-walk indicator that
19/// asks "how many standard deviations away from a random walk is the current
20/// move?".
21///
22/// For each lookback `i ∈ [2, period]`, RWI computes the ratio of the actual
23/// price displacement over `i` bars to the expected displacement of a random
24/// walk of the same length:
25///
26/// ```text
27/// RWI_High_t(i) = (high_t  − low_{t-i+1})  / (ATR_i(t) * sqrt(i))
28/// RWI_Low_t(i)  = (high_{t-i+1} − low_t)   / (ATR_i(t) * sqrt(i))
29/// ```
30///
31/// where `ATR_i(t)` is the simple average of true-range over the most recent
32/// `i` bars. The reported `RWI_High_t` / `RWI_Low_t` are the maxima of these
33/// ratios across all lookbacks `i ∈ [2, period]`.
34///
35/// `RWI_High` crossing above `RWI_Low` and exceeding 1 (`> 2` is the typical
36/// strong-trend threshold) signals an uptrend dominating random-walk; the
37/// mirror situation flags a downtrend. When both lines are below 1, neither
38/// direction beats a random walk and the market is read as ranging.
39///
40/// The first output is emitted after `period` candles (the second one provides
41/// the first `period = 2` lookback, so the indicator emits at index
42/// `period - 1`).
43///
44/// # Example
45///
46/// ```
47/// use wickra_core::{Candle, Indicator, Rwi};
48///
49/// let mut indicator = Rwi::new(14).unwrap();
50/// let mut last = None;
51/// for i in 0..80 {
52///     let base = 100.0 + f64::from(i);
53///     let candle =
54///         Candle::new(base, base + 2.0, base - 2.0, base + 1.0, 10.0, i64::from(i)).unwrap();
55///     last = indicator.update(candle);
56/// }
57/// assert!(last.is_some());
58/// ```
59#[derive(Debug, Clone)]
60pub struct Rwi {
61    period: usize,
62    /// Rolling window of the most recent `period` candles (oldest at the front).
63    candles: VecDeque<Candle>,
64    /// Rolling window of `period` true-range values aligned with `candles`
65    /// after the first bar (so `tr[0]` corresponds to `candles[1]`).
66    trs: VecDeque<f64>,
67    last: Option<RwiOutput>,
68}
69
70impl Rwi {
71    /// Construct a new RWI with the given lookback period.
72    ///
73    /// # Errors
74    ///
75    /// Returns [`Error::PeriodZero`] if `period == 0`.
76    /// Returns [`Error::InvalidPeriod`] if `period < 2` — RWI's shortest
77    /// lookback is `i = 2`, so a one-bar window would emit nothing.
78    pub fn new(period: usize) -> Result<Self> {
79        if period == 0 {
80            return Err(Error::PeriodZero);
81        }
82        if period < 2 {
83            return Err(Error::InvalidPeriod {
84                message: "RWI requires period >= 2",
85            });
86        }
87        Ok(Self {
88            period,
89            candles: VecDeque::with_capacity(period),
90            trs: VecDeque::with_capacity(period),
91            last: None,
92        })
93    }
94
95    /// Configured period.
96    pub const fn period(&self) -> usize {
97        self.period
98    }
99
100    /// Current value if available.
101    pub const fn value(&self) -> Option<RwiOutput> {
102        self.last
103    }
104}
105
106impl Indicator for Rwi {
107    type Input = Candle;
108    type Output = RwiOutput;
109
110    fn update(&mut self, candle: Candle) -> Option<RwiOutput> {
111        // Compute the true range of this candle vs. the previous close (if any),
112        // then slide the windows.
113        let tr = if let Some(prev) = self.candles.back() {
114            candle.true_range(Some(prev.close))
115        } else {
116            candle.high - candle.low
117        };
118
119        if self.candles.len() == self.period {
120            self.candles.pop_front();
121        }
122        self.candles.push_back(candle);
123
124        // `trs` aligns with `candles` from index 1 onward; only push once we
125        // have at least one previous candle (the bar's TR-vs-prev is what we
126        // store). With the first bar in `candles`, no TR is recorded yet.
127        if self.candles.len() >= 2 {
128            if self.trs.len() == self.period - 1 {
129                self.trs.pop_front();
130            }
131            self.trs.push_back(tr);
132        }
133
134        // Need a full `period` candles before we can scan lookbacks i ∈ [2,period].
135        if self.candles.len() < self.period {
136            return None;
137        }
138
139        // Slice access for indexed maths.
140        let candles: Vec<&Candle> = self.candles.iter().collect();
141        let trs: Vec<f64> = self.trs.iter().copied().collect();
142        let n = candles.len(); // == self.period
143        let last_high = candles[n - 1].high;
144        let last_low = candles[n - 1].low;
145
146        let mut rwi_high = 0.0_f64;
147        let mut rwi_low = 0.0_f64;
148        // For lookback i in [2, period]: compare bar `n - 1` to bar `n - i`.
149        // The TRs covered are those at trs indices [n - i .. n - 1], which is
150        // `i - 1` TR values (TR at index n - i is the TR of candle n - i + 1
151        // vs. candle n - i, the first TR contributing to the i-bar ATR... or
152        // strictly the ATR over the i-bar window is the mean of the i-1 TRs
153        // _between_ those bars). We use the i-1-TR mean to keep the indicator
154        // strictly causal.
155        for i in 2..=self.period {
156            // Trs slice indices (within trs Vec): start = n - i, end = n - 1 (excl.).
157            // trs has length n - 1; trs[k] = TR of candle k+1 vs candle k.
158            // count = i - 1, which is >= 1 for i >= 2.
159            let tr_start = n - i;
160            let tr_end = n - 1;
161            let count = tr_end - tr_start;
162            let atr_i: f64 = trs[tr_start..tr_end].iter().sum::<f64>() / (count as f64);
163            let denom = atr_i * (i as f64).sqrt();
164            if denom == 0.0 {
165                continue;
166            }
167            let old_low = candles[n - i].low;
168            let old_high = candles[n - i].high;
169            let h = (last_high - old_low) / denom;
170            let l = (old_high - last_low) / denom;
171            if h > rwi_high {
172                rwi_high = h;
173            }
174            if l > rwi_low {
175                rwi_low = l;
176            }
177        }
178
179        let out = RwiOutput {
180            high: rwi_high,
181            low: rwi_low,
182        };
183        self.last = Some(out);
184        Some(out)
185    }
186
187    fn reset(&mut self) {
188        self.candles.clear();
189        self.trs.clear();
190        self.last = None;
191    }
192
193    fn warmup_period(&self) -> usize {
194        // First emission once the rolling window holds `period` candles.
195        self.period
196    }
197
198    fn is_ready(&self) -> bool {
199        self.last.is_some()
200    }
201
202    fn name(&self) -> &'static str {
203        "RWI"
204    }
205}
206
207#[cfg(test)]
208mod tests {
209    use super::*;
210    use crate::traits::BatchExt;
211
212    fn candle(h: f64, l: f64, c: f64, ts: i64) -> Candle {
213        Candle::new(c, h, l, c, 1.0, ts).unwrap()
214    }
215
216    #[test]
217    fn rejects_zero_period() {
218        assert!(matches!(Rwi::new(0), Err(Error::PeriodZero)));
219    }
220
221    #[test]
222    fn rejects_period_one() {
223        assert!(matches!(Rwi::new(1), Err(Error::InvalidPeriod { .. })));
224    }
225
226    #[test]
227    fn accessors_and_metadata() {
228        let mut r = Rwi::new(14).unwrap();
229        assert_eq!(r.period(), 14);
230        assert_eq!(r.warmup_period(), 14);
231        assert_eq!(r.name(), "RWI");
232        assert!(r.value().is_none());
233        for i in 0..30_i64 {
234            let p = 100.0 + (i as f64);
235            r.update(candle(p + 1.0, p - 1.0, p, i));
236        }
237        assert!(r.value().is_some());
238    }
239
240    #[test]
241    fn first_emission_at_warmup_period() {
242        let candles: Vec<Candle> = (0..40_i64)
243            .map(|i| {
244                let p = 100.0 + ((i as f64) * 0.3).sin() * 5.0;
245                candle(p + 1.0, p - 1.0, p, i)
246            })
247            .collect();
248        let mut r = Rwi::new(5).unwrap();
249        let out = r.batch(&candles);
250        for v in out.iter().take(4) {
251            assert!(v.is_none());
252        }
253        assert!(out[4].is_some());
254    }
255
256    #[test]
257    fn constant_series_yields_zero_outputs() {
258        // Flat market: ATR is zero, so all lookbacks short-circuit on the
259        // denom-zero guard and both lines stay at 0.
260        let candles: Vec<Candle> = (0..30_i64).map(|i| candle(10.0, 10.0, 10.0, i)).collect();
261        let mut r = Rwi::new(5).unwrap();
262        let last = r.batch(&candles).into_iter().flatten().last().unwrap();
263        assert_eq!(last.high, 0.0);
264        assert_eq!(last.low, 0.0);
265    }
266
267    #[test]
268    fn pure_uptrend_high_dominates_low() {
269        // A monotone uptrend should produce RWI_High >> RWI_Low.
270        let candles: Vec<Candle> = (0..40_i64)
271            .map(|i| {
272                let base = 100.0 + (i as f64) * 2.0;
273                candle(base + 1.0, base - 0.5, base + 0.5, i)
274            })
275            .collect();
276        let mut r = Rwi::new(14).unwrap();
277        let last = r.batch(&candles).into_iter().flatten().last().unwrap();
278        assert!(
279            last.high > last.low,
280            "RWI_High {} should exceed RWI_Low {}",
281            last.high,
282            last.low
283        );
284        assert!(
285            last.high > 1.0,
286            "strong uptrend should exceed 1, got {}",
287            last.high
288        );
289    }
290
291    #[test]
292    fn pure_downtrend_low_dominates_high() {
293        let candles: Vec<Candle> = (0..40_i64)
294            .rev()
295            .map(|i| {
296                let base = 100.0 + (i as f64) * 2.0;
297                candle(base + 0.5, base - 1.0, base - 0.5, 40 - i)
298            })
299            .collect();
300        let mut r = Rwi::new(14).unwrap();
301        let last = r.batch(&candles).into_iter().flatten().last().unwrap();
302        assert!(last.low > last.high);
303        assert!(last.low > 1.0);
304    }
305
306    #[test]
307    fn outputs_non_negative() {
308        let candles: Vec<Candle> = (0..120_i64)
309            .map(|i| {
310                let p = 100.0 + ((i as f64) * 0.25).sin() * 6.0;
311                candle(p + 1.5, p - 1.5, p, i)
312            })
313            .collect();
314        let mut r = Rwi::new(10).unwrap();
315        for v in r.batch(&candles).into_iter().flatten() {
316            assert!(v.high >= 0.0 && v.low >= 0.0);
317            assert!(v.high.is_finite() && v.low.is_finite());
318        }
319    }
320
321    #[test]
322    fn batch_equals_streaming() {
323        let candles: Vec<Candle> = (0..80_i64)
324            .map(|i| {
325                let p = 100.0 + ((i as f64) * 0.3).sin() * 5.0;
326                candle(p + 1.0, p - 1.0, p, i)
327            })
328            .collect();
329        let mut a = Rwi::new(7).unwrap();
330        let mut b = Rwi::new(7).unwrap();
331        assert_eq!(
332            a.batch(&candles),
333            candles.iter().map(|c| b.update(*c)).collect::<Vec<_>>()
334        );
335    }
336
337    #[test]
338    fn reset_clears_state() {
339        let candles: Vec<Candle> = (0..30_i64).map(|i| candle(11.0, 9.0, 10.0, i)).collect();
340        let mut r = Rwi::new(5).unwrap();
341        r.batch(&candles);
342        assert!(r.is_ready());
343        r.reset();
344        assert!(!r.is_ready());
345        assert_eq!(r.update(candles[0]), None);
346    }
347}