Moneydesks

🤖 Systematic and algorithmic trading: rules, backtests and honest testing

Open this guide in the app →

🎯 In 30 seconds: A systematic trader follows written rules that say exactly when to buy, sell and how much. An algorithm is those rules turned into code. You can test rules on past prices, called backtesting, before risking money. The hard part is not writing the rules. It is testing them honestly, because a computer can always find rules that would have worked in the past and then fail in the future. Split your data, count your costs, paper trade, and expect live results to be worse than the backtest.

From hunch to rule

Discretionary trading means deciding each trade by judgment. Systematic trading means following rules decided in advance. Algorithmic trading means a computer applies those rules, sometimes placing orders by itself.

A rule is only a rule if two people reading it would make the same trade. "Buy strong stocks on dips" is not a rule. This is:

  • Universe: one S&P 500 index fund.
  • Signal: on the last trading day of each month, compare the closing price with its 200-day simple moving average.
  • Entry: if the close is above the average, hold the fund.
  • Exit: if the close is below the average, sell and hold cash.
  • Timing: act at the next day's opening price.
  • Size: 100% of the account in the fund or 100% in cash.

This trend filter is a teaching example, not a recommendation. You will meet moving averages in Moving averages and this style of system in Trend following.

What a backtest is

A backtest runs your rules over historical prices as if you had traded them, and reports what would have happened. Done well, it can reject bad ideas cheaply. Done badly, it produces confident numbers that mean nothing.

The procedure:

  1. Write the rules down completely before looking at results.
  2. Get clean data: prices adjusted for splits and dividends, and including companies that later disappeared.
  3. Decide what data you will hold back for testing later. Do not look at it yet.
  4. Run the rules day by day, using only information that would have been available at each moment.
  5. Subtract realistic costs: commissions, the bid-ask spread and slippage.
  6. Record the results below, then run the rules once on the held-back data.

The numbers to read

MeasureWhat it tells you
Annual return (CAGR)The yearly growth rate that turns the starting balance into the ending balance.
Maximum drawdownThe largest drop from a peak to a later low. This is the pain you must sit through. See Drawdowns and losing streaks.
VolatilityThe standard deviation of returns, a measure of how bumpy the ride is.
Sharpe ratioReturn above a risk-free rate, per unit of volatility.
Number of tradesFewer than about 30 trades says very little. Results from a handful of trades are mostly luck.
Win rate and expectancyHow often trades win, and the average profit per trade. See Risk/reward and expectancy.
ExposureThe share of time money is actually in the market.

The Sharpe ratio formula, with hypothetical numbers:

Sharpe = (strategy return - risk-free rate) / standard deviation of returns

A system that returned 9% a year, with a 4% risk-free rate and 15% volatility, has a Sharpe ratio of (9 - 4) / 15 = 0.33. A backtest showing a Sharpe ratio of 3 on a simple daily stock strategy should make you suspicious, not excited.

The four biases that fool almost everyone

1. Look-ahead bias. Using information you could not have had at the time. Classic examples: buying at today's close based on today's close, using earnings numbers on the date the quarter ended rather than the date they were announced, or using data that was later revised. Fix: act on the next bar, and time-stamp every input by when it became public.

2. Survivorship bias. Testing only on companies that exist today. The ones that went bankrupt or were dropped from the index are missing, so a "buy cheap stocks" rule looks better than it really was. Fix: use data that includes delisted stocks, or test on the index as it was at each date.

3. Overfitting (data snooping). Trying many rule variations and keeping the one with the best past result. With enough tries, some rule will fit the noise in the past. It has learned the history, not the market.

4. Ignoring costs. A system that trades often can look profitable before costs and lose money after them. What trading really costs lists every cost to include.

How overfitting happens: a worked example

Say you test a moving-average crossover on one index fund. You try fast averages from 5 to 50 days and slow averages from 50 to 300 days, in steps of 5. That is 10 x 51 = 510 combinations. You keep the best one.

Here is the problem in plain arithmetic. If none of the rules had any real edge, and you used a common test that flags a result as "significant" when it would happen by chance only 5% of the time, you would still expect about 5% of 510, roughly 25 rules, to pass. The winner of 510 tries is very likely one of those lucky ones.

Researchers have studied this problem formally. One well-known paper by Bailey, Borwein, López de Prado and Zhu proposes a way to estimate the probability that a backtest's best result is overfit, and argues that the usual hold-out methods are not enough on their own when many variations were tried.

Overfit rule: great in-sample, poor out-of-sampleIn-sample: used to pick the ruleOut-of-sample100150200Best of 510 triesSimple ruleOverfit ruleFixed simple rule
Illustration with made-up numbers. The rule chosen for its in-sample peak loses ground once it meets data it was not fitted to. A simpler rule with fewer choices often holds up better.

Testing honestly: in-sample, out-of-sample, walk-forward

In-sample data is the part you use to build and tune the rules. Out-of-sample data is the part you set aside and touch only once, at the end. If performance collapses out-of-sample, the rule was probably fit to noise.

The weakness: once you look at the out-of-sample result and go back to tweak, that data is no longer out-of-sample. Be strict.

Walk-forward testing repeats the process through time. Tune on one window, test on the next unseen window, then slide both forward and repeat. Join all the test windows together and you get a record of how the method would have done if you had used it in real time.

Walk-forward: tune, test, slide forwardRound 1Round 2Round 3Round 4TimeTuneTest
Only the green test windows count toward the final result. Each one was unseen when its rules were chosen.
The most common mistake
Tweaking until the backtest looks good, then believing it. Every change you make after seeing results uses up some of your data's honesty. Keep a log of every variation you tried, not just the winner. If you tried 50 versions, say so in your notes, and expect the live result to be well below the best backtest.

Costs: a worked example

Hypothetical system: $10,000 account, each trade uses the full $10,000, 100 round trips a year (buy and sell). Suppose the spread plus slippage costs 0.10% per round trip, even with zero commission.

Cost per round trip: $10,000 x 0.10%$10
Round trips per year100
Yearly cost$1,000, or 10% of the account

The system has to earn 10% a year before costs just to break even. A backtest that ignores this is fiction. The more often a system trades, the more the cost assumption matters.

From backtest to live

  1. Paper trade it. Run the rules forward in a paper portfolio for at least a few months. Compare every simulated fill with what the backtest assumed.
  2. Start tiny. Trade the smallest real size possible. Real fills, real emotions and real mistakes show up only with real money.
  3. Set a kill rule in advance. Decide the drawdown or the gap from backtest results that will make you stop and review, for example "if live drawdown exceeds 1.5 times the worst backtest drawdown".
  4. Automate carefully. If code places orders, add limits on order size and number, and a way to shut it off instantly. A bug in a loop can send hundreds of orders in seconds.
Simple beats clever
Prefer rules with few settings and a reason you can explain in one sentence, like "trends tend to persist" or "costs eat fast systems". If a rule only works with a 37-day average and fails with 35 or 40, it is not robust. Test nearby settings: a good rule should work reasonably across a range.

Algorithms in the wider market

Much of the trading in US stocks is done by computers: market makers quoting prices, funds slicing big orders into small ones, and high-frequency firms reacting in microseconds. A 2020 SEC staff report found that algorithmic trading has generally improved liquidity and market quality in normal conditions, mainly in stocks, but that some kinds of algorithmic trading may make periods of stress worse.

As a retail trader you are not going to compete on speed. Where an individual can compete is patience, small size and holding periods that big funds find awkward. Whether any edge exists at all is the question in Does technical analysis work?

Try it

Open the backtest tool and test the 200-day trend filter on a broad index fund.

  1. Before running, write down what you expect for return, maximum drawdown and number of trades.
  2. Run it on the first two thirds of the available history only. Record the results.
  3. Try three nearby settings: 150, 200 and 250 days. Do they give broadly similar results? Write down all three.
  4. Now run the 200-day version once on the last third. Compare with step 2.
  5. Compare the strategy's maximum drawdown with simply holding the fund. Which one could you actually live with?
Check yourself
  1. What is look-ahead bias? Give an example. Answer: Using information you could not have had at the time, such as buying at today’s close based on today’s close, or using earnings figures before they were announced.
  2. A system returned 9% a year with 15% volatility, and the risk-free rate is 4%. What is its Sharpe ratio? Answer: About 0.33 ((9 - 4) / 15).
  3. You test 510 rule combinations, none with a real edge, using a test that flags 5% of chance results as significant. About how many "pass" anyway? Answer: About 25 (5% of 510). The best of 510 tries is very likely one of the lucky ones.
  4. A system puts $10,000 into each trade, makes 100 round trips a year, and each costs 0.10% in spread and slippage. What does that cost a year? Answer: $1,000, or 10% of the account. The system must earn 10% a year before costs just to break even.

Sources