Expand description
A Simple Moving Average (SMA) calculator module.
The Simple Moving Average is calculated by taking the arithmetic mean of a given set of values over a specified period. For example, a 20-day SMA is calculated by taking the arithmetic mean of the most recent 20 values.
This implementation uses a sliding window backed by a VecDeque
and maintains a running sum for improved performance. In addition to calculating the SMA value,
it also determines the trend (Up, Down, or Neutral) based on the change from the previous SMA.
§Examples
Using the SMA with a period of 3:
use indexes_rs::v1::sma::main::{SimpleMovingAverage, SMAError, SMAResult};
use indexes_rs::v1::types::TrendDirection;
let mut sma = SimpleMovingAverage::new(3).unwrap();
sma.add_value(2.0);
sma.add_value(4.0);
sma.add_value(6.0);
// First calculation returns Sideways trend since no previous value exists.
let result = sma.calculate().unwrap();
assert_eq!(result.value, 4.0);
assert_eq!(result.trend, TrendDirection::Sideways);Re-exports§
Structs§
- Simple
Moving Average - A Simple Moving Average (SMA) calculator that maintains a moving window of values and calculates their average along with a trend indicator.