<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/co-integration-and-pairs-trading/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://statcompute.wordpress.com/2018/07/29/co-integration-and-pairs-trading/"> S+/R – Yet Another Blog in Statistical Computing</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>The co-integration is an important statistical concept behind the statistical arbitrage strategy named “Pairs Trading”. While projecting a stock price with time series models is by all means difficult, it is technically feasible to find a pair of (or even a portfolio of) stocks sharing the common trend such that a linear combination of two series is stationary, which is so-called co-integration. The underlying logic of Pairs Trading is to monitor movements of co-integrated stocks and to look for trading opportunities when the divergence presents. Under the mean-reversion assumption, the stock price would tend to move back to the long-term equilibrium. As a result, the spread between two co-integrated stock prices would eventually converge. Furthermore, given the stationarity of the spread between co-integrated stocks, it becomes possible to forecast such spread with time series models. </p>
<p>Below shows a R utility function helping to identify pairwise co-integrations based upon the Johansen Test out of a arbitrary number of stock prices provided in a list of tickers. </p>
<p>For instance, based on a starting date on 2010/01/01 and a list of tickers for major US banks, we are able to identify 23 pairs of co-integrated stock prices out of 78 pairwise combinations. It is interesting to see that stock prices of two regional players, e.g. Fifth Third and M&amp;T, are highly co-integrated, as visualized in the chart below. </p>
<pre>

pkgs &lt;- list(&quot;quantmod&quot;, &quot;doParallel&quot;, &quot;foreach&quot;, &quot;urca&quot;)
lapply(pkgs, require, character.only = T)
registerDoParallel(cores = 4)

jtest &lt;- function(t1, t2) {
  start &lt;- sd
  getSymbols(t1, from = start)
  getSymbols(t2, from = start)
  j &lt;- summary(ca.jo(cbind(get(t1)[, 6], get(t2)[, 6])))
  r &lt;- data.frame(stock1 = t1, stock2 = t2, stat = j@teststat[2])
  r[, c(&quot;pct10&quot;, &quot;pct5&quot;, &quot;pct1&quot;)] &lt;- j@cval[2, ]
  return(r)
}

pair &lt;- function(lst) {
  d2 &lt;- data.frame(t(combn(lst, 2)))
  stat &lt;- foreach(i = 1:nrow(d2), .combine = rbind) %dopar% jtest(as.character(d2[i, 1]), as.character(d2[i, 2]))
  stat &lt;- stat[order(-stat$stat), ]
  # THE PIECE GENERATING * CAN'T BE DISPLAYED PROPERLY IN WORDPRESS 
  rownames(stat) &lt;- NULL
  return(stat)
}

sd &lt;- &quot;2010-01-01&quot;
tickers &lt;- c(&quot;FITB&quot;, &quot;BBT&quot;, &quot;MTB&quot;, &quot;STI&quot;, &quot;PNC&quot;, &quot;HBAN&quot;, &quot;CMA&quot;, &quot;USB&quot;, &quot;KEY&quot;, &quot;JPM&quot;, &quot;C&quot;, &quot;BAC&quot;, &quot;WFC&quot;)
pair(tickers)

   stock1 stock2      stat pct10 pct5  pct1 coint
1     STI    JPM 27.207462 12.91 14.9 19.19  ***
2    FITB    MTB 21.514142 12.91 14.9 19.19  ***
3     MTB    KEY 20.760885 12.91 14.9 19.19  ***
4    HBAN    KEY 19.247719 12.91 14.9 19.19  ***
5       C    BAC 18.573168 12.91 14.9 19.19   **
6    HBAN    JPM 18.019051 12.91 14.9 19.19   **
7    FITB    BAC 17.490536 12.91 14.9 19.19   **
8     PNC   HBAN 16.959451 12.91 14.9 19.19   **
9    FITB    BBT 16.727097 12.91 14.9 19.19   **
10    MTB   HBAN 15.852456 12.91 14.9 19.19   **
11    PNC    JPM 15.822610 12.91 14.9 19.19   **
12    CMA    BAC 15.685086 12.91 14.9 19.19   **
13   HBAN    BAC 15.446149 12.91 14.9 19.19   **
14    BBT    MTB 15.256334 12.91 14.9 19.19   **
15    MTB    JPM 15.178646 12.91 14.9 19.19   **
16    BBT   HBAN 14.808770 12.91 14.9 19.19    *
17    KEY    BAC 14.576440 12.91 14.9 19.19    *
18   FITB    JPM 14.272424 12.91 14.9 19.19    *
19    STI    BAC 14.253971 12.91 14.9 19.19    *
20   FITB    PNC 14.215647 12.91 14.9 19.19    *
21    MTB    BAC 13.891615 12.91 14.9 19.19    *
22    MTB    PNC 13.668863 12.91 14.9 19.19    *
23    KEY    JPM 12.952239 12.91 14.9 19.19    *

</pre>
<p><img data-orig-file="https://statcompute.files.wordpress.com/2018/07/screenshot-from-2018-07-29-16-55-27.png?w=456" data-orig-size="1865,681" data-medium-file="https://statcompute.files.wordpress.com/2018/07/screenshot-from-2018-07-29-16-55-27.png?w=456?w=300" data-attachment-id="3186" data-image-meta="{" data-image-description="" src="https://statcompute.files.wordpress.com/2018/07/screenshot-from-2018-07-29-16-55-27.png?w=456" data-permalink="https://statcompute.wordpress.com/2018/07/29/co-integration-and-pairs-trading/screenshot-from-2018-07-29-16-55-27/" data-large-file="https://statcompute.files.wordpress.com/2018/07/screenshot-from-2018-07-29-16-55-27.png?w=456?w=1865" alt="Screenshot from 2018-07-29 16-55-27" data-image-title="Screenshot from 2018-07-29 16-55-27" sizes="(max-width: 700px) 100vw, 700px" data-comments-opened="0" class="wp-image-3186" data-recalc-dims="1"></p>
<p><img data-orig-file="https://statcompute.files.wordpress.com/2018/07/screenshot-from-2018-07-29-15-09-11.png?w=456" data-orig-size="1727,831" data-medium-file="https://statcompute.files.wordpress.com/2018/07/screenshot-from-2018-07-29-15-09-11.png?w=456?w=300" data-attachment-id="3175" data-image-meta="{" data-image-description="" src="https://statcompute.files.wordpress.com/2018/07/screenshot-from-2018-07-29-15-09-11.png?w=456" data-permalink="https://statcompute.wordpress.com/2018/07/29/co-integration-and-pairs-trading/screenshot-from-2018-07-29-15-09-11/" data-large-file="https://statcompute.files.wordpress.com/2018/07/screenshot-from-2018-07-29-15-09-11.png?w=456?w=1727" alt="Screenshot from 2018-07-29 15-09-11" data-image-title="Screenshot from 2018-07-29 15-09-11" sizes="(max-width: 700px) 100vw, 700px" data-comments-opened="0" class="wp-image-3175" data-recalc-dims="1"></p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://statcompute.wordpress.com/2018/07/29/co-integration-and-pairs-trading/"> S+/R – Yet Another Blog in Statistical Computing</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/_TdfLed4RuE" height="1"></a></div>