The Average Investor's Blog

A software developer view on the markets

Archive for May, 2011

ARMA Models for Trading, Part IV

Posted by The Average Investor on May 31, 2011

All posts in this series were combined into a single, extended tutorial and posted on my new blog.

The last post promised to show some back testing results for the ARMA techniques. I decided to use the S&P 500 index for this purpose.

ARMA vs Buy and Hold

ARMA vs Buy and Hold

What really impresses me in the above char it the staggering performance of this approach during the financial crisis. As if it was feeding on the increased market volatility! This fact is also illustrated by the side by side annual comparisons, which I usually use to get an idea of the long term performance.

Year ARMA Buy and Hold
2001 -25.80% -13.04%
2002 7.14% -23.37%
2003 28.04% 26.38%
2004 13.66% 8.99%
2005 8.59% 3.00%
2006 17.26% 13.62%
2007 1.85% 3.53%
2008 100.96% -38.49%
2009 9.75% 23.45
2010 16.82% 12.78

Both systems performed similarly during the 2000/2002 recession, however, there is a stark difference in the performance during the 2008/2009 crisis. While buy and hold was losing money, the ARMA method registered it’s best year ever.

Let’s close with the answers to some obvious questions:

Why did I show only the performance of the last 10 years?
Believe it or not this is a very expensive computation. In fact, I have assembled a powerful i7 CPU machine just to be able to back test and trade these models. Almost 2 full days were necessary to compute the ARMA index for the past 10 years! I will consider publishing back testing for previous years, however, since S&P 500 is not a primary instrument for my trading, that’s not too likely to happen.

What parameters I used to compute the ARMA index?
The code is close to the code I have published in previous posts. I have used the fGarch package and my code always picked a GARCH(1,1) model. For the ARMA part, my code used all models between (0,0,1,1) and (5,5,1,1). I used 500 days of history. I mostly used the skewed Generalized Normal Distribution, specified by the cond.dist=”sged” parameter of garchFit, however, I obtained similar results using cond.dist=”sstd”.

It has become a lengthy and time-consuming post, so I will stop here. If I ever decide to do another post on this topic – I would likely discuss the obstacles I encountered while trading these models on a daily basis …

Posted in R, Strategies | Tagged: , , , | 9 Comments »

The Weekly Update

Posted by The Average Investor on May 29, 2011

The US markets continued their slow but persistent descent. Nothing spectacular, just four consecutive weeks of relatively small (in total about 2.3% on the S&P500 and about 2.8% on the Nasdaq 100) loses. At the same time, the US REIT increased for a second week in a row. More importantly, the Emerging Markets gave a buy signal last Friday after advancing for a second week in a row.

Asset Symbol Position Date In Gain
US REIT VNQ Long 2010-07-23 23.55%
S&P 500 ^GSPC Long 2010-09-30 16.64%
Nasdaq 100 ^NDX Long 2011-03-25 0.85%
Emerging Markets EEM Long 2011-05-27 0%

The month ends on Tuesday, while Monday is a holiday in the US. One of the indexes, the S&P 500, is monitored using the 10 month EMA, thus, a natural question is whether the four weeks of declines will trigger a change in the position. The answer is an affirmative No, barring calamitous events on Tuesday of course. For a sell to get triggered, the S&P 500 needs to drop down below $1247, which is 6.3% lower than the current price of $1331.1. And that needs to happen in a single day.

The only position “in danger” in near term is in fact the Nasdaq 100. The Friday’s close was less than 1% above the EMA (weekly, 20 for this instrument), thus, a drop this week may trigger an exit on Friday. For this to materialize next Friday’s close need to be $2313.89 or lower (the last Friday’s close was $2336.90).

Posted in Uncategorized | Leave a Comment »

The Weekly Update

Posted by The Average Investor on May 23, 2011

Another week in the red, but no new sell signals yet, mostly due to the significant profits the indexes delivered over the past few months. In other words, there is no reason to think this is more than a long-overdue correction.

Asset Symbol Position Date In Gain
US REIT VNQ Long 2010-07-23 21.55%
S&P 500 ^GSPC Long 2010-09-30 16.83%
Nasdaq 100 ^NDX Long 2011-03-25 1.72%

The economy is probably not too bad anywhere in the world, but the excessive amounts of debt keep people and governments hostage. A small turbulence can easily escalate into a major disaster for the same reason. Pneumonia is not too dangerous on its own, but it can be terminal if ones immune system is already weakened for whatever reason.

Posted in Uncategorized | Leave a Comment »

The Weekly Update

Posted by The Average Investor on May 14, 2011

Another red week across the board and after being battered badly for about three weeks now, the Emerging Markets index gave a sell signal at the close on Friday. This trade should have been opened on March 3, 2011 and would have resulted in a 0.89% loss, a loss as the previous two trades in this ETF.

Asset Symbol Position Date In Gain
US REIT VNQ Long 2010-07-23 21.21%
S&P 500 ^GSPC Long 2010-09-30 17.22%
Nasdaq 100 ^NDX Long 2011-03-25 2.71%

To me looking at past performance is an invaluable exercise helping you prepare psychological for the real thing. For instance, if you have started trading EEM using 20 week MA in March 2010 (the first buy signal for the year), currently you would be sitting at about 3.15% losses, which doesn’t seem much, but how easy is to swallow and be persistent after sitting on a loss for more than a year? What if the loss was 20%? 40%? Definitely a reality check.

The markets can teach one a lot. I am not alone in this opinion – Nassim Taleb shares similar thoughts in his exceptional book The Black Swan.

The most important lesson that I have learned from studying the markets is that it is very hard, if not impossible, to beat just inflation, forget about excessive returns. Here I am discussing pure trading gains, excluding fees charged (by a well-known money manager for instance) and taxes. Take a look at the inflation adjusted returns of the S&P 500 since 2000. Amazing isn’t it – 30% off the peak in 2000 (30% on the way down is equivalent to about 42% on the way up) and that’s after the central banks and politicians were congratulating themselves twice for saving the world, first after 2002 and recently for the second time!

Looking at this charts another sinister thought creeps up in my mind – probably this is the explanation why republicans (or conservatives in general) have been adamant against raising taxes – people who have money (not debt) have been taxed at an exceptional rate already since 2000 simply by the operational logistics of central banks! They might be right (until now I have been seriously disgruntled by their reluctance to raise taxes), certainly some food for thought here …

Posted in Uncategorized | Leave a Comment »

The Weekly Update

Posted by The Average Investor on May 9, 2011

A brutal week for the markets, partially caused by the sharp correction in commodity prices. Or was it the other way around – commodities reacting to the economic indicators? Whether it was just a correction or the burst of an ongoing bubble is still left to be seen. To me it certainly looks like a bubble in precious metals, but the situation is not so clear in the more “tangible” sectors.

The week turned out to be quite ugly too for the new strategies I have been writing about lately. I am still debating how to incorporate them in the blog …

No changes to the current positions though – still long all:

Asset Symbol Position Date In Gain
US REIT VNQ Long 2010-07-23 21.07%
S&P 500 ^GSPC Long 2010-09-30 17.44%
Nasdaq 100 ^NDX Long 2011-03-25 2.88%
Emerging Markets EEM Long 2011-03-25 1.92%

Posted in Market Timing, Trades | Leave a Comment »

ARMA Models for Trading, Part III

Posted by The Average Investor on May 2, 2011

All posts in this series were combined into a single, extended tutorial and posted on my new blog.

In the last post I showed how to pick the parameters for the ARMA model. The next step is to determine the position at the close. One way to do that is by a one day ahead prediction, if the prediction comes negative (remember the series we are operating on is the daily returns) then the desired position is short, otherwise it’s long.


getSymbols("SPY", from="1900-01-01")
SPY.rets = diff(log(Ad(SPY)))
SPY.arma = armaFit(~arma(0, 2), data=as.ts(tail(SPY.rets,500)))
predict(SPY.arma, n.ahead=1, doplot=F)

Now, to build an indicator for back testing, one can walk the daily return series and at each point perform the steps we covered so far. The main loop looks like (in pseudocode):

for(ii in history:length(dailyRetSeries))
   tt = as.ts(tail(head(dailyRetSeries, ii), history))
   ttArma = findBestArma()
   predict(ttArma, n.ahead=1, doplot=F)

Where history is the look-back period to consider at each point, I usually use 500, which is about two years of data. Although the above code is simply an illustration, I hope the main idea is pretty clear by now.

As mentioned earlier, findBestArma needs to be surrounded by a tryCatch block. Same goes for the predict – it may fail to converge. What I do is to have predict included in findBestArma, ignoring models for which the prediction fails.

Another improvement is to use ARMA together with GARCH. The latter is a powerful method to model the clustered volatility typically found in financial series. Sounds complex, but it turns out to be pretty straightforward in R. Just to give you an idea:


getSymbols("SPY", from="1900-01-01")
SPY.rets = diff(log(Ad(SPY)))
SPY.garch = garchFit(~arma(0, 2) + garch(1, 1), data=as.ts(tail(SPY.rets, 500)))
predict(SPY.garch, n.ahead=1, doplot=F)

That’s all I have to say on the theoretical side. I will finish this series with more implementation details and some back testing results in the next post …

Posted in coding, R, Strategies | Tagged: , , , | 1 Comment »

%d bloggers like this: