pub struct ExponentialMovingAverage {
pub alpha: f64,
pub current_ema: Option<f64>,
}Expand description
An Exponential Moving Average (EMA) indicator.
Fields§
§alpha: f64The smoothing factor (alpha).
current_ema: Option<f64>The current EMA value.
Implementations§
Source§impl ExponentialMovingAverage
impl ExponentialMovingAverage
Sourcepub fn add_value(&mut self, price: f64) -> Option<f64>
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);Sourcepub fn get_current_value(&self) -> Option<f64>
pub fn get_current_value(&self) -> Option<f64>
Returns the current EMA value.
§Returns
Some(f64)if the EMA has been computed.Noneif 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§
impl Freeze for ExponentialMovingAverage
impl RefUnwindSafe for ExponentialMovingAverage
impl Send for ExponentialMovingAverage
impl Sync for ExponentialMovingAverage
impl Unpin for ExponentialMovingAverage
impl UnwindSafe for ExponentialMovingAverage
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