Struct SimpleMovingAverage

Source
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: usize

The period over which the moving average is calculated.

Implementations§

Source§

impl SimpleMovingAverage

Source

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));
Source

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);
Source

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

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for SimpleMovingAverage

Source§

fn eq(&self, other: &SimpleMovingAverage) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for SimpleMovingAverage

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.