Backtest traps – trailing stoploss

In this post, I will explain why you need to be careful when you are using trailing stoploss in your freqtrade strategy. Let’s start with these simple trailing stoploss logic (please use either one but never both)

trailing_stop = True
trailing_stop_positive = 0.01
trailing_stop_positive_offset = 0.05
trailing_only_offset_is_reached = True
def custom_stoploss(...):
   if current_profit >= 0.05:
      return 0.01

The logic is very simple, when the trade has at least profit of 5%, the stoploss is moved to 1% less of current profit. In dry/live run, what gonna happen is that every throttle secs (which is 5 seconds by default, unless you set different value), the bot fetch current rate from the exchange and calculate current_profit, then supply the value to either function above.

Where the backtest trap lie is in the fact that for backtest, because there is no data of intra-candle movement, the trailing stoploss check gonna be simplified. Let me give example to explain what is written in the official docs

Let’s say on candle A, an enter_long signal is triggered for BTC/USDT coin. The bot would open the trade at open of candle B. Now this is where the assumption begin.

On candle B, if you are using the simple trailing setting (the first snippet above), what the bot do is check whether (open rate + 5% profit) <= high value of candle B. If yes, then the stoploss is moved to (open rate + (5-1)% profit). If the new stoploss is within low and high values of candle B, it means the trade gonna be marked as exit with trailing stoploss reason at 4% profit.

If you are using custom_stoploss function, then on candle B, the bot calculate the profit based of high value of candle B. Let’s say the profit based of the high value is 7%. Then the bot will move the stoploss to 1% less from high value, then the bot check if the new stoploss value fall within low and high values of candle B. If yes, then the trade gonna be marked as exit with trailing stoploss reason at 6% profit.

Assuming trailing stoploss didn’t get triggered at candle B, then for candle C, D, and so on, both method of trailing stoploss using the same logic as custom_stoploss above, which means the bot calculate the profit at high value, then move the stoploss acordingly if it triggers trailing.

As you might have notice, since the bot mostly gonna check profit based of high value, then in case of a long green candle, you might exit near the high, especially if you set a very tight trailing. So the next question is

How to avoid the traps?
There are three ways I use to avoid it, which are

  1. Don’t use trailing stoploss at all (not recommended)
  2. Convert your trailing stoploss into roi, only for backtest. So in above example, set the roi into 4% (we are assuming the bad case scenario that the price will always go down once your trailing is triggred) and disable trailing stoploss.
  3. Use timeframe-detail to help minimize the effect. But since the lowest workable timeframe available now is 1 minute timeframe, that means it is still prone to such trap if you have a very long 1 minute green candle. But in general, the difference shouldn’t be as big as if you aren’t using it.

13 Comments

  1. What about use the confirm trade exit fuction to exit only at the close of the candle where was a traling stoploss exit signal? This way, it would be more realistic in the backtest.

    • It will still be different. Imagine you have a “weird” candle that fly high to trigger your trailing, but then it went down to the same rate as open rate. In backtest, such candle won’t trigger your trailing. But in dry/live, the bot will exit that trade at the end of that candle. So you will still have different result.

  2. Thanks for the response, if you have any other content about this, especially during backtest. I have a huge profit in the backtest that I am just trying to reproduce using the timeframe detail, but it seem that the logic of it is quite confusing. I used to set a trailing stop loss.

    • Yes, the issue with backtest is it needs to be simplified. So it’s kinda hard if you are trying to do more advanced trailing logic. Since I prefer my strategies to have quite accurate backtest, I prefer simple trailing.

  3. Is it possible to check the profit only of the currrent candle? I mean, if there is alredy a profit, but I want to use the trailing only for the current candle. This way, maybe, it will look like the backtest.

  4. But Freqtrade only provides 1m data minimal, sadly.
    Would you please tell me that whether a simple stoploss value will be affected by this trap?

Leave a Reply

Your email address will not be published. Required fields are marked *