Backtesting: study a rule on past data
Would a rule have worked? A backtest runs a strategy over past data to study how it behaved. It's a science experiment, not a crystal ball. (Educational only — never financial advice.)
The big idea
Backtesting replays a trading rule over historical data to measure how it would have performed — a study tool, not a prediction.
See it in code
The simplest backtest is one line of study: pretend you bought on day one and held to the end, then measure the change. That's the baseline every fancier rule gets compared against:
# Simplest backtest: buy on day 1, hold to the end - study only.
closes = [100, 98, 102, 105, 103, 108]
start = closes[0]
end = closes[-1]
ret = (end - start) / start * 100
print(f"Buy and hold: {ret:.1f}%")Buy and hold: 8.0%
Up 8% over the whole window — but that ignores when the price moved. A smarter rule reacts to the day-to-day changes, so first we need to see them.
Step through the days and compare each close to the one before — the day-over-day move that any rule watches. No money yet, just reading the sequence:
closes = [100, 98, 102, 105, 103, 108]
for i in range(1, len(closes)):
change = closes[i] - closes[i - 1]
direction = "up" if change > 0 else "down"
print(f"Day {i + 1}: {closes[i]} ({direction})")Day 2: 98 (down) Day 3: 102 (up) Day 4: 105 (up) Day 5: 103 (down) Day 6: 108 (up)
Day 3 is the first up day. A rule can act on exactly that moment — so next we let the backtest buy there and hold.
Now put money on the rule: buy once on that first up-day, then hold. We track cash and shares and measure the final value — purely to observe, not to recommend:
# A backtest runs a rule over PAST data to study it - not advice.
closes = [100, 98, 102, 105, 103, 108]
cash = 1000.0
shares = 0
for i in range(1, len(closes)):
if closes[i] > closes[i - 1] and shares == 0:
shares = cash / closes[i]
cash = 0.0
print(f"Day {i + 1}: BUY at {closes[i]}")
value = cash + shares * closes[-1]
print(f"Final value: {value:.2f} (started at 1000)")Day 3: BUY at 102 Final value: 1058.82 (started at 1000)
It bought on Day 3 — the first up-day we spotted above — and held to the end. The final value tells us how this rule did on this frozen data — nothing more. A real backtest tests many rules over long histories, always aware that the past does not promise the future.
Replaying a decision rule over recorded data to evaluate it is a general method: testing a cache policy on request logs, a routing rule on traffic history, a game-AI on past matches. It's controlled experimentation — change one rule, rerun, compare.
Try it yourself
Add a sell rule (sell when the price falls) and see how the final value changes. Then compare two different rules on the same data — which behaved better, and why?
The common mistake
Believing a good backtest guarantees future profit. It doesn't — markets change, and a rule tuned to fit past data ('overfitting') often fails on new data. Backtesting is for understanding a strategy's behavior, not for making real money decisions.
What it unlocks
Backtesting combines market data, conditionals, and trading signals, and is measured with risk basics.