The Average Investor's Blog

A software developer view on the markets

Moving the blog to

Posted by The Average Investor on Oct 20, 2012

It has been a while I must say, but I haven’t been idle. I wanted a little bit more from my blogging platform, so I decided to move the blog to a new, self-hosted home – See you there!

Posted in Uncategorized | Leave a Comment »

Were markets exceptionally volatile in 2011?

Posted by The Average Investor on Jan 3, 2012

2011 was a volatile year, no doubt about that, but was it exceptionally so from a historic point of view? To quantify the volatility, I used the Dow Jones Industrial average, which goes back to 1928 on Yahoo Finance:


getSymbols("^DJI", from="1900-01-01")

dji = Cl(DJI["/2011"])

djiVol = aggregate(
               as.numeric(format(index(dji), "%Y")),
               function(ss) coredata(tail(TTR:::volatility(
                                                   calc="close"), 1)))
# The result is 0.8214286, the 82nd quantile

A volatile year no doubt, but once again confirming the fact that, in markets behaviour at least, history does repeat itself. The following plot clearly shows that the volatility experienced during the great depression dwarves the recent levels:

Dow Jones Annual Volatility

Next, out of pure curiosity, I also computed how much money were to be made with these levels of volatility:


getSymbols("^DJI", from="1900-01-01")

# Compute the absolute returns
absRets = abs(ROC(Cl(DJI["/2011"])))
absRets = reclass(ifelse(,0,absRets),absRets)

# Summarize annually
yy = as.numeric(format(index(absRets), "%Y"))
zz = aggregate(absRets, yy, function(ss) tail(cumprod(1+ss),1))

# The result is 10.64

That’s right, an owner of a crystal ball would have been able to multiply his money 10-fold in 2011 alone! For further comparison, in 1932, the owner of the same ball would have been able to multiply his money … 590 times!

Posted in R | 2 Comments »

The DVI indicator turning

Posted by The Average Investor on Dec 23, 2011

A close on the SPY of above $124.46 (which is -0.65% from the yesterday’s close) will signal a short position according to the DVI indicator, which has been exceptionally successful in predicting the SPY this year (which means nothing of course:)).

It is interesting to note that a close above $126.10 will be above the 200-day moving average. Something unseen since Dec 7th, and the last time this (SPY closing above its 200 day SMA) happened for more than 3 days in a row, was back in August.

In any case, happy trading and merry holidays!

Posted in Market Timing | Leave a Comment »

More orthodox ARMA/GARCH trading

Posted by The Average Investor on Dec 15, 2011

The system described in the earlier series for ARMA trading was in fact an “extreme” version of the more common, orthodox approach prevailing in the literature. Recently I tried using R to reproduce the results of a particular paper, and that lead to a lot of new developments …

How is typically ARMA trading simulated? The data is split into two sets. The first set is used for model estimation, an in-sample testing. Once the model parameters are determined, the model performance is tested and evaluated using the second set, the out-of-sample forecasting. The first set is usually a few times larger than the second and spans four or more years of data (1000+ trading days).

I wanted to be able to repeat the first step once in a while (weekly, monthly, etc) and to use the determined parameters for forecasts until the next calibration. Now, it’s easier to see why I classified my earlier approach as an “extreme” – it does the model re-evaluation on a daily basis. In any case, I wanted to build a framework to test the more orthodox approach.

To test such an approach, I needed to perform а “rolling” forecast (have mercy if that’s not the right term). Let’s assume we use weekly model calibration. Each Friday (or whatever the last day of the week is) we find the best model according to some criteria. At this point we can forecast one day ahead entirely based on previous data. Once the data for Monday arrives, we can forecast Tuesday, again entirely based on previous data, etc.

My problem was that the package I am using, fGarch, doesn’t support rolling forecasts. So before attempting to implement this functionality, I decided to look around for other packages (thanks god I didn’t jump to coding).

At first, my search led me to the forecast package. I was encouraged – it has exactly the forecast function I needed (in fact, it helped me figure out exactly what I need;)). The only problem – it supports only mean models, ARFIMA, no GARCH.

Next I found the gem – the rugarch package. Not only it implements a few different GARCH models, but it also supports ARFIMA mean models! I found the documentation and examples quite easy to follow too, not to mention that there is an additional introduction. All in all – a superb job!

Needless to say this finding left me feeling like a fat kid in a candy store (R is simply amazing in this regard!). Most likely you will be hearing about mew tests soon, meanwhile let’s finish the post with a short illustration of the rugarch package (single in-sample model training with out-of-sample forecast):


getSymbols("SPY", from="1900-01-01")
spyRets = na.trim( ROC( Cl( SPY ) ) )

# Train over 2000-2004, forecast 2005
ss = spyRets["2000/2005"]
outOfSample = NROW(ss["2005"])

spec = ugarchspec(
            mean.model=list(armaOrder=c(4,5), include.mean=T),
fit = ugarchfit(spec=spec, data=ss, out.sample=outOfSample)
fore = ugarchforecast(fit, n.ahead=1, n.roll=outOfSample)

# Build some sort of indicator base on the forecasts
ind = xts(head(as.array(fore)[,2,],-1),["2005"]))
ind = ifelse(ind < 0, -1, 1)

# Compute the performance
mm = merge( ss["2005"], ind, all=F )

# Output (last line): 2005-12-30  1.129232

Hats down to brilliancy!

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

DVI Position Change

Posted by The Average Investor on Dec 13, 2011

Just a heads up that the DVI indicator will indicate a long position as of today’s close as long as the SPY closes below $127.17 (which is 2.38% higher than Monday’s close). The short has been in place since the close of December 1, for a negligible gain as of yesterday’s close. For more details how to pre-compute the DVI actions, see my previous post.

Posted in Market Timing, Trades | Tagged: , , | Leave a Comment »

The Weekly Update

Posted by The Average Investor on Dec 4, 2011

The markets had their best week in more than 2 years, more precisely since March 2009. The S&P 500 ended up the week a whopping 7.31% higher. The up-move was sufficient to push the index above it’s 20-week moving average. It seems I did the math wrong in my previous post. 🙂

The last week also marked the end of month. As of the end of November, the performance of the indicators stood as follows:

Indicator Gain/Loss
Buy and Hold -0.70%
DVI 22.24%
ARMA -5.32%

An impressive performance by the DVI indicator! Let’s see how the year ends.

Posted in Market Timing, Trades | Leave a Comment »

Friday Indicator Preview

Posted by The Average Investor on Dec 2, 2011

Unless the markets close sharply higher, more than 5.13% on the S&P 500, the 20-week moving average will be above the close, thus, no changes to its out-of-market status. Likewise, the contrarian DVI indicator is also indicating a short as of the Friday’s close and so does my ARMA indicators for most of the values (a bit unstable today). Are the indicators foreseeing something we can’t grasp by our emotional thinking? Next week will tell.

Posted in Market Timing | 1 Comment »

S&P 500 10 month SMA action preview

Posted by The Average Investor on Nov 30, 2011

Today is the last trading day for the month and the markets are exploding, so are we close to the S&P 500 closing above its 10-month (or 12-month) moving average? The answer is negative. Unless the SPY (the most liquid S&P 500 ETF) closes at $128.81 or higher, we will still end November below the 10-month SMA. This is 7.23% higher than Thursday’s close. Not quite likely, but hey, put a LOC (limit order at the close) in your Interactive Brokers account.

To compute these values, I use a parallel approach to the one shown for the DVI indicator. The code will be posted soon, but right now I am quite busy with some improvements on the ARMA/GARCH strategies.

Posted in Market Timing | Leave a Comment »

The Weekly Update

Posted by The Average Investor on Nov 20, 2011

A very ugly week in the markets. The S&P 500 lost -3.69%, which put it below its 20-week moving average. The index was above this moving average since Oct 21, catching some of the October rally, but last week slammed this trade into losing territory, -1.61%.

The market was down 4 of the 5 trading days. The DVI indicator was short on Monday (winning) and Tuesday (losing) and wend long afterwards (losing). This resulted in a lost of only -2.8%.

The real loser was my ARMA indicator. Yes that’s right, it was even worst. It got the market direction wrong every single day of the week for a whopping -4.67% lost.

Both DVI and ARMA are long for Monday (check the right bar on the blog) and they seem to indicate that markets are entering into oversold territory.

Posted in Market Timing, Trades | Leave a Comment »

The Weekly Update

Posted by The Average Investor on Nov 12, 2011

Positive, but very volatile week. Here is the performance up-to-date for the two (imaginary) positions based on long term moving averages:

Moving Average Position Since Gain
20 Week Long (SPY) 2011-10-21 2.17%
10 Month Out (IEF) 2011-08-31 0.67%

I am dropping the 200-day moving average because it is hard to follow on weekly basis.

The DVI indicator was long for the entire week, so it followed the performance of the index – 0.94%. As of the Friday close however the DVI indicator went above 0.5, which indicates a short position for Monday.

Posted in Market Timing, Trades | Leave a Comment »

%d bloggers like this: