Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Stop-Limit sell orders not triggering on Limit

In the attached code, I execute a buy order and immediately set a stop-limit sell order for the same security (AMZN). However, that order doesn't even get placed until the next day, and even when placed, it doesn't trigger when the limit price is far surpassed.

Day 1: 2014-4-15
We can see from the log output that the buy order was filled within a minute, yet the sell order wasn't even placed (otherwise we would have seen the Bum sell order error in the log. Is this because one can't set a sell order unless those securities are in his portfolio?

Day 2: 2014-4-16
We bought more AMZN stock, giving us 321 shares in our portfolio. This time we see that the sell order is in place with limit of $312. The previous day's stocks were bough at ~$311/share, so i'd expect this limit to have been reached (limit_reached == True), yet the order shows the limit has not reached, and thus the order did not execute. Is this because Day 2's purchase was for ~$317, which is already above the limit price (not sure why this would matter, but if sell-order wasn't placed until after this 2nd buy-order, then maybe the limit-logic is affected by the most recent buy-price?). Note that the portfolio shows the average price paid per share, and that is still below our $312 limit. I'd expect the current price of $317, again, to trigger the sale.

Day 3: 2014-4-17
We can't buy any more shares (broke), but we see that there are now 2 open sale orders to sell all AMZN. Note that even though our code says to sell "all" stock (not a particular qty), both sale orders have quantities (amount=320 and 321). Are sales getting thwarted because of having 2 open sale orders which sum to more shares than are in our portfolio?

The behind-the scenes logic of how limit orders and orders in general are set is a mystery. Can anyone explain why i'm seeing the described/attached behavior?

1 response

The confusion here is in the timing of the orders. These lines,

context.oids.append(order(tkr, +buy_qty))  
    #immediately set a stop-limit sell order  
    context.oids.append(  
        order_target(tkr, 0, style=StopLimitOrder(  
            limit_price=312,  
            stop_price = 218)  

place two simultaneous orders. It places a market order to buy Amazon and a separate StopLimit order to close out the Amazon position. These two orders are independent, placed in the same bar of handle_data.

Instead, you want to place a single StopLimit order on your Amazon position. Once the stop price is reached, the next minute bar your order will convert to a limit order and will buy as long as the market price is below the limit price. This is the single line you want to purchase 100 shares of Amazon with between $218-312:

    context.oids.append(  
        order_target(tkr, 100, style=StopLimitOrder(  
            limit_price=312,  
            stop_price = 218)  

As you continue to code your strategy, I'd recommend to use the "order_target" family of functions instead of the basic "order" function. This family of functions is robust, seeking to it's target positions and keeping your leverage under control.

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.