Walk-forward testing explained
Walk-forward testing repeatedly fits a strategy on past data and evaluates it on the period that follows. Learn how it works, how to choose windows, and what it can and cannot tell you.

A single split between in-sample and out-of-sample data is a good start, but it gives you exactly one test of how a strategy behaves on unseen data. If that one out-of-sample period happened to suit the strategy, you learn very little. Walk-forward testing addresses this by repeating the fit-then-test cycle many times as it moves through history.
The idea mirrors how a strategy would actually be run. At any point in time you can only use the past to choose parameters, and you then live with those choices for a while before reviewing them.
The stitched record is the main output. Every point in it was produced by parameters chosen without seeing that data.
In a rolling walk-forward, the training window has a fixed length and slides forward, so old data eventually drops out. In an anchored walk-forward, the training window always starts at the beginning of the dataset and simply grows. Rolling windows adapt faster to changing conditions but use less data per fit. Anchored windows are more stable but can be slow to reflect structural change. Running both is a cheap way to see how sensitive your conclusions are to that choice.
import pandas as pd
def walk_forward_windows(index, train_size, test_size, anchored=False):
"""Yield (train, test) slices of an index that move forward in time."""
start = 0
while start + train_size + test_size <= len(index):
train_start = 0 if anchored else start
train = index[train_start:start + train_size]
test = index[start + train_size:start + train_size + test_size]
yield train, test
start += test_size
dates = pd.bdate_range("2015-01-01", periods=2520) # about ten years of weekdays
for train, test in walk_forward_windows(dates, train_size=756, test_size=126):
print(train[0].date(), "-", train[-1].date(), "| test", test[0].date(), "-", test[-1].date())There is no universally correct setting, but a few principles help:
Pick window lengths before looking at results. Trying many window combinations and keeping the best one reintroduces the very overfitting the method is meant to expose.
Beyond the stitched equity curve, walk-forward testing gives you useful diagnostics:
Walk-forward testing is not a guarantee against bias. Leakage can still creep in at window boundaries, for example when labels or targets look several bars ahead and overlap the start of the test window. Leaving a small gap between training and test data, sometimes called an embargo, reduces this risk. Costs and realistic fills still matter in every window.
The biggest trap is human. If you run a walk-forward test, dislike the result, change the strategy and run it again, the out-of-sample periods are no longer truly unseen. Keep a record of each iteration and, ideally, hold back a final stretch of data that is only used once.
Walk-forward testing does not prove a strategy works. It shows whether your process for choosing parameters holds up when it has to make decisions without hindsight.
Use a simple in-sample test to check that an idea is coherent, walk-forward testing to evaluate the parameter-selection process, and a final untouched holdout plus paper trading to observe behavior under new conditions. Each step answers a different question.
Learn how walk-forward analysis fits into a full research process in the Factor Models & Quantitative Research course, and see how it is planned within Quantzivo Studio's backtesting tools.
Educational content only. This article explains concepts and methods. It is not investment advice or a recommendation to buy or sell any security. Trading involves risk, including loss of capital. Read the risk disclaimer.
A backtest is only as honest as its assumptions. Here are seven common mistakes that make historical results look better than they really are, and how to catch each one.
Read articleRaw price files are rarely ready for analysis. This walkthrough shows how to parse, deduplicate, validate and resample OHLCV data in pandas so your research starts from a trustworthy base.
Read articleNews sentiment models turn headlines and articles into structured scores. This guide explains the main approaches, from word lists to language models, and the limitations every researcher should understand.
Read article