Backtrader Community

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

    Bracket order with a Trailing Stop

    Indicators/Strategies/Analyzers
    4
    11
    3567
    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.
    • S
      Søren Pallesen last edited by Søren Pallesen

      Any input to how I smartest set a bracket order plus a trailing stop such that the 1) stop and the 2) limit in the bracket have a OCO (aka One Cancel Others) relationship with 3) the trailing stop?

      if not self.position and self.myindicator > self.params.p:
          self.buy_bracket(limitprice=self.data.close[0]*self.params.lPerc, price=None, stopprice=self.data.close[0]*(1-self.params.sPerc))
          self.tstop = self.sell(exectype=bt.Order.StopTrail, trailpercent=self.params.tPerc)
      

      In other word tstop gets canceled immediately in the same next() loop if the stop or the limit order in the bracket is executed, cancelled or expires - AND visa versa.

      ? 1 Reply Last reply Reply Quote 0
      • ?
        A Former User @Søren Pallesen last edited by

        @søren-pallesen You can use trailing stops in buy_bracket with the latest release, but note that there might be a bug. See my posts regarding the bug.

        You would write something like this:

        self.buy_bracket(...,stopexec=bt.Order.StopTrail,trailamount=trailamount,...)
        
        S 1 Reply Last reply Reply Quote 0
        • S
          Søren Pallesen @Guest last edited by

          @cheez can i have both a stop and a trailing stop in the same bracket order?

          ? 1 Reply Last reply Reply Quote 0
          • ?
            A Former User @Søren Pallesen last edited by

            @søren-pallesen Ah, no I don't think that's possible.

            1 Reply Last reply Reply Quote 0
            • S
              Søren Pallesen last edited by

              Anyone? ...maybe @backtrader? thx in advance ...

              1 Reply Last reply Reply Quote 0
              • A
                ab_trader last edited by

                What is the reason to have 2 stops at the same time? Only one will work anyway.

                • If my answer helped, hit reputation up arrow at lower right corner of the post.
                • Python Debugging With Pdb
                • New to python and bt - check this out
                S 1 Reply Last reply Reply Quote 0
                • S
                  Søren Pallesen @ab_trader last edited by Søren Pallesen

                  @ab_trader There are three live orders on the position: One is a stop (to limit loss) and the other is a trailing stop (to take profit) and the last is a limit (to take profit).

                  They all trigger on different conditions.

                  1 Reply Last reply Reply Quote 0
                  • A
                    ab_trader last edited by

                    From two stops only one will work, the one which is closer to the current price. IMO it is useless to have two stops for single position.

                    • If my answer helped, hit reputation up arrow at lower right corner of the post.
                    • Python Debugging With Pdb
                    • New to python and bt - check this out
                    1 Reply Last reply Reply Quote 0
                    • S
                      Søren Pallesen last edited by

                      A trailing stop and a (normal) stop works completly different. For example on position bought at $10 a stop at say $9 would protect against a loss. A trailing stop of 20% would ensure that you sold if the price drops 20% at any time of the life span on the position. So if the price increased to $20 and drops to $16 it would be sold with a profit of $6.

                      A normal stop is static protecting against a loss and a trailingstop dynamic and typically primarily used to lock in a profit while keeping the upside of further increases.

                      1 Reply Last reply Reply Quote 0
                      • S
                        Søren Pallesen last edited by

                        This worked fo me (it seems - still have more testing to do) in case anyone is interested ... :-)

                                        mainside = self.buy(transmit=False)
                                        lowside  = self.sell(price=self.data.close[0]*(1-self.params.sPerc), size=mainside.size, exectype=bt.Order.Stop, transmit=False, parent=mainside)
                                        highside = self.sell(price=self.data.close[0]*self.params.lPerc, size=mainside.size, exectype=bt.Order.Limit, transmit=False, parent=mainside)
                                        trailside = self.sell(size=mainside.size, exectype=bt.Order.StopTrail, trailpercent=self.params.tPerc, transmit=True, parent=mainside)
                        

                        Incendently anyone know the differences in doing this with 'OCO' vs the 'Parent' property?

                        B 1 Reply Last reply Reply Quote 0
                        • B
                          backtrader administrators @Søren Pallesen last edited by

                          OCO orders are all active at the same time.

                          Bracket orders (the ones you submit with parent) have a different lifecycle:

                          • parent is 1st alive. children are inactive
                          • If the parent is cancelled the children are simply discarded (they weren't active)
                          • If the parent is executed, the children are activated
                          • Both children behave now like OCO orders
                          1 Reply Last reply Reply Quote 0
                          • 1 / 1
                          • First post
                            Last post
                          Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors