Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Momentum strategy using volatility and volume

Hi everyone,

I'm trying to build a strategy that ranks stocks according to their bullish momentum and buys every month the top 10 of them and takes a short position in SPY, as large as all long positions together.

Unfortunately, as you can see in the backtest result, my implementation doesn't remain at a constant total of 11 positions during the test period, but keeps some from the previous month. I've been looking for my mistake in the rebalance function for a long time, but I just can't find it.

Does anyone see what I'm doing wrong?

8 responses

Unfilled sell orders are part of it. Also note cancel_order(order) wasn't working because order was a security object instead of an order object or id (and it did not complain, that's a bug I would say).

This adds track_orders(). Other changes as I tinkered around don't solve it, hopefully offers some ideas.

Thanks for your assistance! That really helps me a lot to understand what's going on there.

Also note cancel_order(order) wasn't working because order was a security object instead of an order object or id (and it did not complain, that's a bug I would say).

Joys of Python!

Indeed Gary, that's a bug. We'll have to get that fixed!

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 made a copy/paste oversight in your code Raphael, if not get_open_orders(stock) was supposed to be if not get_open_orders(context.spy), added because I had noticed there were two different simultaneous orders for SPY at times. Forgot to change stock to context.spy. Happened to notice my error when I went back to your code to use it for something else unrelated, thanks.

Thanks for letting me know.

I now figured out that the unfilled sell orders were due to delisting of these stocks. I fixed that by selling them before they were delisted, just for the sake of a more representative backtest.

I guess that delist code works well then, now it is on my radar where I had glossed over it before.

Also tools, recommend for seeing more clearly, took your backtest from -14% to +40%, progress.
track values       track_orders()       pvr()

Wow, these are some very useful tools and your modifications are highly inspiring, thank you!