Skip to main content

wickra_core/indicators/
vidya.rs

1//! Variable Index Dynamic Average (VIDYA).
2
3use crate::error::{Error, Result};
4use crate::indicators::cmo::Cmo;
5use crate::traits::Indicator;
6
7/// Tushar Chande's Variable Index Dynamic Average — an EMA whose smoothing
8/// factor is scaled by the absolute Chande Momentum Oscillator (`CMO`).
9///
10/// Strong directional momentum (high `|CMO|`) pushes the effective smoothing
11/// constant toward the EMA-of-`period`'s natural rate; flat / choppy windows
12/// (`|CMO|` close to zero) shrink it toward zero so VIDYA coasts on its prior
13/// value:
14///
15/// ```text
16/// alpha_base = 2 / (period + 1)
17/// alpha_t    = alpha_base * |CMO(cmo_period)| / 100
18/// VIDYA_t    = alpha_t * price_t + (1 - alpha_t) * VIDYA_{t-1}
19/// ```
20///
21/// The series is seeded with the first price emitted after the `CMO`
22/// warm-up (i.e. after `cmo_period + 1` inputs).
23///
24/// Reference: Tushar Chande, *Stocks & Commodities*, 1992.
25///
26/// # Example
27///
28/// ```
29/// use wickra_core::{Indicator, Vidya};
30///
31/// let mut vidya = Vidya::new(14, 9).unwrap();
32/// let mut last = None;
33/// for i in 0..80 {
34///     last = vidya.update(100.0 + f64::from(i));
35/// }
36/// assert!(last.is_some());
37/// ```
38#[derive(Debug, Clone)]
39pub struct Vidya {
40    period: usize,
41    cmo_period: usize,
42    alpha_base: f64,
43    cmo: Cmo,
44    current: Option<f64>,
45}
46
47impl Vidya {
48    /// # Errors
49    /// Returns [`Error::PeriodZero`] if either period is zero.
50    pub fn new(period: usize, cmo_period: usize) -> Result<Self> {
51        if period == 0 || cmo_period == 0 {
52            return Err(Error::PeriodZero);
53        }
54        let alpha_base = 2.0 / (period as f64 + 1.0);
55        Ok(Self {
56            period,
57            cmo_period,
58            alpha_base,
59            cmo: Cmo::new(cmo_period)?,
60            current: None,
61        })
62    }
63
64    /// Configured `(period, cmo_period)`.
65    pub const fn periods(&self) -> (usize, usize) {
66        (self.period, self.cmo_period)
67    }
68}
69
70impl Indicator for Vidya {
71    type Input = f64;
72    type Output = f64;
73
74    fn update(&mut self, input: f64) -> Option<f64> {
75        if !input.is_finite() {
76            return self.current;
77        }
78        let cmo = self.cmo.update(input)?;
79        let alpha = self.alpha_base * (cmo.abs() / 100.0);
80        let prev = self.current.unwrap_or(input);
81        let next = alpha * input + (1.0 - alpha) * prev;
82        self.current = Some(next);
83        Some(next)
84    }
85
86    fn reset(&mut self) {
87        self.cmo.reset();
88        self.current = None;
89    }
90
91    fn warmup_period(&self) -> usize {
92        self.cmo_period + 1
93    }
94
95    fn is_ready(&self) -> bool {
96        self.current.is_some()
97    }
98
99    fn name(&self) -> &'static str {
100        "VIDYA"
101    }
102}
103
104#[cfg(test)]
105mod tests {
106    use super::*;
107    use crate::traits::BatchExt;
108    use approx::assert_relative_eq;
109
110    #[test]
111    fn rejects_zero_period() {
112        assert!(matches!(Vidya::new(0, 9), Err(Error::PeriodZero)));
113        assert!(matches!(Vidya::new(14, 0), Err(Error::PeriodZero)));
114    }
115
116    #[test]
117    fn accessors_and_metadata() {
118        let v = Vidya::new(14, 9).unwrap();
119        assert_eq!(v.periods(), (14, 9));
120        assert_eq!(v.warmup_period(), 10);
121        assert_eq!(v.name(), "VIDYA");
122    }
123
124    #[test]
125    fn constant_series_yields_the_constant() {
126        // Flat input -> CMO = 0 -> alpha = 0 -> VIDYA holds its seed value.
127        let mut v = Vidya::new(14, 4).unwrap();
128        let out = v.batch(&[42.0_f64; 30]);
129        for x in out.iter().skip(4).flatten() {
130            assert_relative_eq!(*x, 42.0, epsilon = 1e-12);
131        }
132    }
133
134    #[test]
135    fn pure_uptrend_alpha_equals_base() {
136        // Monotonic uptrend: CMO saturates at +100, so alpha = alpha_base.
137        // After warmup the recurrence is a plain EMA with that alpha; once
138        // the series is long enough VIDYA closely tracks the latest input.
139        let mut v = Vidya::new(2, 4).unwrap();
140        let prices: Vec<f64> = (1..=40).map(f64::from).collect();
141        let out = v.batch(&prices);
142        let last = out.last().unwrap().unwrap();
143        let latest = *prices.last().unwrap();
144        // alpha_base = 2/3, EMA(2) tracks close — last value is within 2 of
145        // the latest input after this many bars.
146        assert!(
147            (latest - last).abs() < 2.0,
148            "VIDYA should track close on a clean uptrend: {last} vs {latest}"
149        );
150    }
151
152    #[test]
153    fn warmup_emits_first_value_at_cmo_period_plus_one() {
154        let mut v = Vidya::new(14, 3).unwrap();
155        assert_eq!(v.warmup_period(), 4);
156        assert_eq!(v.update(10.0), None);
157        assert_eq!(v.update(11.0), None);
158        assert_eq!(v.update(12.0), None);
159        assert!(v.update(13.0).is_some());
160    }
161
162    #[test]
163    fn batch_equals_streaming() {
164        let prices: Vec<f64> = (1..=60)
165            .map(|i| 100.0 + (f64::from(i) * 0.2).sin() * 5.0)
166            .collect();
167        let mut a = Vidya::new(14, 9).unwrap();
168        let mut b = Vidya::new(14, 9).unwrap();
169        assert_eq!(
170            a.batch(&prices),
171            prices.iter().map(|p| b.update(*p)).collect::<Vec<_>>()
172        );
173    }
174
175    #[test]
176    fn reset_clears_state() {
177        let mut v = Vidya::new(14, 9).unwrap();
178        v.batch(&(1..=40).map(f64::from).collect::<Vec<_>>());
179        assert!(v.is_ready());
180        v.reset();
181        assert!(!v.is_ready());
182        assert_eq!(v.update(1.0), None);
183    }
184
185    #[test]
186    fn ignores_non_finite_input() {
187        let mut v = Vidya::new(14, 4).unwrap();
188        v.batch(&(1..=20).map(f64::from).collect::<Vec<_>>());
189        let before = v.update(21.0).unwrap();
190        assert_eq!(v.update(f64::NAN), Some(before));
191        assert_eq!(v.update(f64::INFINITY), Some(before));
192    }
193}