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ₙ]
2
3New 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.

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

oog
zealynx

Subscribe to Our Newsletter

Stay updated with our latest security insights and blog posts

© 2024 Zealynx