Walk-forward backtests without lying to yourself
S. Roy
S. Roy
TEMPLATE. Most backtests that look too good are not wrong about the strategy. They are wrong about what was knowable when. The bias rarely sits in the signal; it hides in the calendar, the universe and the fills.
This post walks through the walk-forward loop in the backtesting engine and the three places it most often leaks the future.
A walk-forward run splits history into consecutive windows. Each one fits on the window before it and trades the one after, so every decision is made with data that existed at the time.
for train, test in walk_forward(bars, train="3Y", test="6M"):
model = strategy.fit(bars.loc[train])
signals = model.predict(bars.loc[test])
book.trade(signals, fills=next_open)The fills=next_open line matters more than it looks: a signal computed on today's close can't be filled at today's close.
Corporate actions, holidays and late prints all shift what a given date means. Bars are adjusted as of each test window's start, never with adjustments announced later.
Testing today's index members over ten years quietly drops every company that failed. The universe is rebuilt at each rebalance from the constituents known on that day:
Fills assume a spread and a participation limit; a strategy that only works with free, instant execution doesn't work. (TEMPLATE: expand.)