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§
Sourcefn required_len(&self) -> usize
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.
Sourcefn required_columns(&self) -> &[&'static str]
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".
Sourcefn calculate(
&self,
candles: &[Candle],
) -> Result<IndicatorOutput, IndicatorError>
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
IndicatorOutputwith one or more named columns. - Leading warm-up rows should be
f64::NAN. - Returns
Err(IndicatorError::InsufficientData)ifcandles.len() < required_len().