Skip to content

Automation

Backtesting trading strategies: methods, metrics and pitfalls

A backtest applies a strategy's rules to historical data to estimate how it would have behaved. It is a filter for bad ideas, not proof of a good one. The most common errors make results look better than reality, so a careful backtest is designed to be pessimistic.

Key points

  • Historical results do not predict future results.
  • Look-ahead bias, survivorship bias and ignored costs inflate backtests.
  • Judge results by drawdown, trade count and robustness, not just total return.
  • Walk-forward and out-of-sample tests reduce, but do not remove, overfitting.

What a backtest can and cannot tell you

A backtest answers a narrow question: if these exact rules had been followed on this data, with these assumptions about costs and fills, what trades would have occurred? It can reveal that an idea loses money, trades too rarely, or suffers deep drawdowns. It cannot show that the future will resemble the past.

Data quality comes first

  • Use data with correct session times and time zone; Indian equity sessions run on IST.
  • Adjust share prices for splits and bonuses, or your signals will react to artificial jumps.
  • Handle futures expiries and rollovers explicitly; a continuous series is a construction with its own assumptions.
  • Look for gaps, duplicate bars and zero-volume periods, and decide how to treat them before testing.
  • Know whether your candles are traded prices or transformed values such as Heikin-Ashi averages.

Biases that inflate results

Common backtest biases
BiasWhat happensMitigation
Look-aheadRules use information not available at decision time, such as the bar's close for an entry at its openEvaluate on closed bars and enter on the next bar
SurvivorshipThe test universe excludes companies that were delisted or removed from an indexUse point-in-time constituent lists
OverfittingParameters are tuned until noise looks like signalFew parameters, out-of-sample and walk-forward tests
Cost blindnessBrokerage, taxes, exchange fees and slippage are ignoredModel all costs conservatively
Fill optimismLimit orders are assumed filled whenever price touches themRequire price to trade through, or add slippage

Modelling costs and fills

Indian trades carry several charges: brokerage, securities transaction tax or commodities transaction tax, exchange transaction charges, SEBI turnover fees, stamp duty and GST on certain charges. Rates differ by segment and change over time, so take current figures from your broker's charges page and apply them per trade.

Add slippage on top. A simple approach is a fixed number of ticks per side, larger for less liquid instruments and for stop orders, which tend to fill at worse prices during fast moves. High-frequency strategies with small average profits are the most sensitive to these assumptions.

Reading the results

Metrics worth checking
MetricWhy it matters
Number of tradesFew trades mean results may be luck
Expectancy after costsAverage outcome per trade in rupees or R
Maximum drawdownThe deepest peak-to-trough fall; can you sit through it?
Profit factorGross profit divided by gross loss, after costs
Longest losing streakTests your sizing and your patience
Results by year or regimeShows whether a few periods carry everything
Maximum drawdown
Max drawdown = max over time of (previous equity peak − current equity) ÷ previous equity peak

Out-of-sample and walk-forward testing

Split history into an in-sample period for development and an out-of-sample period you look at only once at the end. Walk-forward analysis repeats this: optimise on a window, test on the following window, roll forward and stitch together only the test-window results. If performance collapses outside the tuning windows, the strategy was probably fitted to noise.

After historical testing, forward paper trading reveals issues history cannot: data delays, session-start behaviour and operational errors. The paper testing tutorial walks through that stage.

A pre-flight checklist before trusting a backtest

  1. Can every rule be evaluated with data available at that moment, with entries on the next bar?
  2. Does the instrument universe include stocks that were later delisted or removed from indices?
  3. Are all charges and a realistic slippage allowance applied to both entry and exit?
  4. Is the trade count large enough, and are results spread across years rather than concentrated in a few trades?
  5. Do nearby parameter values give broadly similar results?
  6. Has the out-of-sample period been examined only once?
  7. Would you be able to tolerate the maximum drawdown and the longest losing streak with real money?

If any answer is no or unknown, treat the result as provisional. A modest result that survives these checks is more informative than an impressive one that does not.

Frequently asked questions

How many trades does a backtest need?

There is no fixed number, but a few dozen trades say little about a strategy's long-run behaviour. More trades across different market conditions give more information.

Why do backtests look better than live results?

Typical reasons are underestimated costs and slippage, look-ahead or survivorship bias, overfitted parameters and changes in market behaviour after the test period.

Can AlgoStok backtest options strategies?

AlgoStok refuses option backtests that lack contract data rather than inventing prices. Historical testing features depend on the data available in your environment.