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



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



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


  • administrators

    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)



  • @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?



  • @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 %
    		)
    


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