refactor: use CCXT for pair to symbol conversion

This commit is contained in:
Meng Xiangzhuo
2024-11-13 11:53:58 +08:00
parent c4cf582c9d
commit 37726fba58
2 changed files with 22 additions and 6 deletions

View File

@@ -7,8 +7,10 @@ import datetime
import io import io
import logging import logging
import zipfile import zipfile
from typing import Any
import aiohttp import aiohttp
import ccxt
import pandas as pd import pandas as pd
from pandas import DataFrame from pandas import DataFrame
@@ -36,6 +38,7 @@ async def fetch_ohlcv(
timeframe: str, timeframe: str,
since_ms: int, since_ms: int,
until_ms: int | None, until_ms: int | None,
markets: dict[str, Any] | None = None,
stop_on_404: bool = True, stop_on_404: bool = True,
) -> DataFrame: ) -> DataFrame:
""" """
@@ -53,7 +56,14 @@ async def fetch_ohlcv(
asset_type = "futures/um" asset_type = "futures/um"
else: else:
raise ValueError(f"Unsupported CandleType: {candle_type}") raise ValueError(f"Unsupported CandleType: {candle_type}")
symbol = symbol_ccxt_to_binance(pair)
if markets:
symbol = markets[pair]["id"]
else:
binance = ccxt.binance()
binance.load_markets()
symbol = binance.markets[pair]["id"]
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()

View File

@@ -133,7 +133,7 @@ def make_response_from_url(start_date, end_date):
), ),
( (
CandleType.SPOT, CandleType.SPOT,
dt_utc(2019, 1, 1), dt_utc(2019, 12, 25),
dt_utc(2020, 1, 5), dt_utc(2020, 1, 5),
dt_utc(2020, 1, 1), dt_utc(2020, 1, 1),
dt_utc(2020, 1, 3, 23), dt_utc(2020, 1, 3, 23),
@@ -165,7 +165,7 @@ def make_response_from_url(start_date, end_date):
), ),
( (
CandleType.SPOT, CandleType.SPOT,
dt_utc(2019, 1, 1), dt_utc(2019, 12, 25),
dt_utc(2020, 1, 5), dt_utc(2020, 1, 5),
None, None,
None, None,
@@ -201,7 +201,10 @@ async def test_fetch_ohlcv(mocker, candle_type, since, until, first_date, last_d
history_start = dt_utc(2020, 1, 1).date() history_start = dt_utc(2020, 1, 1).date()
history_end = dt_utc(2020, 1, 3).date() history_end = dt_utc(2020, 1, 3).date()
timeframe = "1h" timeframe = "1h"
pair = "BTCUSDT" if candle_type == CandleType.SPOT:
pair = "BTC/USDT"
else:
pair = "BTC/USDT:USDT"
since_ms = dt_ts(since) since_ms = dt_ts(since)
until_ms = dt_ts(until) until_ms = dt_ts(until)
@@ -209,8 +212,9 @@ async def test_fetch_ohlcv(mocker, candle_type, since, until, first_date, last_d
mocker.patch( mocker.patch(
"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)
) )
markets = {"BTC/USDT": {"id": "BTCUSDT"}, "BTC/USDT:USDT": {"id": "BTCUSDT"}}
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, markets, 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
@@ -222,12 +226,14 @@ async def test_fetch_ohlcv(mocker, candle_type, since, until, first_date, last_d
async def test_fetch_ohlcv_exc(mocker): async def test_fetch_ohlcv_exc(mocker):
timeframe = "1h" timeframe = "1h"
pair = "BTCUSDT" pair = "BTC/USDT"
since_ms = dt_ts(dt_utc(2020, 1, 1)) since_ms = dt_ts(dt_utc(2020, 1, 1))
until_ms = dt_ts(dt_utc(2020, 1, 2)) until_ms = dt_ts(dt_utc(2020, 1, 2))
mocker.patch("aiohttp.ClientSession.get", side_effect=RuntimeError) mocker.patch("aiohttp.ClientSession.get", side_effect=RuntimeError)
mocker.patch("ccxt.binance.binance")
mocker.patch("ccxt.binance.binance.markets", {"BTC/USDT": {"id": "BTCUSDT"}})
df = await fetch_ohlcv(CandleType.SPOT, pair, timeframe, since_ms, until_ms) df = await fetch_ohlcv(CandleType.SPOT, pair, timeframe, since_ms, until_ms)