For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

ATR Trailing Stop Query

  • Re: Updating Stop Loss based on current profit

    @run-out firstly, I want to thank you for sharing the code on updating the stop loss price. I have problem writing codes for ATR trailing stop until I saw your post. I have modified the codes to my needs and the ATR trailing stop works as expected. However, when I introduce another exit condition (self.b_crossdown) and the exit condition is met, the stop order is still outstanding. Hence when the price meets the stop order price, a position will be opened. How can I cancel the stop order? I have tried putting = [] to clear the stop order but it doesn't work. I have also tried to cancel the order by indicating self.cancel( But the below error message is shown. Hope you are able to guide me on this. tks.


    My code:

        def next(self):
            ''' Simply log the closing price of the series from the reference '''
            # self.log('Close, %.2f' % self.dataclose[0])
            # Check if an order is pending ... if yes, we cannot send a 2nd one
            if self.order:
                if not self.position:     # not in the market      
           = []
                    if self.buy_sig:
                        #self.log('BUY CREATE, %.2f' % self.dataclose[0])
                        self.buyprice = self.dataclose
                        self.b_stop_price = self.buyprice - (self.atr[0] * self.p.loss_atr_mult)
                        self.b_profit_price = self.buyprice + (self.atr[0] * self.p.profit_atr_mult)  
                        self.order =                        
               = [self.sell(exectype=bt.Order.Stop, price=self.b_stop_price,
                        self.po = [self.sell(exectype=bt.Order.Limit, price=self.b_profit_price,
                else:                     # in the market   
                    if self.position.size > 0:  
                        close = self.dataclose
                        b_stop_price = self.b_stop_price
                        atrdist = self.atr[0] * self.p.loss_atr_mult
                        if len( > 0:
                            # Update only if greater than
                            self.b_stop_price = max(b_stop_price, close - atrdist)
                  [0].created.price = self.b_stop_price
                    if self.position.size > 0 and self.b_crossdown:
                        #self.log('EXIT BUY CREATE, %.2f' % self.dataclose[0])
                        self.order = self.close()                

  • Try iterating through the list Something like:

    for s in

  • @run-out tks for the solution. The stop order can be cancelled now.

    I'm still trying to figure out if I understand everything correctly. Let's say if I don't specify the manual bracket orders inside a list, the stop loss and take profit orders will be cancelled automatically once the position is closed, right?

  • Excecution of one of the stop loss or the take profit will cancel the other.

  • @run-out Noted. tks.

Log in to reply