Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pairs Trading in R Amst-R-dam meet-up June 2011 Øyvind Foshaug.

Similar presentations


Presentation on theme: "Pairs Trading in R Amst-R-dam meet-up June 2011 Øyvind Foshaug."— Presentation transcript:

1 Pairs Trading in R Amst-R-dam meet-up June 2011 Øyvind Foshaug

2 ret(Spread) A,B,t = ret(A) t – ret(B) t

3 url="http://www.euronext.com/search/download/trapridownloadpopup.jc sv?belongsToList=market_EURLS&capitalizationList=capi_CPTA&cha=7213 &features=1&filter=1&lan=NL&mep=8626&pricesearchresults=actif&resul tsTitle=Amsterdam+-+Euronext+- +Segment+A+%28Blue+chips%29&totalOfInstruments=52"

4 library(TTR) library(fSeries) #Get symbol info from Euronext symbols=read.csv(row.names=NULL,na.strings="- ",sep=';',header=T,skip=3,url) #Get historical price info and AEX index from YAHOO li=sapply(c("^AEX",paste(symbols[,5],".AS",sep='')),function(x) try(getYahooData(x, 20050101, 20110601)[,"Close"]),simplify=FALSE) #Filter on length minlength=sapply(li,function(x) length(x[!is.na(x)])>530) li2=li[minlength] names=symbols[minlength,1] #Create matrix s=do.call(merge,li2) colnames(s)=1:dim(s)[2] #Plot result plot(as.zoo(s))

5

6 skip=c(4,6,8,12,14,24,27,32,37,39,42:44,46,47,48) s2=s[,-skip] plot(as.zoo(s2)) colnames(s2)=names(li2)[-skip]

7 library(dlmodeler) build.fun <- function(p,ind) { m1=dlmodeler.build.arima(ar=c(p[3]), ma=c(), d=0, sigmaH = exp(p[1]), sigmaQ = exp(p[2]), name = "OU") m2=dlmodeler.build.regression( t(na.omit(ind)), sigmaQ=0, sigmaH=0, intercept = TRUE, name='beta') dlmodeler.add(m1,m2,name='spread') } ret(S) t =µ+β*ret(AEX) t +O t +h t O t =αO t-1 +q t

8 estSS=function(x,s) { ts0=na.omit(s[,c(1,x)]) # Include AEX ts0=window(ts0,start=(end(ts0)-60*60*24*365),end=end(ts0)) ret=returns(cbind(ts0[,1],ts0[,2]/ts0[,3])) fit <- dlmodeler.fit.MLE(na.omit(ret[,2]), build.fun, c(0,0,0), verbose=FALSE,ind=ret[,1]) f <- dlmodeler.filter(na.omit(ret[,2]), fit$mod, smooth=TRUE) list(fit=fit,f=f,ts=ts0, retts=ret) } SSmat=apply(combs(2:dim(s2)[2],2),1,function(x) try(estSS(x,s=s2))) ss=SSmat[!sapply(SSmat,function(x) class(x)=="try-error")] 1 2 S t, Σ (µ+β*AEX) t, Σ E(O) t

9

10 #Main window with horizontally stacked widgets win = ggroup(horizontal=FALSE,container=gwindow("Pairs Trading",expand=TRUE,width=600)) #Data frame with pair names df=as.data.frame(sapply(ss,function(x) paste(names[as.numeric(colnames(x$ts)[2])],names[as.numeric(colnames (x$ts)[3])],sep="/"))) names(df)="Pair #create a notebook (sheet) widget and add it to the main window notebook = gnotebook(container=win,expand=T,height=500) #create a table widget with the data-frame table = gtable(df,expand=TRUE) #Add table to notebook Notebook2 = add(notebook,table,label="PAIRS") #Another notebook for plots added to main window plot=gnotebook(container=win,expand=T,tab.pos=4,height=200) #Add a grpaphics widget to the second notebook add(plot,ggraphics(),label="Plot1",expand=TRUE)

11 addhandlerclicked(plot, handler = function(h,...) { id<-svalue(h$obj,index=TRUE) f=ss[id][[1]]$f fit=ss[id][[1]]$fit ret=ss[id][[1]]$retts s.ce <- dlmodeler.extract(f$smooth, fit$model,type="observation", value="mean") r=as.zoo(as.xts(ret[,2])) O=cumsum(s.ce$OU[1,-1]) S=cumsum(r[-1]) Sm=cumsum(s.ce$spread[1,]) Beta=cumsum(s.ce$beta[1,]) plot(S,ylim=c(min(O,S,Sm),max(O,S,Sm))) lines(zoo(Sm,index(S)),col="green") lines(zoo(O,index(S)),col="red") lines(zoo(Beta,index(S)),col="blue") })


Download ppt "Pairs Trading in R Amst-R-dam meet-up June 2011 Øyvind Foshaug."

Similar presentations


Ads by Google