feat: fall back to rest API by catching all exceptions

This commit is contained in:
Meng Xiangzhuo
2024-11-02 05:17:16 +08:00
parent 76187d31cf
commit e2ee7f7b2f
2 changed files with 30 additions and 28 deletions
+7 -2
View File
@@ -42,6 +42,7 @@ async def fetch_ohlcv(
:return: the date range is between [since_ms, until_ms), :return: the date range is between [since_ms, until_ms),
return and empty DataFrame if no data available in the time range return and empty DataFrame if no data available in the time range
""" """
try:
if candle_type == CandleType.SPOT: if candle_type == CandleType.SPOT:
asset_type = "spot" asset_type = "spot"
elif candle_type == CandleType.FUTURES: elif candle_type == CandleType.FUTURES:
@@ -52,8 +53,8 @@ async def fetch_ohlcv(
start = dt_from_ts(since_ms) start = dt_from_ts(since_ms)
end = dt_from_ts(until_ms) if until_ms else dt_now() 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 # We use two days ago as the last available day because the daily archives are daily
# and have several hours delay # uploaded and have several hours delay
last_available_date = dt_now() - datetime.timedelta(days=2) last_available_date = dt_now() - datetime.timedelta(days=2)
end = min(end, last_available_date) end = min(end, last_available_date)
if start >= end: if start >= end:
@@ -62,6 +63,10 @@ async def fetch_ohlcv(
logger.info( logger.info(
f"Downloaded data for {pair} from https://data.binance.vision/ with length {len(df)}." 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()
if not df.empty: if not df.empty:
return df.loc[(df["date"] >= start) & (df["date"] < end)] return df.loc[(df["date"] >= start) & (df["date"] < end)]
else: else:
+1 -4
View File
@@ -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) "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: if df.empty:
assert first_date is None and last_date is None assert first_date is None and last_date is None
else: else:
assert candle_type in [CandleType.SPOT, CandleType.FUTURES]
assert df["date"].iloc[0] == first_date assert df["date"].iloc[0] == first_date
assert df["date"].iloc[-1] == last_date assert df["date"].iloc[-1] == last_date
else:
with pytest.raises(ValueError):
await fetch_ohlcv(candle_type, pair, timeframe, since_ms, until_ms, stop_on_404)
async def test_fetch_ohlcv_exc(mocker): async def test_fetch_ohlcv_exc(mocker):