TradeLabs AILearn

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.

Advanced3 min readUpdated 3 Oct 2026
Markdown
Lesson 37 of 46

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 windowExpanding window
Data usedThe last N observationsAll observations from the start to now
Reacts to changeQuicklySlowly, more so as the window grows
NoiseHigher with short windowsLower
Typical useVolatility, correlation, moving averagesLong run averages, model training in walk forward tests

Common rolling statistics#

StatisticExampleLesson
Moving average20 or 200 day average priceMoving Averages Explained
Rolling volatility20 or 60 day standard deviation of returnsHistorical and Realized Volatility
Rolling correlation60 day correlation between two assetsCovariance and Correlation
Rolling beta1 year beta to the marketAlpha and Beta
Rolling z scorePrice relative to its 20 day mean and volatilityPercentiles, Quantiles and Z-Scores
Rolling Sharpe ratioPerformance over the last 12 monthsSharpe 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.

Check your understanding

3 quick questions on this lesson. Get them all right to finish it.

Turn on JavaScript to take the quiz.

Finished this lesson?Sign in to save your progress across devices.
Next lessonStructural Breaks and Regime ChangesMarkets switch between regimes such as calm and turbulent, or trending and ranging. Learn how to detect regimes, the models used and how to adapt strategies.

Mentioned in