Backtesting means running a trading rule over past prices to see how it would have done. Take a simple rule that holds a Nasdaq-100 fund only while its price is above its average of the last 200 trading days. Tested with a one-day timing error, it returned 29.6% a year. With the error fixed, it returned 17.4% a year.
Both tests ran from August 2017 to September 2026. Nothing else changed. A backtest lets you test an idea before risking money on it, but it is easy to get wrong. Here is how one works, then four traps that make one look better than it is. The guide ends with the checks that catch them.
What is backtesting? Backtesting is testing a trading rule on past prices, as if you had traded it at the time. It shows how the rule would have done in the past, but it cannot prove the rule will work in future.
Picking the best of many rules finds luck. We made 1,000 rules that switched between an S&P 500 fund and cash on a coin toss. The one that did best from 2016 to 2021 returned 4.0% a year from 2022 to 2026, while the fund returned 12.1%.
A one-day timing error, called look-ahead bias, lifted a rule that trades a Nasdaq-100 fund from 17.4% to 29.6% a year.
Costs and missing data flatter too. Costs of 0.1% a trade cut a rule that traded 373 times from 10.0% to 5.6% a year. Only about 38% of US equity funds open in 2006 still existed 20 years later.
Why backtest results shrink on new data
In a 2016 study in the Journal of Finance, R. David McLean and Jeffrey Pontiff tested 97 patterns that academic papers had found to predict share returns. In the years after each paper's data ended, but before it was published, the patterns' returns were 26% smaller than in the papers. After publication, they were 58% smaller.
The authors treat the first drop as the upper limit of what luck in the original tests explains. So a backtest is a best case. The rest of this guide shows how one is built, then the four traps that flatter it.
How a backtest works
A backtest needs four things. A rule written down in full. Price history. A clear time at which each trade happens. A cost for each trade.
Our example rule is simple. A moving average is the mean closing price over a set number of past days. Our rule uses 200 days. Hold QQQ (the Nasdaq-100 exchange-traded fund, or ETF) while its close is above that average. Otherwise, hold cash earning the 3-month Treasury bill rate. A Treasury bill is a loan to the US government, repaid within a year.
The backtest then steps through history one day at a time:
Decide using only the prices known at that day's close.
Trade at the next day's close.
Charge the cost of every trade.
Record the return of whatever is held.
We ran ours in Python on YX Insights price data, with dividends counted. TradingView or a spreadsheet can do the same steps. From August 2017 to September 2026, the rule returned 17.4% a year before costs. Holding QQQ throughout returned 20.7%. Before costs, the rule's worst fall from a peak to a later low was 21.9%, against 35.1% for holding the fund. We read the full report line by line in How to Read a Backtest Report.
Overfitting: the best of 1,000 coin-flip rules
Overfitting means fitting a rule so closely to past data that it learns noise. The quickest way to do it is to test many rules and keep the best. To show this, we built 1,000 rules that carry no information at all. At each month end, each rule tossed a coin. Heads, it held SPY (the S&P 500 ETF) for the next month. Tails, it held cash.

Source: YX Insights price data; FRED (DTB3); YX Insights
The chart follows the coin-flip rule that did best up to December 2021, beside SPY. It then carries on into data it had never seen.
We picked that rule on July 2016 to December 2021. This stretch is the in-sample period, the data used to choose the rule. Only 8 of the 1,000 rules beat SPY over it. The best returned 21.7% a year, against SPY's 18.2%, before costs.
From January 2022 to September 2026, the out-of-sample period, the winner returned 4.0% a year. SPY returned 12.1%. The median rule returned 7.7%. Of the other 999 rules, 844 did better than the winner. By design, its past record could tell us nothing about its future.
Look-ahead bias: a one-day error in the QQQ rule
Look-ahead bias means using information a trader could not have had at the time. The QQQ rule decides at each close. So it can only trade at the next close. The slip is to apply that day's decision to that same day's return. That is as if the rule had traded at the previous close, already knowing today's close.

Source: YX Insights price data; FRED (DTB3); YX Insights
The chart shows the honest rule and the same rule with this one-day slip, both before costs. The slip lifted the yearly return from 17.4% to 29.6%. It also shrank the worst fall from 21.9% to 13.6%.
The reason is that the rule switches on big days. On the days the faulty version switched into QQQ, the fund rose 2.04% on average. On the days it switched out, the fund fell 2.44%. The faulty test banks the gain and dodges the loss on exactly those days.
Trading costs: why fast rules suffer most
Every trade costs money. Costs include fees and the gap between the buying and selling price. The price can also move before an order is filled. A backtest that ignores them flatters any rule that trades often.

Source: YX Insights price data; FRED (DTB3); YX Insights
The 200-day rule switched 41 times in just over nine years. A 10-day version of the same rule switched 373 times. At an assumed 0.1% a trade, the 10-day rule's return fell from 10.0% to 5.6% a year. The 200-day rule's fell only from 17.4% to 16.9%.
Survivorship bias: testing only the winners
Survivorship bias comes from testing only assets that still exist. Funds that closed and companies that failed drop out of the data, so the past looks better than it was.
S&P Dow Jones Indices tracks this for US funds. Of 2,190 US equity funds open in mid-2006, only about 38% still existed 20 years later, at the end of June 2026. A test on today's funds would leave out the other 62%, which were merged or closed. Price data built from today's list of names has the same gap, ours included.
How to check a backtest
Six questions test a backtest:
How was the rule chosen? A rule fixed in advance, like the 200-day average, is harder to overfit than the best of many versions, such as many average lengths. In our 200-day moving average test, the best average length in one half of each fund's or share's history told us almost nothing about the other half.
Was it judged on unseen data? Look for an out-of-sample period, or a walk-forward test that picks settings on the past and judges them on the next slice. Right for years: edge or luck? explains how we run one.
When does it trade? The decision must come before the trade, never on the same price.
Are costs included? A stated cost for every trade, plus a count of trades.
How many events are there? A rule that buys SPY after a 15% fall from its high had six chances between 2010 and 2026, as Right for years shows. SPY was higher three months later each time. Six events are too few to prove a rule.
Is the data complete? It should include assets that closed or were removed from the stock exchange.
A backtest shows how a rule would have done. It never shows how it will do. Trust one only when it passes four tests. It was judged on data it never saw. It trades only after it decides. It pays for every trade. Its data includes the losers.
Learn more with YX Insights
This explainer is part of the YX Insights Academy. Each one takes a single idea and checks it against real data.
The same approach runs through everything else we publish:
Systematic Portfolio: ready-made portfolios for Macro & Megacaps and for Commodities. We publish the holdings and every change.
Multi-model Signals: a daily long-or-flat call on every name we cover. Each call shows how strongly our four models agree.
Research: company deep dives, macro commentary and essays on how we test.
Good places to start on the website:
Academy: more explainers like this one.
Start here: get our free weekly letter by email.
Macro & Megacaps Systematic Portfolio and Commodities: the two portfolios in detail.
Track record: live results since 3 August 2026, plus the ten-year backtest.
Upgrade: get the Systematic Portfolio, Multi-model Signals and research in full.
DISCLAIMER: This article is strictly educational. Any information or analysis in this note is not an offer to sell or the solicitation of an offer to buy any securities. Nothing in this note is intended to be investment advice and nor should it be relied upon to make investment decisions. Any opinions, analyses, or probabilities expressed in this note are those of the author as of the note's date of publication and are subject to change without notice.