Test one trading rule with costs on real price history

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

The rule Marcus kept seeing in his group chat was simple: own the index when it's above its 200-day moving average, sit in cash when it's below. The claim was that it keeps you in for 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. This exercise runs one of them yourself, in a spreadsheet, with costs, so that you know what the rule did rather than what someone said it did.

Allow about forty-five minutes once you have the data. The result is a backtest tab in your workbook. It isn't a trading system, and the point is the method, not a verdict on any rule.

Step 1: choose one rule and get the data

Pick one simple rule and write it down before you look at any results. Marcus chose the 200-day rule above, with the settings everyone uses rather than ones he tuned. When out of the index, the rule holds cash earning a T-bill rate.

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

Step 2: build the columns

Create 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 and cumulative value for both the rule and buying and holding

The moving average is the average of the last 200 closes. The signal is 1 when the close is above the average and 0 when it's below. The position is the previous day's signal, not today's. That one-day shift 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 is look-ahead bias, and it makes almost any rule look better than it could ever have been.

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

Here are five made-up days to check your formulas against, with a cost of 0.15% per trade and no cash return, to keep it short. Closes run 100, 98, 97, 99 and 101, against a 200-day average of about 99. On day one the close is above the average, so the signal is 1. On day two the index falls 2%, and because the position comes from day one's signal, the rule loses 2% too. Day two's close of 98 is below the average, so the signal turns to 0, and on day three the rule is out: it misses the 1% fall but pays 0.15% to sell. Day three's close is still below the average. Day four's close of 99 is just above it, so the signal turns back to 1, but the position on day four is still 0, and the rule misses a 2.1% rise. On day five it's back in, gains about 2.0% and pays 0.15% to buy. In five days the rule sold at 98 and bought back at 99, which is the whipsaw from lesson 10.3, Moving averages, RSI and MACD, and how they fail, priced in.

Step 3: deduct a realistic cost for every trade

Take your cost per trade from your own broker's fee schedule and from the spreads you measured in lesson 5.8, Measure the true cost of five paper trades. For a large ETF with a low commission, half the spread plus commission might come to a made-up 0.15% of each trade. For a smaller fund or a broker with a high minimum fee it can be several times that. Put the figure in one cell, so you can change it and watch the result move. The net return is the strategy return minus the cost on any day with a trade.

Step 4: compare with buying and holding

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

Marcus's made-up results over 15 years: buying and holding compounded at about 7.0% a year with a maximum drawdown of minus 34%. The rule, before costs, compounded at about 6.4% a year, with a maximum drawdown of minus 19%, and made 44 trades, 22 exits and 22 re-entries. At 0.15% per trade, costs cut its return to about 5.9% a year. At 0.5% per trade it would have been about 4.8%.

He wrote this under the table: "Over 15 years the rule trailed buying and holding by about 1.1 points a year after costs, but cut the worst fall from 34% to 19%. Thirteen of its 22 exits were followed by buying back at a higher price within two months. It didn't beat buy and hold on return. Whether a shallower drawdown is worth 1.1 points a year is a risk question, and my written allocation already answers it more cheaply by holding 20% in bonds and cash."

What a finished tab looks like

A finished tab has at least ten years of daily data, the one-day shift between signal and position, a cost cell that every trade reads from, cumulative values for the rule and for buying and holding, and a small summary of yearly return, maximum drawdown and number of trades for each. Under it sit two or three sentences on whether the rule beat buying and holding after costs, and what you'd need to see before trusting it, such as the same result on a second index or on years you didn't test. If the 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 for every trade and write whether it beat buy and hold after costs.

Course

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