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 EMA4uint256 public constant PRECISION = 1e18;56function updateEMA(uint256 newPrice) external returns (uint256) {7 if (ema == 0) {8 ema = newPrice;9 return ema;10 }1112 // EMA = α * newPrice + (1 - α) * oldEMA13 ema = (ALPHA_NUMERATOR * PRECISION * newPrice / ALPHA_DENOMINATOR14 + (ALPHA_DENOMINATOR - ALPHA_NUMERATOR) * PRECISION * ema / ALPHA_DENOMINATOR)15 / PRECISION;1617 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
| Property | Short EMA (low n) | Long EMA (high n) |
|---|---|---|
| Responsiveness | High | Low |
| Noise filtering | Low | High |
| Manipulation resistance | Lower | Higher |
| Lag | Minimal | Significant |
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.
Articles Using This Term
Learn more about Exponential Moving Average (EMA) in these articles:
Related Terms
Moving Average
A calculation that smooths price data over a specified period by continuously updating the average as new data arrives.
Simple Moving Average (SMA)
An average computed by summing values over a fixed number of periods and dividing by that count, giving equal weight to each observation.
TWAP (Time-Weighted Average Price)
A price calculation method that averages asset prices over a time period to resist short-term manipulation.
Oracle
A service that provides external data (prices, events, random numbers) to smart contracts that cannot access off-chain information directly.
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

