Moving Average
A calculation that smooths price data over a specified period by continuously updating the average as new data arrives.
A Moving Average is a statistical calculation that creates a continuously updated average of data points over a specified time window. In DeFi, moving averages are used far beyond traditional technical analysis — they serve as active components in smart contracts, influencing price oracles, interest rate models, AMM parameters, and governance mechanisms. By smoothing out short-term noise, moving averages provide more stable reference values for on-chain decision-making, though they also introduce lag and potential manipulation vectors.
How moving averages work
The core concept is simple: maintain a rolling window of observations and compute their average. As each new data point arrives, the oldest one is dropped:
1Window: [P₁, P₂, P₃, ..., Pₙ]23New price arrives (Pₙ₊₁):4Window becomes: [P₂, P₃, P₄, ..., Pₙ₊₁]
The two most common types in DeFi are the Simple Moving Average (equal weighting) and the Exponential Moving Average (recent data weighted more heavily).
DeFi applications
Moving averages serve critical roles across DeFi:
- Price oracles: TWAP implementations use time-weighted moving averages to resist flash loan price manipulation.
- Interest rates: Lending protocols use moving averages of utilization rates to dynamically adjust borrowing costs.
- AMM parameters: Some AMMs adjust fees or bonding curves based on moving averages of trading volume.
- Governance: DAOs may use moving averages of participation to adjust quorum requirements.
On-chain challenges
Implementing moving averages in smart contracts introduces specific challenges:
- Storage costs: Maintaining a window of historical values requires on-chain storage, which is expensive on Ethereum.
- Precision loss: Solidity's integer-only arithmetic means division operations lose precision, and these rounding errors can compound over time.
- Data availability: Unlike traditional markets with discrete closing prices, DeFi operates 24/7 with continuous price updates.
- Manipulation risk: Attackers who understand the averaging window can time trades to shift the average in their favor.
Security considerations
When auditing protocols that use moving averages:
- Verify the averaging window is long enough to resist manipulation
- Check for precision loss in the calculation logic
- Ensure staleness checks prevent use of outdated averages
- Confirm circuit breakers exist for abnormal deviations
- Test behavior at extreme values and edge cases (empty windows, single observation)
Moving averages are foundational infrastructure in DeFi, but their security depends on careful implementation, appropriate window selection, and layered defenses against manipulation.
Articles Using This Term
Learn more about Moving Average in these articles:
Related Terms
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.
Exponential Moving Average (EMA)
A weighted moving average that gives exponentially decreasing weight to older observations, making it more responsive to recent data.
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.
Price Oracle Manipulation
An attack where an attacker artificially skews the price reported by a price oracle to exploit protocols that rely on it.
Need expert guidance on Moving Average?
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

