diff --git a/freqtrade/exchange/binance_public_data.py b/freqtrade/exchange/binance_public_data.py index 1a577c76e..8290a807b 100644 --- a/freqtrade/exchange/binance_public_data.py +++ b/freqtrade/exchange/binance_public_data.py @@ -42,26 +42,31 @@ async def fetch_ohlcv( :return: the date range is between [since_ms, until_ms), return and empty DataFrame if no data available in the time range """ - if candle_type == CandleType.SPOT: - asset_type = "spot" - elif candle_type == CandleType.FUTURES: - asset_type = "futures/um" - else: - raise ValueError(f"Unsupported CandleType: {candle_type}") - symbol = symbol_ccxt_to_binance(pair) - start = dt_from_ts(since_ms) - end = dt_from_ts(until_ms) if until_ms else dt_now() + try: + if candle_type == CandleType.SPOT: + asset_type = "spot" + elif candle_type == CandleType.FUTURES: + asset_type = "futures/um" + else: + raise ValueError(f"Unsupported CandleType: {candle_type}") + symbol = symbol_ccxt_to_binance(pair) + start = dt_from_ts(since_ms) + end = dt_from_ts(until_ms) if until_ms else dt_now() + + # We use two days ago as the last available day because the daily archives are daily + # uploaded and have several hours delay + last_available_date = dt_now() - datetime.timedelta(days=2) + end = min(end, last_available_date) + if start >= end: + return DataFrame() + df = await _fetch_ohlcv(asset_type, symbol, timeframe, start, end, stop_on_404) + logger.info( + f"Downloaded data for {pair} from https://data.binance.vision/ with length {len(df)}." + ) + except Exception as e: + logger.debug("An exception occurred", exc_info=e) + df = DataFrame() - # We use two days ago as the last available day because the daily archives are daily uploaded - # and have several hours delay - last_available_date = dt_now() - datetime.timedelta(days=2) - end = min(end, last_available_date) - if start >= end: - return DataFrame() - df = await _fetch_ohlcv(asset_type, symbol, timeframe, start, end, stop_on_404) - logger.info( - f"Downloaded data for {pair} from https://data.binance.vision/ with length {len(df)}." - ) if not df.empty: return df.loc[(df["date"] >= start) & (df["date"] < end)] else: diff --git a/tests/exchange/test_binance_public_data.py b/tests/exchange/test_binance_public_data.py index d92dcf51c..e4f665afd 100644 --- a/tests/exchange/test_binance_public_data.py +++ b/tests/exchange/test_binance_public_data.py @@ -209,17 +209,14 @@ async def test_fetch_ohlcv(mocker, candle_type, since, until, first_date, last_d "aiohttp.ClientSession.get", side_effect=make_response_from_url(history_start, history_end) ) - if candle_type in [CandleType.SPOT, CandleType.FUTURES]: - df = await fetch_ohlcv(candle_type, pair, timeframe, since_ms, until_ms, stop_on_404) + df = await fetch_ohlcv(candle_type, pair, timeframe, since_ms, until_ms, stop_on_404) - if df.empty: - assert first_date is None and last_date is None - else: - assert df["date"].iloc[0] == first_date - assert df["date"].iloc[-1] == last_date + if df.empty: + assert first_date is None and last_date is None else: - with pytest.raises(ValueError): - await fetch_ohlcv(candle_type, pair, timeframe, since_ms, until_ms, stop_on_404) + assert candle_type in [CandleType.SPOT, CandleType.FUTURES] + assert df["date"].iloc[0] == first_date + assert df["date"].iloc[-1] == last_date async def test_fetch_ohlcv_exc(mocker):