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/

    Trailing take profit

    General Code/Help
    4
    6
    2579
    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.
    • T
      tw00000 last edited by tw00000

      Hi all,

      I'm trying to understand how to implement a trailing take profit with backtrader.

      I see that the docs explain trailing stop loss (but not trailing take profit, which could just be verbiage and maybe they're the same): https://www.backtrader.com/docu/strategy.html?highlight=buy#backtrader.Strategy.buy ... also the community post here: https://community.backtrader.com/topic/163/trailing-stop-loss/11 shows examples like so: self.buy(size=1, exectype=bt.Order.StopTrail, trailpercent=0.02) # last price will be used as reference ... which makes sense to me for stop loss but I may just be missing something with regard to how to implement this for my take profit targets

      Here's a GIF that illustrates what I'd like to achieve: Trailing Take Profit Example

      In the gif, we have:

      • buy level
      • take profit level
      • trailing take profit price
      • max price with deviation

      Perhaps in backtrader we would do (using the image as a direct example):

      self.buy_bracket(limitprice=0.000692, # take profit level
                      price=0.00066, # buy level
                      stopprice=0.00055, # not shown in gif
      		limitexec=bt.Order.StopTrailLimit,
      		trailpercent=0.01, # follow max price deviation %
      		)
      

      There's also split take profit targets in the image: when price hits Take Profit 1, it market sells for 50% of the position, and then begins trailing, at which point it hits Take Profit 2 and sells 50% of the position after 1% deviation, which is probably a topic for a different post...

      Thanks greatly for any reply!

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

        There are no trailing take profit, you still use trailing stop loss. Look for chandelier exit, seems to me this is what you want to achieve.

        • 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
        T 1 Reply Last reply Reply Quote 1
        • B
          backtrader administrators last edited by

          The documentation doesn't explain stop loss, it explains Stop orders. Whether the result of the order execution results in taking a loss or taking a profit is a different story.

          StopTrail will do what you expect:

          • It will be triggered at a price level x (which is the close price unless you specify other)

          • It will set an order to be executed at a price distance, which will move only if the price keeps moving in your favour.

          StopTrailLimit does the same but using a Limit order for your take profit order (which may result in no execution even if the price is touched, of course)

          T 1 Reply Last reply Reply Quote 1
          • T
            tw00000 @ab_trader last edited by tw00000

            @ab_trader said in Trailing take profit:

            There are no trailing take profit, you still use trailing stop loss. Look for chandelier exit, seems to me this is what you want to achieve.

            The chandelier exit is actually more complicated than what I'm looking for. In fact, it's better than what I was asking for, but probably too complicated to implement and maintain. Maybe when I'm older :) This implementation looks like it wouldn't be too complicated to get moving though, and would be a nice accompaniment to a reversals type strategy I imagine

            Does the code I shared above seem correct to implement trade shown in the gif in backtrader?

            1 Reply Last reply Reply Quote 0
            • T
              tw00000 @backtrader last edited by

              @backtrader said in Trailing take profit:

              The documentation doesn't explain stop loss, it explains Stop orders. Whether the result of the order execution results in taking a loss or taking a profit is a different story.

              StopTrail will do what you expect:

              • It will be triggered at a price level x (which is the close price unless you specify other)

              • It will set an order to be executed at a price distance, which will move only if the price keeps moving in your favour.

              StopTrailLimit does the same but using a Limit order for your take profit order (which may result in no execution even if the price is touched, of course)

              Ah I see, generally the word Stop is used in reference to Stop Loss, so I assumed the documentation was only talking about stop losses.

              Thanks for the explanation on StopTrail vs. StopTrailLimit, it's very helpful. Out of curiosity, what is the benefit of StopTrailLimit? It does seem in times of volatility the StopTrailLimit would be very disadvantageous - are there any advantages?

              So, does this code seem like the correct implementation of the trade shown in the gif (assuming price followed that movement, of course):

              self.buy_bracket(limitprice=0.000692, # take profit level
                              price=0.00066, # buy level
                              stopprice=0.00055, # not shown in gif
              		limitexec=bt.Order.StopTrail,
              		trailpercent=0.01, # follow max price deviation %
              		)
              
              N 1 Reply Last reply Reply Quote 0
              • N
                nooby_mcnoob @tw00000 last edited by

                @tw00000 said in Trailing take profit:

                It does seem in times of volatility the StopTrailLimit would be very disadvantageous - are there any advantages?

                It can be problematic. I've had markets go right over my limit. The only thing I do is have a OCO stop market order further than the trail in case markets go crazy and I'm not there to do something.

                1 Reply Last reply Reply Quote 0
                • 1 / 1
                • First post
                  Last post
                Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors