Split your history into consecutive windows. Tune the strategy on one window, test the chosen settings on the next window it has never seen, then roll both forward and repeat. The out-of-sample pieces joined end to end are your walk-forward result, and they are the only part that counts.
Why one split is not enough
A single in-sample and out-of-sample split gives you one test on one stretch of market. If that stretch happened to suit the strategy, you learn nothing about the others. Walk-forward testing repeats the split many times. It also copies what you would really do: re-tune every so often on recent data, then trade the result until the next re-tune.
The loop
- Pick the window lengths. A training window long enough to hold many trades (often one to three years of daily bars, or a few months of minute bars), and a test window that matches how often you would re-tune in practice (a quarter, a month).
- Tune on the training window. Run every parameter set you are considering and choose one by a rule you fixed in advance, for example the best Sharpe ratio with at least 50 trades.
- Test on the next window. Run only the chosen set on the test window. Record that result and do not change anything because of it.
- Roll forward by the test window's length and repeat until the data runs out.
- Join the test results. Judge the strategy on the combined out-of-sample trades, never on the training results.
Two variants are common. *Rolling* windows keep the training length fixed and drop the oldest data. *Anchored* windows always start at the first bar and grow. Rolling adapts faster to a changing market; anchored gives each tune more data.
In Python
The loop is a few lines with any engine that can run a time range. The pattern below works with plain pandas, and the same shape applies to any library:
import itertools
import pandas as pd
def windows(index, train, test):
start = index[0]
while start + train + test <= index[-1]:
yield start, start + train, start + train + test
start += test
grid = [dict(fast=f, slow=s) for f, s in itertools.product([5, 10, 20], [50, 100, 200]) if f < s]
oos = []
for t0, t1, t2 in windows(bars.index, pd.Timedelta(days=730), pd.Timedelta(days=90)):
scores = [(run(params, bars[t0:t1]).sharpe, params) for params in grid]
best = max(scores, key=lambda s: s[0])[1]
oos.append(run(best, bars[t1:t2]))
Here run is your backtest call. Note that the training slice ends at the test start: a slice that overlaps the test window leaks the future into the choice.
What to look at in the result
- Out-of-sample against in-sample. A large drop from the training figures to the test figures is the clearest sign of overfitting.
- How often the chosen parameters change. If every window picks a different set from across the grid, the edge depends on tuning noise.
- How many test windows are profitable. One great window carrying ten flat ones is fragile.
- The joined equity curve with realistic costs, since that is what you would have traded.
How Reamer Research handles it
Reamer Research takes start= and end= on every run as epoch seconds UTC, both inclusive, so each training and test window is a time range over the same data file with no slicing or copying. With .bin files the data is memory-mapped once and each window reads its part in place.
import reamer_research as rr
class Crossover:
lookback = 200
def __init__(self, fast, slow):
self.fast, self.slow = fast, slow
def on_bar(self, d):
if d.close[0] == 0:
return None
up = d.close[-self.fast:].mean() > d.close[-self.slow:].mean()
if up and d.position() == 0:
return rr.buy_market(10)
if not up and d.position() > 0:
return rr.close_position()
def run(params, t0, t1):
return rr.backtest(Crossover(**params), 'SPY.bin', start=t0, end=t1,
commission_per_unit=0.005, spread=0.01, periods_per_year=252)
Passing a configured object is one of three forms: the same rule also runs as a function on_bar(data) or as a class the engine creates. Every run is byte-identical at a fixed seed, so a difference between two windows comes from the data and the parameters, never from the run itself. Each result keeps every closed trade, so the out-of-sample trades join into one list with result.trades_frame().
Limits to know
- You write the loop. The engine runs each window; choosing the windows and the selection rule is your code, about a dozen lines.
- Many tickers or long histories need a licence. Evaluation Mode runs one ticker over at most 30 days of bars, which is enough to try the loop but not a multi-year walk-forward.
- No native Windows build. It runs on Linux x86-64, on macOS on Apple Silicon, and on Windows through WSL2.
A $20 month includes the full kit, so you can run a full walk-forward on your own data and cancel if it does not fit.