Path: blob/master/Analysis/05 Pairs Trading Strategy Based on Cointegration.ipynb
1001 views

Pairs Trading Based on Cointegration
Pairs trading is a market neutral trading strategy and it belongs to statistical arbitrage. The basic idea is to select two stocks which move similarly, sell the high priced stock and buy the low priced stock where there is a price divergence between the pairs.
Check the algorithm implemented on LEAN
Cointegration
Before using pairs trading, we need to know the cointegration. Cointegration is a statistical property of time series (that is a series of random variables)
Correlation specify the co-movement of return, it is a short-term relationship
Cointegration specify co-movement of price, it is a long-term relationship
Generally speaking, the weak stationary process means the mean and the autocovariance do not vary over time. For example, white noise is an example of stationary time series because and .
If two series {} and {} are not stationary but their linear combination is a stationary process, then we say {xt} and {yt} are cointegrated. Here we take the first-order difference of x and y, then we say their relationship is first-order cointegration. Most of the financial time series are integrated of order 1.
In general, we use Augmented Dickey-Fuller test to test cointegration.
Step 1: Find two likely cointegrated stocks
Two stocks we choose here is XOM and CVX. They are two American multinational oil and gas corporations. They are of the same industry.
Step 2: Estimate Spreads
If we have two stocks, X & Y, that are cointegrated in their price movements, then any divergence in the spread from 0 should be temporary and mean-reverting. Next step we will estimate the spread series.
Step 3: Check Stationarity
From the above plot, the first order difference seems to be stationary and mean-reverting. Next we will check if it is stationary. We use the ADF test to check the stationarity of the spread series.
Running the example prints the test statistic value of -3.45. The more negative this statistic, the more likely we are to reject the null hypothesis (there is a unit root).
As part of the output, we get a table to help determine the ADF statistic. We can see that our statistic value of -3.45 is less than the value of -3.435 at 1%. The p-value 0.009 is less than 0.05.
This suggests that we can reject the null hypothesis with a significance level of less than 5%. Rejecting the null hypothesis means that the process has no unit root, and in turn that the time series is stationary or does not have time-dependent structure.
Step 4: Create Trading Signal
Here we use 1.5 times the standard deviation as our threshold. If the sample is out of this range, then we think there is a price divergence between these two stocks price which means we capture a pairs trading opportunity. The rolling window of the history close price over 250 days is used to perform the regression.
Trading strategy development in LEAN cloud