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

Order.StopTrail turns positive shares into short position



  • Hello,

    I am trying to implement a trailing stop order, and I'm using self.close with bt.Order.StopTrail. The first sell is expected, but I don't understand as to why self.close() would short the position thereafter?

            if self.position.size == 0:
                if self.myind.ema_s[0] > self.myind.ema_l[0] and self.myind.ema_s[-1] < self.myind.ema_l[-1] and  self.myind.ema_s[-2] < self.myind.ema_l[-2]:                    # amount to invest
                        amount_to_invest = self.params.order_percentage * self.broker.cash
                        self.size = math.floor(amount_to_invest/self.dataclose)
                        # previous close less than the previous close
                        # BUY, BUY, BUY!!! (with all possible default parameters)
                        self.log('BUY CREATE, %.2f' % self.dataclose[0])
                        self.order = self.buy(exectype = bt.Order.Limit,
                        price = self.dataclose[0], 
                        valid = datetime.now() + timedelta(hours = 1),
                        size = self.size)       
    
           elif self.position.size > 0:          
                       self.close(exectype=bt.Order.StopTrail, trailamount = 8)                
    

    My log is the following:

    2020-08-11T02:05:30, BUY CREATE, 1586.35
    2020-08-11T06:00:35, Buy Executed at 1586.35
    2020-08-11, position size:5
    2020-08-11T13:55:13, Sell Executed at 1592.1
    2020-08-11, position size:-35
    2020-08-11T13:55:13, Sell Executed at 1592.1
    2020-08-11, position size:-35
    2020-08-11T13:55:13, Sell Executed at 1592.1
    2020-08-11, position size:-35
    2020-08-11T13:55:13, Sell Executed at 1592.1
    2020-08-11, position size:-35
    2020-08-11T13:55:13, Sell Executed at 1592.1
    2020-08-11, position size:-35
    2020-08-11T13:55:13, Sell Executed at 1592.1
    2020-08-11, position size:-35
    2020-08-11T13:55:13, Sell Executed at 1592.1
    2020-08-11, position size:-35
    2020-08-11T13:55:13, Sell Executed at 1592.1
    2020-08-11, position size:-35
    2020-08-11T14:08:01, Sell Executed at 1591.6
    2020-08-11, position size:-45
    2020-08-11T14:08:01, Sell Executed at 1591.6
    2020-08-11, position size:-45
    2020-08-11T14:16:13, Sell Executed at 1590.35
    2020-08-11, position size:-85
    2020-08-11T14:16:13, Sell Executed at 1590.35
    2020-08-11, position size:-85
    2020-08-11T14:16:13, Sell Executed at 1590.35
    2020-08-11, position size:-85
    2020-08-11T14:16:13, Sell Executed at 1589.75
    2020-08-11, position size:-85
    2020-08-11T14:16:13, Sell Executed at 1589.75
    2020-08-11, position size:-85
    2020-08-11T14:16:13, Sell Executed at 1589.75
    2020-08-11, position size:-85
    2020-08-11T14:16:13, Sell Executed at 1589.75
    2020-08-11, position size:-85
    2020-08-11T14:16:13, Sell Executed at 1589.75
    2020-08-11, position size:-85


  • @Adham-Suliman said in Order.StopTrail turns positive shares into short position:

       elif self.position.size > 0:          
                   self.close(exectype=bt.Order.StopTrail, trailamount = 8) 
    

    You're creating a stop order for every next call where your position is open. Assign this order to a variable and only create a new one if it doesn't exist.



  • Thank You!


Log in to reply
 

});