Rolling and Expanding Windows
Rolling windows use a fixed recent period; expanding windows use all data so far. Learn when to use each, window length trade offs and how to avoid look ahead bias.
Markets change, so statistics calculated over all of history may not describe today. Traders therefore compute averages, volatilities, correlations and model parameters over windows of data that move through time. A rolling window uses a fixed number of the most recent observations, dropping the oldest as new ones arrive. An expanding window starts at a fixed date and grows to include all data up to now. The choice between them, and the length of the window, shapes how quickly an analysis reacts to change and how noisy it is.
Rolling vs expanding#
| Rolling window | Expanding window | |
|---|---|---|
| Data used | The last N observations | All observations from the start to now |
| Reacts to change | Quickly | Slowly, more so as the window grows |
| Noise | Higher with short windows | Lower |
| Typical use | Volatility, correlation, moving averages | Long run averages, model training in walk forward tests |
Common rolling statistics#
| Statistic | Example | Lesson |
|---|---|---|
| Moving average | 20 or 200 day average price | Moving Averages Explained |
| Rolling volatility | 20 or 60 day standard deviation of returns | Historical and Realized Volatility |
| Rolling correlation | 60 day correlation between two assets | Covariance and Correlation |
| Rolling beta | 1 year beta to the market | Alpha and Beta |
| Rolling z score | Price relative to its 20 day mean and volatility | Percentiles, Quantiles and Z-Scores |
| Rolling Sharpe ratio | Performance over the last 12 months | Sharpe Ratio |
Choosing a window length#
Exponentially weighted windows#
Exponentially weighted moving averages (EWMA) give more weight to recent observations and gradually less to older ones, avoiding cliff effects:
EWMA variance_t = λ × variance_(t-1) + (1 - λ) × return_t²
RiskMetrics, developed at J.P. Morgan in the 1990s, popularised λ = 0.94 for daily volatility. See GARCH and Value at Risk (VaR).
Windows in backtesting#
- Rolling or expanding training windows are the core of walk forward analysis: fit the model on past data, test on the next period, then move forward. See Walk-Forward Analysis.
- Rolling windows adapt to changing markets; expanding windows use more data and are more stable.
- Many researchers test both and compare.
Avoiding look ahead bias#
Every window must include only data available at that time:
- Use trailing windows, not centred ones, for anything used in trading decisions.
- Normalise with past data only: z scores and scaling based on the full sample leak future information. See Look-Ahead Bias.
- Align timestamps carefully, especially across markets. See Timestamps, Time Zones and Daylight Saving.
In pandas, rolling(window=20) uses the current and previous 19 values, which is safe; a center=True option would use future values. See NumPy and Pandas for Traders.
Minimum periods#
Early in a series, a rolling window may not be full. Decide whether to wait until enough data exists or allow partial windows. Statistics from very few observations are unreliable.
Frequently asked questions#
What is a rolling window?#
A fixed length window of the most recent observations, used to calculate statistics that update as new data arrives and old data drops out.
What is the difference between rolling and expanding windows?#
A rolling window keeps a fixed length and drops old data; an expanding window keeps growing and includes all data from a start date.
How long should a rolling window be?#
It depends on the goal: short windows react quickly but are noisy, long windows are stable but slow. Many traders compare several lengths or use exponential weighting.
Next, learn how market behaviour changes in Structural Breaks and Regime Changes.
3 quick questions on this lesson. Get them all right to finish it.
Turn on JavaScript to take the quiz.
Mentioned in
- Percentiles, Quantiles and Z-ScoresMath and Statistics
- Covariance and CorrelationMath and Statistics
- Granger CausalityMath and Statistics
- GARCHMath and Statistics
- Currency CorrelationsForex
- NumPy and Pandas for TradersProgramming and Data