Expand description
§Exponential Moving Average (EMA) Module
This module implements an Exponential Moving Average (EMA) indicator. The EMA applies a smoothing factor to give more weight to recent prices. The smoothing factor (alpha) is computed as:
[\alpha = \frac{2}{\text{period} + 1}]
On the first data point, the EMA is simply set to the price. On subsequent calls, the EMA is updated using the formula:
[\text{EMA}{\text{new}} = \text{price} \times \alpha + \text{EMA}{\text{prev}} \times (1 - \alpha)]
§Examples
use indexes_rs::v1::ema::main::ExponentialMovingAverage;
// Create an EMA indicator with a period of 10.
let mut ema = ExponentialMovingAverage::new(10);
// The first value sets the EMA to the price itself.
assert_eq!(ema.add_value(100.0).unwrap(), 100.0);
// Subsequent values update the EMA using the smoothing factor.
let second = ema.add_value(105.0).unwrap();
println!("Updated EMA: {:.2}", second);
// Get the current EMA value.
assert_eq!(ema.get_current_value().unwrap(), second);Structs§
- Exponential
Moving Average - An Exponential Moving Average (EMA) indicator.