# 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.

Source: https://learn.tradelabsai.com/math/rolling-and-expanding-windows/  
Track: Math and Statistics · Level: Advanced · Updated: 2026-10-03  
Publisher: TradeLabs AI (https://tradelabsai.com). Education, not financial advice.  
Cite as: TradeLabs Learn, "Rolling and Expanding Windows", https://learn.tradelabsai.com/math/rolling-and-expanding-windows/

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](https://learn.tradelabsai.com/indicators/moving-averages-explained/) |
| Rolling volatility | 20 or 60 day standard deviation of returns | [Historical and Realized Volatility](https://learn.tradelabsai.com/volatility/historical-volatility/) |
| Rolling correlation | 60 day correlation between two assets | [Covariance and Correlation](https://learn.tradelabsai.com/math/covariance-and-correlation/) |
| Rolling beta | 1 year beta to the market | [Alpha and Beta](https://learn.tradelabsai.com/portfolio/alpha-and-beta/) |
| Rolling z score | Price relative to its 20 day mean and volatility | [Percentiles, Quantiles and Z-Scores](https://learn.tradelabsai.com/math/z-scores/) |
| Rolling Sharpe ratio | Performance over the last 12 months | [Sharpe Ratio](https://learn.tradelabsai.com/portfolio/sharpe-ratio/) |

## Choosing a window length

**Example: Short vs long volatility windows**
A stock's 10 day volatility jumps from 20% to 45% after an earnings surprise, then falls back within three weeks. Its 120 day volatility rises only from 22% to 26% and stays elevated for months as the event remains in the window, then drops suddenly when the event leaves it. Short windows react fast but are noisy; long windows are stable but slow and have "cliff effects" when big events drop out. Many practitioners compare several windows, or use exponentially weighted averages.

## 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](https://learn.tradelabsai.com/math/garch/) and [Value at Risk (VaR)](https://learn.tradelabsai.com/portfolio/value-at-risk/).

## 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](https://learn.tradelabsai.com/research/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](https://learn.tradelabsai.com/research/look-ahead-bias/).
- **Align timestamps carefully,** especially across markets. See [Timestamps, Time Zones and Daylight Saving](https://learn.tradelabsai.com/programming/timestamps-and-time-zones/).

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](https://learn.tradelabsai.com/programming/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](https://learn.tradelabsai.com/math/regime-changes/).

## Continue learning

- Next lesson: [Structural Breaks and Regime Changes](https://learn.tradelabsai.com/math/regime-changes/)
- Previous lesson: [Granger Causality](https://learn.tradelabsai.com/math/granger-causality/)
- Related: [Granger Causality](https://learn.tradelabsai.com/math/granger-causality/): Granger causality tests whether past values of one series help predict another. Learn how the test works, market lead lag examples and its limits.
- Related: [Time Series Basics](https://learn.tradelabsai.com/math/time-series-basics/): Market data is a time series: values ordered in time. Learn the components of a time series, why order matters, returns vs prices and the core tools for analysis.
- Related: [Walk-Forward Analysis](https://learn.tradelabsai.com/research/walk-forward-analysis/): Walk forward analysis repeatedly optimises a strategy on past data and tests it on the next period. Learn how it works, window choices, efficiency ratios and limits.
- Related: [Look-Ahead Bias](https://learn.tradelabsai.com/research/look-ahead-bias/): Look ahead bias happens when a backtest uses information that was not available at the time. Learn common sources, real examples and how to prevent it in code.
- Related: [Historical and Realized Volatility](https://learn.tradelabsai.com/volatility/historical-volatility/): Historical volatility measures how much a price actually moved, using past returns. Learn the standard formula, range based estimators and how traders use it.
- Related: [Moving Averages Explained](https://learn.tradelabsai.com/indicators/moving-averages-explained/): Moving averages smooth price to show the trend. Learn SMA vs EMA, popular settings like the 20, 50 and 200 day, crossovers, dynamic support and common mistakes.
