# Pairs Trading

Pairs Trading

Basically, we use pairs trading to hedge the risk of the portfolio. The mechanism of this strategy is to wait for weakness in the correlation of two similar stocks, and then go long on the under-performer while simultaneously going short on the over-performer, closing the positions as the relationship returns to its statistical norm. The trick of pairs trading is to find two stocks are not just correlated but also cointegrated( ‘they might not necessarily follow a similar path, but they both end up at this destination’). Here I find the examples from Quantopian:

The first picture has two lines with the correlation with each other but no cointegration since they are not approaching to the same destination but moving further away; the second one has two lines may not correlate with each other but they have cointegration.

Thus, the most important part of pairs trading is to identify pairs of securities whose prices exhibited similar historical price movements( with high cointegration).

Another important mechanism is that we should always long the under-performer and short the over-performer when their prices are moving apart or when they are coming close. As for the profit, if they both go down, we neither make nor lose money and likewise if both go up. (If the stock you are longing now is the one with higher price then you will lose money when both prices go down; but if the stock you are shorting now is the one with higher price then you will make a profit). We make money on the difference of the two reverting to the mean. Therefore, a profit can be realized if the long position goes up more than the short, or the short position goes down more than the long( but the ideal situation that we expect is the long position will rise and short position will fall). Thus, it is possible for pairs trader to make profits in various market situations such as when the market goes up, down or sideways or even during periods of either low or high volatility.

The first step when we are going to construct a Pairs Trading Portfolio is to find real securities that behave like each other(usually they are from the same industry). The industry I choose here is Biotech. I start from Loxo Oncology, Inc.(that we are interested in) then I find a list of companies(I will only show the abbreviation of those companies) which are the competitors of Loxo Oncology:

IG GSK NVS CPHD PBH GNC JNJ PFE MRTX

Also, I include the S&P500(SPY) into the symbol list too. I collect those stocks closing prices from 01/01/2014 to 06/01/2015 on daily basis(but I will only show the output of the first 10 days) and here is the price table in the end:

Then here is the outcome of cointegrated test on those stocks’ prices:

The assumption we have here is: these two stocks have cointegration(confidence coefficiency I set is 95%) which is measured by p-value. If p-value <= 0.05 then we cannot reject the hypothesis. The darker the green the lower the p-value and the qualified combination is only one here S1 = ‘CPHD’, S2 = ‘GNC’. I run the test on the set and the result is below:

The p-value of this set is 0.03 which is smaller than 0.05 so we cannot reject the null hypothesis.(These stocks have cointegeration relationship.)

The only factor I care about is the gap or spread between two stocks. So I plot the spread graph of those two stocks(CPHD & GNC) and find the mean value line on the graph:

The mean spread of these two stocks is $6.73 throughout the first 6 months of 2015.

Then I need to convert this distribution into normal distribution by calculating the z-score and re-plot the graph to find the z-score when it is 1 and -1( which implies that in the 68% of spread will be included in this range). As for the boundaries in dollar value are $1.15(when z-score = -1) and $12.31(when z-score = 1) respectively.

Strategy:

- Long S1(CPHD) and short S2(GNC) when z-score < -1(S1 — S2 < $1.15); the probability <= 16% under this circumstance which means there is less than 16% probability that spread between two stocks will smaller than $1.15, that is to say, most likely(around 84% possibility) either price of S1(CPHD) will increase or price of S2(GNC) will decrease in the future.
- Short S1(CPHD) and long S2(GNC) when z-score > 1(S1-S2 > $12.31); the probability <= 16% under this circumstance which means there is less than 16% probability that spread between two stocks will larger than $12.31, that is to say, most likely(around 84% possibility) either price of S2(GNC) will increase or price of S1(CPHD) will decrease in the future.
- Exit the positions when z-score approaches 0.( It is hard to create arbitrage opportunity under this situation.)

I will soon implement this strategy and do the backtest. I will update the process here soon.

Also, please feel free to comment on inefficiencies about anything I explained here since I am a beginner and I believe the more mistakes I made and then fix them the faster I will make progress.

**Reference:**

**Quantopian: Clone Notebook — — Pairs Trading****http://www.investopedia.com/university/guide-pairs-trading/**

Here is the code I used to generate the outcomes above:

Also, I have learnt to use the moving averages as the criteria to calculate the z-score of two stocks and make the trading strategy.

Here is the introduction about the use of moving average in trading:

Moving averages are utilized as trend indicators and to identify significant support and resistance levels. Traders and market analysts watch for crossovers of longer-term moving averages by shorter-term moving averages as possible indicators of trend change in intraday trading and in regard to long-term trends.

So I choose 15 days as short-term mavg period and 60 days as long-term mavg period to find the different moving average between two stocks(S1, S2).

In this step, I calculate the z-score of two ‘diff_mavg’ s and measure the probability distribution between them with the z-scores.

The same principle as the prices trading explained before. Based on the distribution of z-score we can make the trading strategy(long, short) for our portfolio. Here I want to show one more graph which is the combined graph which has both stock prices movement and z-scores of the difference of moving averages.(Careful: I divided the stock price by 10 in order to put the stock prices and z-score at the same graph).

Also, please feel free to comment on inefficiencies about anything I explained here since I am the beginner for this and I believe the more mistakes I fix the faster I can grow.