Poor man’s pairs trading… 7

There is a central notion in Time Series Econometrics, cointegration. Loosely it refers to finding the long run equilibrium of two non-stationary series. As the most know non-stationary series examples comes from finance, cointegration is nowadays a tool for traders (not a common one though!). They use it as the theory behind pairs trading (aka Statistical Arbitrage).

In the following lines we use a simple pairs trading technique, studying the ratio of the two price evolution series. We use the New York versions of two of the greatest players in ATHEX, NBG and OTE.

stock <- "NBG"
stock1 <- "OTE"
start.date <- "2003-10-20"
end.date <- Sys.Date()
quote <- paste("http://ichart.finance.yahoo.com/table.csv?s=",
 stock,
 "&a=", substr(start.date,6,7),
 "&b=", substr(start.date, 9, 10),
 "&c=", substr(start.date, 1,4),
 "&d=", substr(end.date,6,7),
 "&e=", substr(end.date, 9, 10),
 "&f=", substr(end.date, 1,4),
 "&g=d&ignore=.csv", sep="")
quote1 <- paste("http://ichart.finance.yahoo.com/table.csv?s=",
 stock1,
 "&a=", substr(start.date,6,7),
 "&b=", substr(start.date, 9, 10),
 "&c=", substr(start.date, 1,4),
 "&d=", substr(end.date,6,7),
 "&e=", substr(end.date, 9, 10),
 "&f=", substr(end.date, 1,4),
 "&g=d&ignore=.csv", sep="")
dataNBG.l <- read.csv(quote, as.is=TRUE)
dataOTE.l <- read.csv(quote1, as.is=TRUE)
X2=dataOTE.l[order(dataOTE.l$Date),];Y2=dataNBG.l[order(dataNBG.l$Date),]


Now, let’s plot the ratio of the two. We locate a traing opportunity when there is an exceedence of the 2sigmas. Then we go short on the OTE and long on the NBG and close the positions on when the ratio is approaching the mean.

par(mfrow=c(1,1),fg = gray(0.7), bty="7")
plot(X2$Close,ylim=c(0,max(X2$Close)),ylab="Price",type="l")
lines(Y2$Close,col="red");grid()
lines(X2$Close/Y2$Close,col="green")

pairt=X2$Close/Y2$Close
abline(h=mean(pairt)+c(-2:2)*sd(pairt),col=c("black","purple"))
abline(h=mean(pairt),col="red")

Looking at the above there was a opportunity to start a pair trading at the end of the 2009…

X2$Date[which(abs(pairt-mean(pairt))>2*sd(pairt))]
#   [1] "2008-11-19" "2008-11-20" "2008-11-21" "2008-12-01" "2008-12-02"
#   [6] "2008-12-03" "2008-12-04" "2008-12-05" "2008-12-08" "2008-12-09"
#  [11] "2008-12-10" "2008-12-11" "2008-12-12" "2008-12-15" "2008-12-16"
#  [16] "2008-12-17" "2008-12-18" "2008-12-19" "2008-12-22" "2008-12-23"
#  [21] "2008-12-24" "2008-12-26" "2008-12-29" "2008-12-30" "2008-12-31"
#  [26] "2009-01-02" "2009-01-05" "2009-01-06" "2009-01-07" "2009-01-08"
#  [31] "2009-01-09" "2009-01-12" "2009-01-13" "2009-01-14" "2009-01-15"
#  [36] "2009-01-16" "2009-01-20" "2009-01-21" "2009-01-22" "2009-01-23"
#  [41] "2009-01-26" "2009-01-27" "2009-01-28" "2009-01-29" "2009-01-30"
#  [46] "2009-02-02" "2009-02-03" "2009-02-04" "2009-02-05" "2009-02-06"
#  [51] "2009-02-09" "2009-02-10" "2009-02-11" "2009-02-12" "2009-02-13"
#  [56] "2009-02-17" "2009-02-18" "2009-02-19" "2009-02-20" "2009-02-23"
#  [61] "2009-02-24" "2009-02-25" "2009-02-26" "2009-02-27" "2009-03-02"
#  [66] "2009-03-03" "2009-03-04" "2009-03-05" "2009-03-06" "2009-03-09"
#  [71] "2009-03-10" "2009-03-11" "2009-03-12" "2009-03-13" "2009-03-16"
#  [76] "2009-03-17" "2009-03-18" "2009-03-19" "2009-03-20" "2009-03-23"
#  [81] "2009-03-24" "2009-03-25" "2009-03-26" "2009-03-27" "2009-03-30"
#  [86] "2009-03-31" "2009-04-01" "2009-04-02" "2009-04-03" "2009-04-06"
#  [91] "2009-04-07" "2009-04-08" "2009-04-09" "2009-04-13" "2009-04-14"
#  [96] "2009-04-15" "2009-04-16" "2009-04-17" "2009-04-20" "2009-04-21"
# [101] "2009-04-22" "2009-04-23" "2009-04-24" "2009-04-27"



7 thoughts on “Poor man’s pairs trading…

  1. Pingback: Pair Trading Software

  2. Reply Paul Teetor Apr 12, 2010 3:12 am

    Where are you testing for cointegration? In other words, how to you know this pair will revert to their mean? Perhaps they will continue moving apart you will go broke.

    Paul

    • Reply M. Parzakonis Apr 12, 2010 10:45 am

      Obviously, I don’t know if there is an underlying mean-reversion process.

      My initial purpose was to give more of an illustrative example and get back with another post (more technical on stats) in a couple of days.

  3. Reply Shane Apr 13, 2010 3:23 am

    Nice, thanks!

  4. Reply Jared Aug 21, 2010 5:14 am

    Interesting article, thanks!

    Also I find looking at correlation reading in tandem with co-integration can help signal performance. And like any other good trading system, looking at related news items. It’s often said that liquidity plays a key role in pair trading alpha generation, for example industry news may be released, the largest liquid stocks can price that news in quicker, however small less liquid stocks may take longer, creating a lead-lag effect and of course a pair trading opportunity. Knowing why it works is just as important as proving it works.

  5. Reply Dirk Diggler Apr 4, 2012 6:56 pm

    Why are the standard deviation fixed, surely they change over the course of time ?

Leave a Reply