Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Incomplete pair trading algo: intercept, adfuller, ARMA

I cloned Fawce's stat arb algorithm and tried to improve it some. This is still work in progress as you can see but I thought I post the code and see if other people might want to help out getting it to work.

What did I change from the original?

  • added intercept to the estimation (sm.add_constant)
  • adfuller test whether the residuals are stationary (implies cointegration) (commented out)
  • ARMA model fit that might be able to predict which direction the spread is moving (commented out)

The reason the last two are commented out is that they seem to take too long to compute and the backtest time-out kicks in to cancel the backtest.

The results are not very promising so far, high volatility but I'm sure this can be leveraged.

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.

4 responses

I should add: instead of continuously buying/selling if the zscore is above/below a certain point, I only enter end exit once.

@Thomas - very cool. Do you have a sense for how long you would need to do the adfuller and ARMA calculations? Our choice of a timeout was pure guesswork, and the purpose is to catch coding errors that lead to infinite loops and other stability threatening conditions. If you're actually doing real calculations, we are more than happy to have you work over the Quantopian cloud :).

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.

I don't know Python, so I am challenged to skim through the posted code to understand the algorithm. I'm interested in understanding the workings of the algorithm, so perhaps someone would be willing to provide a sketch of the approach being applied here. Having poked around the Quantopian site a bit, my general sense is that it would benefit from some visualization tools (e.g. basic flowcharts). Even if the algorithms were written in MATLAB (a language familiar to me), I would be more inclined to engage with the site if I could quickly understand the flow of a given algorithm. In my experience, even if code is well-written and has been thoroughly commented, it is tedious to unravel an algorithm from the code.

That's an interesting idea, Grant. I looked around at some open source workflow generators. I'll take a closer look and see where it takes me.

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.