Pattern matching: a quick look at the last 11 months

John Hussman posted the following pattern on his twitter:

Source: John Hussman’s Twitter

So I thought this would be a great time to see which points in history the Systematic Investor Toolbox pattern matching algorithm would highlight. Instead of looking at the Dow Jones I will use the S&P500 Spyder (SPY). Choosing a window period of approximately 11 months and looking at 20 years of data lets see what happens:

###############################################################################
# Load Systematic Investor Toolbox (SIT)
# http://systematicinvestor.wordpress.com/systematic-investor-toolbox/
###############################################################################
con = gzcon(url('http://www.systematicportfolio.com/sit.gz', 'rb'))
source(con)
close(con)

#*****************************************************************
# Load historical data
#******************************************************************
load.packages('quantmod')
tickers = 'SPY'

data = getSymbols(tickers, src = 'yahoo', from = '1950-01-01', auto.assign = F)


#*****************************************************************
# Dynamic time warping distance
#******************************************************************
# http://en.wikipedia.org/wiki/Dynamic_time_warping
# http://dtw.r-forge.r-project.org/
#******************************************************************
load.packages('dtw')

obj = bt.matching.find(Cl(data), normalize.fn = normalize.mean, dist.fn = 'dist.DTW', plot=T,, n.query=228,n.reference = 252*20)

matches = bt.matching.overlay(obj, plot.index=1:90, plot=T)

layout(1:2)
matches = bt.matching.overlay(obj, plot=T, layout=T)
bt.matching.overlay.table(obj, matches, plot=T, layout=T)

Source: Time Series Matching with Dynamic Time Warping

Interestingly enough, the runup starting in july 2011 is chosen as the closest match, and we all know how that ended. There are multiple other scenarios however.

The average returns of the top 9 matches seem to under perform the anytime/benchmark return (not shown).

More pattern matching posts to come in the future….

Now back to turkey day…..

Update: I ran the same code for the DJIA diamonds (DIA):


The same conclusions hold true, the runup starting in july 2011 is the closest match and the next week/month return is below average. In the future we will examine more pattern matching methods….