pub struct SimpleMovingAverage {
pub period: usize,
/* private fields */
}Expand description
A Simple Moving Average (SMA) calculator that maintains a moving window of values and calculates their average along with a trend indicator.
The SMA is calculated by taking the arithmetic mean of a given set of values over a specified period. The trend is determined by comparing the current SMA value with the previous SMA value:
- If the current SMA is greater, the trend is
Up. - If it is lower, the trend is
Down. - If it is the same (or if no previous value exists), the trend is
Sideways.
Fields§
§period: usizeThe period over which the moving average is calculated.
Implementations§
Source§impl SimpleMovingAverage
impl SimpleMovingAverage
Sourcepub fn new(period: usize) -> Result<Self, SMAError>
pub fn new(period: usize) -> Result<Self, SMAError>
Creates a new SimpleMovingAverage instance with the specified period.
§Arguments
period- The number of values to include in the moving average calculation.
§Returns
Ok(SimpleMovingAverage)- A new instance with the specified period.Err(SMAError)- If the period is invalid (e.g., zero).
§Examples
Using a valid period:
use indexes_rs::v1::sma::main::{SimpleMovingAverage, SMAError};
let sma = SimpleMovingAverage::new(3);
assert!(sma.is_ok());Using an invalid period:
use indexes_rs::v1::sma::main::{SimpleMovingAverage, SMAError};
let sma = SimpleMovingAverage::new(0);
assert_eq!(sma, Err(SMAError::InvalidPeriod));Sourcepub fn add_value(&mut self, value: f64)
pub fn add_value(&mut self, value: f64)
Adds a new value to the moving window.
If the window is full (i.e., the number of stored values equals the period), the oldest value is removed before adding the new one.
§Arguments
value- The new value to add to the moving window.
§Examples
use indexes_rs::v1::sma::main::SimpleMovingAverage;
let mut sma = SimpleMovingAverage::new(3).unwrap();
sma.add_value(2.0);
sma.add_value(4.0);
sma.add_value(6.0);Sourcepub fn calculate(&mut self) -> Option<SMAResult>
pub fn calculate(&mut self) -> Option<SMAResult>
Calculates the current Simple Moving Average and determines the trend.
§Returns
Some(SMAResult)- The calculated SMA and trend if enough values are available.None- If there aren’t enough values to calculate the average.
§Examples
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);
let result = sma.calculate().unwrap();
// The average of [2.0, 4.0, 6.0] is 4.0, and since no previous SMA exists, the trend is Sideways.
assert_eq!(result.value, 4.0);
assert_eq!(result.trend, TrendDirection::Sideways);Trait Implementations§
Source§impl Debug for SimpleMovingAverage
impl Debug for SimpleMovingAverage
Source§impl PartialEq for SimpleMovingAverage
impl PartialEq for SimpleMovingAverage
impl StructuralPartialEq for SimpleMovingAverage
Auto Trait Implementations§
impl Freeze for SimpleMovingAverage
impl RefUnwindSafe for SimpleMovingAverage
impl Send for SimpleMovingAverage
impl Sync for SimpleMovingAverage
impl Unpin for SimpleMovingAverage
impl UnwindSafe for SimpleMovingAverage
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more