Monday, April 14, 2008

Backtesting Moving Averages

Ever looked at a chart with a moving average (MA) and thinking if it would have resulted in an outperformance when buying and selling whenever the moving average would have been crossed. Even played around with different settings for the MA with whatever tool or web site you have used?

Now with STOCKscreener. i n f o you can SEE both, the moving average AND the backtested result immediately next to (below) each other:

Let's take the Euro Stoxx 50 Index and what everyone looks at, the 200 day moving average:

Buying whenever the moving average is crossed from below and selling on the way down, trading this index since 1987 would result in an overall gain of 436.65%.

Now this is indeed a decent outperformance of simply buying and holding. Buy and hold would result in a total gain of 307.55% (just 6.82% annually, before you get too excited:).

Here are the details of when the crossing (and buying and selling) of the MA took place.

So this is all nice and beautiful, but, maybe it is not very realistic that we would be able to get in and out of any financial instrument matching this index at exactly the prices of the intersections (for an index on top we would have to buy and sell an ETF, future, or mutual fund instead).
So let's say we do the same exercise with a transaction cost (and slippage) of 1% for a single buy or sell (so in and out costs us 2%).

Wow, all of a sudden the total return drops to a meager 54.33% (again, see the details here)! The big difference is because there have been 124 transactions, each costing us 1% of our capital, and then there is the compounding effect.

Hmm, ok, to make money in real life, what can we do. Of course, we can try to reduce the number of transactions while still capturing the big price movements. So here is the picture for using a 355 day moving average:

This reduces the number of transactions to 58, but as you can see above, still captures the big moves (and leaves out the big drawdown!). And the result improved a lot to 275.21%.
Still, not as good as the 200 day MA that had no transaction costs. If you look at the list of transactions, you will see that there are a view big profits and a lot of smaller losses.

We can introduce a third parameter, an extra threshold that needs to be crossed before a transaction will take place. This will avoid doing too many extra trades while the share price jitters around the moving average. So requiring the index price not only to cross but to cross an extra 1% before exercising a trade will more than half the number of transactions down to 22 and the profit jumps up to 453.36%, more than the initial result with the 200 day MA and NO transaction costs!

A threshold of 2% will increase the result even slightly more so. Anyway, the threshold will lead to fewer trades and a reduced number of small losses. But this also decreases the size of the big profitable trades a bit.

BTW, the moving average used is actually the simple moving average (SMA). Also, the results are as of 2008-04-14 and sure will change over time.

Feel free to play around yourself for an optimal set of parameters or experiment with other indices or stocks of your interest.

No comments: