Learn / Technical analysis

How to backtest a trading strategy

Backtesting runs a trading rule on past prices to see how it would have done. A step-by-step method, which results to read, costs, and the traps to avoid.

Intermediate · 7 min read · Updated 12 Nov 2026

Key points

  • A backtest applies exact entry and exit rules to past prices to see how a strategy would have behaved.
  • Read more than profit: number of trades, win rate, average win and loss, profit factor and maximum drawdown.
  • Include costs and realistic fills; a small edge often disappears after brokerage, taxes and slippage.
  • The biggest danger is overfitting: tuning rules until they fit the past perfectly. Test on data you did not tune on.

Backtesting means taking a trading idea, writing it as exact rules, and running those rules on past prices to see what would have happened. Instead of looking at three charts where an idea "worked", you look at every time the rule would have fired over months or years. It is the closest thing traders have to an experiment, and it turns vague beliefs ("RSI below 30 is a good buy") into numbers you can check.

A backtest cannot tell you what will happen next. Markets change, and a rule that worked for five years can stop working. What it can do is reject bad ideas cheaply, show you how bad the losing streaks can get, and tell you whether an idea is worth testing further with paper trading.

Step 1: write the rules so a computer could follow them

"Buy strong stocks on dips" is not a rule. A rule has no judgement left in it:

  • Instrument and timeframe: NIFTY on 15-minute bars, or a list of F&O stocks on daily bars.
  • Entry: for example, "close crosses above the 20-period EMA and RSI (14) is above 50".
  • Exit: a signal ("close crosses below the 20 EMA"), a stop loss ("1.5% below entry"), a target ("3% above entry"), or a time ("square off at 15:15").
  • Direction and size: long or short, how many shares or lots.

If two people would trade your rules differently, they are not finished. Write the rules down before you run the test, so you cannot quietly change them after seeing the results.

Step 2: run it on enough data

Use as much history as the rule's timeframe allows, covering rising, falling and sideways markets. Aim for many trades: a test with 12 trades proves almost nothing, because a few lucky trades dominate. Fifty is a bare minimum; a few hundred is better. If the rule fires rarely, test it on more instruments.

The ScreenerCharts backtester runs indicator rules, written in the same language as the scans, on NIFTY, other indices and F&O stocks using 5, 15 and 60-minute or daily bars, long or short, with stop loss and target in percent and an optional intraday square-off. It also tests option strategies (straddles, strangles and iron condors) on the site's own option-chain recordings.

Step 3: make the fills realistic

Many backtests look good only because they cheat in small ways. A careful backtest avoids them:

  • No look-ahead. A signal that appears on a bar's close cannot be traded at that same close, because you only know the close after it happens. The ScreenerCharts backtester trades at the next bar's open.
  • Stop before target. When one bar touches both your stop and your target, you cannot know which came first. Assuming the stop came first is the cautious choice, and it is what the backtester does.
  • Bid and ask for options. Option sellers receive the bid and buyers pay the ask. Using the last traded price flatters results. The option backtests use recorded bid and ask where available.
  • Costs. Add a cost per trade for brokerage, STT, exchange charges, GST and stamp duty, plus slippage. Check the current rates with your broker; the trading costs guide lists what to include.

Step 4: read the right results

Total profit is the least useful number on its own. Look at these together:

MeasureWhat it tells you
Number of tradesWhether the result is based on enough cases to mean anything
Win rateShare of trades that made money; meaningless without the average win and loss
Average win vs average lossA 35% win rate is fine if wins are three times losses
Profit factorGross profit ÷ gross loss; above 1 means profitable before anything else goes wrong
Maximum drawdownThe worst fall from a peak in the equity curve: how much pain you must sit through

Expectancy per trade = win rate × average win − loss rate × average loss

Example: 200 trades on a stock, 45% winners averaging ₹2,400, 55% losers averaging ₹1,500, ₹100 cost per trade already deducted. Expectancy = 0.45 × 2,400 − 0.55 × 1,500 = ₹1,080 − ₹825 = ₹255 per trade, or about ₹51,000 over 200 trades. If the maximum drawdown was ₹35,000, ask whether you could keep following the rules through a ₹35,000 losing stretch. If not, trade smaller; the position sizing guide explains how.

Step 5: avoid overfitting

Overfitting means adjusting a rule until it fits the past perfectly. Try enough combinations ("RSI 13, not 14; EMA 23, not 20; stop 1.7%") and one of them will look brilliant purely by chance. It will usually fail on new data. Defences:

  • Keep rules simple. Fewer settings, fewer ways to fit noise.
  • Out-of-sample testing. Tune on one period (for example the first two years), then test the final rules once on a later period you did not touch.
  • Check neighbours. If EMA 20 works but EMA 18 and 22 fail, the result is probably luck. Robust rules work across a range of settings.
  • Test on several instruments. A rule that only works on one stock is suspicious.

Backtesting option strategies

Option rules depend on the premium, which moves with time and volatility as well as price. A rule like "sell the NIFTY ATM straddle at 09:20, exit at 15:15 or on a 30% loss" can only be tested on recorded option prices, not on NIFTY candles. Here is today's NIFTY straddle, the kind of series such a test runs on:

Watch how much the straddle line moves within a single day. A stop on the combined premium will be hit on days with a sharp rise, even if the straddle ends lower by the close, and a backtest shows how often that happened. Recorded history covers a shorter period than stock prices, so option backtests usually include fewer trades; read them with that in mind.

From backtest to real trading

A sensible path: backtest, then test on unseen data, then paper trade for a few weeks to see whether live results resemble the test, then trade small. Keep a trading journal to compare real fills with the backtest's assumptions. If live results are much worse, the usual culprits are costs, slippage and not following the rules exactly.

Common mistakes

  • Too few trades. Twenty trades cannot separate skill from luck.
  • Ignoring costs. Many intraday edges vanish after charges.
  • Survivorship bias. Testing only on today's index members ignores stocks that were removed after falling.
  • Only looking at profit. A big profit with a deep drawdown may be impossible to stick with.
  • Endless tweaking. Each change you make after seeing results uses up the test's honesty.
  • Treating a backtest as a promise. Past results do not guarantee future ones.

Questions

What is backtesting in trading?

Running a trading rule with exact entry and exit conditions on past price data to see how it would have performed: how many trades, how often it won, and how deep the losing streaks were.

How many trades does a backtest need?

There is no magic number, but results from fewer than about 50 trades are unreliable. A few hundred trades across different market conditions give a clearer picture.

What is overfitting?

Tuning a strategy's settings until it fits past data almost perfectly. Such a strategy has usually learned random noise and tends to fail on new data.

Should I include brokerage and taxes in a backtest?

Yes. Add a cost per trade covering brokerage, STT, exchange charges, GST and stamp duty, plus some slippage. Check current rates with your broker. Small edges often disappear after costs.

Can I backtest option strategies on ScreenerCharts?

Yes. The backtester tests straddles, strangles and iron condors on recorded option prices with entry and exit times and stop loss or target on the whole position, as well as indicator rules on indices and F&O stocks.

Does a good backtest mean the strategy will make money?

No. It means the rules would have worked in the past under the test's assumptions. Test on unseen data and paper trade before risking money.

Open the backtester ›

For education only, not investment advice. Live examples use ScreenerCharts' own data at the time you open the page.