Skip to main content

wickra_core/indicators/
evwma.rs

1//! Elastic Volume-Weighted Moving Average (EVWMA).
2
3use std::collections::VecDeque;
4
5use crate::error::{Error, Result};
6use crate::ohlcv::Candle;
7use crate::traits::Indicator;
8
9/// Christian P. Fries' Elastic Volume-Weighted Moving Average.
10///
11/// Unlike `VWMA` which is a per-bar weighted mean, `EVWMA` runs an
12/// "elastic" recurrence whose smoothing weight is the bar's volume relative
13/// to the running window-volume:
14///
15/// ```text
16/// V_sum_t  = Σ volume_i over the last `period` candles
17/// EVWMA_t  = ((V_sum_t - volume_t) * EVWMA_{t-1} + volume_t * close_t) / V_sum_t
18/// ```
19///
20/// A bar whose volume is small compared to the window total barely moves the
21/// average; a bar whose volume dominates the window pulls it strongly toward
22/// the bar's close. The series is seeded with the close of the first candle
23/// after the volume window has filled (i.e. after `period` candles).
24///
25/// If `V_sum_t == 0` (every candle in the window has zero volume), the
26/// recurrence is undefined; the indicator holds its previous value.
27///
28/// Reference: Christian P. Fries, *Wilmott Magazine*, 2001.
29///
30/// # Example
31///
32/// ```
33/// use wickra_core::{Candle, Evwma, Indicator};
34///
35/// let mut evwma = Evwma::new(20).unwrap();
36/// let mut last = None;
37/// for i in 0..40 {
38///     let p = 100.0 + f64::from(i);
39///     let candle = Candle::new(p, p + 1.0, p - 1.0, p, 10.0, i64::from(i)).unwrap();
40///     last = evwma.update(candle);
41/// }
42/// assert!(last.is_some());
43/// ```
44#[derive(Debug, Clone)]
45pub struct Evwma {
46    period: usize,
47    /// Rolling window of `(close, volume)` pairs, oldest at the front.
48    window: VecDeque<(f64, f64)>,
49    sum_v: f64,
50    current: Option<f64>,
51}
52
53impl Evwma {
54    /// # Errors
55    /// Returns [`Error::PeriodZero`] if `period == 0`.
56    pub fn new(period: usize) -> Result<Self> {
57        if period == 0 {
58            return Err(Error::PeriodZero);
59        }
60        if period > crate::error::MAX_PERIOD {
61            return Err(Error::InvalidPeriod {
62                message: crate::error::PERIOD_ABOVE_MAX,
63            });
64        }
65        Ok(Self {
66            period,
67            window: VecDeque::with_capacity(period),
68            sum_v: 0.0,
69            current: None,
70        })
71    }
72
73    /// Configured period.
74    pub const fn period(&self) -> usize {
75        self.period
76    }
77
78    /// Current value if available.
79    pub const fn value(&self) -> Option<f64> {
80        self.current
81    }
82}
83
84impl Indicator for Evwma {
85    type Input = Candle;
86    type Output = f64;
87
88    #[inline]
89    fn update(&mut self, candle: Candle) -> Option<f64> {
90        let close = candle.close;
91        let volume = candle.volume;
92        if self.window.len() == self.period {
93            let (_, old_v) = self.window.pop_front().expect("window is non-empty");
94            self.sum_v -= old_v;
95        }
96        self.window.push_back((close, volume));
97        self.sum_v += volume;
98        if self.window.len() < self.period {
99            return None;
100        }
101        // The volume sum may be zero (every bar in the window had zero
102        // volume); the recurrence is undefined, so seed/hold instead.
103        if self.sum_v <= 0.0 {
104            if self.current.is_none() {
105                self.current = Some(close);
106            }
107            return self.current;
108        }
109        let prev = self.current.unwrap_or(close);
110        let next = ((self.sum_v - volume) * prev + volume * close) / self.sum_v;
111        self.current = Some(next);
112        Some(next)
113    }
114
115    fn reset(&mut self) {
116        self.window.clear();
117        self.sum_v = 0.0;
118        self.current = None;
119    }
120
121    #[inline]
122    fn warmup_period(&self) -> usize {
123        self.period
124    }
125
126    #[inline]
127    fn is_ready(&self) -> bool {
128        self.current.is_some()
129    }
130
131    #[inline]
132    fn name(&self) -> &'static str {
133        "EVWMA"
134    }
135}
136
137#[cfg(test)]
138mod tests {
139    use super::*;
140    use crate::traits::BatchExt;
141    use approx::assert_relative_eq;
142
143    fn candle(close: f64, volume: f64, ts: i64) -> Candle {
144        Candle::new(close, close, close, close, volume, ts).unwrap()
145    }
146
147    #[test]
148    fn rejects_zero_period() {
149        assert!(matches!(Evwma::new(0), Err(Error::PeriodZero)));
150    }
151
152    #[test]
153    fn accessors_and_metadata() {
154        let mut e = Evwma::new(5).unwrap();
155        assert_eq!(e.period(), 5);
156        assert_eq!(e.warmup_period(), 5);
157        assert_eq!(e.name(), "EVWMA");
158        assert_eq!(e.value(), None);
159        for i in 0..5 {
160            e.update(candle(10.0, 1.0, i));
161        }
162        assert!(e.value().is_some());
163    }
164
165    #[test]
166    fn constant_series_yields_the_constant() {
167        // A flat close — every (V_sum - v) * prev + v * close reduces to
168        // V_sum * close, so the recurrence preserves the constant after the
169        // first seeded sample.
170        let mut e = Evwma::new(5).unwrap();
171        let candles: Vec<Candle> = (0..30).map(|i| candle(42.0, 3.0, i)).collect();
172        let out = e.batch(&candles);
173        for v in out.iter().skip(4).flatten() {
174            assert_relative_eq!(*v, 42.0, epsilon = 1e-12);
175        }
176    }
177
178    #[test]
179    fn reference_value_period_2() {
180        // EVWMA(2). Bars: (close, volume) = (10, 1), (20, 3), (30, 1).
181        //   Bar 1: window not full (size 1) -> None.
182        //   Bar 2: window full, sum_v = 4, prev seeds to 20.
183        //          EVWMA = ((4 - 3) * 20 + 3 * 20) / 4 = 80 / 4 = 20.
184        //   Bar 3: window slides, sum_v = 4 (drops the 1, gains the 1).
185        //          EVWMA = ((4 - 1) * 20 + 1 * 30) / 4 = (60 + 30) / 4 = 22.5.
186        let mut e = Evwma::new(2).unwrap();
187        assert_eq!(e.update(candle(10.0, 1.0, 0)), None);
188        assert_relative_eq!(
189            e.update(candle(20.0, 3.0, 1)).unwrap(),
190            20.0,
191            epsilon = 1e-12
192        );
193        assert_relative_eq!(
194            e.update(candle(30.0, 1.0, 2)).unwrap(),
195            22.5,
196            epsilon = 1e-12
197        );
198    }
199
200    #[test]
201    fn warmup_emits_first_value_at_period() {
202        let mut e = Evwma::new(4).unwrap();
203        for i in 0..3 {
204            assert_eq!(e.update(candle(10.0, 1.0, i)), None);
205        }
206        assert!(e.update(candle(10.0, 1.0, 3)).is_some());
207    }
208
209    #[test]
210    fn zero_volume_window_holds_value() {
211        // Every bar has zero volume: no participation, so the recurrence
212        // can't move and EVWMA simply seeds to the first close.
213        let mut e = Evwma::new(3).unwrap();
214        e.update(candle(10.0, 0.0, 0));
215        e.update(candle(15.0, 0.0, 1));
216        let v = e.update(candle(20.0, 0.0, 2)).unwrap();
217        assert_relative_eq!(v, 20.0, epsilon = 1e-12);
218        // Next bar still flat-zero volume: holds 20.
219        let v2 = e.update(candle(50.0, 0.0, 3)).unwrap();
220        assert_relative_eq!(v2, 20.0, epsilon = 1e-12);
221    }
222
223    #[test]
224    fn batch_equals_streaming() {
225        let candles: Vec<Candle> = (0..60_i64)
226            .map(|i| {
227                let c = 100.0 + (i as f64 * 0.3).sin() * 8.0;
228                candle(c, 1.0 + (i % 7) as f64, i)
229            })
230            .collect();
231        let batch = Evwma::new(10).unwrap().batch(&candles);
232        let mut b = Evwma::new(10).unwrap();
233        let streamed: Vec<_> = candles.iter().map(|c| b.update(*c)).collect();
234        assert_eq!(batch, streamed);
235    }
236
237    #[test]
238    fn reset_clears_state() {
239        let mut e = Evwma::new(3).unwrap();
240        let candles: Vec<Candle> = (0..10).map(|i| candle(10.0 + i as f64, 2.0, i)).collect();
241        e.batch(&candles);
242        assert!(e.is_ready());
243        e.reset();
244        assert!(!e.is_ready());
245        assert_eq!(e.update(candle(10.0, 1.0, 0)), None);
246    }
247}