Exponential Moving Average (EMA)

A weighted moving average that gives exponentially decreasing weight to older observations, making it more responsive to recent data.

An Exponential Moving Average (EMA) is a type of moving average that applies exponentially decreasing weights to older data points, giving greater importance to recent observations. Unlike a Simple Moving Average that requires storing an entire window of historical values, an EMA can be updated incrementally with a single stored value, making it significantly more gas-efficient for on-chain implementations.

Formula

1EMA_new = α × Price_current + (1 - α) × EMA_previous

Where α (alpha) is the smoothing factor, typically calculated as:

1α = 2 / (n + 1)

Where n is the number of periods. A higher α makes the EMA more responsive to recent data.

On-chain implementation

EMAs are more gas-efficient than SMAs because they only require storing one value:

1uint256 public ema;
2uint256 public constant ALPHA_NUMERATOR = 2;
3uint256 public constant ALPHA_DENOMINATOR = 11; // α = 2/11 for 10-period EMA
4uint256 public constant PRECISION = 1e18;
5
6function updateEMA(uint256 newPrice) external returns (uint256) {
7 if (ema == 0) {
8 ema = newPrice;
9 return ema;
10 }
11
12 // EMA = α * newPrice + (1 - α) * oldEMA
13 ema = (ALPHA_NUMERATOR * PRECISION * newPrice / ALPHA_DENOMINATOR
14 + (ALPHA_DENOMINATOR - ALPHA_NUMERATOR) * PRECISION * ema / ALPHA_DENOMINATOR)
15 / PRECISION;
16
17 return ema;
18}

DeFi use cases

  • Synthetix: Uses EMA for exchange rate calculations, balancing manipulation resistance with responsiveness.
  • Interest rate models: Lending protocols apply EMAs to utilization rates for smoother interest rate adjustments.
  • Volatility estimation: EMAs of price changes help estimate on-chain volatility for risk parameters.

Trade-offs

PropertyShort EMA (low n)Long EMA (high n)
ResponsivenessHighLow
Noise filteringLowHigh
Manipulation resistanceLowerHigher
LagMinimalSignificant

Security considerations

  • Initialization: The first EMA value has outsized influence on subsequent calculations. Protocols should seed the EMA with a trusted price.
  • Update frequency: Infrequent updates can cause the EMA to become stale, creating staleness risks.
  • Precision loss: The multiplication and division steps in EMA calculations can accumulate rounding errors over time.
  • Alpha selection: Too high an alpha makes the EMA susceptible to short-term manipulation; too low makes it unresponsive to legitimate price movements.

EMAs offer an efficient on-chain alternative to SMAs, trading some simplicity for better gas efficiency and faster trend detection.

Need expert guidance on Exponential Moving Average (EMA)?

Our team at Zealynx has deep expertise in blockchain security and DeFi protocols. Whether you need an audit or consultation, we're here to help.

Get a Quote

oog
zealynx

Subscribe to Our Newsletter

Stay updated with our latest security insights and blog posts

© 2024 Zealynx