## 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):

library(quantmod) library(rugarch) 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( variance.model=list(garchOrder=c(1,1)), mean.model=list(armaOrder=c(4,5), include.mean=T), distribution.model="sged") 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), order.by=index(ss["2005"])) ind = ifelse(ind < 0, -1, 1) # Compute the performance mm = merge( ss["2005"], ind, all=F ) tail(cumprod(mm[,1]*mm[,2]+1)) # Output (last line): 2005-12-30 1.129232

Hats down to brilliancy!

## Costa said

Your code doesn’t run. Something missing…

## your name said

he uses 2000/2005 while the getSymbols() returns 2007-2011

## your name said

and aa instead of ss

## The Average Investor said

Fixed. Sorry.

## Adam said

Dear Sir,

Why is ARMA order 4,5?

Thanks for your help.

## The Average Investor said

The way I calibrate the model is to cycle through a range of parameter values, let’s say (0,0) to (10,10), picking the best model best on the Akaike Information Criterion (AIC). Here, I run my function that does that, and (4,5) showed as best. You can find an example in the second post in the series, https://theaverageinvestor.wordpress.com/2011/04/21/arma-models-for-trading-part-ii/, however, it is based on the fArma code and has only mean component (no GARCH). Hope this helps!

## Paul said

Dear Average Investor,

Is it possible to extract forecasted values (one step ahead rolling window) from the above code? Namely from

fore = ugarchforecast(fit, n.ahead=1, n.roll=outOfSample)

or

ugarchroll () function?

Best Regards,

Paul

## The Average Investor said

The “fore” in the above code contains the forecasted daily returns. The following code uses them via “as.array(fore)[,2,]”.

## ARMA+GARCH Experiences said

[…] is that they don’t support rolling forecasts, a feature supported by the other packages (this older post of mine illustrates this feature using the rugarch […]

## SophiaFernandez said

I can’t run the code the errors that R throws me are:

> ind = xts(head(as.array(fore)[,2,],-1), order.by=index(ss[“2005”]))

Error en as.array.default(fore) : invalid first argument

> ind = ifelse(ind < 0, -1, 1)

Error en ifelse(ind

> # Compute the performance

> mm = merge( ss[“2005”], ind, all=F )

Error en merge.xts(ss[“2005”], ind, all = F) : object ‘ind’ not found

> tail(cumprod(mm[,1]*mm[,2]+1))

Error en tail(cumprod(mm[, 1] * mm[, 2] + 1)) : object ‘mm’ not found

help?

## danny b said

I too have the same error

Help?

## The Average Investor said

Looks like things got changed in R, or in rugarch since I used it. From quickly looking at it, I suspect that the line should be:

ind = xts(head(as.numeric(fore@forecast$seriesFor),-1), order.by=index(rets[“2005”]))

However, I might be wrong. It should be relatively straightforward to correct if you look at the format of the *fore* object.