Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Bug with stop orders

I suppose a sell stop order with a higher stop price will be easier to be triggered and filled.
But in my case the opposite situation happened.

def rebalance(sids,data,factor,type=1):  
    for sid in sids:  
        #order = get_order(order_target_percent(sid, sids[sid]*factor,style=LimitOrder(data.current(sid,'price'))))  
        #order = get_order(order_target_percent(sid, sids[sid]*factor))  
        if type == 1:  
            order_target_percent(sid, sids[sid]*factor)  
        elif type == 2:  
            order_target_percent(sid,sids[sid]*factor,style=StopLimitOrder(data.current(sid,'price')*0.995,data.current(sid,'price')*1.003))  
        elif type == 3:  
            order_target_percent(sid,sids[sid]*factor,style=StopOrder(data.current(sid,'price')*1.005))  
        elif type == 4:  
            order_target_percent(sid,sids[sid]*factor,style=StopOrder(data.current(sid,'price')*1.1))  

orders with type == 4 should be easier to fill. But in my backtest type== 4 gave 6 order not filled while type == 3 only gave 1 order not filled?
Type==4 Type == 3 Do I understand the stop order correct?

3 responses

Hi Eric -

I think getting the full code will be helpful for the debugging. Can you share a backtest?

Some thoughts:

  • Keep in mind that "triggering" and "filling" are different, and we have to keep them separate when we're trying to figure out what's happening. The "trigger" is when the stop order is converted into a market order - depending on when that happens, you will get different fills. A fill of a market order depends on the slippage model you're using, and the default Quantopian slippage model is dependent on the minute-to-minute volume. So a trigger in one minute can get a very different fill from a trigger in another minute, depending on the volume variation.
  • The size of the order will also affect the fills you get. It's not clear what the order sizes are in the two examples you've got.
  • The direction of the order, buy or sell, also matters. It's not clear what the direction of the orders you've got there.

Hope that helps.

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.

@Dan Sorry for the late reply. I did found out one logic mistake causing my fill problems.

But here's my new question. What's the right way to place a stop order for short sell stocks? In my experience of shorting VXX,

order_target_percent(sid,sids[sid]*factor,style=StopLimitOrder(data.current(sid,'price')*0.997,data.current(sid,'price')*1.003))  

will fill 98% of the time. But on specific days a short sell will be difficult to fill for some reasons. An simple backtest is included, a stop price with 0.5%*current_price is a big gap .

Hi Eric,

The traditional way of placing a stop order for a short position is by setting the stop price to be below the current market price. The rationale behind this is that you want to enter a short position if the security is showing downward momentum. I attached a simple example that I refactored from your code.

Additionally, you want to keep in mind that using order_target_percent with a large initial capital amount will place a large order. Depending on the slippage model, a large order might be split into several, smaller orders and some of them could fail to fill by the end of the day. This does not necessarily mean that the stop price was not reached. I would suggest you checking out the Slippage and Commissions lesson from the Getting Started tutorial for more details on this.

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.