Blog / Trading systems

Walk-forward testing explained with a worked example

The easiest way to make a trading strategy look good is to tune it on the same data you test it on. Try enough settings and one will look brilliant on the past, by luck as much as skill. Walk-forward testing is the standard defence: the strategy is always scored on data its settings never saw.

The idea

  1. Split your history into consecutive windows.
  2. Tune the strategy’s settings on one window: the in-sample period.
  3. Test those settings, unchanged, on the window that comes straight after: the out-of-sample period.
  4. Slide both windows forward and repeat.
  5. Judge the strategy only on the joined-up out-of-sample results.

A worked example

Say you have a breakout strategy with one setting: how many bars of range it waits for, from 10 to 50. You have three years of 15-minute bars, from January 2022 to December 2024. You choose six months to tune and three months to test.

  • Round 1. Tune on January to June 2022. The best setting is 30 bars. Test 30 bars on July to September 2022.
  • Round 2. Tune on April to September 2022. The best setting is now 25. Test 25 on October to December 2022.
  • Round 3. Tune on July to December 2022. Best: 35. Test on January to March 2023.
  • Continue until the test window reaches December 2024.

You end up with about ten three-month test periods, each traded with settings chosen without seeing it. Join them together and that is your walk-forward result.

Reading the results

Compare in-sample and out-of-sample. Out-of-sample results are almost always worse. A modest drop is normal. A collapse means the strategy was mostly fitted to noise.

Look at the settings. If the best setting jumps around wildly each round, from 10 to 50 to 15, the strategy has no stable edge in that setting. If it stays in a narrow band, that is a better sign.

Look at each window, not just the total. A good total made by one lucky quarter is not a good total.

Run it across instruments. A strategy that only works on one contract deserves suspicion. Our research tooling runs walk-forward tests across many instruments at once, so a result that holds on one and fails on the rest shows up immediately.

Common mistakes

  • Peeking. Adjusting the strategy after seeing out-of-sample results, then re-running. That turns the test data into tuning data.
  • Windows too short. Too few trades in each test period to mean anything.
  • Ignoring costs. Leaving out fees and slippage flatters every result.
  • A leaky simulator. Walk-forward cannot rescue a backtest that can see the future. Fix look-ahead bias first.

Rolling or anchored windows

The example above uses rolling windows: the tuning period is always six months, sliding forward. The alternative is anchored windows: the tuning period always starts at the beginning of your data and grows each round, from six months to nine to twelve.

  • Rolling adapts to recent market behaviour, and drops old conditions that may no longer apply.
  • Anchored uses more data each round, which steadies the chosen settings, but adapts more slowly.

Neither is right for every strategy. Running both and comparing them is itself informative: if the results differ wildly, the strategy is sensitive to which history it learned from.

This post is about software engineering. It is not investment advice, and no software can guarantee that a strategy makes money.

Where this comes from. We do this work for clients. See trading systems, or the case study on Market structure research.

Questions about this post? Email contact@naxfront.com. Nancy reads every one.

WhatsApp