refactor: strip fast download logic into a method

This commit is contained in:
Meng Xiangzhuo
2024-11-13 10:16:06 +08:00
parent cf0f232635
commit acc53065e5
2 changed files with 47 additions and 13 deletions

View File

@@ -110,11 +110,13 @@ class Binance(Exchange):
candle_type: CandleType,
is_new_pair: bool = False,
until_ms: Optional[int] = None,
only_from_ccxt: bool = False,
) -> DataFrame:
"""
Overwrite to introduce "fast new pair" functionality by detecting the pair's listing date
Does not work for other exchanges, which don't return the earliest data when called with "0"
:param candle_type: Any of the enum CandleType (must match trading mode!)
:param only_from_ccxt: Only download data using the API provided by CCXT
"""
if is_new_pair:
x = self.loop.run_until_complete(
@@ -134,6 +136,37 @@ class Binance(Exchange):
)
return DataFrame(columns=DEFAULT_DATAFRAME_COLUMNS)
if only_from_ccxt:
return super().get_historic_ohlcv(
pair=pair,
timeframe=timeframe,
since_ms=since_ms,
candle_type=candle_type,
is_new_pair=is_new_pair,
until_ms=until_ms,
)
else:
return self.get_historic_ohlcv_fast(
pair=pair,
timeframe=timeframe,
since_ms=since_ms,
candle_type=candle_type,
is_new_pair=is_new_pair,
until_ms=until_ms,
)
def get_historic_ohlcv_fast(
self,
pair: str,
timeframe: str,
since_ms: int,
candle_type: CandleType,
is_new_pair: bool = False,
until_ms: Optional[int] = None,
):
"""
Fetch ohlcv fast by utilizing https://data.binance.vision
"""
if (candle_type == CandleType.SPOT and timeframe in ["1s", "1m", "3m", "5m"]) or (
candle_type == CandleType.FUTURES and timeframe in ["1m", "3m", "5m", "15m", "30m"]
):
@@ -163,15 +196,16 @@ class Binance(Exchange):
until_ms=until_ms,
)
all_df = concat([df, rest_df])
return all_df
return super().get_historic_ohlcv(
pair=pair,
timeframe=timeframe,
since_ms=since_ms,
candle_type=candle_type,
is_new_pair=is_new_pair,
until_ms=until_ms,
)
else:
return super().get_historic_ohlcv(
pair=pair,
timeframe=timeframe,
since_ms=since_ms,
candle_type=candle_type,
is_new_pair=is_new_pair,
until_ms=until_ms,
)
return all_df
def funding_fee_cutoff(self, open_date: datetime):
"""

View File

@@ -2224,6 +2224,7 @@ class Exchange:
candle_type: CandleType,
is_new_pair: bool = False,
until_ms: Optional[int] = None,
only_from_ccxt: bool = False,
) -> DataFrame:
"""
Get candle history using asyncio and returns the list of candles.
@@ -2232,8 +2233,10 @@ class Exchange:
:param pair: Pair to download
:param timeframe: Timeframe to get data for
:param since_ms: Timestamp in milliseconds to get history from
:param until_ms: Timestamp in milliseconds to get history up to
:param candle_type: '', mark, index, premiumIndex, or funding_rate
:param is_new_pair: used by binance subclass to allow "fast" new pair downloading
:param until_ms: Timestamp in milliseconds to get history up to
:param only_from_ccxt: Only download data using the API provided by CCXT
:return: Dataframe with candle (OHLCV) data
"""
pair, _, _, data, _ = self.loop.run_until_complete(
@@ -2242,7 +2245,6 @@ class Exchange:
timeframe=timeframe,
since_ms=since_ms,
until_ms=until_ms,
is_new_pair=is_new_pair,
candle_type=candle_type,
)
)
@@ -2255,13 +2257,11 @@ class Exchange:
timeframe: str,
since_ms: int,
candle_type: CandleType,
is_new_pair: bool = False,
raise_: bool = False,
until_ms: Optional[int] = None,
) -> OHLCVResponse:
"""
Download historic ohlcv
:param is_new_pair: used by binance subclass to allow "fast" new pair downloading
:param candle_type: Any of the enum CandleType (must match trading mode!)
"""