From 1ba2df79c6a770958bddb3bf1e1fee6bd8c41f0f Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 7 May 2020 06:51:02 +0200 Subject: [PATCH] Ause isclose for comparison, assign filled to variable add some comments --- freqtrade/freqtradebot.py | 8 ++++++-- tests/test_freqtradebot.py | 3 ++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 9db7e4467..57eb7d1a8 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -898,6 +898,7 @@ class FreqtradeBot: Buy timeout - cancel order :return: True if order was fully cancelled """ + # Cancelled orders may have the status of 'canceled' or 'closed' if order['status'] not in ('canceled', 'closed'): reason = "cancelled due to timeout" corder = self.exchange.cancel_order_with_result(trade.open_order_id, trade.pair, @@ -909,7 +910,10 @@ class FreqtradeBot: logger.info('Buy order %s for %s.', reason, trade) - if safe_value_fallback(corder, order, 'filled', 'filled') == 0.0: + # Using filled to determine the filled amount + filled_amount = safe_value_fallback(corder, order, 'filled', 'filled') + + if isclose(filled_amount, 0.0, abs_tol=constants.MATH_CLOSE_PREC): logger.info('Buy order fully cancelled. Removing %s from database.', trade) # if trade is not partially completed, just delete the trade Trade.session.delete(trade) @@ -921,7 +925,7 @@ class FreqtradeBot: # cancel_order may not contain the full order dict, so we need to fallback # to the order dict aquired before cancelling. # we need to fall back to the values from order if corder does not contain these keys. - trade.amount = safe_value_fallback(corder, order, 'filled', 'filled') + trade.amount = filled_amount trade.stake_amount = trade.amount * trade.open_rate self.update_trade_state(trade, corder, trade.amount) diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index 7a30e9015..c502e264c 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -2313,7 +2313,7 @@ def test_handle_timedout_limit_buy(mocker, caplog, default_conf, limit_buy_order Trade.session = MagicMock() trade = MagicMock() trade.pair = 'LTC/ETH' - limit_buy_order['filled'] = 0 + limit_buy_order['filled'] = 0.0 limit_buy_order['status'] = 'open' assert freqtrade.handle_timedout_limit_buy(trade, limit_buy_order) assert cancel_order_mock.call_count == 1 @@ -2323,6 +2323,7 @@ def test_handle_timedout_limit_buy(mocker, caplog, default_conf, limit_buy_order assert not freqtrade.handle_timedout_limit_buy(trade, limit_buy_order) assert cancel_order_mock.call_count == 1 + limit_buy_order['filled'] = 2 mocker.patch('freqtrade.exchange.Exchange.cancel_order', side_effect=InvalidOrderException) assert not freqtrade.handle_timedout_limit_buy(trade, limit_buy_order)