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

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 self.so = [] to clear the stop order but it doesn't work. I have also tried to cancel the order by indicating self.cancel(self.so). But the below error message is shown. Hope you are able to guide me on this. tks.

    d3a456fa-9295-4d67-8136-4bf8ae77f4ba-image.png

    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:
                return
    
                if not self.position:     # not in the market      
                  
                    self.so = []
    
                    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.buy(transmit=False)                        
                        self.so = [self.sell(exectype=bt.Order.Stop, price=self.b_stop_price,
                                             parent=self.order,transmit=False)] 
                        self.po = [self.sell(exectype=bt.Order.Limit, price=self.b_profit_price,
                                             parent=self.order,transmit=True)]                     
                        
                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(self.so) > 0:
                            # Update only if greater than
                            self.b_stop_price = max(b_stop_price, close - atrdist)
                            self.so[0].created.price = self.b_stop_price
                  
                        else:
                            pass
                              
                    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 self.so. Something like:

    for s in self.so:
        self.cancel(s)
    


  • @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
 

});