Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Tips for improvement on my mean reversion pairing algo

The theory behind my algo is that if you have two competing companies in the same market sector, then their regression is a more accurate estimate of the future stock price. The algo trades when the stock prices of the two stocks become over or under valued relative to the average simple moving average of the two.

I also added in my commissions, and added controls so that it would not buy or sell on margin or make orders larger than 25% of my initial capital base.

Please let me know your thoughts on flaws and improvements.

Thanks,
BSM

6 responses

Better comparison using the two underlying stocks as the benchmark.

Thanks for adding that feature!

Nice algo.
I generalized the code a bit.

If you start it a year earlier, you get a more realistic view. Because the algorithm starts late, in accidentally benefits from a bit of market timing, missing out on the early 2008 drop. If you re-write using the history function, you should be able to avoid this, although you won't be able to run it in daily mode anymore.

Just for grins, I loaded this puppy up with TLT and SPY. Not ready for prime time yet, but it's worth further research.

So say I want to verify that the spreads are not diverging and wanted to use the history of the past 3 months in order to compare the spread between the two securities now and 3 months ago. If they do diverge I want to be able to change the securities being traded to others that the algorithm has identified as cointegrated over the past 3 months.

Any thoughts on the code for how to do this?

A quick note about the new set_benchmark feature - you can only initialize one benchmark per algo. If its not initialized, the default is SPY. If you have a list of set_benchmark's, it will take the last item in the list. For example, in Richard's backtest, the benchmark is MRK.

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.