Struct ExponentialMovingAverage

Source
pub struct ExponentialMovingAverage {
    pub alpha: f64,
    pub current_ema: Option<f64>,
}
Expand description

An Exponential Moving Average (EMA) indicator.

Fields§

§alpha: f64

The smoothing factor (alpha).

§current_ema: Option<f64>

The current EMA value.

Implementations§

Source§

impl ExponentialMovingAverage

Source

pub fn new(period: usize) -> Self

Creates a new ExponentialMovingAverage indicator with the specified period.

§Arguments
  • period - The number of periods for the EMA calculation.
§Examples
use indexes_rs::v1::ema::main::ExponentialMovingAverage;

let ema = ExponentialMovingAverage::new(10);
Source

pub fn add_value(&mut self, price: f64) -> Option<f64>

Adds a new price value to update the EMA.

If no previous EMA exists, the current price is used as the initial EMA. Otherwise, the EMA is updated using:

[\text{EMA}{\text{new}} = \text{price} \times \alpha + \text{EMA}{\text{prev}} \times (1 - \alpha)]

§Arguments
  • price - The latest price.
§Returns
  • Some(f64) containing the updated EMA value.
§Examples
use indexes_rs::v1::ema::main::ExponentialMovingAverage;

let mut ema = ExponentialMovingAverage::new(10);
let first = ema.add_value(100.0).unwrap();
// first EMA is equal to 100.0
assert_eq!(first, 100.0);
let second = ema.add_value(105.0).unwrap();
println!("Updated EMA: {:.2}", second);
Source

pub fn get_current_value(&self) -> Option<f64>

Returns the current EMA value.

§Returns
  • Some(f64) if the EMA has been computed.
  • None if no values have been added yet.
§Examples
use indexes_rs::v1::ema::main::ExponentialMovingAverage;

let mut ema = ExponentialMovingAverage::new(10);
ema.add_value(100.0);
assert_eq!(ema.get_current_value().unwrap(), 100.0);

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.