Skip to main content

ema

Function ema 

Source
pub fn ema(data: &[f64], period: usize) -> Vec<Option<f64>>
Expand description

Calculate Exponential Moving Average (EMA).

EMA gives more weight to recent prices, making it more responsive than SMA. The first value is calculated as an SMA, then subsequent values use the EMA formula.

§Arguments

  • data - Price data (typically close prices)
  • period - Number of periods for the moving average

§Formula

  • First EMA = SMA(period)
  • Multiplier = 2 / (period + 1)
  • EMA = (Close - Previous EMA) × Multiplier + Previous EMA

§Example

use finance_query::indicators::ema;

let prices = vec![10.0, 11.0, 12.0, 13.0, 14.0];
let result = ema(&prices, 3);

// First 2 values are None (insufficient data)
assert!(result[0].is_none());
assert!(result[1].is_none());
// Subsequent values are calculated using EMA formula
assert!(result[2].is_some());