From bf182dc01e6187f81d3bd61dfcb4a43e754e1c5b Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 7 Jan 2021 20:03:34 +0100 Subject: [PATCH 1/2] Fix wrong key usage in trade_history_timebased --- freqtrade/exchange/exchange.py | 2 +- tests/conftest.py | 10 +++++----- tests/exchange/test_exchange.py | 25 +++++++++++++------------ 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index 11a0ef8e6..b610b28f4 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -936,7 +936,7 @@ class Exchange: while True: t = await self._async_fetch_trades(pair, since=since) if len(t): - since = t[-1][1] + since = t[-1][0] trades.extend(t) # Reached the end of the defined-download period if until and t[-1][0] > until: diff --git a/tests/conftest.py b/tests/conftest.py index 9eda0e973..75a98dcc5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1492,11 +1492,11 @@ def trades_for_order(): @pytest.fixture(scope="function") def trades_history(): - return [[1565798389463, '126181329', None, 'buy', 0.019627, 0.04, 0.00078508], - [1565798399629, '126181330', None, 'buy', 0.019627, 0.244, 0.004788987999999999], - [1565798399752, '126181331', None, 'sell', 0.019626, 0.011, 0.00021588599999999999], - [1565798399862, '126181332', None, 'sell', 0.019626, 0.011, 0.00021588599999999999], - [1565798399872, '126181333', None, 'sell', 0.019626, 0.011, 0.00021588599999999999]] + return [[1565798389463, '12618132aa9', None, 'buy', 0.019627, 0.04, 0.00078508], + [1565798399629, '1261813bb30', None, 'buy', 0.019627, 0.244, 0.004788987999999999], + [1565798399752, '1261813cc31', None, 'sell', 0.019626, 0.011, 0.00021588599999999999], + [1565798399862, '126181cc332', None, 'sell', 0.019626, 0.011, 0.00021588599999999999], + [1565798399872, '1261aa81333', None, 'sell', 0.019626, 0.011, 0.00021588599999999999]] @pytest.fixture(scope="function") diff --git a/tests/exchange/test_exchange.py b/tests/exchange/test_exchange.py index a42ff52e4..f33382da4 100644 --- a/tests/exchange/test_exchange.py +++ b/tests/exchange/test_exchange.py @@ -1759,36 +1759,37 @@ async def test__async_get_trade_history_id(default_conf, mocker, caplog, exchang @pytest.mark.asyncio @pytest.mark.parametrize("exchange_name", EXCHANGES) async def test__async_get_trade_history_time(default_conf, mocker, caplog, exchange_name, - trades_history): + fetch_trades_result): caplog.set_level(logging.DEBUG) async def mock_get_trade_hist(pair, *args, **kwargs): - if kwargs['since'] == trades_history[0][0]: - return trades_history[:-1] + if kwargs['since'] == fetch_trades_result[0]['timestamp']: + return fetch_trades_result[:-1] else: - return trades_history[-1:] + return fetch_trades_result[-1:] caplog.set_level(logging.DEBUG) exchange = get_patched_exchange(mocker, default_conf, id=exchange_name) # Monkey-patch async function - exchange._async_fetch_trades = MagicMock(side_effect=mock_get_trade_hist) + exchange._api_async.fetch_trades = MagicMock(side_effect=mock_get_trade_hist) pair = 'ETH/BTC' - ret = await exchange._async_get_trade_history_time(pair, since=trades_history[0][0], - until=trades_history[-1][0]-1) + ret = await exchange._async_get_trade_history_time(pair, + since=fetch_trades_result[0]['timestamp'], + until=fetch_trades_result[-1]['timestamp']-1) assert type(ret) is tuple assert ret[0] == pair assert type(ret[1]) is list - assert len(ret[1]) == len(trades_history) - assert exchange._async_fetch_trades.call_count == 2 - fetch_trades_cal = exchange._async_fetch_trades.call_args_list + assert len(ret[1]) == len(fetch_trades_result) + assert exchange._api_async.fetch_trades.call_count == 2 + fetch_trades_cal = exchange._api_async.fetch_trades.call_args_list # first call (using since, not fromId) assert fetch_trades_cal[0][0][0] == pair - assert fetch_trades_cal[0][1]['since'] == trades_history[0][0] + assert fetch_trades_cal[0][1]['since'] == fetch_trades_result[0]['timestamp'] # 2nd call assert fetch_trades_cal[1][0][0] == pair - assert fetch_trades_cal[0][1]['since'] == trades_history[0][0] + assert fetch_trades_cal[1][1]['since'] == fetch_trades_result[-2]['timestamp'] assert log_has_re(r"Stopping because until was reached.*", caplog) From 4f126bea3582242155f412a5e4175b8cfe363694 Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 7 Jan 2021 20:06:26 +0100 Subject: [PATCH 2/2] Change trades-test2 to better test correct behaviour --- tests/exchange/test_exchange.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/tests/exchange/test_exchange.py b/tests/exchange/test_exchange.py index f33382da4..7d9954cb9 100644 --- a/tests/exchange/test_exchange.py +++ b/tests/exchange/test_exchange.py @@ -1718,8 +1718,8 @@ async def test__async_fetch_trades(default_conf, mocker, caplog, exchange_name, @pytest.mark.asyncio @pytest.mark.parametrize("exchange_name", EXCHANGES) -async def test__async_get_trade_history_id(default_conf, mocker, caplog, exchange_name, - trades_history): +async def test__async_get_trade_history_id(default_conf, mocker, exchange_name, + fetch_trades_result): exchange = get_patched_exchange(mocker, default_conf, id=exchange_name) pagination_arg = exchange._trades_pagination_arg @@ -1727,28 +1727,29 @@ async def test__async_get_trade_history_id(default_conf, mocker, caplog, exchang async def mock_get_trade_hist(pair, *args, **kwargs): if 'since' in kwargs: # Return first 3 - return trades_history[:-2] - elif kwargs.get('params', {}).get(pagination_arg) == trades_history[-3][1]: + return fetch_trades_result[:-2] + elif kwargs.get('params', {}).get(pagination_arg) == fetch_trades_result[-3]['id']: # Return 2 - return trades_history[-3:-1] + return fetch_trades_result[-3:-1] else: # Return last 2 - return trades_history[-2:] + return fetch_trades_result[-2:] # Monkey-patch async function - exchange._async_fetch_trades = MagicMock(side_effect=mock_get_trade_hist) + exchange._api_async.fetch_trades = MagicMock(side_effect=mock_get_trade_hist) pair = 'ETH/BTC' - ret = await exchange._async_get_trade_history_id(pair, since=trades_history[0][0], - until=trades_history[-1][0]-1) + ret = await exchange._async_get_trade_history_id(pair, + since=fetch_trades_result[0]['timestamp'], + until=fetch_trades_result[-1]['timestamp'] - 1) assert type(ret) is tuple assert ret[0] == pair assert type(ret[1]) is list - assert len(ret[1]) == len(trades_history) - assert exchange._async_fetch_trades.call_count == 3 - fetch_trades_cal = exchange._async_fetch_trades.call_args_list + assert len(ret[1]) == len(fetch_trades_result) + assert exchange._api_async.fetch_trades.call_count == 3 + fetch_trades_cal = exchange._api_async.fetch_trades.call_args_list # first call (using since, not fromId) assert fetch_trades_cal[0][0][0] == pair - assert fetch_trades_cal[0][1]['since'] == trades_history[0][0] + assert fetch_trades_cal[0][1]['since'] == fetch_trades_result[0]['timestamp'] # 2nd call assert fetch_trades_cal[1][0][0] == pair