TRADING / PRACTICAL GUIDE
Backtesting Trading Strategies Without Cheating
A backtest applies defined rules to historical data. It can show how those rules would have behaved under the data and assumptions used. It cannot prove future profitability.
In this guide
- A toy strategy, stated precisely
- Where the look-ahead leaks in
- The one-line fix, and why the curve changes
- Now add what it actually costs to trade
- Reserve data the rules never saw
- A short checklist before trusting a result
- What even a clean backtest still cannot tell you
- What to do next
- Sources and further reading
A toy strategy, stated precisely
Take a simple rule to test: when a stock finishes a day more than 3% higher and that day's volume is above its prior 20-day average, enter at the next session's open and exit at that session's close. The timing words matter. A naive version often says to buy at the triggering day's close, even though the final close and volume are only known after that price has passed.
Where the look-ahead leaks in
The rule as written decides whether to buy using the day's own closing price and the day's own volume — and then, in a naive backtest, fills the trade at that same closing price. In live trading, you cannot know a day's official closing price and final volume until the session has already ended, at which point the closing print is no longer available to trade into. The backtest is quietly using information — today's own close — to both trigger and fill a trade that a live trader could never have placed.
Worth remembering: This single mistake is one of the most common ways an attractive-looking backtest turns out to be unplayable. The rule wasn't wrong. The fill assumption was.
The one-line fix, and why the curve changes
The fix is a single change: shift every fill to the next available price after the signal is known — in this case, the next day's open, not the triggering day's close. Once that change is made, the same rule stops trading into a price it could never have reached, and any result that depended on same-day, same-price fills disappears along with it. A rule that still looks reasonable after this fix has cleared a basic bar. A rule that only worked with the impossible fill has not.
Now add what it actually costs to trade
Even with the look-ahead fixed, the rule still assumes a perfect fill exactly at the next day's opening price, with nothing paid to trade. A more honest version subtracts a spread, a commission, and some slippage from every entry and exit before the result is counted.
| Cost source | What it does to the result |
|---|---|
| Bid-ask spread | Buying at the ask and selling at the bid, instead of at the mid-price the raw data usually shows |
| Commission | A fixed or per-share fee subtracted from every entry and every exit, not just the net position |
| Slippage | The gap between the intended price and the price actually available once the order reaches the market, often worse in fast-moving or thin names |
| Borrowing cost | Relevant only if the rule shorts a stock — a cost with no equivalent when the rule only buys |
A strategy that trades often, in small names, with a tight raw edge is exactly the kind of rule these costs can erase entirely. A strategy that trades rarely, in liquid names, with a wide raw edge can usually absorb them and still say something useful.
Reserve data the rules never saw
Even after the look-ahead is fixed and costs are included, testing and adjusting the same rule against the same stretch of history repeatedly turns that history into training data rather than a genuine test. Split the available data in two: develop and adjust the rule using only the first portion, then run it exactly once, unchanged, against the second. A rule that survives that single unseen run, even with a smaller or less exciting result, is worth more than one polished against the full history.
A short checklist before trusting a result
- Every rule is executable: No phrase like "strong trend" survives without a measurable definition — the universe, timeframe, entry, exit and sizing are all specified.
- No information from the future: Every decision uses only data that would genuinely have been available at that exact moment.
- Real costs are included: Spread, commission and slippage are subtracted before the result is read, not after.
- Some data was held back: At least one unseen stretch of history was reserved and used only once, at the end.
What even a clean backtest still cannot tell you
A backtest, even one built without these mistakes, only shows how a rule would have behaved under the specific data, costs and period tested. It cannot prove the rule will work in conditions that have not occurred yet, and a small sample of trades — even a clean one — can still be the product of chance rather than a durable edge.
What to do next
- Write out one rule you currently believe in as precisely as the toy example above, including the exact fill timing.
- Check whether any part of the entry decision uses a price or value that would not have been known at that exact moment.
- Rebuild the test with a shifted fill, realistic costs, and one unseen stretch of history reserved until the very end.
Sources and further reading
Continue learning
Educational purpose: This guide provides general education. It does not provide personalised financial, investment, legal or tax advice.