From 7508f79b6c03c8f0049a5411a267cbfd942462ba Mon Sep 17 00:00:00 2001 From: Sam Germain Date: Sat, 26 Feb 2022 10:30:35 -0600 Subject: [PATCH 1/5] test_freqtradebot, is_short tests --- tests/test_freqtradebot.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index d6930bc24..843aa6551 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -2457,6 +2457,7 @@ def test_check_handle_timedout_buy_exception( ) freqtrade = FreqtradeBot(default_conf_usdt) + # open_trade.is_short = True Trade.query.session.add(open_trade) # check it does cancel buy orders over the time limit @@ -2473,7 +2474,6 @@ def test_check_handle_timedout_sell_usercustom( default_conf_usdt, ticker_usdt, limit_sell_order_old, mocker, is_short, open_trade_usdt, caplog ) -> None: - # TODO-lev: use is_short or remove it default_conf_usdt["unfilledtimeout"] = {"buy": 1440, "sell": 1440, "exit_timeout_count": 1} limit_sell_order_old['id'] = open_trade_usdt.open_order_id @@ -2651,6 +2651,7 @@ def test_check_handle_timedout_partial_fee( limit_buy_order_old_partial_canceled, mocker ) -> None: # TODO-lev: use is_short or remove it + # open_trade.is_short = is_short rpc_mock = patch_RPCManager(mocker) limit_buy_order_old_partial['id'] = open_trade.open_order_id limit_buy_order_old_partial_canceled['id'] = open_trade.open_order_id @@ -2695,7 +2696,7 @@ def test_check_handle_timedout_partial_except( limit_buy_order_old_partial, trades_for_order, limit_buy_order_old_partial_canceled, mocker ) -> None: - # TODO-lev: use is_short or remove it + open_trade.is_short = is_short rpc_mock = patch_RPCManager(mocker) limit_buy_order_old_partial_canceled['id'] = open_trade.open_order_id limit_buy_order_old_partial['id'] = open_trade.open_order_id @@ -2821,7 +2822,6 @@ def test_handle_cancel_enter(mocker, caplog, default_conf_usdt, limit_order, is_ indirect=['limit_buy_order_canceled_empty']) def test_handle_cancel_enter_exchanges(mocker, caplog, default_conf_usdt, is_short, limit_buy_order_canceled_empty) -> None: - # TODO-lev: use is_short or remove it patch_RPCManager(mocker) patch_exchange(mocker) cancel_order_mock = mocker.patch( @@ -2833,10 +2833,14 @@ def test_handle_cancel_enter_exchanges(mocker, caplog, default_conf_usdt, is_sho reason = CANCEL_REASON['TIMEOUT'] trade = MagicMock() trade.pair = 'LTC/ETH' - trade.enter_side = "buy" + trade.enter_side = "sell" if is_short else "buy" assert freqtrade.handle_cancel_enter(trade, limit_buy_order_canceled_empty, reason) assert cancel_order_mock.call_count == 0 - assert log_has_re(r'Buy order fully cancelled. Removing .* from database\.', caplog) + assert log_has_re( + f'{trade.enter_side.capitalize()} order fully cancelled. ' + r'Removing .* from database\.', + caplog + ) assert nofiy_mock.call_count == 1 From 5332c441b9a0f34abe5bda3bcfac4d74648eabfa Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 3 Mar 2022 07:00:10 +0100 Subject: [PATCH 2/5] Fix fail test - add more TODO's --- tests/test_freqtradebot.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index 843aa6551..71f47155a 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -2443,7 +2443,6 @@ def test_check_handle_timedout_buy_exception( default_conf_usdt, ticker_usdt, limit_buy_order_old, open_trade, is_short, fee, mocker ) -> None: - # TODO-lev: use is_short or remove it rpc_mock = patch_RPCManager(mocker) cancel_order_mock = MagicMock() patch_exchange(mocker) @@ -2457,7 +2456,7 @@ def test_check_handle_timedout_buy_exception( ) freqtrade = FreqtradeBot(default_conf_usdt) - # open_trade.is_short = True + open_trade.is_short = is_short Trade.query.session.add(open_trade) # check it does cancel buy orders over the time limit @@ -2476,7 +2475,8 @@ def test_check_handle_timedout_sell_usercustom( ) -> None: default_conf_usdt["unfilledtimeout"] = {"buy": 1440, "sell": 1440, "exit_timeout_count": 1} limit_sell_order_old['id'] = open_trade_usdt.open_order_id - + # TODO-lev: + # open_trade_usdt.is_short = is_short rpc_mock = patch_RPCManager(mocker) cancel_order_mock = MagicMock() patch_exchange(mocker) @@ -2547,6 +2547,7 @@ def test_check_handle_timedout_sell( default_conf_usdt, ticker_usdt, limit_sell_order_old, mocker, is_short, open_trade_usdt ) -> None: + # TODO-lev: use is_short or remove it rpc_mock = patch_RPCManager(mocker) cancel_order_mock = MagicMock() limit_sell_order_old['id'] = open_trade_usdt.open_order_id @@ -2582,6 +2583,7 @@ def test_check_handle_cancelled_sell( is_short, mocker, caplog ) -> None: """ Handle sell order cancelled on exchange""" + # TODO-lev: use is_short or remove it rpc_mock = patch_RPCManager(mocker) cancel_order_mock = MagicMock() limit_sell_order_old.update({"status": "canceled", 'filled': 0.0}) @@ -2614,6 +2616,7 @@ def test_check_handle_timedout_partial( default_conf_usdt, ticker_usdt, limit_buy_order_old_partial, is_short, leverage, open_trade, mocker ) -> None: + # TODO-lev: use is_short or remove it rpc_mock = patch_RPCManager(mocker) open_trade.leverage = leverage limit_buy_order_old_partial['id'] = open_trade.open_order_id @@ -2700,6 +2703,8 @@ def test_check_handle_timedout_partial_except( rpc_mock = patch_RPCManager(mocker) limit_buy_order_old_partial_canceled['id'] = open_trade.open_order_id limit_buy_order_old_partial['id'] = open_trade.open_order_id + if is_short: + limit_buy_order_old_partial['side'] = 'sell' cancel_order_mock = MagicMock(return_value=limit_buy_order_old_partial_canceled) patch_exchange(mocker) mocker.patch.multiple( From bc37f67e7632ee23503fe5a4887518c1fab4086b Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 6 Mar 2022 14:18:25 +0100 Subject: [PATCH 3/5] Add one more test --- tests/test_freqtradebot.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index 71f47155a..ef48bda4e 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -2475,8 +2475,10 @@ def test_check_handle_timedout_sell_usercustom( ) -> None: default_conf_usdt["unfilledtimeout"] = {"buy": 1440, "sell": 1440, "exit_timeout_count": 1} limit_sell_order_old['id'] = open_trade_usdt.open_order_id - # TODO-lev: - # open_trade_usdt.is_short = is_short + if is_short: + limit_sell_order_old['side'] = 'buy' + open_trade_usdt.is_short = is_short + rpc_mock = patch_RPCManager(mocker) cancel_order_mock = MagicMock() patch_exchange(mocker) @@ -2500,28 +2502,34 @@ def test_check_handle_timedout_sell_usercustom( assert cancel_order_mock.call_count == 0 freqtrade.strategy.check_sell_timeout = MagicMock(return_value=False) + freqtrade.strategy.check_buy_timeout = MagicMock(return_value=False) # Return false - No impact freqtrade.check_handle_timedout() assert cancel_order_mock.call_count == 0 assert rpc_mock.call_count == 0 assert open_trade_usdt.is_open is False - assert freqtrade.strategy.check_sell_timeout.call_count == 1 + assert freqtrade.strategy.check_sell_timeout.call_count == (0 if is_short else 1) + assert freqtrade.strategy.check_buy_timeout.call_count == (1 if is_short else 0) freqtrade.strategy.check_sell_timeout = MagicMock(side_effect=KeyError) + freqtrade.strategy.check_buy_timeout = MagicMock(side_effect=KeyError) # Return Error - No impact freqtrade.check_handle_timedout() assert cancel_order_mock.call_count == 0 assert rpc_mock.call_count == 0 assert open_trade_usdt.is_open is False - assert freqtrade.strategy.check_sell_timeout.call_count == 1 + assert freqtrade.strategy.check_sell_timeout.call_count == (0 if is_short else 1) + assert freqtrade.strategy.check_buy_timeout.call_count == (1 if is_short else 0) # Return True - sells! freqtrade.strategy.check_sell_timeout = MagicMock(return_value=True) + freqtrade.strategy.check_buy_timeout = MagicMock(return_value=True) freqtrade.check_handle_timedout() assert cancel_order_mock.call_count == 1 assert rpc_mock.call_count == 1 assert open_trade_usdt.is_open is True - assert freqtrade.strategy.check_sell_timeout.call_count == 1 + assert freqtrade.strategy.check_sell_timeout.call_count == (0 if is_short else 1) + assert freqtrade.strategy.check_buy_timeout.call_count == (1 if is_short else 0) # 2nd canceled trade - Fail execute sell caplog.clear() From 81d4a61353fd94cc697d3287d45eb9f77f7c50ad Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 6 Mar 2022 14:33:04 +0100 Subject: [PATCH 4/5] Update more trades --- tests/test_freqtradebot.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index ef48bda4e..313365d29 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -23,8 +23,8 @@ from freqtrade.persistence.models import PairLock from freqtrade.strategy.interface import SellCheckTuple from freqtrade.worker import Worker from tests.conftest import (create_mock_trades, get_patched_freqtradebot, get_patched_worker, - log_has, log_has_re, patch_edge, patch_exchange, patch_get_signal, - patch_wallet, patch_whitelist) + log_has, log_has_re, open_trade_usdt, patch_edge, patch_exchange, + patch_get_signal, patch_wallet, patch_whitelist) from tests.conftest_trades import (MOCK_TRADE_COUNT, enter_side, exit_side, mock_order_1, mock_order_2, mock_order_2_sell, mock_order_3, mock_order_3_sell, mock_order_4, mock_order_5_stoploss, mock_order_6_sell) @@ -2555,10 +2555,10 @@ def test_check_handle_timedout_sell( default_conf_usdt, ticker_usdt, limit_sell_order_old, mocker, is_short, open_trade_usdt ) -> None: - # TODO-lev: use is_short or remove it rpc_mock = patch_RPCManager(mocker) cancel_order_mock = MagicMock() limit_sell_order_old['id'] = open_trade_usdt.open_order_id + limit_sell_order_old['side'] = 'buy' if is_short else 'sell' patch_exchange(mocker) mocker.patch.multiple( 'freqtrade.exchange.Exchange', @@ -2572,10 +2572,12 @@ def test_check_handle_timedout_sell( open_trade_usdt.close_date = arrow.utcnow().shift(minutes=-601).datetime open_trade_usdt.close_profit_abs = 0.001 open_trade_usdt.is_open = False + open_trade_usdt.is_short = is_short Trade.query.session.add(open_trade_usdt) freqtrade.strategy.check_sell_timeout = MagicMock(return_value=False) + freqtrade.strategy.check_buy_timeout = MagicMock(return_value=False) # check it does cancel sell orders over the time limit freqtrade.check_handle_timedout() assert cancel_order_mock.call_count == 1 @@ -2583,6 +2585,7 @@ def test_check_handle_timedout_sell( assert open_trade_usdt.is_open is True # Custom user sell-timeout is never called assert freqtrade.strategy.check_sell_timeout.call_count == 0 + assert freqtrade.strategy.check_buy_timeout.call_count == 0 @pytest.mark.parametrize("is_short", [False, True]) @@ -2591,10 +2594,10 @@ def test_check_handle_cancelled_sell( is_short, mocker, caplog ) -> None: """ Handle sell order cancelled on exchange""" - # TODO-lev: use is_short or remove it rpc_mock = patch_RPCManager(mocker) cancel_order_mock = MagicMock() limit_sell_order_old.update({"status": "canceled", 'filled': 0.0}) + limit_sell_order_old['side'] = 'buy' if is_short else 'sell' patch_exchange(mocker) mocker.patch.multiple( 'freqtrade.exchange.Exchange', @@ -2607,6 +2610,7 @@ def test_check_handle_cancelled_sell( open_trade_usdt.open_date = arrow.utcnow().shift(hours=-5).datetime open_trade_usdt.close_date = arrow.utcnow().shift(minutes=-601).datetime open_trade_usdt.is_open = False + open_trade_usdt.is_short = is_short Trade.query.session.add(open_trade_usdt) @@ -2615,7 +2619,8 @@ def test_check_handle_cancelled_sell( assert cancel_order_mock.call_count == 0 assert rpc_mock.call_count == 1 assert open_trade_usdt.is_open is True - assert log_has_re("Sell order cancelled on exchange for Trade.*", caplog) + exit_name = 'Buy' if is_short else 'Sell' + assert log_has_re(f"{exit_name} order cancelled on exchange for Trade.*", caplog) @pytest.mark.parametrize("is_short", [False, True]) @@ -2624,10 +2629,11 @@ def test_check_handle_timedout_partial( default_conf_usdt, ticker_usdt, limit_buy_order_old_partial, is_short, leverage, open_trade, mocker ) -> None: - # TODO-lev: use is_short or remove it rpc_mock = patch_RPCManager(mocker) + open_trade.is_short = is_short open_trade.leverage = leverage limit_buy_order_old_partial['id'] = open_trade.open_order_id + limit_buy_order_old_partial['side'] = 'sell' if is_short else 'buy' limit_buy_canceled = deepcopy(limit_buy_order_old_partial) limit_buy_canceled['status'] = 'canceled' @@ -2661,11 +2667,14 @@ def test_check_handle_timedout_partial_fee( limit_buy_order_old_partial, trades_for_order, limit_buy_order_old_partial_canceled, mocker ) -> None: - # TODO-lev: use is_short or remove it - # open_trade.is_short = is_short + open_trade.is_short = is_short + open_trade.orders[0].ft_order_side = 'sell' if is_short else 'buy' rpc_mock = patch_RPCManager(mocker) limit_buy_order_old_partial['id'] = open_trade.open_order_id limit_buy_order_old_partial_canceled['id'] = open_trade.open_order_id + limit_buy_order_old_partial['side'] = 'sell' if is_short else 'buy' + limit_buy_order_old_partial_canceled['side'] = 'sell' if is_short else 'buy' + cancel_order_mock = MagicMock(return_value=limit_buy_order_old_partial_canceled) mocker.patch('freqtrade.wallets.Wallets.get_free', MagicMock(return_value=0)) patch_exchange(mocker) @@ -2697,7 +2706,7 @@ def test_check_handle_timedout_partial_fee( assert trades[0].amount == (limit_buy_order_old_partial['amount'] - limit_buy_order_old_partial['remaining']) - 0.023 assert trades[0].open_order_id is None - assert trades[0].fee_updated('buy') + assert trades[0].fee_updated(open_trade.enter_side) assert pytest.approx(trades[0].fee_open) == 0.001 From 08d8dfaee6f40ee3ffc36022151f11c6e99fbd7b Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 6 Mar 2022 14:47:26 +0100 Subject: [PATCH 5/5] Remove wrong import --- tests/test_freqtradebot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index 313365d29..d55ac4fc5 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -23,8 +23,8 @@ from freqtrade.persistence.models import PairLock from freqtrade.strategy.interface import SellCheckTuple from freqtrade.worker import Worker from tests.conftest import (create_mock_trades, get_patched_freqtradebot, get_patched_worker, - log_has, log_has_re, open_trade_usdt, patch_edge, patch_exchange, - patch_get_signal, patch_wallet, patch_whitelist) + log_has, log_has_re, patch_edge, patch_exchange, patch_get_signal, + patch_wallet, patch_whitelist) from tests.conftest_trades import (MOCK_TRADE_COUNT, enter_side, exit_side, mock_order_1, mock_order_2, mock_order_2_sell, mock_order_3, mock_order_3_sell, mock_order_4, mock_order_5_stoploss, mock_order_6_sell)