Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Questions on Implementation of My Strategy

Hey everyone,

Just wanted to see if someone could answer some questions I have about the code and respective backtest.

First, the idea of my strategy is a "breakout". It needs a lot of fine tuning, but I was just trying to get the framework in there. Here's what I envisioned and tried to implement. Please tell me if there's something missing or done incorrectly.

I have UWTI in there now as the ETF being traded. Every minute, from 3 minutes into trading day until 6 hours into trading day, it checks if the price and volume have increased by a set value compared to the past few minutes. If so, it will use all of the portfolio funds to purchase UWTI shares. Then, it will also check each minute to see if an order is open. If one is and the price is either > 0.1% or < 0.5% of the purchase price, sell all shares. I didn't want to hold overnight, so the last thing was to sell with 10 minutes left in the day no matter what the current returns.

So I coded that as best I know how and tried to back test. This is where a lot of my questions come in or stem from. One, why isn't it selling at the end of the first day? That function must not be operating properly. Two, why does it buy a small percentage of new shares each time it sells the bulk of shares? Three, I can understand that it may sell at larger than 0.1% gain or 0.5% loss because it's on a discrete minute timeframe. But on some backtests (maybe not this one), it was selling like 3-4% gains or losses. It happened a few times and seems unlikely that a stock jumps that much in a minute.

EDIT: Realized in that code that I have sell if < 1.005 * purchase price. Changed to < 0.995 * purchase price and got the exact same transactions on the backtest.

1 response

Hi Zach,

Your algorithm isn't working correctly because of your get_open_orders checks on lines 90 and 110. get_open_orders will return a non-empty list given a specific equity if and only if there is an open order for that equity at that time (i.e., an order which was placed but has not yet been filled). However, most of the time there will not be an open order out for that equity, since orders will get filled pretty quickly.

For your close function, if you want to close your position in the equity if you have a position in it, you can check if context.security_list in context.portfolio.positions. For your sell function, if you want to check if you have a position in the equity, you can do the same thing. In both functions you should also cancel all open orders in your specified equity right before placing the order_target_percent for 0.

Please let me know if this resolves it for you.

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.