EWMA Volatility calculates market volatility using an exponentially weighted moving average of squared log returns with bias correction. Unlike simple historical volatility that weights all observations equally, EWMA gives more weight to recent observations while still considering historical data, making it more responsive to current market conditions.
## Historical Context
Exponentially Weighted Moving Average volatility emerged from J.P. Morgan's RiskMetrics methodology in the 1990s. The approach addressed a key limitation of simple historical volatility: equal weighting of all past observations regardless of age. Financial practitioners recognized that recent market movements often provide more relevant information about current risk than distant historical data.
The original RiskMetrics Technical Document (1996) proposed a "decay factor" (λ) of 0.94 for daily data, meaning approximately 6% weight goes to the most recent observation. This implementation uses an equivalent RMA (Running Moving Average) formulation with period-based smoothing plus bias correction to address the initialization problem that affects early estimates.
## Architecture & Physics
### 1. Log Return Calculation
The foundation uses continuously compounded returns:
$$
r_t = \ln\left(\frac{P_t}{P_{t-1}}\right)
$$
Log returns are preferred over simple returns because:
- They are additive across time periods
- They are symmetric (±10% moves have similar magnitude)
- They approximate percentage changes for small movements
### 2. RMA Smoothing of Squared Returns
The squared returns are smoothed using RMA (Running Moving Average):
Note: This implementation is based on the PineScript reference at `ewma.pine`. The mathematical properties and mode consistency are validated through comprehensive unit tests.
## Common Pitfalls
1.**Annualization Confusion**: The `annualPeriods` parameter should match your data frequency. Use 252 for daily bars, 52 for weekly, 12 for monthly. Using incorrect values produces misleading annualized volatility.
2.**Period Selection**: Shorter periods (e.g., 10) respond faster to volatility changes but are noisier. Longer periods (e.g., 50) are smoother but slower to react. The classic RiskMetrics λ=0.94 corresponds to period≈17.
3.**First Value Interpretation**: The first output is always 0 (no return calculated yet). The second output may show high volatility if there's a large price change from the first bar.
4.**Log Return Assumptions**: EWMA assumes returns are approximately normally distributed. During extreme market events (fat tails), volatility may be underestimated.
5.**Bar Correction (isNew=false)**: When correcting the current bar's price, the indicator properly rolls back state. Multiple corrections within the same bar are handled correctly.
6.**Invalid Inputs**: NaN, Infinity, zero, and negative prices are replaced with the last valid price to maintain calculation continuity.