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
- Don’t use trailing stoploss at all (not recommended)
- 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.
- 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.