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

Hi,
I am creating an algo which rebalances weekly on a Monday. When i enter orders either short or long i use the following syntax
order_target_percent(stock, weight,style=StopOrder(stop_price = current_price+stop_price)). I check the order is correctly entered with a good value for the stop and it looks correct. I have a function i call daily to log the current prices of the portfolio positions and I can see that the stops haven't worked. For instance if I enter a short position and the current price is 5 and I put a stop loss of 6 in, I can see the price on subsequent days exceed this threshold. So what is happening ? Does the order_target_percent stop only hold for the day i enter it and it's cancelled at the end of the session ? I was hoping the stop information would be passed with the order and be held for the lifetime of that position.

Many thanks for help.

e.

4 responses

If you enter a stop or limit or stoplimit when a position doesn't yet exist like at the same time as a market order to open the position, it will never come into existence so maybe that's what's happening. You can verify that in the debugger or print taking a look at get_open_orders() after the stop order. Wait a minute for the order to be created, keep partial fills in mind, and then enter the stop order on existing shares. Or wait until the original market order to open the position is no longer in open orders (fully filled). They have to be renewed each morning.

Hi Blue,

i do check that the order is created correctly, my log message for example is

35551266a32d49abb0c62e1e50e47366 for -32198 of SUNE ,cur 3.255, stop 3.98

which i comes from the following code

for sec_,order_ in open_orders.iteritems():  
    for oo_ in order_:  
        message = '\n{order} for {amount} of {stock} ,cur {price}, stop {stop}'  
        message = message.format(order=oo_.id, amount=oo_.amount, stock=sec_.symbol, price=data.current(sec_,'price'), stop=oo_.stop)  
        msg = msg + message

log.info(msg)           

It just seems empirically to cancel the stop loss at the end of the day, but can that be correct ?

Yes, all open orders are canceled at the end of day, again the stop orders have to be renewed each morning. Use a scheduled task. If you want to keep the previous stop prices you can use an end-of-day task to save the orders that will be automatically canceled and pull the stop prices from them at market open in recreating. In combination with other code, conditions, things can become pretty complex. (context.portfolio.positions[sec].amount - o.filled) * o.filled < 0 will tell you that an order is closing a position, can be surprisingly useful at times.

Thanks Blue,

I think this is a great platform but there does seem to be a weakness here around portfolio management. Surely this must be very similar for a lot of strategies and there is very little code available (that I could find), so one would have to build their own and as you said it's complex and quite a lot of code.