Technical analysisModule 6, lesson 29 of 32
Backtesting
How to turn chart rules into a testable system, measure the results honestly, and include brokerage, taxes, spreads and slippage, so a backtest is a fair test.
In this lesson
- Turn a chart idea into precise, testable rules.
- Describe the steps of a backtest and the measures used to judge it.
- Include brokerage, taxes, spreads and slippage in a backtest.
- Compare a strategy's results with a simple benchmark.
Builds on
- Expectancy, losing streaks and risk of ruinBacktests are judged by expectancy, drawdowns and losing streaks, explained there.
- Adjusted and unadjusted pricesBacktests must use prices adjusted for splits and bonuses.
- Trading costs and taxes on sharesCosts and taxes must be included, using the charges explained there.
On this page
In simple terms
A backtest asks: if I had followed these exact rules in the past, what would have happened? It is the main way to check a chart-based idea before risking money on it.
A backtest can be honest research or a sales pitch, as the misleading backtests lesson explains. This lesson describes how to do the honest kind. The next lessons cover the biases that distort backtests and the problem of overfitting.
Step 1: write precise rules
"Buy breakouts with good volume" cannot be tested. A testable rule specifies everything:
- Universe: which shares, chosen how, and when (for example, all shares in a given index at each date).
- Entry: for example, a daily close at least 2% above the highest close of the previous 60 sessions, with volume at least 1.5 times its 20-day average.
- Exit: a stop at two ATRs below entry, a target, a trailing rule or a time limit.
- Position size: for example, risking 1% of capital per trade, as the position size lesson explains.
- Execution: at what price you assume trades happen, for example the next day's open, not the close that triggered the signal.
If two people following your rules on the same data would get different trades, the rules are not precise enough.
Step 2: use clean, adjusted data
Use prices adjusted for splits and bonuses, as the adjusted prices lesson explains, so that corporate actions do not appear as crashes. Include shares that were later delisted or removed from the index, which the biases lesson explains.
Step 3: include every cost
| Cost | How to include it |
|---|---|
| Brokerage | Your broker's rate per order |
| Securities transaction tax, stamp duty, exchange and regulatory charges, GST | The current rates published by the exchanges and the government |
| Bid-ask spread | Assume you buy at the ask and sell at the bid, or deduct half the typical spread on each side |
| Slippage | The difference between the expected price and the actual fill, larger in thin shares and fast markets |
| Taxes on gains | Short-term gains tax, at current rates, where relevant |
The trading costs lesson explains the charges. Rates change, so take current ones from official sources when you test.
Worked example: Costs on a frequent strategy
A made-up strategy makes 300 trades over three years with a gross profit of ₹1,80,000. Suppose, for this example, that all costs together average ₹700 per round trip.
| Gross profit | ₹1,80,000 |
|---|---|
| Costs: 300 × ₹700 | ₹2,10,000 |
| Net result | −₹30,000 |
The strategy looked profitable and lost money. Frequent trading strategies are the most sensitive to costs.
Step 4: measure the results
Look at several measures, not just total profit:
- Number of trades: too few means results are mostly luck.
- Win rate, average win, average loss and expectancy after costs.
- Total and yearly return on capital.
- Maximum drawdown: the largest fall from a peak in capital.
- Longest losing streak.
- Exposure: how much of the time money was invested.
Step 5: compare with a benchmark
A strategy that made 9% a year sounds good, unless simply holding an index fund over the same period made 12% with less effort and similar risk. Always compare with simple alternatives over the same period:
- holding a broad index;
- holding the same shares the strategy traded;
- random entries with the same exits, which shows whether the entry rule adds anything.
A test the idea can fail
Decide in advance what result would make you reject the idea, for example net expectancy below zero, or a return below the benchmark. If you would find a reason to accept any result, the backtest is not a test.
Why it matters
Backtesting is how traders and fund managers check ideas before risking money. Done honestly, it often shows that an appealing chart idea does not survive costs or beat a simple benchmark, which saves money. Done carelessly, it produces confident numbers that lead to losses.
Limitations
Even a carefully built backtest describes the past. Markets change, costs change, and a rule that worked for a period may stop working. Backtests also cannot capture everything about real trading, such as the emotional difficulty of following rules through a losing streak.
Common mistakes
- Vague rules that leave room for judgement after the fact.
- Ignoring costs and slippage.
- Assuming trades happen at the signal price rather than the next available price.
- Not comparing with a benchmark.
- Testing on too few trades.
Try it yourself
- Write a moving-average crossover rule precisely: universe, entry, exit, size and execution price.
- In a spreadsheet, apply it to five years of adjusted daily prices for one share.
- Count the trades, and calculate the result before and after an assumed cost per trade.
- Compare the net result with simply holding the share for the five years.
Key takeaways
- A backtest applies precise rules to past data to see how they would have performed. Every rule must be written so a computer, or another person, could follow it exactly.
- Judge results by expectancy, total return, maximum drawdown, longest losing streak and number of trades, not by total profit alone.
- Include brokerage, taxes, exchange charges, the bid-ask spread and slippage. For frequent trading these can turn a profit into a loss.
- Compare the strategy with a simple benchmark, such as holding an index fund or the same shares, over the same period.
- A fair backtest is a test that the idea can fail. If you would accept any result, it is not a test.
Check your understanding
Finished reading?
Your progress is saved only in this browser.
Terms used in this lesson
- Backtest
- A test of how a set of trading or investing rules would have performed on past prices, as if they had been followed at the time.
- Slippage
- The difference between the price you expected when placing an order and the average price you actually got.
- Expectancy
- The average result per trade, equal to win rate times average win minus loss rate times average loss, after costs.
- Drawdown
- The fall in value of an investment or index from its most recent peak, usually expressed as a percentage.
References
- SEBI turnover fees, STT and other levies, National Stock Exchange of India. The exchange's page on transaction charges and taxes to include in a backtest.
Spotted a mistake or something unclear? Report it by email. Here is how corrections work.
This lesson is general education, not investment advice. Examples use made-up or clearly dated figures to explain ideas; they are not recommendations. Read the disclaimer.