Module 13: Trading Systems and Automation

Backtesting Basics

Chapter 3 of 12 · about 5 minutes

3.1 Testing before risking

Mint had written precise rules for a trend-following system. "So now I trade it?"

"Now you test it," said Hash. "If you apply your rules to years of past data, you can see how they would have behaved: how often they won, how big the losses got, how long the bad periods lasted. It won't tell you the future. But it can tell you if an idea was never any good, without losing a cent."

3.2 What a backtest is

A backtest applies a strategy's rules to historical data to simulate how it would have performed. It's the systematic version of the pattern testing Node did by hand in Module 4, Chapter 4.

StepWhat you do
1. Get dataHistorical prices, and volume or funding if your rules need them
2. Code or apply rulesStep through the data one period at a time, applying entries, exits, and sizing
3. Include costsFees, slippage, and funding (Module 7, Chapter 11)
4. Record every tradeEntry, exit, result, and account value over time
5. Calculate metricsReturn, drawdown, win rate, expectancy, and more
6. Compare with a benchmarkUsually simply holding BTC (Module 11, Chapter 12)

Backtests can be done with spreadsheets, charting platforms with built-in strategy testers, or programming languages like Python.

3.3 Data matters

Data issueWhy it matters
SourcePrices differ across exchanges (Module 1, Chapter 6); use a reliable source, ideally the market you'll trade
LengthShould cover bull markets, bear markets, and ranges
GranularityDaily data for daily strategies; finer data for intraday strategies
Gaps and errorsBad data points can create fake trades
SurvivorshipTesting only tokens that still exist ignores those that died (Chapter 4)

3.4 Key metrics

MetricWhat it tells you
Total return and annual returnHow much the strategy made
Maximum drawdownThe worst peak-to-low fall (Module 12, Chapter 6)
Number of tradesWhether there's enough data to trust the results
Win rateHow often trades won
Average win and loss in RSize of wins and losses relative to risk
ExpectancyAverage result per trade (Module 4, Chapter 16)
Sharpe ratioReturn per unit of volatility; higher means smoother returns for the return earned
Time in marketShare of time the strategy held a position
Longest losing streak and longest drawdown periodHow long the pain lasted

A Sharpe ratio compares a strategy's return, above a risk-free rate, with how much its returns fluctuate. It's useful for comparing strategies with different volatility.

3.5 Mint's illustrative backtest

Here are illustrative results for Mint's daily BTC trend system from Chapter 2, over a hypothetical six-year period including two bull markets and one deep bear market, compared with simply holding BTC.

MetricTrend system (illustrative)Buy and hold BTC (illustrative)
Annual return38%45%
Maximum drawdown10%77%
Number of trades481
Win rate35%
Average win4.2R
Average loss1.0R
Expectancy~+0.82R per trade
Time in market52%100%
Longest losing streak7 trades

Reading these results:

  1. The system earned less than buy and hold, but with a far smaller maximum drawdown, roughly one-seventh of buy and hold's.
  2. It lost on most trades, but the winners were much larger than the losers, the classic trend-following profile.
  3. It was out of the market almost half the time, which avoided much of the bear market.
  4. A seven-trade losing streak happened, which matters for psychology (Module 12, Chapter 6).

"So it's not better in every way," said Mint. "It's a trade-off: lower return, much smaller crashes."

"Exactly," said Hash. "For many people, avoiding a 77% drawdown is worth giving up some return. Especially if a 77% drawdown would make them panic sell."

3.6 Is it enough data?

Forty-eight trades is a modest sample. With small samples, results can be heavily influenced by a few lucky or unlucky trades.

Number of tradesConfidence in results
Under 30Low; results could easily be luck
30 to 100Moderate; look for consistency across periods
Over 100Higher, but still check different market conditions

Long-timeframe strategies naturally produce fewer trades. One way to gain confidence is to test the same rules on other markets, like ETH, or other periods, and see whether the results are broadly similar.

3.7 Equity curves

An equity curve charts the account value over time in the backtest. Look at its shape, not just the end result:

Equity curve patternWhat it suggests
Steady rise with modest dipsConsistent strategy
Most gains from one short periodResults depend on one event; fragile
Long flat or falling periodsStrategy may be hard to stick with
Very smooth, almost perfect lineSuspicious; check for errors or overfitting (Chapter 4)

3.8 Try it yourself

Use a charting platform's strategy tester, or a spreadsheet, to backtest the simple rules from Chapter 2 on BTC daily data for as many years as available. Include 0.1% costs per trade. Record the metrics from section 3.4 and compare with buy and hold. Then test the same rules on ETH. Are the results similar?

Key takeaways from this chapter

  1. A backtest applies strategy rules to historical data to see how they would have performed.
  2. Steps include getting data, applying rules, including costs, recording trades, calculating metrics, and comparing with a benchmark.
  3. Good data covers different market conditions and comes from reliable sources.
  4. Key metrics include return, maximum drawdown, trade count, win rate, R-multiples, expectancy, Sharpe ratio, time in market, and losing streaks.
  5. Strategies involve trade-offs, such as lower return in exchange for smaller drawdowns.
  6. Small samples are unreliable; test across markets and periods.
  7. Look at the equity curve's shape, and be suspicious of results that look too perfect.