TradeByVote

Learn › How to backtest a trading rule honestly

How to backtest a trading rule honestly

Look-ahead bias, survivorship bias, overfitting, costs and overlapping returns: the traps that make trading rules look better on paper than they really are.

What a backtest is – and why most flatter

A backtest applies a trading rule to past prices as if you had followed it at the time, and measures what would have happened. It is the natural first step for any rule: if a rule did not work in the past, there is little reason to expect it to work in future.

The trouble runs the other way. A rule that worked well in a backtest very often disappoints in real trading, because backtests are easy to get wrong in ways that make results look better than they were. This guide walks through the most common traps. As a running example it uses the backtest behind TradeByVote’s own research, Does a stock screener beat random picks?, including the places where that test falls short.

In brief, that test took the five chart rules of an open-source stock scanner, applied them to daily closing prices of about 1,600 liquid US stocks on each of 190 trading days in 2026, selected the nine most heavily traded hits each day and measured their returns after 1, 5 and 20 trading days.

Look-ahead bias

Look-ahead bias means the test uses information that was not yet available at the moment of the decision. The classic mistake is to generate a signal from today’s closing price and assume you bought at today’s opening price. Subtler versions use financial data that was published weeks after the period it describes, or index membership as it is today rather than as it was then.

How our backtest handles it. Signals are computed only from closing prices up to and including the signal day, and returns are measured from that day’s close onwards – so a trader could in principle have acted at the close.

Where it falls short. The free price data used has no historical trading volume. So the liquidity filter (price at least $5 and at least one million shares traded per day) and the ranking that picks the top nine hits both use today’s price and volume, not the values on the signal day. That is look-ahead: a company whose shares rose strongly during the year tends to be more valuable and more heavily traded today, so it is more likely to pass the filter and rank high. Stocks that sank below $5 drop out. Both effects tilt the results in a favourable direction, and the ranking effect benefits the selected list more than the random comparison.

Survivorship bias

A test that only includes companies that exist today ignores the ones that went bankrupt, were taken over or were delisted along the way. Those missing companies are disproportionately the failures, so the surviving universe looks healthier than the real market did.

Our backtest starts from a list of currently listed US symbols. Companies that disappeared during the test period are therefore missing. Over nine months the effect is smaller than over decades, but it is not zero, and it pushes every number in the same direction: upwards. The honest fix is a database that includes delisted securities, which is rarely free.

Data snooping and overfitting

If you test enough rules, some will look excellent purely by luck. Suppose you try 20 rules that have no real edge, each judged by a test that a useless rule passes 5 % of the time by chance. On average one of the 20 will pass. If you then publish only that one, you have found noise, not a strategy. This is called data snooping, data mining or multiple testing.

Overfitting is the same problem inside one rule: adjusting parameters – 20 days or 25? RSI below 30 or below 28? – until the past results look best. Every adjustment fits the rule more closely to the particular history you tested and less to the future.

Our backtest did not tune any parameters; it uses the scanner’s rules exactly as published. But it does report five patterns separately, and in our follow-up, Five chart patterns tested, one of them looks noticeably better than the others. With five candidates, one standing out by chance is quite likely, and that article treats the result accordingly.

Costs and slippage

A backtest that ignores trading costs can turn a losing rule into a winning one, especially for rules that trade often. Besides commissions there is slippage: the difference between the price in the data and the price you would really get, including the bid-ask spread.

Our backtest subtracts 0.1 % per round trip (buy plus sell). For heavily traded US shares and reasonably large positions, that is a plausible order of magnitude. For a small account it is optimistic. Spread a €10,000 portfolio across nine stocks a day in five daily slices, as our portfolio simulation does, and each position is worth about €220. A minimum commission of $1 per order – the minimum in the fee model on our page on trading costs – is then roughly 0.4 to 0.5 % on each side, several times the assumed cost, before any currency conversion. Always check whether a backtest’s cost assumption fits the account size you have in mind.

Overlapping periods: fewer observations than it seems

Our backtest contains 1,674 picks with a 5-day return. That sounds like a large sample. It is not, for two reasons.

Treating these as independent makes estimates look far more precise than they are. Our test therefore compares the daily list with random picks from the same day, and keeps only every fifth day for the 5-day comparison and every twentieth day for the 20-day comparison. That leaves 38 independent 5-day periods and just 9 independent 20-day periods out of 190 trading days – a much more sobering sample size.

Compare with random, not only with the index

Most backtests compare a rule with a market index. That is useful, but it mixes two questions: did the rule pick well, and did the kind of stocks it can choose from do well? A rule that selects from mid-sized US stocks, for example, will beat or trail a large-company index partly because of how mid-sized stocks did in that period, not because of the rule.

A cleaner baseline is random selection from the same universe, on the same days, with the same number of stocks. Our test draws nine stocks at random from the same liquid universe each day, using a fixed random seed so anyone re-running the code gets the same draw. This made a real difference: over 20 days, the random picks beat the S&P 500 only 43 % of the time, so even a rule with no skill would have looked different from the index. The question that matters is whether the rule beats chance.

In-sample and out-of-sample

The data used to build or choose a rule is called in-sample. Data the rule has never seen is out-of-sample. Results only start to mean something when a rule chosen in-sample holds up out-of-sample. A common approach is to set aside a later period before starting, design on the earlier part and test once on the reserved part – once, because testing repeatedly on the reserve turns it into in-sample data too.

For our backtest the rules were written by someone else before we tested them, which helps. The most convincing out-of-sample test, though, is the future: the same comparison now runs every day on our server, recording each morning list and nine random picks as they happen, with no possibility of hindsight.

Confidence intervals: how sure can we be?

An average return on its own says nothing about how much luck is in it. A confidence interval gives a range of values consistent with the data. A rough 95 % interval for an average is:

mean ± 1.96 × standard error standard error = standard deviation ÷ √(number of independent observations)

Our backtest’s daily list beat random picks by 0.66 percentage points per 5-day period on average, with a rough 95 % range of −0.43 to +1.76 points. Because the range includes zero, the data cannot rule out that the true difference is nothing at all. With only 9 independent 20-day periods the range runs from about −7.4 to +8.2 points – almost no information. The formula also assumes the observations are independent and that the average behaves roughly normally; with heavily skewed returns and few periods, even this range should be read loosely.

The square root in the formula is the bad news for anyone in a hurry: to halve the width of the range you need four times as many independent observations.

A checklist

QuestionOur backtest
Only data available at the time?Prices yes; liquidity filter and ranking use today’s volume
Delisted companies included?No – survivorship bias
Parameters tuned on the test data?No, but five patterns compared
Realistic costs?0.1 % per round trip – optimistic for small accounts
Independent observations counted honestly?Yes, for the comparison with random
Random baseline?Yes, same universe, same days, seeded
Out-of-sample test?Running live from now on
Several market phases?No – one period of about nine months

No backtest passes every line. The point of the checklist is to know which direction the remaining errors push – and to be suspicious of any result, including our own, that has not been through it. Our guide on behavioural biases explains why we all want to believe the good-looking ones.

Next guide: Does a stock screener beat random picks? 190 trading days of data · All guides

Last updated November 2026 · Educational content only. Nothing on this page is a recommendation to buy or sell any security.