Thursday, September 29, 2011

A simple moving average system in R

I am currently working my way through the Stanford Machine Learning course as well as trying to get a bit more familiar with the statistical language R. 

I am also interested in using tools to help make better trading decisions. I am not particularly sold on pure systematic/algorithmic trading systems, and have spent a reasonable chunk of the last few years investigating them. Price tells an important story, and one that is easily absorbed by looking at a chart. It's not so easy to quantify that into an algorithm, and there are some structural and practical issues that bother me about systematic trading. 

But hey, it's fun to play with, and you never know when it might come in handy.

The base system is for now going long when price is above the 200 day moving average. Mebane Faber at  Cambria Investment Management has some solid research around moving average systems available in his paper A Quantitative Approach to Tactical Asset Allocation. It's usually worth watching what everyone else is watching, if only because that's what everyone else is watching, and for equities at least the 200 day moving average is not uncommon. 

Why long only? Well that is what the political economy wants, and who am I to argue.

A table of performance stats, with and without the moving average is as follows:


So, we objectively reduced drawdowns (some very large, hairy drawdowns), and increased the average annual return. But in absolute terms nothing at all to write home about. 

I'm using numbers like the Sharpe ratio not because I believe its particularly great, but it is fairly common and well understood. I am looking to use it as a relative measure to determine if modifications lead to improvements, namely by higher risk adjusted return, which Sharpe reflects at least on some level.

Charts of the two equity curves and the code are below. The code is based off the version published by Joshua Ulrich in this post which I am very greatful for. 

Next we'll take a look at adding a simple volatility filter using VIX.
require(quantmod)
require(PerformanceAnalytics)

#get the data and fill out the MA
getSymbols('SPY', from='1999-01-01')
SPY$ma200 <- SMA(Cl(SPY), 200)

#lets look at it from 2000 to 2011
spy <- SPY['2000/2011']

#our baseline, unfiltered results
ret <- ROC(Cl(spy)) 
eq <- exp(cumsum(na.omit(ret)))

#our comparision, filtered result
ma_sig <- Lag(ifelse(Cl(spy) > spy$ma200, 1, 0))
ma_ret <- ROC(Cl(spy)) * ma_sig
ma_eq <- exp(cumsum(na.omit(ma_ret)))

maxDrawdown(ret)
maxDrawdown(ma_ret)

table.AnnualizedReturns(ret)
table.AnnualizedReturns(ma_ret)

Sunday, September 25, 2011

Data Mining Tools

Sometimes you're not really sure about what you want do, or you have a data set you just want to throw a bunch of stuff at and see what sticks. I've found the following useful:

Rapid Miner This is a fully functional data mining tool. You create a processing workflow via drag and drop components, typically defining an input source, some processing and an output source. It supports everyone's favourite machine learning/AI techniques like SVMs, Neutral Nets, KNN etc. Thomas Ott at Neural Market Trends has some great rapidminer tutorials that can help you get up to speed.

Eureqa This is a tool which takes a data set and will try and find the function that describes it best. A bit more lightweight in terms of functionality compared to rapid miner, but can also be quite handy. It is windows based and I have not used it extensively, however it did seem to do what it said on the box.

Book Review: The French Revolution: A Very Short Introduction

All work and no play makes Jack a dull boy so I decided to expand my reading circles. I got a copy of The French Revolution: A Very Short Introduction by William Doyle. I am a big fan of the Very Short Introduction series, they are usually a great overview for stuff you would like to know more about but have limited time or a short attention span.

I found the book well written and over all easy to follow, however the prose was slightly heavy in places. The book details the underlying causes, the specific events in chronological order, the aftermath and rise of Napoleon, giving some context to Napoleon's reign, which as an ignorant Australian I had never quite understood.

There were two main things I found interesting. Firstly, the causes were largely economic, France was burdened with crippling debts and its people were suffering due to rising food prices. The leadership of the day was an absolute monarchy, seen to be isolated and out of touch with the travails of day to day common life. The country was experiencing a financial crisis due to protracted wars. This  remained unresolved as the monarchy was beholden to the nobility and clergy, preventing any chance of effective resolution. In practical terms this would have meant tax reform, which the both the nobility and the church were opposed to. I just can't think of a modern parallel.  

The second was its impact on the subsequent 200 years. It gave rise to the concept of the sovereignty of a nation of people, rather than a monarch. It is a reminder that the political structures we have today are still relatively new, and perhaps have not yet reached some optimal maximum. It also showed how the concepts from the Declaration of the Rights of Man and Citizen have found their way into places like the Universal Declaration of Human Rights and formed the basis for many of the liberal democracies we enjoy today.

I really enjoyed reading it, and at 150 pages is not too great a commitment.