Skip to main content

quantwave_core/indicators/incremental/
ma_stream.rs

1//! TA-Lib compatible streaming moving averages (SMA / EMA).
2
3use crate::indicators::incremental::talib_ema::TalibEma;
4use crate::indicators::incremental::talib_sma::TalibSma;
5use crate::traits::Next;
6use talib_rs::MaType;
7
8/// Streaming MA matching TA-Lib `compute_ma` for SMA and EMA.
9#[derive(Debug, Clone)]
10pub enum MaStream {
11    Sma(TalibSma),
12    Ema(TalibEma),
13}
14
15impl MaStream {
16    pub fn new(period: usize, ma_type: MaType) -> Self {
17        match ma_type {
18            MaType::Ema => Self::Ema(TalibEma::new(period)),
19            _ => Self::Sma(TalibSma::new(period)),
20        }
21    }
22
23    pub fn next(&mut self, v: f64) -> f64 {
24        match self {
25            Self::Sma(s) => s.next(v),
26            Self::Ema(e) => e.next(v),
27        }
28    }
29}