Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Trading Strategy: Statistical Arbitrage and Mean Reversion

There are several ways to test for mean reversion properties in securities and here is a way that looks at the spread between a cointegrated pair of securities. The following algorithm uses an (already known) cointegrated pair of security (Royal Dutch Share stocks) and goes long or short Royal Dutch Share A according to the current spread's deviation from the mean.

If the Spread is
- Greater than or less than Average_spread +- 2*Standard_deviation the algo will go long or short accordingly.

The algorithm does limit leverage a bit. It currently uses Royal Dutch Share stocks (A and B) to test, so perhaps playing around with different stocks would provide better returns.

I feel this algorithm has a lot of potential so will definitely try and take new suggestions for improvement.

-Seong

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

5 responses

And what if the pair is no longer cointegrated and the spread widens while you're in a position?

Good question.
Not quite sure, I'm thinking we'd need a test along with the exit_position method in order to check for cointegration. That will be next to include

If I'm correct your algorithm only trades in 1 security. Therefore there's quite a bit of market exposure involved in the strategy. Also, as the spread widens it just buys/sells more and more of the stock until the spread has converged. Quite some risks involved there imo.

Thanks for pointing that out, I'm thinking now that the first step might be to use a universe of stocks and test for cointegration between two pairs but am not sure if that's a viable option. Would you have any ideas on how I could approach that?

Well, when you have n stocks and you want to test them all for cointegration you'll need to perform n(n-1)/2 cointegration tests. So it gets pretty computationally heavy fast. One way would be to perform cointegration tests with an easy acceptance for cointegration on some longer timeframes, and use the stock combinations from these. You could also pre-group them yourself by sector which will reduce the number of cointegration tests. I also came across an algorithm here that grouped co-fluctuating stocks but I'm not sure if that's usefull since co-fluctuation != co-integration.