Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Gold Long Short

Since the new Pipeline masking feature came out it has been possible to do some more complicated computations, including linear regression. This is a quick and dirty algorithm where I long stocks that have a low magnitude beta to gold prices, and short stocks that have a high magnitude beta to gold.

Enjoy!

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.

7 responses

Very interesting idea.

Here's a tearsheet of the algo, drawdown is high in the 09 era (not surprising) and seems to improve thereafterwards.

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.

Great!

I am curious as to how I can swap out regressing against gold for SPY for example. Or are you limited here to Quandl data?

take a look at the just released correlation and regression factors where you can regress against SPY, or any dataset of the proper form.

https://www.quantopian.com/posts/new-pipeline-features-slicing-and-correlation-slash-regression-methods

Really useful code! I'm Confused why the loop is in that custom factor? It obviously works, but I understood that pipeline performs the calc for each stock each day - so why is this separately looping through stocks?

I wrote this algo before the new regression with datasets functionality was added, so i had to hack someparts.

Here is the post about the new regression factor methods https://www.quantopian.com/posts/new-pipeline-features-slicing-and-correlation-slash-regression-methods

Post back here when you update it, I'd like to see the much cleaner code.

Unfortunately, the regression factor methods are too limited for anything I'm trying to do. E.g only one variable and cant alter estimation methods.

The statsmodels .fit functions still results in a misaligned matrix error without the loop. But the loop seems to make it extremely slow. So I'm currently testing if I can make it usable by performing regressions outside a pipeline and taking top/bottom rankings.
Cheers.

Fair point on the estimation methods. You may want to consider looking at the implementation in zipline and try your own implementation replacing scipy.stats.linregress with sklearn.linear_models...