strateva.

Articles

Walk-forward analysis: rolling, expanding and nested — and when it doesn't count

In this article

You have a strategy with a couple of parameters — say, a moving-average length and a filter level. You optimise it over ten years of data, pick the best combination, and the backtest looks beautiful.

The trouble is that backtest answers a question you don't care about: "which parameters would have worked best, knowing what happened?" What you actually want to know is different: "if I had used this method to pick parameters at the time, how much would I have made?"

Walk-forward analysis is how you answer that second question. This article explains how it works, the two ways of moving the window (rolling and expanding), when nested is needed, and the mistakes that turn it into an ordinary backtest with a different name.

In-sample and out-of-sample, in one sentence

In-sample data is what you use to decide: choose parameters, filters or the model itself. Out-of-sample data is data the strategy hasn't seen while making those decisions, and is used only to measure.

A backtest optimised over the whole history has no out-of-sample. The whole period was used to decide, so the whole period is "contaminated" by that decision.

How a walk-forward works

The idea is to simulate the passage of time:

  1. Take a first chunk of history — say, the first three years. That's the training window.
  2. Inside that window, choose the parameters using whatever rules you've fixed.
  3. Apply those parameters, untouched, to the next chunk — say, six months. That's the test window.
  4. Move the calendar forward six months and repeat.
  5. At the end, stitch all the test windows together. That chained curve is the out-of-sample result.

Every piece of that curve was produced with parameters chosen without seeing it. It's as close to live trading as historical data allows.

Rolling, expanding and nested

Rolling. The training window always has the same length and slides forward: years 1–3, then 1.5–3.5, and so on. It forgets the distant past. Useful if you believe markets change and old data is no longer representative. The price: each decision is made with less data, so it's noisier.

Expanding. The window starts on the same date and keeps growing: years 1–3, then 1–3.5, then 1–4. It uses all the history available at each point. Decisions are more stable, but they adapt more slowly when market behaviour shifts.

Neither is "the right one". What matters is choosing one before you look at the results, and not trying both to keep whichever looks better. That would be optimising again, just one level up.

Nested. It isn't a third kind of window: it's combined with rolling or expanding. This is how walk-forward is done whenever parameters are selected — and that includes the two-parameter moving-average example. The selection is part of the process being evaluated: inside each training window the parameters are chosen with a fixed rule, and the test window only measures. The windows (folds), the selection rule and the tests are fixed before any result is seen. Splitting an already-optimised curve in two after the fact and calling the second half "out-of-sample" is not enough.

The gap between training and testing

One technical detail is often forgotten. If your strategy predicts the return over the next 20 days, the last training observation has an "answer" that only becomes known 20 days later. If the test window starts the very next day, that answer overlaps the test period: training has accidentally seen part of the future.

The fix is to leave a gap (also called an embargo) between the training and test windows, at least as long as the horizon of what you're predicting. Predicting one day ahead, a small gap is enough. Predicting a month ahead, the gap has to be at least a month. The gap doesn't appear on its own: it has to be set explicitly in the test design.

When a walk-forward doesn't count

These are the most common mistakes. Any one of them makes the result look a lot like an ordinary optimised backtest:

  • Doing it after the fact. You first optimise over the whole history, see which parameters work, then "check" with a walk-forward whose search is restricted to those values. The test windows are no longer blind: you've already seen them.
  • Re-running the walk-forward until it looks good. You change the window length, the re-optimisation period or the parameter range until the out-of-sample curve pleases you. Each re-run uses up a little of the test data's cleanliness. It's the same selection bias we cover in the article on the Deflated Sharpe Ratio and PBO.
  • No gap when the label looks ahead. As explained above, if what you predict spans several days, the windows overlap and the result is inflated.
  • Picking the asset universe with the full history. If the stocks in the test were chosen because they're today's big winners, every test window inherits that information. Walk-forward doesn't fix a badly chosen universe. More in survivorship bias.
  • Normalising or scaling with the full history. Computing a mean, a volatility or a percentile over the whole period and using it in every window pushes future information into the past.
  • Test windows that are too short or too few. Three one-month windows say almost nothing. Out-of-sample results are noisy too, and with little data the noise dominates.
  • No costs or execution delay. If the strategy re-optimises often and changes positions a lot, costs can eat the whole edge. A walk-forward that ignores them is incomplete. More in execution costs.

A good walk-forward isn't a guarantee either

Even when done properly, a walk-forward still uses historical data that you, the author, may already have known. If you designed the strategy after living through the 2020 or 2022 sell-offs, your intuition has been shaped by those periods even if the code never sees them.

That's why it helps to distinguish between:

  • Historical out-of-sample: past data the procedure didn't use to decide.
  • Prospective period: data that didn't exist yet when the strategy was frozen. It's the only test that can't be contaminated by what you knew.

In our own EWMA63 case, a strategy we designed ourselves and published with its report, we state that the parameters were chosen after seeing the history they're tested on, so there's no untouched future period. No re-run can close that limitation; only time can. In the RF100 case the model was fixed, with no parameter search on the evaluation period — the simplest way not to contaminate the test.

What to ask when someone shows you a walk-forward

  • What was decided inside each window, and by which exact rule?
  • Rolling or expanding, and was that chosen before seeing the results?
  • How many walk-forward set-ups were tried before this one?
  • Is there a gap between training and testing? How long, and what's the signal horizon?
  • How was the asset universe chosen?
  • Do the results include costs and the delay between signal and order?

If the answers are vague, treat the walk-forward as an ordinary backtest.

Where it fits in a validation

Our validation can evaluate a causal walk-forward, rolling or expanding, and nested whenever parameters are selected, provided the process can be repeated and the sample is sufficient. The windows, the gap between training and testing, execution delay and costs are agreed when the engagement is scoped, before any results are seen. When the design doesn't allow a clean out-of-sample, the report says so instead of presenting it as if it did.

These checks are part of a quantitative validation. Strateva validates stock, ETF, spot crypto and spot FX strategies, always unleveraged and without margin. We do not take work on CFDs, futures, perpetuals or options. For US stocks and ETFs, commissions are modelled on the Interactive Brokers fee schedule; for spot crypto and spot FX, cost assumptions are agreed for each engagement.

To check whether it fits, email stan@strateva.ai with the market (stocks, ETFs, crypto or FX, spot only), frequency, how many parameters you optimise and whether your walk-forward is rolling, expanding or not built yet. Please don't send private code or passwords in a first message.


Educational and research content. Not investment advice. The window sizes in the text (three years of training, six months of testing) are only an example to explain the mechanism, not a recommendation.

Related cases

EWMA63 — Does beta selection add value beyond de-risking?RF100 — Can a Random Forest beat simple momentum?

All articles

CONTACT

Tell me about your strategy. I will say whether it fits within 48 business hours.

Tell me what you want checked, the asset, the frequency and what you have: rules, a report or code. I will tell you whether it fits and which plan it would be.

What to include in your email

  • Market, instruments and frequency (Spot only).
  • What you want checked, and the period.
  • Language and approximate code size, without pasting the code.
  • What data you have, and whether you need an NDA.

Do not send private code, keys or passwords.

stan@strateva.ai

Reply within 48 h (business days). No private material in the first message. No obligation to commission work.

Who replies

I am Filip Stanciu. I have an MSc in Quantitative Finance from the University of Alcalá, a degree in Finance from the Complutense University of Madrid, and a MiFID II certificate. I have worked in quantitative research for an asset manager, validating stock and ETF strategies. I read and reply to every email myself.

I am not a certification body and I am not a large team. I am one person, and I say so.

Nicolae Filip Stanciu