# Statistical Arbitrage – Trading a cointegrated pair

In my last post http://gekkoquant.com/2012/12/17/statistical-arbitrage-testing-for-cointegration-augmented-dicky-fuller/ I demonstrated cointegration, a mathematical test to identify stationary pairs where the spread by definition must be mean reverting.

In this post I intend to show how to trade a cointegrated pair and will continue analysing Royal Dutch Shell A vs B shares (we know they’re cointegrated from my last post). Trading a cointegrated pair is straight forward, we know the mean and variance of the spread, we know that those values are constant. The entry point for a stat arb is to simply look for a large deviation away from the mean.

A basic strategy is:

• If spread(t) >= Mean Spread + 2*Standard Deviation then go Short
• If spread(t) <= Mean Spread – 2*Standard Deviation then go Long
There are many variations of this strategy
Moving average / moving standard deviation (this will be explored later):
• If spread(t) >= nDay Moving Average + 2*nDay Rolling Standard deviation then go Short
• If spread(t) <= nDay Moving Average – 2*nDay Rolling Standard deviation then go long
Wait for mean reversion:
• Advantage is that we only trade when we see the mean reversion, where as the other models are hoping for mean reversion on a large deviation from the mean (is the spread blowing up?)
All the above strategies look to exit their position when the spread has reverted to the mean. Personally I wouldn’t trade any of the above as they don’t specify an exit strategy for adverse trades. Ie if there is a 6 standard deviation move in the spread is this an amazing trade opportunity? OR more likely did the spread just blow up.

This post will look at the moving average and rolling standard deviation model for Royal Dutch Shell A vs B shares, it will use the hedge ratio found in the last post.  Sharpe Ratio Shell A & B Stat Arb Shell A
Annualized Sharpe Ratio (Rf=0%):

Shell A&B Stat Arb 0.8224211

Shell A 0.166307

The stat arb has a Superior Sharpe ratio over simply investing in Shell A. At a first glance the sharpe ratio of 0.8 looks disappointing, however since the strategy spends most of it’s time out of the market it will have a low annualized sharpe ratio. To increase the sharpe ratio one can look at trading higher frequencies or have a portfolio pairs so that more time is spent in the market.

Onto the code:

?View Code RSPLUS
 ```library("quantmod") library("PerformanceAnalytics")       backtestStartDate = as.Date("2010-01-02") #Starting date for the backtest   symbolLst<-c("RDS-A","RDS-B") title<-c("Royal Dutch Shell A vs B Shares")   ### SECTION 1 - Download Data & Calculate Returns ### #Download the data symbolData <- new.env() #Make a new environment for quantmod to store data in getSymbols(symbolLst, env = symbolData, src = "yahoo", from = backtestStartDate)   #We know this pair is cointegrated from the tutorial #http://gekkoquant.com/2012/12/17/statistical-arbitrage-testing-for-cointegration-augmented-dicky-fuller/ #The tutorial found the hedge ratio to be 0.9653 stockPair <- list( a = coredata(Cl(eval(parse(text=paste("symbolData\$\"",symbolLst,"\"",sep=""))))) #Stock A ,b = coredata(Cl(eval(parse(text=paste("symbolData\$\"",symbolLst,"\"",sep=""))))) #Stock B ,hedgeRatio = 0.9653 ,name=title)   simulateTrading <- function(stockPair){ #Generate the spread spread <- stockPair\$a - stockPair\$hedgeRatio*stockPair\$b   #Strategy is if the spread is greater than +/- nStd standard deviations of it's rolling 'lookback' day standard deviation #Then go long or short accordingly lookback <- 90 #look back 90 days nStd <- 1.5 #Number of standard deviations from the mean to trigger a trade   movingAvg = rollmean(spread,lookback, na.pad=TRUE) #Moving average movingStd = rollapply(spread,lookback,sd,align="right", na.pad=TRUE) #Moving standard deviation / bollinger bands   upperThreshold = movingAvg + nStd*movingStd lowerThreshold = movingAvg - nStd*movingStd   aboveUpperBand <- spread>upperThreshold belowLowerBand <- spreadmovingAvg belowMAvg <- spread