Use cancel_order_with_result when cancelling orders after timeout

This commit is contained in:
Matthias 2020-04-17 17:53:56 +02:00
parent 5e3e0e819f
commit 1069cb3616
2 changed files with 11 additions and 14 deletions

View File

@ -891,11 +891,8 @@ class FreqtradeBot:
if order['status'] != 'canceled': if order['status'] != 'canceled':
reason = "cancelled due to timeout" reason = "cancelled due to timeout"
try: try:
corder = self.exchange.cancel_order(trade.open_order_id, trade.pair) corder = self.exchange.cancel_order_with_result(trade.open_order_id, trade.pair,
# Some exchanges don't return a dict here. trade.amount)
if not isinstance(corder, dict):
corder = {}
logger.info('Buy order %s for %s.', reason, trade)
except InvalidOrderException: except InvalidOrderException:
corder = {} corder = {}
logger.exception( logger.exception(
@ -904,7 +901,8 @@ class FreqtradeBot:
# Order was cancelled already, so we can reuse the existing dict # Order was cancelled already, so we can reuse the existing dict
corder = order corder = order
reason = "cancelled on exchange" reason = "cancelled on exchange"
logger.info('Buy order %s for %s.', reason, trade)
logger.info('Buy order %s for %s.', reason, trade)
if safe_value_fallback(corder, order, 'remaining', 'remaining') == order['amount']: if safe_value_fallback(corder, order, 'remaining', 'remaining') == order['amount']:
logger.info('Buy order fully cancelled. Removing %s from database.', trade) logger.info('Buy order fully cancelled. Removing %s from database.', trade)
@ -921,7 +919,7 @@ class FreqtradeBot:
trade.amount = order['amount'] - safe_value_fallback(corder, order, trade.amount = order['amount'] - safe_value_fallback(corder, order,
'remaining', 'remaining') 'remaining', 'remaining')
trade.stake_amount = trade.amount * trade.open_rate trade.stake_amount = trade.amount * trade.open_rate
self.update_trade_state(trade, corder if 'fee' in corder else order, trade.amount) self.update_trade_state(trade, corder, trade.amount)
trade.open_order_id = None trade.open_order_id = None
logger.info('Partial buy order timeout for %s.', trade) logger.info('Partial buy order timeout for %s.', trade)

View File

@ -1948,7 +1948,7 @@ def test_check_handle_timedout_buy(default_conf, ticker, limit_buy_order_old, op
'freqtrade.exchange.Exchange', 'freqtrade.exchange.Exchange',
fetch_ticker=ticker, fetch_ticker=ticker,
get_order=MagicMock(return_value=limit_buy_order_old), get_order=MagicMock(return_value=limit_buy_order_old),
cancel_order=cancel_order_mock, cancel_order_with_result=cancel_order_mock,
get_fee=fee get_fee=fee
) )
freqtrade = FreqtradeBot(default_conf) freqtrade = FreqtradeBot(default_conf)
@ -2055,7 +2055,7 @@ def test_check_handle_cancelled_sell(default_conf, ticker, limit_sell_order_old,
'freqtrade.exchange.Exchange', 'freqtrade.exchange.Exchange',
fetch_ticker=ticker, fetch_ticker=ticker,
get_order=MagicMock(return_value=limit_sell_order_old), get_order=MagicMock(return_value=limit_sell_order_old),
cancel_order=cancel_order_mock cancel_order_with_result=cancel_order_mock
) )
freqtrade = FreqtradeBot(default_conf) freqtrade = FreqtradeBot(default_conf)
@ -2082,7 +2082,7 @@ def test_check_handle_timedout_partial(default_conf, ticker, limit_buy_order_old
'freqtrade.exchange.Exchange', 'freqtrade.exchange.Exchange',
fetch_ticker=ticker, fetch_ticker=ticker,
get_order=MagicMock(return_value=limit_buy_order_old_partial), get_order=MagicMock(return_value=limit_buy_order_old_partial),
cancel_order=cancel_order_mock cancel_order_with_result=cancel_order_mock
) )
freqtrade = FreqtradeBot(default_conf) freqtrade = FreqtradeBot(default_conf)
@ -2109,7 +2109,7 @@ def test_check_handle_timedout_partial_fee(default_conf, ticker, open_trade, cap
'freqtrade.exchange.Exchange', 'freqtrade.exchange.Exchange',
fetch_ticker=ticker, fetch_ticker=ticker,
get_order=MagicMock(return_value=limit_buy_order_old_partial), get_order=MagicMock(return_value=limit_buy_order_old_partial),
cancel_order=cancel_order_mock, cancel_order_with_result=cancel_order_mock,
get_trades_for_order=MagicMock(return_value=trades_for_order), get_trades_for_order=MagicMock(return_value=trades_for_order),
) )
freqtrade = FreqtradeBot(default_conf) freqtrade = FreqtradeBot(default_conf)
@ -2146,7 +2146,7 @@ def test_check_handle_timedout_partial_except(default_conf, ticker, open_trade,
'freqtrade.exchange.Exchange', 'freqtrade.exchange.Exchange',
fetch_ticker=ticker, fetch_ticker=ticker,
get_order=MagicMock(return_value=limit_buy_order_old_partial), get_order=MagicMock(return_value=limit_buy_order_old_partial),
cancel_order=cancel_order_mock, cancel_order_with_result=cancel_order_mock,
get_trades_for_order=MagicMock(return_value=trades_for_order), get_trades_for_order=MagicMock(return_value=trades_for_order),
) )
mocker.patch('freqtrade.freqtradebot.FreqtradeBot.get_real_amount', mocker.patch('freqtrade.freqtradebot.FreqtradeBot.get_real_amount',
@ -2208,7 +2208,7 @@ def test_handle_timedout_limit_buy(mocker, caplog, default_conf, limit_buy_order
patch_RPCManager(mocker) patch_RPCManager(mocker)
patch_exchange(mocker) patch_exchange(mocker)
cancel_order_mock = MagicMock(return_value=limit_buy_order) cancel_order_mock = MagicMock(return_value=limit_buy_order)
mocker.patch('freqtrade.exchange.Exchange.cancel_order', cancel_order_mock) mocker.patch('freqtrade.exchange.Exchange.cancel_order_with_result', cancel_order_mock)
freqtrade = FreqtradeBot(default_conf) freqtrade = FreqtradeBot(default_conf)
@ -2226,7 +2226,6 @@ def test_handle_timedout_limit_buy(mocker, caplog, default_conf, limit_buy_order
mocker.patch('freqtrade.exchange.Exchange.cancel_order', side_effect=InvalidOrderException) mocker.patch('freqtrade.exchange.Exchange.cancel_order', side_effect=InvalidOrderException)
assert not freqtrade.handle_timedout_limit_buy(trade, limit_buy_order) assert not freqtrade.handle_timedout_limit_buy(trade, limit_buy_order)
assert log_has_re(r"Could not cancel buy order", caplog)
@pytest.mark.parametrize('cancelorder', [ @pytest.mark.parametrize('cancelorder', [