Let’s do some data mining – Death Cross revisited

It’s all well and good looking for the Death Cross signal to tell when to be in and out of the markets…but is that just an artefact of the data?  I’m curious…

We established last time that being long when the S&P500 was above its 200 DMA, then selling when the price closed below the same DMA, was a somewhat ‘meh’ strategy: returns were about the same as being long the entire period since 1950, but risk/return was a bit better using the strategy.  Using the joy of AmiBroker, plus the plethora of free data on offer from Google Finance etc., we can get some idea of how robust the Death Cross really is.

Here’s what I’ve done:

  1. Downloaded all historical data for the investable S&P500 ETF – SPY.  This begins Feb 1993, so has a few ups and downs.
  2. Using AmiBroker, I simulated a realistic-ish simple trading strategy:
    1. Be long SPY if (an exponentially-weighted) moving average of the price (MA1) is above another moving average of the price (MA2).  Let’s not assume the shorter moving average needs to be higher than the longer for the time being.  BTW, a moving average of 1 = the current price; thus the Death Cross is equivalent to MA1(1) > MA2(200).
    2. If the first MA crosses below the second MA, exit the market until they reverse.
    3. Assume we check these values at the close of each day, and transact on the next day’s open.  No cheating!
    4. Assume a flat $9 for each trade.  That’s about average these days for US brokers.
  3. Vary the lengths of MA1 and MA2 over the following ranges:
    1. MA1: 1 day through 80 days, incrementing by 1 day
    2. MA2: 2 days through 300 days, incrementing by 2 days
  4. Record the net profit of each run, and place in large PivotTable.
  5. Colour said large PivotTable to create the below piece of abstract art:
The closest I come to art.  Green = relatively better idea than Red.  Sources: AmiBroker and Google Finance.

The closest I come to art. Green = relatively better idea than Red. Sources: AmiBroker and Google Finance.

Lovely.  Each row represents a different MA1 value; each column a different MA2.  The colour scheme helps us see some interesting patterns:

  1. Longer averages = better results.  The Death Cross result is that small blue circle, deep in the lower-value area of the chart.  Why?  By having such a reactive average (i.e. the underlying price), each time the price falls below the 200 DMA there’s a lot of buying and selling.  For example, if you unloaded yesterday due to the DMA being crossed, you might very well be getting back in tomorrow.  That’s 2 transaction fees for not much benefit.
  2. ‘Sweet spot’ = around 50 vs 100 DMA.  Over this time period, the darkest green area is around the MA1 = 50 and MA2 = 100 days.  I guess this is because those runs run away from the 2008 crash well: the 50/100 run exits Nov 2007, then re-enters June 2009.
  3. Still doesn’t beat returns of long-only.  Even the best run has about a 105% total return (excluding dividends) since 1993.  Buy-and-hold SPY returned over 300% (excluding dividends) in the same period.  But the drawdowns are perhaps more palatable – around 18% Max drawdown for the 50/100 run, versus 55% for long-only.

What are the lessons?

  • Trading momentum strategies, like the Death Cross, helps the risk profile of an equity portfolio.  It doesn’t help the return profile, at least since 1993.
  • Data mining is sometimes very dangerous – if using data analysis to come up with a really good-looking, overfit strategy.  I think it can be a helpful tool to see how robust a strategy is – as in, what if I’ve completely misspecified the strategy?  In this case, you can be really loose about the 50/100 parameters and still come out better than the Death Cross.

Shortcut time!

I’ve spent a lot of time recently programming, i.e. hitting my head against the wall.  Every time I start working in my (very nice) Python IDE, it takes roughly 30 seconds before I run into the next problem to search Stack Overflow for a solution.  My skills are that bad.

There are times I just need to code a quick trading algorithm, and want ‘good enough’ out of a box.  In comes AmiBroker: a top-spec analysis program which has inbuilt interaction with my execution broker.  The usual technical indicators are built-in, and can be combined easily with the simplified programming language AFL.

Example: I just spent 3 days coding an algorithm in Python, which I just implemented in about 5 minutes using AmiBroker/AFL.  Ahhh…