Wednesday, May 18, 2011

Wonderful New Blog TimeSeriesIreland

I returned from Scotland to find a wonderful new blog from Ireland http://timeseriesireland.wordpress.com.  To highlight his work, I thought I would apply his most recent post AIB Stock Price, EGARCH-M, and rgarch to the S&P 500.  Clearly the author of TimeSeriesIreland has a much better grasp of time series statistics than I do, so I will not attempt to change lag orders or perfect the model.  Rather I will use his model specifications for AIB daily data for S&P 500 weekly data.  This should be fun; maybe this will provoke some comments.

From TimelyPortfolio
From TimelyPortfolio
From TimelyPortfolio
From TimelyPortfolio
From TimelyPortfolio
From TimelyPortfolio

And, it is a shame that I need to disclaim, but THIS IS FOR ILLUSTRATIVE PURPOSES ONLY AND SHOULD NOT BE CONSIDERED INVESTMENT ADVICE.  YOU ARE RESPONSIBLE FOR YOUR OWN GAINS AND LOSSES.  I built a very basic system around the fittedmodel$z just for fun.  Here are the results.

From TimelyPortfolio

R code:

#all credit for this code goes to the very insightful author
#of http://timeseriesireland.wordpress.com
#based on the first couple of posts I look forward to following him
#for explanation of the statistics and their use, please see
#http://timeseriesireland.wordpress.com/2011/05/17/aib-stock-price-egarch-m-and-rgarch/#more-295

#I change the code to use SP500 weekly xts data instead of AIB daily tseries data

require(rgarch)
require(urca)
require(ggplot2)
require(quantmod)
require(PerformanceAnalytics)

#define start and end dates
start<-"1929-01-01"
end<- format(Sys.Date(),"%Y-%m-%d") # yyyy-mm-dd

tckr<-"^GSPC"
#use quantmod to get SP500 data
getSymbols(tckr,from=start,to=end)
GSPC<-to.weekly(GSPC)

#get log returns, could also use ROC with type = "continuous"
LGSPC<-log(GSPC[,4])
retGSPC<-diff(LGSPC)

#data frame allows us to use ggplot with date data from xts
#I have not found any better way to ggplot xts data
df1<-data.frame(index(GSPC),coredata(GSPC[,4]))
colnames(df1)<-c("dates","sp500")

### Plot sp500 price:
gg1.1<-ggplot(df1,aes(dates,sp500)) + xlab(NULL) + ylab("SP500 log Price") + scale_y_log10()
gg1.2<-gg1.1+geom_line(colour="darkblue") + opts(title="Weekly SP500 Price 1950-current")
gg1.2

#set first return to 0
retGSPC[1]<-0

df2<-data.frame(index(retGSPC),coredata(retGSPC))
colnames(df2)<-c("dates","sp500")

gg2.1<-ggplot(df2,aes(dates,sp500)) + xlab(NULL) + ylab("Log Changes")
gg2.2<-gg2.1+geom_line(colour="darkred") + opts(title="Weekly SP500 Price Return")
gg2.2

### ACFs and PACFs
par(mfrow=c(2,1))
acf(retGSPC, main="ACF of SP500 Log Returns", lag = 50)
pacf(retGSPC, main="PACF of SP500 Log Returns", lag = 50)

ar9<-arima(retGSPC, order=c(9,0,0))
acf(ar9$residuals)

ressq<-(ar9$residuals)^2

Box.test(ressq, lag = 8, type = "Ljung-Box")

pacf(ressq, main="PACF of Squared Residuals", lag = 30)

# Note that the GARCH order is revered from what I have discussed above
specm1 <- ugarchspec(variance.model=list(model="eGARCH", garchOrder=c(2,4), submodel = NULL),
        mean.model=list(armaOrder=c(9,0), include.mean=TRUE, garchInMean = TRUE))
#this might take a while
fitm1 <- ugarchfit(data = retGSPC, spec = specm1)
fitm1
#plot(fitm1) #use option 8

fittedmodel <- fitm1@fit
sigma1<-fittedmodel$sigma

df2<-data.frame(index(retGSPC),coredata(retGSPC),sigma1)
colnames(df2)<-c("dates","sp500","sigma1")

gg3.1<-ggplot(df2,aes(dates)) + xlab(NULL) + ylab("Log Changes")
gg3.2<-gg3.1+geom_line(aes(y = sp500, colour="Log Returns")) + opts(title="Weekly Log Return with 2 Conditional Standard Deviations")
gg3.3<-gg3.2 + geom_line(aes(y = sigma1*2, colour="2 S.D.")) + geom_line(aes(y = sigma1*-2, colour="2 S.D.")) + scale_colour_hue("Series:") + opts(legend.position=c(.18,0.8))
gg3.3

fitm2 <- ugarchfit(data = retGSPC,out.sample = 10, spec = specm1)
fitm2
pred <- ugarchforecast(fitm2, n.ahead = 10,n.roll = 0)
pred.fpm <- fpm(pred)
pred.fpm

#just because I cannot stand it
#I'll play with a system
#not something I would bet my money on
signal<-runMean(as.xts(fittedmodel$z,order.by=index(retGSPC)),50)
#chartSeries(signal)
signal<-lag(signal,k=1)
signal[is.na(signal)]<-0

ret<-ifelse(signal > 0,ROC(GSPC[,4],1,type="discrete"),0)
returnCompare<-merge(ret,ROC(GSPC[,4],1,type="discrete"))
colnames(returnCompare)<-c("ZSystem","SP500")
charts.PerformanceSummary(returnCompare,ylog=TRUE,main="Just for Fun Z System")

4 comments:

  1. Very nice example. Nevertheless, the strategy is based on insample information and as such does not provide any insight as to how the system will perform out of sample. For true out of sample testing you would really need to base your signals on the forecasts (ideally a rolling estimation/forecast).
    Its my experience that simpler systems with less lags will also perform better out of sample.

    ReplyDelete
  2. I love comments. Keep them coming. The system was a byproduct and not the focus of the post, so the exact opposite of the way I like to build systems. I have no intentions to trade this system.

    For me, I need robust results from out of sample other markets and other timeframes before I commit to a system, so I agree completely there. Even though I do not expect this system to go anywhere, I'll try to write a post testing other markets and timeframes.

    By lags, I'm assuming you are meaning the lag chosen in the GARCH, and I just chose the model specified in the original timeseriesireland post. In another post, I can also play with these. If by lag, you mean the 50 week average of the z, then in my experience, the longer the better in terms of robustness. Past 1 year though I have not had much luck but might learn something from http://papers.ssrn.com/sol3/papers.cfm?abstract_id=1833613&http://papers.ssrn.com/sol3/papers.cfm?abstract_id=1833613.

    Simpler is always better in system building. Great point and one I have not focused on in my posts.

    You have inspired another blog post. Look for it in the next couple of days. Thanks again for the comment.

    ReplyDelete
  3. The authors have deleted this blog. Why? Do you know author's email? Send we all an email to ask him to change his mind

    ReplyDelete
  4. Thanks for the update. Unfortunately, I have no idea what happened after such a wonderful debut. The site is gone, and the email I had no longer works. I have no other way of contacting him.

    ReplyDelete