wickra_core/indicators/
vidya.rs1use crate::error::{Error, Result};
4use crate::indicators::cmo::Cmo;
5use crate::traits::Indicator;
6
7#[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 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 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 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 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 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}