From 070a1990e8e85dc1e43d962dd21d4d668c7aaf2c Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 14 Aug 2023 16:46:25 +0200 Subject: [PATCH] Improve handling of None values from custom_stoploss --- freqtrade/optimize/backtesting.py | 6 +++--- freqtrade/strategy/interface.py | 17 +++++++++-------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index 6a3e1949c..21075a1df 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -578,10 +578,10 @@ class Backtesting: """ Rate is within candle, therefore filled""" return row[LOW_IDX] <= rate <= row[HIGH_IDX] - def _call_adjust_stop(self, current_date: datetime, trade: Trade, current_rate: float): + def _call_adjust_stop(self, current_date: datetime, trade: LocalTrade, current_rate: float): profit = trade.calc_profit_ratio(current_rate) - self.strategy.ft_stoploss_adjust(current_rate, trade, current_date, profit, 0, - after_fill=True) + self.strategy.ft_stoploss_adjust(current_rate, trade, # type: ignore + current_date, profit, 0, after_fill=True) def _try_close_open_order( self, order: Optional[Order], trade: LocalTrade, current_date: datetime, diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index 5010f3db7..43d2a0baf 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -1188,15 +1188,16 @@ class IStrategy(ABC, HyperStrategyMixin): bound = (low if trade.is_short else high) bound_profit = current_profit if not bound else trade.calc_profit_ratio(bound) if self.use_custom_stoploss and dir_correct: - stop_loss_value = strategy_safe_wrapper(self.custom_stoploss, default_retval=None, - supress_error=True - )(pair=trade.pair, trade=trade, - current_time=current_time, - current_rate=(bound or current_rate), - current_profit=bound_profit, - after_fill=after_fill) + stop_loss_value_custom = strategy_safe_wrapper( + self.custom_stoploss, default_retval=None, supress_error=True + )(pair=trade.pair, trade=trade, + current_time=current_time, + current_rate=(bound or current_rate), + current_profit=bound_profit, + after_fill=after_fill) # Sanity check - error cases will return None - if stop_loss_value: + if stop_loss_value_custom: + stop_loss_value = stop_loss_value_custom trade.adjust_stop_loss(bound or current_rate, stop_loss_value, allow_refresh=after_fill) else: