1 / 11

Pairs Trading in R

Pairs Trading in R . Amst-R-dam meet-up June 2011 Øyvind Foshaug. ret(Spread) A,B,t = ret(A) t – ret(B) t.

sonora
Download Presentation

Pairs Trading in R

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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.jcsv?belongsToList=market_EURLS&capitalizationList=capi_CPTA&cha=7213&features=1&filter=1&lan=NL&mep=8626&pricesearchresults=actif&resultsTitle=Amsterdam+-+Euronext+-+Segment+A+%28Blue+chips%29&totalOfInstruments=52"url="http://www.euronext.com/search/download/trapridownloadpopup.jcsv?belongsToList=market_EURLS&capitalizationList=capi_CPTA&cha=7213&features=1&filter=1&lan=NL&mep=8626&pricesearchresults=actif&resultsTitle=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. 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]

  6. ret(S)t=µ+β*ret(AEX)t+Ot+ht Ot=αOt-1+qt 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') }

  7. St , Σ(µ+β*AEX)t, ΣE(O)t 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

  8. #Main window with horizontally stacked widgets win = ggroup(horizontal=FALSE,container=gwindow("PairsTrading",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 plotsadded 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)

  9. 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") })

More Related