From 861c5771384fd4fe9c24197438dc51760cac8634 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 29 Mar 2023 07:05:39 +0200 Subject: [PATCH] Support partially filled stop orders closes #8374 --- freqtrade/freqtradebot.py | 6 ++++-- freqtrade/persistence/trade_model.py | 21 ++++++++++++--------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 9746ac3d8..1cc1a0bae 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -853,7 +853,8 @@ class FreqtradeBot(LoggingMixin): logger.info(f"Canceling stoploss on exchange for {trade}") co = self.exchange.cancel_stoploss_order_with_result( trade.stoploss_order_id, trade.pair, trade.amount) - trade.update_order(co) + self.update_trade_state(trade, trade.stoploss_order_id, co, stoploss_order=True) + # Reset stoploss order id. trade.stoploss_order_id = None except InvalidOrderException: @@ -1171,7 +1172,8 @@ class FreqtradeBot(LoggingMixin): logger.warning('Unable to fetch stoploss order: %s', exception) if stoploss_order: - trade.update_order(stoploss_order) + self.update_trade_state(trade, trade.stoploss_order_id, stoploss_order, + stoploss_order=True) # We check if stoploss order is fulfilled if stoploss_order and stoploss_order['status'] in ('closed', 'triggered'): diff --git a/freqtrade/persistence/trade_model.py b/freqtrade/persistence/trade_model.py index 17117d436..4b59cbdbe 100644 --- a/freqtrade/persistence/trade_model.py +++ b/freqtrade/persistence/trade_model.py @@ -692,21 +692,24 @@ class LocalTrade(): else: logger.warning( f'Got different open_order_id {self.open_order_id} != {order.order_id}') + + elif order.ft_order_side == 'stoploss' and order.status not in ('open', ): + self.stoploss_order_id = None + self.close_rate_requested = self.stop_loss + self.exit_reason = ExitType.STOPLOSS_ON_EXCHANGE.value + if self.is_open: + logger.info(f'{order.order_type.upper()} is hit for {self}.') + else: + raise ValueError(f'Unknown order type: {order.order_type}') + + if order.ft_order_side != self.entry_side: amount_tr = amount_to_contract_precision(self.amount, self.amount_precision, self.precision_mode, self.contract_size) if isclose(order.safe_amount_after_fee, amount_tr, abs_tol=MATH_CLOSE_PREC): self.close(order.safe_price) else: self.recalc_trade_from_orders() - elif order.ft_order_side == 'stoploss' and order.status not in ('canceled', 'open'): - self.stoploss_order_id = None - self.close_rate_requested = self.stop_loss - self.exit_reason = ExitType.STOPLOSS_ON_EXCHANGE.value - if self.is_open: - logger.info(f'{order.order_type.upper()} is hit for {self}.') - self.close(order.safe_price) - else: - raise ValueError(f'Unknown order type: {order.order_type}') + Trade.commit() def close(self, rate: float, *, show_msg: bool = True) -> None: