Repository: freqtrade/freqtrade

Issue:
Downloading Kraken Data
Operating system: Windows 
Exchange: Kraken

I'm trying to download historical data, and when I run the download data command, I get an error: List index out of range.

```
C:\Users\chris\ft_userdata>docker compose run freqtrade download-data --exchange kraken --dl-trades
2023-07-03 21:59:59,621 - freqtrade - INFO - freqtrade 2023.5.1
2023-07-03 21:59:59,621 - freqtrade.configuration.load_config - INFO - Using config: user_data/config.json ...
2023-07-03 21:59:59,623 - freqtrade.loggers - INFO - Verbosity set to 0
2023-07-03 21:59:59,624 - freqtrade.configuration.configuration - INFO - Using exchange kraken
2023-07-03 21:59:59,722 - freqtrade.configuration.configuration - INFO - Using user-data directory: /freqtrade/user_data ...
2023-07-03 21:59:59,724 - freqtrade.configuration.configuration - INFO - Using data directory: /freqtrade/user_data/data/kraken ...
2023-07-03 21:59:59,725 - freqtrade.configuration.configuration - INFO - timeframes --timeframes: ['1m', '5m']
2023-07-03 21:59:59,725 - freqtrade.configuration.configuration - INFO - Detected --dl-trades: True
2023-07-03 21:59:59,725 - freqtrade.exchange.check_exchange - INFO - Checking exchange...
2023-07-03 21:59:59,727 - freqtrade.exchange.check_exchange - INFO - Exchange "kraken" is officially supported by the Freqtrade development team.
2023-07-03 21:59:59,727 - freqtrade.configuration.configuration - INFO - Using pairlist from configuration.
2023-07-03 21:59:59,727 - freqtrade.configuration.config_validation - INFO - Validating configuration ...
2023-07-03 21:59:59,730 - freqtrade.exchange.exchange - INFO - Instance is running with dry_run enabled
2023-07-03 21:59:59,730 - freqtrade.exchange.exchange - INFO - Using CCXT 3.1.5
2023-07-03 21:59:59,730 - freqtrade.exchange.exchange - INFO - Applying additional ccxt config: {'enableRateLimit': True}
2023-07-03 21:59:59,732 - freqtrade.exchange.exchange - INFO - Applying additional ccxt config: {'enableRateLimit': True, 'rateLimit': 3100}
2023-07-03 21:59:59,733 - freqtrade.exchange.exchange - INFO - Using Exchange "Kraken"
2023-07-03 21:59:59,733 - freqtrade.resolvers.exchange_resolver - INFO - Using resolved exchange 'Kraken'...
2023-07-03 21:59:59,733 - freqtrade.exchange.exchange - INFO - Markets were not loaded. Loading them now..
2023-07-03 22:00:04,356 - freqtrade.commands.data_commands - INFO - About to download pairs: ['ETH/USD', 'LTC/USD', 'SOL/USD', 'BCH/USD', 'EOS/USD', 'DOGE/USD', 'ADA/USD', 'MATIC/USD', 'LINK/USD', 'DOT/USD', 'XMR/USD', 'GALA/USD', 'GBP/USD', 'FIL/USD', 'COMP/USD'], intervals: ['1m', '5m'] to /freqtrade/user_data/data/kraken
2023-07-03 22:00:04,357 - freqtrade.data.history.history_utils - INFO - Downloading trades for pair ETH/USD.
2023-07-03 22:00:04,360 - freqtrade.data.history.history_utils - INFO - Current Amount of trades: 0
2023-07-03 22:00:07,602 - freqtrade.data.history.history_utils - ERROR - Failed to download historic trades for pair: "ETH/USD".
Traceback (most recent call last):
  File "/freqtrade/freqtrade/data/history/history_utils.py", line 366, in _download_trades_history
    new_trades = exchange.get_historic_trades(pair=pair,
  File "/freqtrade/freqtrade/exchange/exchange.py", line 2347, in get_historic_trades
    return self.loop.run_until_complete(
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File "/freqtrade/freqtrade/exchange/exchange.py", line 2322, in _async_get_trade_history
    return await self._async_get_trade_history_id(
  File "/freqtrade/freqtrade/exchange/exchange.py", line 2253, in _async_get_trade_history_id
    from_id = t[-1][1]
IndexError: list index out of range
```




It repeats this error for each pair.



Discussion:

Resolving patch:
diff --git a/freqtrade/data/history/history_utils.py b/freqtrade/data/history/history_utils.py
--- a/freqtrade/data/history/history_utils.py
+++ b/freqtrade/data/history/history_utils.py
@@ -354,7 +354,7 @@ def _download_trades_history(exchange: Exchange,
             trades = []
 
         if not since:
-            since = int((datetime.now() - timedelta(days=-new_pairs_days)).timestamp()) * 1000
+            since = int((datetime.now() - timedelta(days=new_pairs_days)).timestamp()) * 1000
 
         from_id = trades[-1][1] if trades else None
         if trades and since < trades[-1][0]:


Test patch:
diff --git a/tests/exchange/test_ccxt_compat.py b/tests/exchange/test_ccxt_compat.py
--- a/tests/exchange/test_ccxt_compat.py
+++ b/tests/exchange/test_ccxt_compat.py
@@ -43,6 +43,7 @@
         'hasQuoteVolumeFutures': True,
         'leverage_tiers_public': False,
         'leverage_in_spot_market': False,
+        'trades_lookback_hours': 4,
         'private_methods': [
             'fapiPrivateGetPositionSideDual',
             'fapiPrivateGetMultiAssetsMargin'
@@ -98,6 +99,7 @@
         'timeframe': '1h',
         'leverage_tiers_public': False,
         'leverage_in_spot_market': True,
+        'trades_lookback_hours': 12,
     },
     'kucoin': {
         'pair': 'XRP/USDT',
@@ -640,7 +642,21 @@ def test_ccxt__calculate_funding_fees(self, exchange_futures: EXCHANGE_FIXTURE_T
         assert isinstance(funding_fee, float)
         # assert funding_fee > 0
 
-    # TODO: tests fetch_trades (?)
+    def test_ccxt__async_get_trade_history(self, exchange: EXCHANGE_FIXTURE_TYPE):
+        exch, exchangename = exchange
+        if not (lookback := EXCHANGES[exchangename].get('trades_lookback_hours')):
+            pytest.skip('test_fetch_trades not enabled for this exchange')
+        pair = EXCHANGES[exchangename]['pair']
+        since = int((datetime.now(timezone.utc) - timedelta(hours=lookback)).timestamp() * 1000)
+        res = exch.loop.run_until_complete(
+            exch._async_get_trade_history(pair, since, None, None)
+        )
+        assert len(res) == 2
+        res_pair, res_trades = res
+        assert res_pair == pair
+        assert isinstance(res_trades, list)
+        assert res_trades[0][0] >= since
+        assert len(res_trades) > 1200
 
     def test_ccxt_get_fee(self, exchange: EXCHANGE_FIXTURE_TYPE):
         exch, exchangename = exchange