Skip to main content

Indicator

Trait Indicator 

Source
pub trait Indicator:
    Send
    + Sync
    + Debug {
    // Required methods
    fn name(&self) -> &'static str;
    fn required_len(&self) -> usize;
    fn required_columns(&self) -> &[&'static str];
    fn calculate(
        &self,
        candles: &[Candle],
    ) -> Result<IndicatorOutput, IndicatorError>;

    // Provided method
    fn check_len(&self, candles: &[Candle]) -> Result<(), IndicatorError> { ... }
}
Expand description

The core trait every indicator must implement.

Analogous to indicators/base.py :: class Indicator(ABC).

§Implementing an indicator

use crate::indicator::{Indicator, IndicatorOutput};
use crate::error::IndicatorError;
use crate::types::Candle;

pub struct Sma {
    pub period: usize,
    pub column: PriceColumn,
}

impl Indicator for Sma {
    fn name(&self) -> &str { "SMA" }

    fn required_len(&self) -> usize { self.period }

    fn required_columns(&self) -> &[&str] { &["close"] }

    fn calculate(&self, candles: &[Candle]) -> Result<IndicatorOutput, IndicatorError> {
    }
}

Required Methods§

Source

fn name(&self) -> &'static str

Short canonical name, e.g. "SMA", "RSI", "MACD".

Source

fn required_len(&self) -> usize

Minimum number of candles required before output is non-NaN. Mirrors Python’s implicit warm-up period used for validation.

Source

fn required_columns(&self) -> &[&'static str]

Which OHLCV fields this indicator reads.

Mirrors @classmethod required_columns() in Python. Valid values: "open", "high", "low", "close", "volume".

Source

fn calculate( &self, candles: &[Candle], ) -> Result<IndicatorOutput, IndicatorError>

Compute the indicator over a full candle slice (batch mode).

Mirrors def calculate(self, data: pd.DataFrame, price_column) -> pd.DataFrame.

  • Returns IndicatorOutput with one or more named columns.
  • Leading warm-up rows should be f64::NAN.
  • Returns Err(IndicatorError::InsufficientData) if candles.len() < required_len().

Provided Methods§

Source

fn check_len(&self, candles: &[Candle]) -> Result<(), IndicatorError>

Validate that enough data was supplied, returning a descriptive error if not.

Call this at the top of every calculate() implementation.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§