Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    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

    General Code/Help
    2
    3
    33
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Adham Suliman
      Adham Suliman last edited by

      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
      hghhgghdf dfdf 1 Reply Last reply Reply Quote 0
      • hghhgghdf dfdf
        hghhgghdf dfdf @Adham Suliman last edited by

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

        Adham Suliman 1 Reply Last reply Reply Quote 1
        • Adham Suliman
          Adham Suliman @hghhgghdf dfdf last edited by

          Thank You!

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
          $(document).ready(function () { app.coldLoad(); }); }