Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Confused with regards to Limit Order Stop Price vs. Limit Price. Which should be set higher. I thought it was the Stop Price.

I'm confused about StopLimitOrder. I understand how it's supposed to work in practice, being a trader, but I'm struggling with the implementation. I want to trail an order upwards as it rises. I can do this effectively with a Stop Order, but that simply turns into a Market order and I want to avoid that by setting a limit, even though I understand there is no guarantee it will execute. Should the code below not accomplish this?

            def trail_stop(context,data,*limit):  
                if context.trail_counter < TRAIL_INTERVAL:  
                    context.trail_counter += 1  
                    return  
                else:  
                  context.trail_counter = 0  
                  for s in context.portfolio.positions:  
                   if s not in context.closed:  
                     cp = data.current(s,'price')  
                     cb = context.portfolio.positions[s].cost_basis  
                     amt = context.portfolio.positions[s].amount

                     if cp > (cb * TRAIL_STOP):  
                         if (cp * TRAIL_STOP) > context.trail[s]:  
                             context.trail[s] = cp  
                             if limit == True:  
                              tc(sl,[s,0,cp*.99,cp,True],["SL - Trail Price Moved => " + s.symbol + " => " + str(cp),'StopLimit Error'])  
                             else:  
                               tc(so,[s,0,cp,True],["SO - Trail Price Moved => " + s.symbol + " => " + str(cp),'StopOrder Error'])

            def sl(stock,amount,priceA,priceB,*cancel):  
                    co(stock,cancel)  
                    order_target(stock,amount,style=StopLimitOrder(priceA,priceB))

            def co(stock,*cancel):  
                if cancel == True:  
                 for s in get_open_orders(stock):  
                    cancel_order(s)  
                    print s

            # TRY CATCH WAPPER - ALLOWS FOR MESSAGES ON SUCCESS AND FAILURE  
            def tc(fn,prms,msg):  
                try:  
                    fn(*prms)  
                    print prms[0].symbol + ' => ' +  msg[0]  
                except:  
                    print prms[0].symbol + ' => ' + msg[1]