Skip to main content

wickra_core/indicators/
stochastic.rs

1//! Stochastic Oscillator (%K and %D).
2
3use std::collections::VecDeque;
4
5use crate::error::{Error, Result};
6use crate::indicators::sma::Sma;
7use crate::ohlcv::Candle;
8use crate::traits::Indicator;
9
10/// Stochastic Oscillator output.
11#[derive(Debug, Clone, Copy, PartialEq)]
12pub struct StochasticOutput {
13    /// Raw %K: `100 * (close - LL) / (HH - LL)` over the lookback.
14    pub k: f64,
15    /// %D: SMA of %K over the smoothing period.
16    pub d: f64,
17}
18
19/// Fast Stochastic Oscillator.
20///
21/// Maintains rolling highest-high and lowest-low over the lookback period via a
22/// monotonic deque, giving O(1) amortized updates. %D is an SMA of the %K series.
23///
24/// # Example
25///
26/// ```
27/// use wickra_core::{Candle, Indicator, Stochastic};
28///
29/// let mut indicator = Stochastic::new(5, 3).unwrap();
30/// let mut last = None;
31/// for i in 0..80 {
32///     let base = 100.0 + f64::from(i);
33///     let candle =
34///         Candle::new(base, base + 2.0, base - 2.0, base + 1.0, 10.0, i64::from(i)).unwrap();
35///     last = indicator.update(candle);
36/// }
37/// assert!(last.is_some());
38/// ```
39#[derive(Debug, Clone)]
40pub struct Stochastic {
41    k_period: usize,
42    d_period: usize,
43    candles: VecDeque<Candle>,
44    // Monotonic deques over candle indices in the rolling window.
45    hh_idx: VecDeque<usize>, // indices of candidates for highest high (front = current max)
46    ll_idx: VecDeque<usize>, // indices of candidates for lowest low (front = current min)
47    // Absolute count of candles ever ingested. Used so monotonic-deque indices stay unique.
48    count: usize,
49    d_sma: Sma,
50    last_k: Option<f64>,
51}
52
53impl Stochastic {
54    /// Construct a stochastic with %K lookback and %D smoothing periods.
55    ///
56    /// # Errors
57    ///
58    /// Returns [`Error::PeriodZero`] if either period is zero.
59    pub fn new(k_period: usize, d_period: usize) -> Result<Self> {
60        if k_period == 0 || d_period == 0 {
61            return Err(Error::PeriodZero);
62        }
63        Ok(Self {
64            k_period,
65            d_period,
66            candles: VecDeque::with_capacity(k_period),
67            hh_idx: VecDeque::with_capacity(k_period),
68            ll_idx: VecDeque::with_capacity(k_period),
69            count: 0,
70            d_sma: Sma::new(d_period)?,
71            last_k: None,
72        })
73    }
74
75    /// Classic fast stochastic: `%K = 14`, `%D = 3`.
76    pub fn classic() -> Self {
77        Self::new(14, 3).expect("classic stochastic periods are valid")
78    }
79
80    /// Configured `(k_period, d_period)`.
81    pub const fn periods(&self) -> (usize, usize) {
82        (self.k_period, self.d_period)
83    }
84
85    fn push_window(&mut self, candle: Candle) {
86        let idx = self.count;
87        self.count += 1;
88        // Drop deque entries that are outside the window.
89        let oldest_keep_idx = idx.saturating_sub(self.k_period - 1);
90        while let Some(&front) = self.hh_idx.front() {
91            if front < oldest_keep_idx {
92                self.hh_idx.pop_front();
93            } else {
94                break;
95            }
96        }
97        while let Some(&front) = self.ll_idx.front() {
98            if front < oldest_keep_idx {
99                self.ll_idx.pop_front();
100            } else {
101                break;
102            }
103        }
104        // Maintain monotonic-decreasing deque for highs.
105        while let Some(&back) = self.hh_idx.back() {
106            let back_off = back - idx.saturating_sub(self.candles.len());
107            if self.candles[back_off].high <= candle.high {
108                self.hh_idx.pop_back();
109            } else {
110                break;
111            }
112        }
113        self.hh_idx.push_back(idx);
114        // Maintain monotonic-increasing deque for lows.
115        while let Some(&back) = self.ll_idx.back() {
116            let back_off = back - idx.saturating_sub(self.candles.len());
117            if self.candles[back_off].low >= candle.low {
118                self.ll_idx.pop_back();
119            } else {
120                break;
121            }
122        }
123        self.ll_idx.push_back(idx);
124
125        if self.candles.len() == self.k_period {
126            self.candles.pop_front();
127        }
128        self.candles.push_back(candle);
129    }
130
131    fn current_extremes(&self) -> (f64, f64) {
132        let base = self.count - self.candles.len();
133        let hi = self.candles[self.hh_idx[0] - base].high;
134        let lo = self.candles[self.ll_idx[0] - base].low;
135        (hi, lo)
136    }
137}
138
139impl Indicator for Stochastic {
140    type Input = Candle;
141    type Output = StochasticOutput;
142
143    fn update(&mut self, candle: Candle) -> Option<StochasticOutput> {
144        self.push_window(candle);
145        if self.candles.len() < self.k_period {
146            return None;
147        }
148        let (hh, ll) = self.current_extremes();
149        let range = hh - ll;
150        let k = if range == 0.0 {
151            // Flat range; convention: 50 (neutral, like RSI on flat input).
152            50.0
153        } else {
154            100.0 * (candle.close - ll) / range
155        };
156        self.last_k = Some(k);
157        let d = self.d_sma.update(k)?;
158        Some(StochasticOutput { k, d })
159    }
160
161    fn reset(&mut self) {
162        self.candles.clear();
163        self.hh_idx.clear();
164        self.ll_idx.clear();
165        self.count = 0;
166        self.d_sma.reset();
167        self.last_k = None;
168    }
169
170    fn warmup_period(&self) -> usize {
171        self.k_period + self.d_period - 1
172    }
173
174    fn is_ready(&self) -> bool {
175        self.d_sma.is_ready()
176    }
177
178    fn name(&self) -> &'static str {
179        "Stochastic"
180    }
181}
182
183#[cfg(test)]
184mod tests {
185    use super::*;
186    use crate::traits::BatchExt;
187    use approx::assert_relative_eq;
188
189    fn c(h: f64, l: f64, cl: f64) -> Candle {
190        Candle::new(cl, h, l, cl, 1.0, 0).unwrap()
191    }
192
193    /// Naive %K computation for cross-checks.
194    fn naive_k(candles: &[Candle], k_period: usize) -> Vec<Option<f64>> {
195        candles
196            .iter()
197            .enumerate()
198            .map(|(i, _)| {
199                if i + 1 < k_period {
200                    None
201                } else {
202                    let w = &candles[i + 1 - k_period..=i];
203                    let hh = w.iter().map(|x| x.high).fold(f64::NEG_INFINITY, f64::max);
204                    let ll = w.iter().map(|x| x.low).fold(f64::INFINITY, f64::min);
205                    let range = hh - ll;
206                    let cl = candles[i].close;
207                    Some(if range == 0.0 {
208                        50.0
209                    } else {
210                        100.0 * (cl - ll) / range
211                    })
212                }
213            })
214            .collect()
215    }
216
217    #[test]
218    fn rejects_zero_periods() {
219        assert!(matches!(Stochastic::new(0, 3), Err(Error::PeriodZero)));
220        assert!(matches!(Stochastic::new(14, 0), Err(Error::PeriodZero)));
221    }
222
223    /// Cover the `Stochastic::classic()` convenience constructor plus the
224    /// `periods()` const accessor and the Indicator-impl `warmup_period`
225    /// / `name` methods. Existing tests called `Stochastic::new(_, _)`
226    /// directly and never asked for the configured periods, warmup
227    /// length, or name.
228    #[test]
229    fn classic_periods_and_metadata() {
230        let s = Stochastic::classic();
231        assert_eq!(s.periods(), (14, 3));
232        // Warmup for the classic config: k_period + d_period - 1 = 14 + 3 - 1 = 16.
233        assert_eq!(s.warmup_period(), 16);
234        assert_eq!(s.name(), "Stochastic");
235    }
236
237    #[test]
238    fn close_at_high_yields_k_100() {
239        let candles = vec![
240            c(10.0, 8.0, 9.0),
241            c(11.0, 9.0, 10.0),
242            c(12.0, 10.0, 12.0), // close == high == HH
243        ];
244        let mut s = Stochastic::new(3, 1).unwrap();
245        let out = s.batch(&candles);
246        assert_relative_eq!(out[2].unwrap().k, 100.0, epsilon = 1e-12);
247    }
248
249    #[test]
250    fn close_at_low_yields_k_0() {
251        let candles = vec![
252            c(10.0, 8.0, 9.0),
253            c(11.0, 9.0, 10.0),
254            c(12.0, 8.0, 8.0), // close == LL
255        ];
256        let mut s = Stochastic::new(3, 1).unwrap();
257        let out = s.batch(&candles);
258        assert_relative_eq!(out[2].unwrap().k, 0.0, epsilon = 1e-12);
259    }
260
261    #[test]
262    fn flat_range_yields_k_50() {
263        let candles: Vec<Candle> = (0..20).map(|_| c(10.0, 10.0, 10.0)).collect();
264        let mut s = Stochastic::new(14, 3).unwrap();
265        for o in s.batch(&candles).into_iter().flatten() {
266            assert_relative_eq!(o.k, 50.0, epsilon = 1e-12);
267            assert_relative_eq!(o.d, 50.0, epsilon = 1e-12);
268        }
269        // Cross-check: the naive_k test helper must agree on the flat-range
270        // convention. The k_matches_naive test only feeds oscillating prices,
271        // so the helper's flat-range branch was never exercised.
272        let ks = naive_k(&candles, 14);
273        for k in ks.into_iter().skip(13) {
274            assert_relative_eq!(k.expect("ready after 14 inputs"), 50.0, epsilon = 1e-12);
275        }
276    }
277
278    #[test]
279    fn k_matches_naive() {
280        let candles: Vec<Candle> = (0..60)
281            .map(|i| {
282                let mid = 50.0 + (f64::from(i) * 0.4).sin() * 10.0;
283                c(mid + 2.0, mid - 2.0, mid + (f64::from(i) * 0.7).cos())
284            })
285            .collect();
286        let mut s = Stochastic::new(14, 3).unwrap();
287        let out = s.batch(&candles);
288        let naive = naive_k(&candles, 14);
289        for (i, got) in out.iter().enumerate() {
290            if let Some(o) = got {
291                let n = naive[i].expect("naive ready");
292                assert_relative_eq!(o.k, n, epsilon = 1e-9);
293            }
294        }
295    }
296
297    #[test]
298    fn d_is_sma_of_k() {
299        let candles: Vec<Candle> = (0..60)
300            .map(|i| {
301                let mid = 50.0 + f64::from(i).sin() * 5.0;
302                c(mid + 1.5, mid - 1.5, mid)
303            })
304            .collect();
305        let mut s = Stochastic::new(14, 3).unwrap();
306        let out = s.batch(&candles);
307        // The naive %K series gives us the ground-truth values that %D should average.
308        let naive_ks = naive_k(&candles, 14);
309        // The first emitted %D corresponds to the SMA of the first three valid %K values
310        // (i.e. those at indices 13, 14, 15). At that point %D becomes ready, and the
311        // first `Some(_)` output appears at index 15.
312        let first_emit_idx = out
313            .iter()
314            .position(Option::is_some)
315            .expect("d eventually emits");
316        let first_d = out[first_emit_idx].unwrap().d;
317        let k_window = &naive_ks[first_emit_idx - 2..=first_emit_idx];
318        let want = k_window
319            .iter()
320            .map(|v| v.expect("naive K ready inside window"))
321            .sum::<f64>()
322            / 3.0;
323        assert_relative_eq!(first_d, want, epsilon = 1e-9);
324    }
325
326    #[test]
327    fn batch_equals_streaming() {
328        let candles: Vec<Candle> = (0..50)
329            .map(|i| {
330                let mid = 100.0 + f64::from(i) * 0.5;
331                c(mid + 2.0, mid - 2.0, mid)
332            })
333            .collect();
334        let mut a = Stochastic::new(14, 3).unwrap();
335        let mut b = Stochastic::new(14, 3).unwrap();
336        assert_eq!(
337            a.batch(&candles),
338            candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
339        );
340    }
341
342    #[test]
343    fn reset_clears_state() {
344        let mut s = Stochastic::new(5, 3).unwrap();
345        let candles: Vec<Candle> = (0..10).map(|i| c(10.0 + f64::from(i), 5.0, 7.0)).collect();
346        s.batch(&candles);
347        assert!(s.is_ready());
348        s.reset();
349        assert!(!s.is_ready());
350        assert_eq!(s.update(candles[0]), None);
351    }
352}