From 147cc4f0b6691fe2d10e55b1491f606c4e5ca834 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 13 Aug 2023 11:09:26 +0200 Subject: [PATCH] Initial version of stop "after_fill" --- freqtrade/freqtradebot.py | 7 +++++++ freqtrade/strategy/interface.py | 12 +++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index beca1f09c..f5a6c38b2 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -1894,6 +1894,13 @@ class FreqtradeBot(LoggingMixin): )) except DependencyException: logger.warning('Unable to calculate liquidation price') + if self.strategy.use_custom_stoploss: + current_rate = self.exchange.get_rate( + trade.pair, side='exit', is_short=trade.is_short, refresh=True) + profit = trade.calc_profit_ratio(current_rate) + self.strategy.ft_stoploss_adjust(current_rate, trade, + datetime.now(timezone.utc), profit, 0, + after_fill=True) # Updating wallets when order is closed self.wallets.update() Trade.commit() diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index eca3e3ede..feb561743 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -373,7 +373,7 @@ class IStrategy(ABC, HyperStrategyMixin): return True def custom_stoploss(self, pair: str, trade: Trade, current_time: datetime, current_rate: float, - current_profit: float, **kwargs) -> float: + current_profit: float, after_fill: bool, **kwargs) -> float: """ Custom stoploss logic, returning the new distance relative to current_rate (as ratio). e.g. returning -0.05 would create a stoploss 5% below current_rate. @@ -389,6 +389,7 @@ class IStrategy(ABC, HyperStrategyMixin): :param current_time: datetime object, containing the current datetime :param current_rate: Rate, calculated based on pricing settings in exit_pricing. :param current_profit: Current profit (as ratio), calculated based on current_rate. + :param after_fill: True if the stoploss is called after the order was filled. :param **kwargs: Ensure to keep this here so updates to this won't break your strategy. :return float: New stoploss value, relative to the current_rate """ @@ -1160,7 +1161,7 @@ class IStrategy(ABC, HyperStrategyMixin): def ft_stoploss_adjust(self, current_rate: float, trade: Trade, current_time: datetime, current_profit: float, force_stoploss: float, low: Optional[float] = None, - high: Optional[float] = None) -> None: + high: Optional[float] = None, after_fill: bool = False) -> None: """ Adjust stop-loss dynamically if configured to do so. :param current_profit: current profit as ratio @@ -1186,11 +1187,12 @@ class IStrategy(ABC, HyperStrategyMixin): )(pair=trade.pair, trade=trade, current_time=current_time, current_rate=(bound or current_rate), - current_profit=bound_profit) + current_profit=bound_profit, + after_fill=after_fill) # Sanity check - error cases will return None if stop_loss_value: - # logger.info(f"{trade.pair} {stop_loss_value=} {bound_profit=}") - trade.adjust_stop_loss(bound or current_rate, stop_loss_value) + trade.adjust_stop_loss(bound or current_rate, stop_loss_value, + allow_refresh=after_fill) else: logger.warning("CustomStoploss function did not return valid stoploss")