# Statistical Arbitrage – Trading a cointegrated pair

It only contains information for the top or so pairs. There is no need to choose a copula function so there is only a 12 month rolling formation period. This usually results in a lower Sharpe ratio. Simon's right, mid-frequency strategies generally should be fairly robust to bid-ask spreads. Please refer to step 3 to see the formulas.

Pairs trading is a form of mean reversion that has a distinct advantage of always being hedged against market movements. It is generally a high alpha strategy when backed up by some rigorous statistics. This notebook runs through the following concepts What is cointegration? How to test for cointegration? What is pairs trading? How to find cointegrated pairs?

## Determine the correlation level

In this scenario, one stock moves up while the other moves down relative to each other. If you expect this divergence to revert back to normal with time, you can make a pairs trade.

When there is a temporary divergence, the pairs trade would be to sell the outperforming stock the stock that moved up and to buy the underperforming stock the stock that moved down. Hence, pairs trading is a market neutral trading strategy enabling traders to profit from virtually any market conditions: Then we perform a cumulative sum to get the value of X on each day. Now we generate Y which has a deep economic link to X, so price of Y should vary pretty similarly as X.

We model this by taking X, shifting it up and adding some random noise drawn from a normal distribution. Cointegration, very similar to correlation, means that the ratio between two series will vary around a mean.

The two series, Y and X follow the follwing:. For pairs trading to work between two timeseries, the expected value of the ratio over time must converge to the mean, i. The time series we constructed above are cointegrated. There is a convenient test that lives in statsmodels. We should see a very low p-value, as we've artificially created two series that are as cointegrated as physically possible. Correlation and cointegration, while theoretically similar, are not the same.

First let's check the correlation of the series we just generated. But how would two series that are correlated but not cointegrated look? A simple example is two series that just diverge.

A simple example of cointegration without correlation is a normally distributed series and a square wave. The correlation is incredibly low, but the p-value shows perfect cointegration! Because two cointegrated time series such as X and Y above drift towards and apart from each other, there will be times when the spread is high and times when the spread is low.

We make a pairs trade by buying one security and selling another. We only make or lose money if securities X and Y move relative to each other. The best way to do this is to start with securities you suspect may be cointegrated and perform a statistical test.

Multiple comparisons bias is simply the fact that there is an increased chance to incorrectly generate a significant p-value when many tests are run, because we are running a lot of tests. If tests are run on random data, we should expect to see 5 p-values below 0. To avoid this, pick a small number of pairs you have reason to suspect might be cointegrated and test each individually.

This will result in less exposure to multiple comparisons bias. These stocks operate in a similar segment and could have cointegrated prices. We scan through a list of securities and test for cointegration between all pairs. It returns a cointegration test score matrix, a p-value matrix, and any pairs for which the p-value was less than 0. This method is prone to multiple comparison bias and in practice the securities should be subject to a second verification step. This is known as a confounding variable and it is important to check for market involvement in any relationship you find.

The ratio does look like it moved around a stable mean. It is more helpful to normalize our signal by treating it as a z-score. Z score is defined as:. In practice this is usually done to try to give some scale to the data, but this assumes an underlying distribution.

However, much financial data is not normally distributed, and we must be very careful not to simply assume normality, or any specific distribution when generating statistics.

The true distribution of ratios could be very fat-tailed and prone to extreme values messing up our model and resulting in large losses. Here we are trying to create a signal that tells us if the ratio is a buy or a sell at the next instant in time, i. By design we should also have a high likelihood of being in a trade when this happens so the impact could be quite high.

The problem in detecting this is that if the relationship re-establishes quickly the performance won't suffer. But if we include a time period in which the relationship doesn't return quickly, as Vladimir did, the results are noticeable. I added a few lines to close any positions that are open when the statistical tests break down. There are probably better ways of handling the exit logic, but this simple change shows the benefit of having it there.

The algorithm doesn't do as well during the original test period but the performance improves over the extended period. I also made minor change on lines 20 and 21 to use sid function to set x and y assets rather than symbol. The rest of the algorithm is unchanged.

This paper offers a systematic comparison of copula methods and cointegration methods when applied to US goldmine stocks. Also, the paper contrasts the pair selection criteria based on the ADF statistic, Kendall's tau , Spearman's rho and distance metric I'm not the author.

I put in lot of resources time and money to get copulas working on Q. But you can use this for a start:. I put a lot of time and money in zipline-live and the algos's I developed and I still share One day karma will come and pay graciously.

Here is the Gaussian Copula class. You can use it to trade a pair or a basket. Let me know if you have any questions:.

The closer the value is to 0. The closer the value is to 0 implies greater levels of mean-reversion. The backtest results below incorporate two of these tests: ADF-test p-value, computed over a day e. Here is how the ADF-test p-value parameters are defined: The 5 parameters are: Boolean, True if you want the algo to use this test 'lookback': Integer, value of how many lookback periods of the timeseries to be used in running the computation 'function': Integer or Float, the maximum returned by 'function' to determine if a trade can be triggered Support for Intraday Frequency Let me know if you run into issues with this, as I haven't done as much testing with it as I have with just daily freq You can configure this algo to be run on intraday minutely data as well.

There was an error loading this backtest. Backtest from to with initial capital. Returns 1 Month 3 Month 6 Month 12 Month. Alpha 1 Month 3 Month 6 Month 12 Month.

Beta 1 Month 3 Month 6 Month 12 Month. Sharpe 1 Month 3 Month 6 Month 12 Month. Sortino 1 Month 3 Month 6 Month 12 Month. Volatility 1 Month 3 Month 6 Month 12 Month. We have migrated this algorithm to work with a new version of the Quantopian API. The code is different than the original version, but the investment rationale of the algorithm has not changed. We've put everything you need to know here on one page. Rationale for this is that since the algo is trading off of mean reversion that a hedge ratio that excludes N days of recency, e.

Trade exits are handled above. There was a runtime error. Sorry for the inconvenience. Try using the built-in debugger to analyze your code. If you would like help, send us an email.

Same algo just start 9 month earlier. Hey Justin, Thanks for the share. Thanks for sharing info. Any ideas why this would be occurring? Adam, You probably run algo in daily mode and it only work in minute mode. Myself or someone on our team here at Q can try to develop a template for this and share it.

Pair trading using Copula methods instead of cointegration is the new rage. I had a go at it and results are looking very good. Anything you can share Aqua, to play with? But you can use this for a start: Thanks, I was more looking for Q code to play with Peter, I will try to put up something and post it without disclosing my secret sauce: Hi Peter, Here is the Gaussian Copula class.

Let me know if you have any questions: T[0] if len self. Attached notebook on usage. Notebook previews are currently unavailable. Deleted Account shared this notebook. So how are the baskets identified?

## Assemble a list of potentially related pairs

Statistical Arbitrage – Trading a cointegrated pair Trading a cointegrated pair is straight forward, we know the mean and variance of the spread, we know that those values are constant. The entry point for a stat arb is to simply look for a large deviation away from the mean. A basic strategy is: If spread(t) >= Mean Spread + 2*Standard. Cointegration is a useful econometric tool for identifying assets which share a common equilibrium. Cointegrated pairs trading is a trading strategy which attempts to take a profit when. E cient Pair Selection for Pair-Trading Strategies Advanced Financial Data Analysis - Patrick McSharry Also, from the implementation of a pair-trading strategy perspective, having a tis said to be cointegrated (of order 1) if: X tand Y.