chore: warning when fall back to rest API

This commit is contained in:
xzmeng
2024-11-14 04:36:24 +08:00
parent c869dfea3f
commit 40f70a1cc0

View File

@@ -23,7 +23,10 @@ logger = logging.getLogger(__name__)
class Http404(Exception): class Http404(Exception):
pass def __init__(self, msg, date, url):
super().__init__(msg)
self.date = date
self.url = url
class BadHttpStatus(Exception): class BadHttpStatus(Exception):
@@ -83,7 +86,7 @@ async def fetch_ohlcv(
end = min(end, last_available_date) end = min(end, last_available_date)
if start >= end: if start >= end:
return DataFrame() return DataFrame()
df = await _fetch_ohlcv(asset_type, symbol, timeframe, start, end, stop_on_404) df = await _fetch_ohlcv(asset_type, symbol, pair, timeframe, start, end, stop_on_404)
logger.debug( logger.debug(
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)}."
) )
@@ -108,6 +111,7 @@ def concat(dfs) -> DataFrame:
async def _fetch_ohlcv( async def _fetch_ohlcv(
asset_type: str, asset_type: str,
symbol: str, symbol: str,
pair: str,
timeframe: str, timeframe: str,
start: datetime.date, start: datetime.date,
end: datetime.date, end: datetime.date,
@@ -129,6 +133,8 @@ async def _fetch_ohlcv(
current_day += 1 current_day += 1
if isinstance(result, Http404): if isinstance(result, Http404):
if stop_on_404: if stop_on_404:
logger.debug(f"Failed to download {result.url} due to 404.")
# A 404 error on the first day indicates missing data # A 404 error on the first day indicates missing data
# on https://data.binance.vision, we provide the warning and the advice. # on https://data.binance.vision, we provide the warning and the advice.
# https://github.com/freqtrade/freqtrade/blob/acc53065e5fa7ab5197073276306dc9dc3adbfa3/tests/exchange_online/test_binance_compare_ohlcv.py#L7 # https://github.com/freqtrade/freqtrade/blob/acc53065e5fa7ab5197073276306dc9dc3adbfa3/tests/exchange_online/test_binance_compare_ohlcv.py#L7
@@ -140,6 +146,13 @@ async def _fetch_ohlcv(
"data before 2020 (using `--timerange yyyymmdd-20200101`), and " "data before 2020 (using `--timerange yyyymmdd-20200101`), and "
"then download the full data you need." "then download the full data you need."
) )
else:
logger.warning(
f"Binance fast download for {pair} stopped at {result.date} due to"
f"data missing: {result.url}, fall back to rest API for the "
"remaining data download, this can take more time."
)
logger.debug("Abort downloading from data.binance.vision due to 404") logger.debug("Abort downloading from data.binance.vision due to 404")
return concat(dfs) return concat(dfs)
else: else:
@@ -241,7 +254,7 @@ async def get_daily_ohlcv(
return df return df
elif resp.status == 404: elif resp.status == 404:
logger.debug(f"No data available for {symbol} in {format_date(date)}") logger.debug(f"No data available for {symbol} in {format_date(date)}")
raise Http404 raise Http404(f"404: {url}", date, url)
else: else:
raise BadHttpStatus(f"{resp.status} - {resp.reason}") raise BadHttpStatus(f"{resp.status} - {resp.reason}")
except Exception as e: except Exception as e: