Cointegration based trading strategies

The pairs in self. If we put too many pairs in the list, the backtesting would be too time consuming. If the first pair contains stock A and stock B, and the second pair contains stock B and stock C, we would remove the second pair because the overlapped signal would disturb the balance of our portfolio.

This part is under the OnData step. We set self. During this period we fill the stock prices in lists, and assign each stock's price list to the symbol as a property. We would also remove the symbol from the symbol list if it has no data. This process is also under the OnData step. This step would generate pairs if it is the first trading period of this algorithm. If it's not, it will update the DataFrame and correlation coefficient of each pair in self. After that the pairs have a correlation coefficient higher than 0.

Then all the pairs in self.

Latest commit

This step will also limit the number of stocks in the final list, by default we set self. Once it reach 1-month amount, that means one trading period is passed and it would be set to 0. It would be too long to read if we paste all the code in trading period together. Thus we would separate the code into three part: updating pairs, opening pairs trading and closing pairs trading. But all those lines are under OnData step and are under the condition: if self.

A cointegrating stock trading strategy: application to listed tanker shipping companies

This means it's in the trading period. This step would update the stock prices in each pair. For each pair in self. Once a pairs trading is open, this pair would be add to the list, and it would be removed when the trading is closed. The property 'touch' is signal.

How to Get Best Site Performance

We long stock B and short stock A. For those pairs with -1 signal, if the error cross over negative threshold, we long Stock A and short stock B. When we opening a trade, we need to record the current model, current mean and standard deviation of the residual. This is necessary because if we enter a new trading period and the trade has not been closed yet, the cointegration model, mean and standard deviation of the pairs would be changed. We need to use the original thresholds to close the trades. This part controls pairs trading exit. It works similar to the opening part. It uses the recorded original model and thresholds to determine whether or not we should close the position.

Mean Reversion Trading Strategies

If the residual continue to deviate from the mean and goes too far, we would also close the position to stop loss. When we close a pairs trading, we also remove the pairs from self. We used minute resolution data to backtest the strategy from Jan to Dec To demonstrate the in sample training results, we randomly selected a training period that from to The following table demonstrates the top 10 selected pairs in the training period mentioned above. We can see that the pairs with the highest correlation coefficient doesn't not necessarily has the best ADF test value.


  • An Introduction to Cointegration for Pairs Trading - Hudson & Thames?
  • Pairs trade!
  • etoro forex us;
  • Strategy Library.
  • forex factory calendar csv!

We made the rank by ADF test value because it's more robust. The lower part plots by how many times standard deviation the residual deviate from its mean. There are 5 trading opportunities if we set the opening threshold to be 2. The following chart is the density plot of the residual error. From the shape we can see the error is approximately normal distributed. Out backtested beta is Theoretically, the higher resolution we use, the higher win rate is because on one hand the higher resolution would increase the number of datapoint in our training period, which would make it's harder to past the two-stage test; on the other hand the higher resolution data would let us capture minor profit more accurately.

However, there is a trade off between performance and backtesting time. The higher resolution will lead backtesting time to increase drastically. The number of stocks in the initialize step would also affect our performance. Theoretically, the more stock we have, we better pairs we are likely to pick. But too many stocks would also be time consuming.


  1. Pairs Trading Basics: Correlation, Cointegration And Strategy;
  2. simple winning forex strategy?
  3. Literature review!
  4. options trading ce.
  5. trading options with broker!
  6. It depends on the features of the price patterns in the specific industry. Traders looking to expand their repertoire of available trading strategies, or enhance their existing skill set, might want to consider a deeper dive into pairs trading. If a price divergence presented itself, such that the correlation temporarily broke down, traders utilizing a pairs approach might view this as an opportunity to deploy a spread.

    Pairs are most commonly used when trading equities and futures. For the latter group, some of the best-known pairs include oil vs. The foundation of pairs trades, as observed in these three examples, is that a strong positive or negative correlation has been established between the two over a long period of time. The ability to profit on a pairs position hinges on a belief that the pair will revert back to their historical average, after a temporary breakdown in correlation.

    In this regard, a trader deploying a pairs trade is banking on a widening or narrowing of the spread between the underlying prices of the two securities. At its core, a pairs trade therefore represents the expression of an opinion on the direction of the spread. For example, gold and silver share a well-known positive correlation that hovers historically right around 0. The gold-silver pair is so famous that it even has its own ratio named after it: the gold-silver ratio.

    Algorithmic Trading Strategies: Pair Trading & Mean Reversion Strategies

    This ratio is computed by simply taking the price per ounce of gold and dividing it by the price per ounce of silver, the result of which reports how many ounces of silver are required to purchase an ounce of gold. For reference, the gold-silver ratio has traded between roughly 40 and 90 over the last four decades, and the mean of the ratio is right around When the ratio is at the lower end of that range, pairs traders consider buying gold, and selling silver, in hopes that the ratio will rise.

    Alternatively, pairs traders sell gold and buy silver when playing a potential decline in the ratio. From this example, one can see that identifying and deploying an attractive pairs trade depends on finding suitable pairs and tracking when correlations break down. Traders seeking to take a deeper dive on pairs are recommended to review a new episode of Market Measures on the tastytrade financial network when scheduling allows.

    This particular episode explores new research conducted by tastytrade that highlights the importance of cointegration when it comes to identifying optimal pairs. As outlined on the show, correlation is a great metric for reporting the degree to which a certain pair moves together, but cointegration appears to be an even more robust method of analyzing potential pairs trades because this metric better describes the mean reverting behavior of a given pair. For added insight on these positions, traders may also want to review a previous installment of Best Practices which provides a step-by-step breakdown of the pairs trading methodology.

    Pairs Trading Analysis with Python - Introduction

    Sage Anderson is a pseudonym. The contributor has an extensive background in trading equity derivatives and managing volatility-based portfolios as a former prop trading firm employee. Cointegration, a concept that helped Clive W. Granger win the Nobel Prize in Economics in see Footnote 1 , is a cornerstone of pairs and multi-asset trading strategies. Indeed, the concept of cointegration is not immediately apparent from its name. Therefore, in this article, I will attempt to answer the following questions:.

    Hopefully, after reading this article, you will understand better why cointegration techniques, which were initially intended to avoid spurious regression results when using non-stationary regressors in macroeconomic time series analysis McDermott, , became an indispensable member of the statistical arbitrage arsenal. I will try my best to cut down the amount of hypnotizing math formulae as intuition is more important.

    Is it?