refactor: rename asset_type to asset_type_url_segment

This commit is contained in:
xzmeng
2024-11-14 07:51:58 +08:00
parent 660863392b
commit bfdbf0248c
3 changed files with 25 additions and 21 deletions

View File

@@ -64,9 +64,9 @@ async def download_archive_ohlcv(
""" """
try: try:
if candle_type == CandleType.SPOT: if candle_type == CandleType.SPOT:
asset_type = "spot" asset_type_url_segment = "spot"
elif candle_type == CandleType.FUTURES: elif candle_type == CandleType.FUTURES:
asset_type = "futures/um" asset_type_url_segment = "futures/um"
else: else:
raise ValueError(f"Unsupported CandleType: {candle_type}") raise ValueError(f"Unsupported CandleType: {candle_type}")
@@ -87,7 +87,7 @@ async def download_archive_ohlcv(
if start >= end: if start >= end:
return DataFrame() return DataFrame()
df = await _download_archive_ohlcv( df = await _download_archive_ohlcv(
asset_type, symbol, pair, timeframe, start, end, stop_on_404 asset_type_url_segment, 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)}."
@@ -111,7 +111,7 @@ def concat(dfs) -> DataFrame:
async def _download_archive_ohlcv( async def _download_archive_ohlcv(
asset_type: str, asset_type_url_segment: str,
symbol: str, symbol: str,
pair: str, pair: str,
timeframe: str, timeframe: str,
@@ -129,7 +129,9 @@ async def _download_archive_ohlcv(
# the HTTP connections has been throttled by TCPConnector # the HTTP connections has been throttled by TCPConnector
for dates in chunks(list(date_range(start, end)), 1000): for dates in chunks(list(date_range(start, end)), 1000):
tasks = [ tasks = [
asyncio.create_task(get_daily_ohlcv(asset_type, symbol, timeframe, date, session)) asyncio.create_task(
get_daily_ohlcv(asset_type_url_segment, symbol, timeframe, date, session)
)
for date in dates for date in dates
] ]
for task in tasks: for task in tasks:
@@ -198,21 +200,21 @@ def zip_name(symbol: str, timeframe: str, date: datetime.date) -> str:
return f"{symbol}-{timeframe}-{format_date(date)}.zip" return f"{symbol}-{timeframe}-{format_date(date)}.zip"
def zip_url(asset_type: str, symbol: str, timeframe: str, date: datetime.date) -> str: def zip_url(asset_type_url_segment: str, symbol: str, timeframe: str, date: datetime.date) -> str:
""" """
example urls: example urls:
https://data.binance.vision/data/spot/daily/klines/BTCUSDT/1s/BTCUSDT-1s-2023-10-27.zip https://data.binance.vision/data/spot/daily/klines/BTCUSDT/1s/BTCUSDT-1s-2023-10-27.zip
https://data.binance.vision/data/futures/um/daily/klines/BTCUSDT/1h/BTCUSDT-1h-2023-10-27.zip https://data.binance.vision/data/futures/um/daily/klines/BTCUSDT/1h/BTCUSDT-1h-2023-10-27.zip
""" """
url = ( url = (
f"https://data.binance.vision/data/{asset_type}/daily/klines/{symbol}/{timeframe}/" f"https://data.binance.vision/data/{asset_type_url_segment}/daily/klines/{symbol}"
f"{zip_name(symbol, timeframe, date)}" f"/{timeframe}/{zip_name(symbol, timeframe, date)}"
) )
return url return url
async def get_daily_ohlcv( async def get_daily_ohlcv(
asset_type: str, asset_type_url_segment: str,
symbol: str, symbol: str,
timeframe: str, timeframe: str,
date: datetime.date, date: datetime.date,
@@ -224,7 +226,7 @@ async def get_daily_ohlcv(
Get daily OHLCV from https://data.binance.vision Get daily OHLCV from https://data.binance.vision
See https://github.com/binance/binance-public-data See https://github.com/binance/binance-public-data
:asset_type: `spot` or `futures/um` :asset_type_url_segment: `spot` or `futures/um`
:symbol: binance symbol name, e.g. BTCUSDT :symbol: binance symbol name, e.g. BTCUSDT
:timeframe: e.g. 1m, 1h :timeframe: e.g. 1m, 1h
:date: the returned DataFrame will cover the entire day of `date` in UTC :date: the returned DataFrame will cover the entire day of `date` in UTC
@@ -234,7 +236,7 @@ async def get_daily_ohlcv(
:return: This function won't raise any exceptions, it will catch and return them :return: This function won't raise any exceptions, it will catch and return them
""" """
url = zip_url(asset_type, symbol, timeframe, date) url = zip_url(asset_type_url_segment, symbol, timeframe, date)
logger.debug(f"download data from binance: {url}") logger.debug(f"download data from binance: {url}")

View File

@@ -51,11 +51,11 @@ def make_daily_df(date, timeframe):
return df return df
def make_daily_zip(asset_type, symbol, timeframe, date) -> bytes: def make_daily_zip(asset_type_url_segment, symbol, timeframe, date) -> bytes:
df = make_daily_df(date, timeframe) df = make_daily_df(date, timeframe)
if asset_type == "spot": if asset_type_url_segment == "spot":
header = True header = True
elif asset_type == "futures/um": elif asset_type_url_segment == "futures/um":
header = None header = None
else: else:
raise ValueError raise ValueError
@@ -85,8 +85,8 @@ class MockResponse:
def make_response_from_url(start_date, end_date): def make_response_from_url(start_date, end_date):
def make_response(url): def make_response(url):
pattern = ( pattern = (
r"https://data.binance.vision/data/(?P<asset_type>spot|futures/um)/daily/klines/" r"https://data.binance.vision/data/(?P<asset_type_url_segment>spot|futures/um)"
r"(?P<symbol>.*?)/(?P<timeframe>.*?)/(?P=symbol)-(?P=timeframe)-" r"/daily/klines/(?P<symbol>.*?)/(?P<timeframe>.*?)/(?P=symbol)-(?P=timeframe)-"
r"(?P<date>\d{4}-\d{2}-\d{2}).zip" r"(?P<date>\d{4}-\d{2}-\d{2}).zip"
) )
m = re.match(pattern, url) m = re.match(pattern, url)
@@ -97,7 +97,7 @@ def make_response_from_url(start_date, end_date):
if date < start_date or date > end_date: if date < start_date or date > end_date:
return MockResponse(content="", status=404) return MockResponse(content="", status=404)
zip_file = make_daily_zip(m["asset_type"], m["symbol"], m["timeframe"], date) zip_file = make_daily_zip(m["asset_type_url_segment"], m["symbol"], m["timeframe"], date)
return MockResponse(content=zip_file, status=200) return MockResponse(content=zip_file, status=200)
return make_response return make_response

View File

@@ -31,14 +31,14 @@ from freqtrade.util.datetime_helpers import dt_from_ts
class Check: class Check:
def __init__(self, asset_type, timeframe): def __init__(self, asset_type_url_segment, timeframe):
self.asset_type = asset_type self.asset_type_url_segment = asset_type_url_segment
self.timeframe = timeframe self.timeframe = timeframe
self.klines_endpoint = "https://api.binance.com/api/v3/klines" self.klines_endpoint = "https://api.binance.com/api/v3/klines"
self.exchange_endpoint = "https://api.binance.com/api/v3/exchangeInfo" self.exchange_endpoint = "https://api.binance.com/api/v3/exchangeInfo"
self.mismatch = set() self.mismatch = set()
if asset_type == "futures/um": if asset_type_url_segment == "futures/um":
self.klines_endpoint = "https://fapi.binance.com/fapi/v1/klines" self.klines_endpoint = "https://fapi.binance.com/fapi/v1/klines"
self.exchange_endpoint = "https://fapi.binance.com/fapi/v1/exchangeInfo" self.exchange_endpoint = "https://fapi.binance.com/fapi/v1/exchangeInfo"
@@ -52,7 +52,9 @@ class Check:
first_kline_ts = first_kline[0] first_kline_ts = first_kline[0]
date = dt_from_ts(first_kline_ts).date() date = dt_from_ts(first_kline_ts).date()
archive_url = zip_url(self.asset_type, symbol=symbol, timeframe=self.timeframe, date=date) archive_url = zip_url(
self.asset_type_url_segment, symbol=symbol, timeframe=self.timeframe, date=date
)
async with self.session.get( async with self.session.get(
archive_url, params=dict(symbol=symbol, interval=self.timeframe, startTime=0) archive_url, params=dict(symbol=symbol, interval=self.timeframe, startTime=0)
) as resp: ) as resp: