Skip to main content

wickra_core/indicators/
smi.rs

1//! Stochastic Momentum Index (SMI).
2
3use std::collections::VecDeque;
4
5use crate::error::{Error, Result};
6use crate::indicators::ema::Ema;
7use crate::ohlcv::Candle;
8use crate::traits::Indicator;
9
10/// William Blau's Stochastic Momentum Index — a doubly-smoothed,
11/// `±100`-bounded oscillator built from the close's distance to the centre
12/// of the recent high-low range.
13///
14/// Over the lookback `period`, let `HH = max(high)`, `LL = min(low)`,
15/// `C = (HH + LL) / 2` and `R = HH - LL`. The raw displacement is
16/// `d_t = close_t - C_t`. Both `d` and `R` are smoothed twice with `EMA`s,
17/// then combined into the bounded reading:
18///
19/// ```text
20/// D_smoothed  = EMA(EMA(d, d_period), d2_period)
21/// HL_smoothed = EMA(EMA(R, d_period), d2_period)
22/// SMI         = 100 · D_smoothed / (HL_smoothed / 2)
23/// ```
24///
25/// Blau's recommended defaults are `(period = 5, d = 3, d2 = 3)`. Wickra
26/// publishes the SMI value only; the optional signal `EMA(SMI, k)` is left
27/// to the consumer via `Chain` / their own `Ema`.
28///
29/// # Example
30///
31/// ```
32/// use wickra_core::{Candle, Indicator, Smi};
33///
34/// let mut smi = Smi::new(5, 3, 3).unwrap();
35/// let mut last = None;
36/// for i in 0..40 {
37///     let p = 100.0 + f64::from(i);
38///     let candle = Candle::new(p, p + 1.0, p - 1.0, p, 1.0, i64::from(i)).unwrap();
39///     last = smi.update(candle);
40/// }
41/// assert!(last.is_some());
42/// ```
43#[derive(Debug, Clone)]
44pub struct Smi {
45    period: usize,
46    d_period: usize,
47    d2_period: usize,
48    highs: VecDeque<f64>,
49    lows: VecDeque<f64>,
50    ema_d1: Ema,
51    ema_d2: Ema,
52    ema_r1: Ema,
53    ema_r2: Ema,
54    current: Option<f64>,
55}
56
57impl Smi {
58    /// # Errors
59    /// Returns [`Error::PeriodZero`] if any period is zero.
60    pub fn new(period: usize, d_period: usize, d2_period: usize) -> Result<Self> {
61        if period == 0 || d_period == 0 || d2_period == 0 {
62            return Err(Error::PeriodZero);
63        }
64        Ok(Self {
65            period,
66            d_period,
67            d2_period,
68            highs: VecDeque::with_capacity(period),
69            lows: VecDeque::with_capacity(period),
70            ema_d1: Ema::new(d_period)?,
71            ema_d2: Ema::new(d2_period)?,
72            ema_r1: Ema::new(d_period)?,
73            ema_r2: Ema::new(d2_period)?,
74            current: None,
75        })
76    }
77
78    /// Blau's recommended defaults `(period = 5, d = 3, d2 = 3)`.
79    pub fn classic() -> Self {
80        Self::new(5, 3, 3).expect("classic SMI parameters are valid")
81    }
82
83    /// Configured `(period, d_period, d2_period)`.
84    pub const fn periods(&self) -> (usize, usize, usize) {
85        (self.period, self.d_period, self.d2_period)
86    }
87}
88
89impl Indicator for Smi {
90    type Input = Candle;
91    type Output = f64;
92
93    fn update(&mut self, candle: Candle) -> Option<f64> {
94        if self.highs.len() == self.period {
95            self.highs.pop_front();
96            self.lows.pop_front();
97        }
98        self.highs.push_back(candle.high);
99        self.lows.push_back(candle.low);
100        if self.highs.len() < self.period {
101            return None;
102        }
103        let hh = self.highs.iter().copied().fold(f64::NEG_INFINITY, f64::max);
104        let ll = self.lows.iter().copied().fold(f64::INFINITY, f64::min);
105        let center = f64::midpoint(hh, ll);
106        let displacement = candle.close - center;
107        let range = hh - ll;
108
109        // Feed every EMA on every candle so both stacks warm in parallel —
110        // gating the range stack behind the displacement stack would starve
111        // it by one input.
112        let d1 = self.ema_d1.update(displacement);
113        let r1 = self.ema_r1.update(range);
114        let d2 = d1.and_then(|x| self.ema_d2.update(x));
115        let r2 = r1.and_then(|x| self.ema_r2.update(x));
116        let (d2, r2) = (d2?, r2?);
117
118        if r2 <= 0.0 {
119            // Window where the smoothed range collapses to zero: the formula
120            // is undefined. Hold the previous reading rather than emit inf.
121            return self.current;
122        }
123        let value = 100.0 * d2 / (r2 / 2.0);
124        self.current = Some(value);
125        Some(value)
126    }
127
128    fn reset(&mut self) {
129        self.highs.clear();
130        self.lows.clear();
131        self.ema_d1.reset();
132        self.ema_d2.reset();
133        self.ema_r1.reset();
134        self.ema_r2.reset();
135        self.current = None;
136    }
137
138    fn warmup_period(&self) -> usize {
139        // The high-low window needs `period` candles; then both EMA stacks
140        // need `d_period + d2_period - 1` more values to fully warm up.
141        self.period + self.d_period + self.d2_period - 2
142    }
143
144    fn is_ready(&self) -> bool {
145        self.current.is_some()
146    }
147
148    fn name(&self) -> &'static str {
149        "SMI"
150    }
151}
152
153#[cfg(test)]
154mod tests {
155    use super::*;
156    use crate::traits::BatchExt;
157    use approx::assert_relative_eq;
158
159    fn candle(high: f64, low: f64, close: f64, ts: i64) -> Candle {
160        Candle::new(close, high, low, close, 1.0, ts).unwrap()
161    }
162
163    #[test]
164    fn rejects_zero_period() {
165        assert!(matches!(Smi::new(0, 3, 3), Err(Error::PeriodZero)));
166        assert!(matches!(Smi::new(5, 0, 3), Err(Error::PeriodZero)));
167        assert!(matches!(Smi::new(5, 3, 0), Err(Error::PeriodZero)));
168    }
169
170    #[test]
171    fn accessors_and_metadata() {
172        let smi = Smi::new(5, 3, 3).unwrap();
173        assert_eq!(smi.periods(), (5, 3, 3));
174        assert_eq!(smi.warmup_period(), 9);
175        assert_eq!(smi.name(), "SMI");
176    }
177
178    #[test]
179    fn classic_factory() {
180        let smi = Smi::classic();
181        assert_eq!(smi.periods(), (5, 3, 3));
182    }
183
184    #[test]
185    fn close_at_high_pushes_toward_plus_100() {
186        // Every candle's close equals its high in a rising series: the
187        // displacement is at the top of the range every bar, so SMI sits in
188        // the strongly positive region. After enough double-smoothing it
189        // approaches the upper bound.
190        let mut smi = Smi::classic();
191        let mut last = None;
192        for i in 0..80 {
193            let h = 100.0 + f64::from(i);
194            let l = h - 2.0;
195            last = smi.update(candle(h, l, h, i64::from(i)));
196        }
197        let v = last.expect("SMI is warm");
198        assert!(
199            v > 50.0,
200            "close-at-high series should drive SMI well above 0: {v}"
201        );
202    }
203
204    #[test]
205    fn close_at_low_pushes_toward_minus_100() {
206        let mut smi = Smi::classic();
207        let mut last = None;
208        for i in 0..80 {
209            let h = 100.0 - f64::from(i);
210            let l = h - 2.0;
211            last = smi.update(candle(h, l, l, i64::from(i)));
212        }
213        let v = last.expect("SMI is warm");
214        assert!(
215            v < -50.0,
216            "close-at-low series should drive SMI well below 0: {v}"
217        );
218    }
219
220    #[test]
221    fn warmup_emits_first_value_at_warmup_period() {
222        let mut smi = Smi::new(3, 2, 2).unwrap();
223        // period 3 + d 2 + d2 2 - 2 = 5.
224        assert_eq!(smi.warmup_period(), 5);
225        let mut got = None;
226        for i in 0..5 {
227            got = smi.update(candle(11.0, 9.0, 10.0, i));
228        }
229        assert!(got.is_some());
230    }
231
232    #[test]
233    fn flat_close_yields_zero_displacement() {
234        // Every close is exactly at the centre of the range -> displacement
235        // is 0 every bar -> SMI converges to 0.
236        let mut smi = Smi::classic();
237        let mut last = None;
238        for i in 0..60 {
239            // High and low straddle a constant close.
240            last = smi.update(candle(11.0, 9.0, 10.0, i));
241        }
242        let v = last.unwrap();
243        assert_relative_eq!(v, 0.0, epsilon = 1e-12);
244    }
245
246    #[test]
247    fn batch_equals_streaming() {
248        let candles: Vec<Candle> = (0..80_i64)
249            .map(|i| {
250                let c = 100.0 + (i as f64 * 0.3).sin() * 8.0;
251                candle(c + 1.0, c - 1.0, c, i)
252            })
253            .collect();
254        let batch = Smi::classic().batch(&candles);
255        let mut b = Smi::classic();
256        let streamed: Vec<_> = candles.iter().map(|c| b.update(*c)).collect();
257        assert_eq!(batch, streamed);
258    }
259
260    #[test]
261    fn reset_clears_state() {
262        let mut smi = Smi::classic();
263        for i in 0..40 {
264            smi.update(candle(11.0, 9.0, 10.0, i));
265        }
266        assert!(smi.is_ready());
267        smi.reset();
268        assert!(!smi.is_ready());
269    }
270
271    #[test]
272    fn zero_range_holds_previous_value() {
273        // High == low on every bar -> instantaneous range is zero, the
274        // EMA of (range / 2) settles to zero, so `r2 <= 0.0` after warmup
275        // and the indicator must hold its previous value (None here, since
276        // r2 was zero from the very first warm bar) rather than divide by
277        // zero.
278        let mut smi = Smi::new(3, 2, 2).unwrap();
279        // warmup_period = 3 + 2 + 2 - 2 = 5; feed warmup + 2 extra bars.
280        for i in 0..7 {
281            let v = smi.update(candle(10.0, 10.0, 10.0, i));
282            assert_eq!(v, None, "zero-range SMI must hold None, got {v:?}");
283        }
284    }
285}