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/

    Possible bug in `sell/buy_bracket`

    General Discussion
    2
    15
    1845
    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.
    • ?
      A Former User last edited by A Former User

      I noticed that the trailamount is not being used for the stop bracket order, and I think it should be.

      modified   backtrader/strategy.py
      @@ -1113,8 +1113,7 @@ class Strategy(with_metaclass(MetaStrategy, StrategyBase)):
       
               kargs = dict(size=size,
                            data=data, price=price, plimit=plimit, exectype=exectype,
      -                     valid=valid, tradeid=tradeid,
      -                     trailamount=trailamount, trailpercent=trailpercent)
      +                     valid=valid, tradeid=tradeid)
               kargs.update(oargs)
               kargs.update(kwargs)
               kargs['transmit'] = limitexec is None and stopexec is None
      @@ -1123,7 +1122,9 @@ class Strategy(with_metaclass(MetaStrategy, StrategyBase)):
               if stopexec is not None:
                   # low side / stop
                   kargs = dict(data=data, price=stopprice, exectype=stopexec,
      -                         valid=valid, tradeid=tradeid)
      +                         valid=valid, tradeid=tradeid,
      +                         trailpercent=trailpercent,
      +                         trailamount=trailamount)
                   kargs.update(stopargs)
                   kargs.update(kwargs)
                   kargs['parent'] = o
      @@ -1185,8 +1186,7 @@ class Strategy(with_metaclass(MetaStrategy, StrategyBase)):
       
               kargs = dict(size=size,
                            data=data, price=price, plimit=plimit, exectype=exectype,
      -                     valid=valid, tradeid=tradeid,
      -                     trailamount=trailamount, trailpercent=trailpercent)
      +                     valid=valid, tradeid=tradeid)
               kargs.update(oargs)
               kargs.update(kwargs)
               kargs['transmit'] = limitexec is None and stopexec is None
      @@ -1195,7 +1195,9 @@ class Strategy(with_metaclass(MetaStrategy, StrategyBase)):
               if stopexec is not None:
                   # high side / stop
                   kargs = dict(data=data, price=stopprice, exectype=stopexec,
      -                         valid=valid, tradeid=tradeid)
      +                         valid=valid, tradeid=tradeid,
      +                         trailamount=trailamount,
      +                         trailpercent=trailpercent)
                   kargs.update(stopargs)
                   kargs.update(kwargs)
                   kargs['parent'] = o
      
      

      Before this change, the stop looks like this in back tests (blue is the stop):

      0_1519382320951_5818dbee-9948-4fc7-9cac-5a46105181f0-image.png

      After, it looks more correct (in that it's actually trailing):

      0_1519382598608_ecc19b0d-dba3-44cc-bc77-e7055dfebffb-image.png

      S 1 Reply Last reply Reply Quote 1
      • S
        sunnyalgotrader last edited by

        I have observed the same. trailamount is being ignored. Please advise.

        ? 1 Reply Last reply Reply Quote 0
        • ?
          A Former User @sunnyalgotrader last edited by

          @sunnyalgotrader I just patched it locally as per my diff in the original post.

          S 1 Reply Last reply Reply Quote 1
          • S
            sunnyalgotrader @Guest last edited by

            @cheez

            Hi, could you please guide how you are able to show moving StopTrail orders with blue circles in chart above ? That's very useful. Please share an example.

            Thanks

            ? 1 Reply Last reply Reply Quote 0
            • ?
              A Former User @sunnyalgotrader last edited by

              @sunnyalgotrader

              import backtrader as bt
              
              class BuySellStop(bt.observers.BuySell):
                  lines = ('buy','sell','stop')
              
                  plotlines = dict(
                      buy=dict(marker='^', markersize=8.0,fillstyle="full"),
                      sell=dict(marker='v', markersize=8.0,fillstyle="full"),
                      stop=dict(marker='o', markersize=8.0,color='blue',
                                fillstyle='full',ls='')
                  )
              
                  def next(self):
                      super(BuySellStop,self).next()
                      owner:bt.StrategyBase = self._owner
                      getStop = getattr(owner,'getStop',None)
                      if getStop is not None:
                          stop = getStop()
                          if stop is not None:
                              self.lines.stop[0] = stop
              
              S 1 Reply Last reply Reply Quote 2
              • S
                sunnyalgotrader @Guest last edited by

                @cheez

                Could you please share the whole file and also confirm what version you are using ? I see a bit different code. Thanks

                ? 1 Reply Last reply Reply Quote 0
                • ?
                  A Former User @sunnyalgotrader last edited by

                  @sunnyalgotrader thisi s from the develop branch on Git. I can't seem to attach th efile here, sorry!

                  S 1 Reply Last reply Reply Quote 1
                  • S
                    sunnyalgotrader @Guest last edited by

                    @cheez

                    Thanks, downloaded development branch and your patch works.

                    1 Reply Last reply Reply Quote 0
                    • S
                      sunnyalgotrader @Guest last edited by

                      @cheez

                      I tried this. Observer gets added correctly and also shows legend on the left top side of the chart. But actual Stop Trail blue dots are not shown on the chart.

                      FYI, I am using StopTrail orders as part of bracket orders.

                      Could you please advise, what I am missing ?

                      ? 1 Reply Last reply Reply Quote 0
                      • ?
                        A Former User @sunnyalgotrader last edited by

                        @sunnyalgotrader try this. I can't test it though because my data feed is down for some reason.

                        import backtrader as bt
                        
                        class BuySellStop(bt.observers.BuySell):
                            lines = ('buy','sell','stop')
                        
                            plotlines = dict(
                                buy=dict(marker='^', markersize=8.0,fillstyle="full"),
                                sell=dict(marker='v', markersize=8.0,fillstyle="full"),
                                stop=dict(marker='o', markersize=8.0,color='blue',
                                          fillstyle='full',ls='')
                            )
                        
                            def next(self):
                                super(BuySellStop,self).next()
                                owner:bt.Strategy = self._owner
                                for o in owner._orderspending:
                                    order:bt.Order = o
                                    if order.exectype in [bt.Order.Stop,bt.Order.StopLimit,
                                                          bt.Order.StopTrail,bt.Order.StopTrailLimit]:
                                        self.lines.stop[0] = order.created.price
                        
                        S 1 Reply Last reply Reply Quote 1
                        • S
                          sunnyalgotrader @Guest last edited by sunnyalgotrader

                          @cheez

                          Thanks @Cheez. This one worked. It shows up, where trailing stop is placed, and when the stop order is executed.

                          There is one small problem though as compared to BuySell observer. This one shows the Buy and Sell Triangle on the candle rather than below it. Blue dot shows appropriately though.

                          I tried few params but couldn't get it below the candle. Please advise.

                          Out of curosity...Not sure, if this is possible, but to understand backtrader features, is it possible to do following:

                          1. Show different colors of dots when stop order is placed vs it is executed.
                          2. If possible, to show trailing stop dots moving up or down with every candle.
                          ? 1 Reply Last reply Reply Quote 0
                          • ?
                            A Former User @sunnyalgotrader last edited by

                            @sunnyalgotrader said in Possible bug in `sell/buy_bracket`:

                            Thanks @Cheez. This one worked. It shows up, where trailing stop is placed, and when the stop order is executed.
                            There is one small problem though as compared to BuySell observer. This one shows the Buy and Sell Triangle on the candle rather than below it. Blue dot shows appropriately though.

                            That's standard behaviour. The triangle is placed at the entry price.

                            I tried few params but couldn't get it below the candle. Please advise.
                            Out of curosity...Not sure, if this is possible, but to understand backtrader features, is it possible to do following:

                            Show different colors of dots when stop order is placed vs it is executed.

                            I'm pretty sure this is possible, you just use a different line.

                            If possible, to show trailing stop dots moving up or down with every candle.

                            Not sure what you mean by this. The stop dots do move on my charts.

                            S 1 Reply Last reply Reply Quote 0
                            • S
                              sunnyalgotrader @Guest last edited by

                              @cheez said in Possible bug in `sell/buy_bracket`:

                              I'm pretty sure this is possible, you just use a different line.

                              If possible, to show trailing stop dots moving up or down with every candle.

                              Not sure what you mean by this. The stop dots do move on my charts.

                              This is what I meant... if you place a buy order with SL order, and SL doesn't get executed for 10 bars. Is your chart showing 10 bars with blues dots below each bar ? Or you have only 2 blue dots on your chat,1 when SL is placed and 1 when SL is executed. ?

                              ? 2 Replies Last reply Reply Quote 0
                              • ?
                                A Former User @sunnyalgotrader last edited by

                                @sunnyalgotrader I see 10 bars.

                                1 Reply Last reply Reply Quote 0
                                • ?
                                  A Former User @sunnyalgotrader last edited by

                                  @sunnyalgotrader Hmm... I must be crazy because now I see only entry bars.

                                  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(); }); }