diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 26e918f18..48e3ec209 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -1496,17 +1496,18 @@ class FreqtradeBot(LoggingMixin): # Order might be filled above in odd timing issues. if order.get('status') in ('canceled', 'cancelled'): trade.exit_reason = None + trade.open_order_id = None else: trade.exit_reason = exit_reason_prev cancelled = True else: reason = constants.CANCEL_REASON['CANCELLED_ON_EXCHANGE'] trade.exit_reason = None + trade.open_order_id = None self.update_trade_state(trade, trade.open_order_id, order) logger.info(f'{trade.exit_side.capitalize()} order {reason} for {trade}.') - trade.open_order_id = None trade.close_rate = None trade.close_rate_requested = None diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index ab5dd4af5..7bded0f82 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -2955,6 +2955,9 @@ def test_manage_open_orders_exit_usercustom( assert rpc_mock.call_count == 2 assert freqtrade.strategy.check_exit_timeout.call_count == 1 assert freqtrade.strategy.check_entry_timeout.call_count == 0 + trade = Trade.session.scalars(select(Trade)).first() + # cancelling didn't succeed - order-id remains open. + assert trade.open_order_id is not None # 2nd canceled trade - Fail execute exit caplog.clear() @@ -3465,6 +3468,7 @@ def test_handle_cancel_exit_cancel_exception(mocker, default_conf_usdt) -> None: # TODO: should not be magicmock trade = MagicMock() + trade.open_order_id = '125' reason = CANCEL_REASON['TIMEOUT'] order = {'remaining': 1, 'id': '125', @@ -3472,6 +3476,10 @@ def test_handle_cancel_exit_cancel_exception(mocker, default_conf_usdt) -> None: 'status': "open"} assert not freqtrade.handle_cancel_exit(trade, order, reason) + # mocker.patch(f'{EXMS}.cancel_order_with_result', return_value=order) + # assert not freqtrade.handle_cancel_exit(trade, order, reason) + # assert trade.open_order_id == '125' + @pytest.mark.parametrize("is_short, open_rate, amt", [ (False, 2.0, 30.0),