Test one trading rule with costs on real price history

Backtest one simple rule on at least ten years of data and compare it with buying and holding after costs.

To find out what a trading rule actually did, rather than what someone said it did, test it yourself. Put at least ten years of daily prices in a spreadsheet, let the rule act one day after each signal, charge a cost on every trade, and compare the result with buying and holding. Once you have the data, this takes about forty-five minutes.

Marcus keeps seeing one rule recommended in his group chat. You own the index when it is above its 200-day moving average, the average of the last 200 closes, and you hold cash when it is below. The claim in the chat is that it keeps you invested in the good years and out of the crashes. Lesson 10.6, Data mining, overfitting and transaction costs, set out the tests a rule should pass, and this exercise runs one of them. The tab you finish with is not a trading system. You are learning a method for testing any rule, and the result is not a verdict on this one.

Picking a rule and finding ten years of prices

Choose one simple rule and write it down before you look at any results. Use the settings everyone uses, not settings you tuned. Marcus takes the 200-day rule at its standard setting, and decides that whenever the rule is out of the index, it holds cash earning a T-bill rate.

You need at least ten years of daily closing prices for one broad index, or for an ETF that tracks one. A total return series is best because it includes dividends. Index providers publish history for their own indexes, and many finance sites let you download an ETF's adjusted closing prices, which also include dividends. For the cash return, take the T-bill yield for each year from the MAS website. A longer period is better. A period with at least one serious fall and one sideways stretch tests the rule far better than a calm decade.

Setting up the columns

Make a tab called Backtest with one row per trading day and these columns:

date closing price 200-day moving average signal position daily return strategy return trade flag cost net return cumulative value for the rule cumulative value for buy and hold

The signal is 1 when the close is above the moving average and 0 when it is below. The position is the previous day's signal.

That one-day shift in the position column is the most important formula in the tab. You only know today's close after the market shuts, so the earliest you can act on it is the next day. Using today's signal for today's return means acting on information you could not have had in time. This is look-ahead bias, and it makes almost any rule look better than it could ever have been.

Daily return is today's close divided by yesterday's close, minus one. Strategy return is the daily return when the position is 1 and the daily cash return when the position is 0. The trade flag is 1 on any day when the position differs from the day before. Net return is the strategy return minus the cost, on any day with a trade.

For the cost per trade, use your own broker's fee schedule and the spreads you measured in lesson 5.8, Measure the true cost of five paper trades. A rough figure is half the spread plus commission. For a large ETF with low commission, 0.15% of each trade is a reasonable example (an invented figure). For a smaller fund, or a broker with a high minimum fee, the cost can be several times that. Put the cost in a single cell so you can change it and watch the results move.

Checking the formulas on five invented days

Before filling ten years of rows, check your formulas on a short invented case. Set the cost at 0.15% per trade with no cash return. The closes are 100, 98, 97, 99 and 101, and the 200-day average is about 99.

On day 1 the close is 100, above the average, so the signal is 1.

On day 2 the index falls 2%. The position comes from day 1's signal, so the rule loses 2%. The close of 98 is below the average, so the signal turns to 0.

On day 3 the rule is out. It misses the 1% fall and pays 0.15% to sell. The close of 97 is still below the average.

On day 4 the close of 99 is just above the average, so the signal turns to 1. The position is still 0, so the rule misses a 2.1% rise.

On day 5 the rule is back in. It gains about 2.0% and pays 0.15% to buy.

Across the five days the rule sold at 98 and bought back at 99. This is the whipsaw from lesson 10.3, Moving averages, RSI and MACD, and how they fail: as the price crosses the average back and forth, you sell and then buy back at a worse price. Here the cost of both trades is priced in. Your columns should reproduce each day's result before you go on.

Comparing Marcus's rule with buying and holding

Chain the daily net returns into a cumulative value for the rule, and the daily returns into a cumulative value for buying and holding. For each, work out the compound yearly return, the maximum drawdown and the number of trades. Use the running-peak method from lesson 2.2, Drawdown and time under water are the risks you feel, for the maximum drawdown.

Marcus ran his backtest over 15 years, with invented results. Buying and holding returned about 7.0% a year compounded, with a maximum drawdown of minus 34%. Before costs, the rule returned about 6.4% a year with a maximum drawdown of minus 19%. It made 44 trades: 22 exits and 22 re-entries. At 0.15% per trade the rule returned about 5.9% a year. At 0.5% per trade it would have returned about 4.8%.

Under his table Marcus wrote a short note. After costs, the rule trailed buy and hold by about 1.1 points a year (7.0% minus 5.9%), but it cut the worst fall from 34% to 19%. Thirteen of the 22 exits were followed by buying back at a higher price within two months. The rule did not beat buy and hold on return. Whether the shallower drawdown is worth 1.1 points a year is a risk question, and his written allocation already answers it more cheaply by holding 20% in bonds and cash.

Writing your own note under the table

Under your summary table, write two or three sentences. Say whether the rule beat buy and hold after costs, then say what you would need to see before you trusted it, such as the same result on a second index or on years you did not test.

If your rule shows a huge advantage, check the position column first, because a missing one-day shift is the usual cause.

Now pick your rule, download the data and build the tab.

Build a backtest tab for one rule, deduct costs on every trade, and write down whether it beat buy and hold after costs.

Calculators for this lesson

Course

Junxiong-WFG Organisation is an authorised representative of AIA Financial Advisers Private Limited (Reg. No. 201715016G).