Strategy Analysis: Pairs Trading. We are going to walk through the basics... | by Posey | Towards Data Science

Pairs trading is a widely used strategy in which a long position is “paired” with a short position of two highly correlated (or cointegrated) stocks. There are many reasons for taking such a position. The position can be market neutral. That is to say, you can establish a position that seeks to make money regardless of the performance of the broader market. In such a trade, as long as the long goes up more than the short goes up or the short goes down more than the long goes down, it is a profitable trade. That was quite the tongue twister. Let’s explain...
Typically, stocks from the same sector and stocks that are direct competitors to one another are heavily correlated and consequently great candidates for pairs trading. Cointegration is also criteria for a pairs trade, and cointegration is oftentimes the more reliable strategy for successful pairs trading. Cointegration describes the distance between the two assets in price over time, whereas correlation describes the tendency to move in similar directions.
Perhaps the most obvious pairs trade would be Pepsi (PEP) and Coca-Cola (KO); this is the classical example. Let’s say one is unsure of the direction of both stocks, but one is confident that no matter the direction, Coca-Cola will outperform. With this view, one could enter a market neutral position with a long position for Coca-Cola and short position for Pepsi. Even if both stocks went down in price, as long as Pepsi goes down more than Coca-Cola, it will be a profitable trade (provided they were equally weighted positions when the trade was opened).
Let’s see how this strategy would perform. Let’s pretend the trader made this trade at the beginning of April (2019). Let’s assume they started with $100,000 and put $50,000 towards the long position and $50,000 towards the short position, effectively a market neutral position. Let’s do some analysis...
import ffnimport numpy as npprices = ffn.get('pep,ko', start='2019-04-01')stats = prices.calc_stats()stats.display()
As you can see, during this 3-month period KO outperformed PEP by about 1.5%. If you had simply gone long on either stock you would have far outperformed, with a much higher return. Your return for this pairs trade would be about .785% for a $785 return on their $100,000 trade. So why would one perform such a trade? If one had a market-neutral view but believed in KO outperforming Pepsi, this was the right trade to make. Such a trader protected their downside by not betting on market or sector direction. Let’s say you did a 100% long position in KO but the entire market went down, dragging KO with it; you would have been in the red. This market-neutral strategy ensured profits as long as KO outperformed PEP. Many times you may not have a market-neutral view but want to perform a pairs trade. In such a case, you could adjust the trade accordingly by changing the percentages of your short and long positions (change from 50/50 to whatever your market view is).
These stocks are heavily correlated, and we can view this correlation and their performance very easily. First we rebase to view the stocks on the same price scale.
# calculate correlation coefficient and then plot pricescorrelation = np.corrcoef(prices)ax = prices.rebase().plot()
We briefly discussed the basics of pairs trading. Where it gets complicated is identifying pairs, identifying entries and exits to trades, and building strategies that actually work the majority of the time. Another popular way to identify pairs is to find groups of stocks that are typically very correlated and identify the stocks which have deviated far from the mean. Such stocks typically can be profited from via mean reversion.
For example, let’s say a basket of industrials typically trade with a P/E ratio of about 24x. You may notice one of the stocks in that basket is starting to drift above its peers and has a P/E ratio of 27x. You might be mostly market-neutral that sector but believe that single stock is probably overvalued. You could open a pairs trade whereby you’re long one (or more) of the stocks trading about 24x and are short the stock trading at 27x. If the 27x stock reverts back towards the 24x stocks and the 24x stocks perform in lockstep then you’re likely to profit.
There are many more complicated strategies and methods used for pairs trading. This idea was first popularized in the 80s at Morgan Stanley. So it’s been around, been researched extensively, and continues to be widely used. I encourage you to dig even deeper into this strategy if it interests you.
Check out Dataset Daily, a newsletter where we study companies, industries, and markets each week.
This analysis was only a few lines of code, but if you’re interested you can find it and other analysis on Github here. If you are interested in following up on the intricacies of pairs trading you can check out this paper or other articles and papers like it.
poseidon01.ssrn.com
www.investopedia.com
Let’s continue the conversation on Twitter.
Note from Towards Data Science’s editors: While we allow independent authors to publish articles in accordance with our rules and guidelines, we do not endorse each author’s contribution. You should not rely on an author’s works without seeking professional advice. See our Reader Terms for details.