Dalal Street Blogs provides information for educational purposes only and does not constitute investment advice. Please consult a SEBI-registered investment adviser before making any investment decisions.
CAUTION: Dalal Street Blogs publishes market news and educational content only. We do not promote, recommend or endorse any stock, and nothing here is a solicitation to buy or sell securities. Please consult a SEBI-registered adviser before investing.

Indian Stock Market News, Analysis and Trading Guides

Trading Strategy

Walk Forward Analysis: Test a Trading Strategy on Unseen Data

Walk forward analysis: a mountain trail of wooden steps climbing forward along a ridge, one step at a time

Image by adege on Pixabay (Pixabay Content License)

By Dalal Street Blogs Editorial10 min read

Every trader who has ever built a system knows the feeling. You tweak a moving-average crossover on ten years of Nifty stock data, nudge the lookback from 20 to 23 days, add a filter, and suddenly the backtest shows a smooth equity curve climbing at 26% a year. Then you trade it live and it bleeds. Walk forward analysis is the test that tries to catch that problem before your money does, by repeatedly checking a strategy on data it has never seen.

This guide explains how it works, why it is harder to fool than an ordinary backtest, and how to run one yourself on an Indian stock, with a fully worked (and clearly hypothetical) example.

In this article
  1. What walk forward analysis actually is
  2. Why a normal backtest flatters you
  3. Walk forward vs other kinds of testing
  4. A worked example on a hypothetical stock
  5. How to run your own walk forward test
  6. Common ways people still fool themselves
  7. Where walk forward analysis fits in your process

What walk forward analysis actually is

The idea is credited to Robert E. Pardo, who set it out in his 1992 book Design, Testing and Optimization of Trading Systems and expanded it in a 2008 second edition, according to the Wikipedia entry on walk forward optimization. The mechanics are simple to describe.

  1. Take a block of historical data, the in-sample window, and optimise the strategy on it. That means finding the parameter values that worked best there.
  2. Lock those parameters and run them, untouched, on the stretch of data that comes straight after. This is the out-of-sample window. Record the result.
  3. Slide both windows forward by the length of the out-of-sample period and repeat.
  4. When you reach the end of the data, stitch all the out-of-sample results together. That stitched record, not the in-sample numbers, is your estimate of how the system performs.

The Wikipedia article gives a compact version: optimise on the past 1,000 bars, test on the next 200, roll forward 200 bars, and do it again. Its other example uses months: optimise a moving-average rule on months 1 to 3, validate on month 4, re-optimise on months 2 to 4, validate on month 5, and so on. The out-of-sample months, strung together, are the performance that counts.

An ordinary backtest asks how good a strategy could have been. Walk forward analysis asks how good it would have been if you had to pick the settings without knowing the future.

Why a normal backtest flatters you

A plain backtest is useful. Investopedia’s backtesting guide describes it as using historical data to estimate how a strategy might perform without risking capital, and it stresses that a proper backtest should cover different market conditions and include all trading costs. The trouble starts when you optimise on the same data you then judge yourself by.

That is the textbook setup for overfitting. Investopedia defines overfitting as a modelling error where a function is fitted too closely to a limited set of data points, so it works on that data and not much else. Its own market example is pointed: search enough historical data and you can usually find rules that seem to predict returns, which then turn out to be chance patterns once you look outside the sample. The fix it recommends is the same in every case: test against data that was not used to build the model.

Think about what happens when you try 200 combinations of fast and slow moving averages on one ten-year chart. Some combination will look brilliant purely by luck. If you then quote that combination’s return, you are quoting the best of 200 lottery tickets, not the expected payout of one ticket.

A single out-of-sample test helps, but only once. If you split your data 70/30, optimise on the 70, check the 30, dislike the result, and go back to tweak, your “out-of-sample” data has quietly become in-sample. Walk forward analysis reduces this problem by giving you many separate out-of-sample periods, each judged with parameters chosen strictly from the past.

Walk forward vs other kinds of testing

People mix up a few terms here, so it helps to line them up.

  • Backtesting runs fixed rules over historical data. Fast and cheap, but easy to overfit if you optimise.
  • In-sample / out-of-sample split holds back one slice of history for a single honest check.
  • Walk forward analysis repeats the optimise-then-test cycle across the whole history, rolling forward each time. Wikipedia describes it as a specific application of cross-validation, with the twist that the test window always comes after the training window in time.
  • Forward performance testing, or paper trading, follows the system’s signals in a live market with hypothetical money. Investopedia treats this as the next validation step after a backtest.

The time order matters a lot in markets. Ordinary cross-validation from statistics often shuffles data into random folds. With prices, that would let a model “learn” from 2024 and then get tested on 2021, which is a peek into the future that no real trader gets. Walk forward keeps the arrow of time pointing the right way.

A worked example on a hypothetical stock

Let’s build one. Everything below is hypothetical: the stock (call it ABC Ltd), the returns and the results are made up purely to show the method. They are not backtest results for any real share.

The strategy

A plain long-only moving-average crossover on daily closes. Buy ABC when the fast simple moving average crosses above the slow one; exit when it crosses back below. The two settings we optimise are the fast and slow lookbacks. Our grid tries fast values of 10, 15, 20, 25 and 30 days and slow values of 50, 60, 80 and 100 days. That is 20 combinations.

The windows

We have ten years of hypothetical daily data, 2016 to 2025. We use a three-year in-sample window and a one-year out-of-sample window, rolling forward a year at a time:

  • Optimise on 2016-2018, test on 2019
  • Optimise on 2017-2019, test on 2020
  • …and so on until we optimise on 2022-2024 and test on 2025

That gives seven out-of-sample years, 2019 to 2025. Note that the very first three years never appear as test data. That is the price of the method: you always “spend” one in-sample window to get started.

The results

Here is what our hypothetical run produced. “In-sample return” is the annual return of the best parameter pair on its three-year optimisation window. “Out-of-sample return” is what that same pair delivered in the following year, with no changes.

Test year Best pair (fast/slow) In-sample return (annual) Out-of-sample return
2019 20/60 24.0% 9.5%
2020 15/50 21.5% -6.0%
2021 15/50 19.0% 14.0%
2022 25/80 22.5% 3.0%
2023 25/80 18.5% 11.5%
2024 20/60 20.0% 7.0%
2025 30/100 23.0% -2.5%

Now the arithmetic.

  • Average in-sample annual return: (24.0 + 21.5 + 19.0 + 22.5 + 18.5 + 20.0 + 23.0) / 7 = 148.5 / 7 = about 21.2%.
  • Average out-of-sample annual return: (9.5 – 6.0 + 14.0 + 3.0 + 11.5 + 7.0 – 2.5) / 7 = 36.5 / 7 = about 5.2%.
  • Out-of-sample years that made money: 5 of 7.

Run ₹5 lakh through the seven out-of-sample years in sequence, compounding each year’s return, and it ends at roughly ₹7.03 lakh. That works out to about 5.0% a year compounded.

Compare that with the number a careless tester would have quoted. Suppose the single best parameter pair, optimised over the full 2016-2025 history, showed 26% a year. At that rate, ₹5 lakh would have grown to about ₹25.2 lakh over the same seven years. The walk forward result is a little over a quarter of that. Same strategy, same stock, very different story.

In our hypothetical run, the in-sample average was about 21% and the out-of-sample average about 5%. The gap between those two numbers is the size of your self-deception.

Reading the result honestly

One simple check we like is to divide the average out-of-sample return by the average in-sample return. Here that is 5.2 / 21.2, or roughly 0.25. In plain words, the strategy kept about a quarter of the edge the optimiser promised. There is no universal pass mark for this ratio, and you should be suspicious of anyone who quotes one as gospel, but a very low figure tells you most of the in-sample performance was curve-fitting.

Look at the parameters column too. The “best” pair jumped around: 20/60, then 15/50, then 25/80, then 30/100. A robust idea usually has a broad zone of settings that work reasonably well. When the winner keeps hopping to a different corner of the grid, the optimiser is mostly chasing noise.

And 5.2% a year before costs, on a long-only stock strategy that sits in cash part of the time, is not obviously better than just holding an index fund. Once you subtract brokerage, STT, exchange charges, slippage and tax on short-term gains, it may well be worse. A walk forward test that leaves you with that conclusion has done its job. It has saved you from trading a system that only worked in the rear-view mirror.

How to run your own walk forward test

You can do a basic version in a spreadsheet for a simple rule, though Python or a platform with a built-in optimiser makes life much easier. A practical sequence:

  1. Fix the rules before you look. Write down the entry, exit, position size and the parameter grid. Keep the grid small. Twenty combinations is plenty for a two-parameter crossover; two thousand is an invitation to overfit.
  2. Choose the window lengths. The in-sample window needs enough trades to mean something. A rule that trades four times a year needs years of in-sample data; an intraday rule might need only weeks. The out-of-sample window is often a fraction of the in-sample one, like the 1,000-bar / 200-bar split in the Wikipedia example.
  3. Pick the optimisation target. Raw return is the obvious choice and often the worst one, because it rewards a single lucky trade. Many testers optimise on something steadier, such as return relative to drawdown or a profit factor with a minimum trade count.
  4. Include costs from day one. Investopedia’s guide makes the point plainly: a backtest that ignores trading costs is not a fair test. For Indian equity delivery and intraday trades, that means brokerage, STT, exchange transaction charges, GST, stamp duty and a realistic slippage allowance.
  5. Roll, record, never revisit. For each window, optimise, lock, test, write down the out-of-sample result, and move on. Do not go back and re-optimise a window because you didn’t like its test year.
  6. Judge only the stitched out-of-sample record. Look at its total return, worst drawdown, number of losing periods and how it compares with simply holding the index.

Common ways people still fool themselves

Walk forward analysis is harder to game, not impossible. A few traps we see often:

  • Re-running the whole walk forward until it looks good. If you change the rules, the grid or the window lengths after seeing the stitched result, then repeat, the entire history has become in-sample. You are back to curve-fitting, just with more steps.
  • Survivorship bias. Testing only on stocks that are in the Nifty 50 today ignores the companies that dropped out, often after falling. Your universe should be what existed at each point in time.
  • Too few trades per window. Three trades in a test year tells you very little. Results that swing between +14% and -6% on a handful of trades are mostly noise.
  • Look-ahead in the data. Using adjusted prices or index constituents that weren’t known at the time quietly leaks the future into the past.
  • One market regime. If all your out-of-sample years were in a strong bull run, the test hasn’t seen a bear market yet. Investopedia’s guidance to cover varied market conditions applies here too.

Wikipedia also notes a subtler point: a strategy can pass a walk forward test and still show some signs of overfitting, especially as you keep bolting on filters. Every extra rule should earn its place in the out-of-sample record, not just the in-sample one.

Where walk forward analysis fits in your process

It is a filter, not a guarantee. A strategy that fails walk forward analysis almost certainly shouldn’t be traded. One that passes has earned the right to the next step, which is paper trading or very small live size, before real capital goes in.

It also pairs well with other robustness checks. A Monte Carlo simulation can reshuffle the out-of-sample trades to show a realistic range of drawdowns, and sensible position sizing keeps one bad stretch from ending the experiment early. For the wider framework, see our pillar on risk management in trading.

The honest summary: most strategies look better in a backtest than they ever will in your account. Walk forward analysis won’t make a weak idea strong. What it does is show you, before you commit money, roughly how much of that shiny backtest was real.

Disclaimer: This article is for educational purposes only and is not investment advice. ABC Ltd, its returns, parameters and all figures in the worked example are hypothetical and do not describe any real security or backtest. Trading in equities involves risk, including loss of capital. Please consult a SEBI-registered investment adviser before making investment decisions.

Leave a Reply

Join the conversation

Commenting is open to registered readers. Sign up with just your email — no password needed, and it takes under a minute.

Sign up to comment Log in