quant-indicators 0.7.0

Pure indicator math library for trading — MA, RSI, Bollinger, MACD, ATR, HRP
Documentation
use crate::{Ema, HullMa, Indicator, MaType, Sma};

/// Generic moving average — dispatches to the concrete MA variant.
///
/// Used by strategies that need configurable MA types without
/// trait objects. Enum dispatch avoids vtable overhead on the
/// hot-path `next()` / `compute()` calls.
#[derive(Debug, Clone)]
pub enum Ma {
    Ema(Ema),
    Hull(HullMa),
    Sma(Sma),
}

impl Ma {
    /// Create a new MA of the given type and period.
    ///
    /// # Errors
    ///
    /// Returns the underlying indicator's error if the period is invalid.
    pub fn new(period: usize, ma_type: MaType) -> Result<Self, crate::IndicatorError> {
        match ma_type {
            MaType::Ema => Ok(Ma::Ema(Ema::new(period)?)),
            MaType::Hull => Ok(Ma::Hull(HullMa::new(period)?)),
            MaType::Sma => Ok(Ma::Sma(Sma::new(period)?)),
        }
    }

    /// Indicator name (e.g., "EMA(12)", "HullMA(26)", "SMA(50)").
    pub fn name(&self) -> &str {
        match self {
            Ma::Ema(i) => i.name(),
            Ma::Hull(i) => i.name(),
            Ma::Sma(i) => i.name(),
        }
    }

    /// Clone as a boxed `dyn Indicator` for strategy registration.
    pub fn to_boxed(&self) -> Box<dyn Indicator> {
        match self {
            Ma::Ema(i) => Box::new(i.clone()),
            Ma::Hull(i) => Box::new(i.clone()),
            Ma::Sma(i) => Box::new(i.clone()),
        }
    }
}

#[cfg(test)]
#[path = "ma_tests.rs"]
mod tests;