fix: Request all "pages" of instrument info (not just first)
This commit is contained in:
1 parent
27cbd51b1c
commit
bd22f4779d
1 file changed
+61
-26
@@ -32,7 +32,8 @@ def normalize_contract_name(contract: str) -> str:
|
||||
return contract[:-4] + "_" + contract[-4:]
|
||||
return contract
|
||||
|
||||
def fetch_contract_specs(contracts: Iterable[str], use_testnet: bool = True) -> dict:
|
||||
def fetch_futures_specs(contracts: Iterable[str], use_testnet: bool = True) -> dict:
|
||||
# Fetch specs for futures only, explicitly, handling pagination
|
||||
default_specs = {
|
||||
"tick_unit": 1,
|
||||
"tick_base": 10,
|
||||
@@ -43,35 +44,69 @@ def fetch_contract_specs(contracts: Iterable[str], use_testnet: bool = True) ->
|
||||
specs_dict = {contract: default_specs for contract in contracts}
|
||||
try:
|
||||
host = "api-testnet.bybit.com" if use_testnet else "api.bybit.com"
|
||||
url = f"https://{host}/v5/market/instruments-info?category=linear"
|
||||
response = requests.get(url, timeout=5)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
base_url = f"https://{host}/v5/market/instruments-info?category=linear&limit=500"
|
||||
logger.info(f"Querying Bybit API for futures specs: {base_url}")
|
||||
|
||||
found_contracts = set()
|
||||
for item in data.get("result", {}).get("list", []):
|
||||
contract = item.get("symbol")
|
||||
if contract in contracts:
|
||||
price_tick = float(item.get("priceFilter", {}).get("tickSize", "0.1"))
|
||||
lot_size = float(item.get("lotSizeFilter", {}).get("qtyStep", "0.001"))
|
||||
tick_base = int(1 / price_tick) if price_tick != 0 else 10
|
||||
if lot_size >= 1:
|
||||
qty_tick_base = int(lot_size)
|
||||
else:
|
||||
qty_tick_base = int(1 / lot_size) if lot_size != 0 else 10000
|
||||
specs_dict[contract] = {
|
||||
"tick_unit": 1,
|
||||
"tick_base": tick_base,
|
||||
"cash_per_point": price_tick,
|
||||
"qty_tick_unit": 1,
|
||||
"qty_tick_base": qty_tick_base
|
||||
}
|
||||
found_contracts.add(contract)
|
||||
cursor = ""
|
||||
|
||||
while True:
|
||||
url = f"{base_url}&cursor={cursor}" if cursor else base_url
|
||||
logger.info(f"Fetching page with cursor: {cursor or 'initial'}")
|
||||
response = requests.get(url, timeout=5)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
|
||||
if data.get("retCode") != 0:
|
||||
logger.error(f"Futures API error: {data.get('retMsg', 'Unknown error')}")
|
||||
return specs_dict
|
||||
|
||||
result = data.get("result", {})
|
||||
symbols = result.get("list", [])
|
||||
logger.info(f"Loaded page with cursor: {cursor or 'initial'}, symbols: {len(symbols)}")
|
||||
|
||||
for item in symbols:
|
||||
contract = item.get("symbol")
|
||||
if contract in contracts:
|
||||
try:
|
||||
price_tick = float(item.get("priceFilter", {}).get("tickSize", 0.1))
|
||||
lot_size = float(item.get("lotSizeFilter", {}).get("qtyStep", 0.001))
|
||||
|
||||
if price_tick == 0 or lot_size == 0:
|
||||
logger.error(f"Invalid tick or lot size for {contract}: tickSize={price_tick}, qtyStep={lot_size}")
|
||||
continue
|
||||
|
||||
tick_base = min(int(1 / price_tick), 1000000) if price_tick != 0 else 10
|
||||
qty_tick_base = min(int(1 / lot_size), 1000000) if lot_size != 0 else 10000
|
||||
|
||||
specs_dict[contract] = {
|
||||
"tick_unit": 1,
|
||||
"tick_base": tick_base,
|
||||
"cash_per_point": price_tick,
|
||||
"qty_tick_unit": 1,
|
||||
"qty_tick_base": qty_tick_base
|
||||
}
|
||||
found_contracts.add(contract)
|
||||
logger.info(f"Fetched futures specs for {contract}: {specs_dict[contract]}")
|
||||
except (ValueError, TypeError) as e:
|
||||
logger.error(f"Failed to parse futures specs for {contract}: {e}")
|
||||
continue
|
||||
|
||||
cursor = result.get("nextPageCursor", "")
|
||||
if not cursor:
|
||||
logger.info("No more pages to fetch; pagination complete")
|
||||
break
|
||||
|
||||
for contract in contracts:
|
||||
if contract not in found_contracts:
|
||||
logger.warning(f"Contract {contract} not found in API response, using default specs")
|
||||
logger.warning(f"Futures contract {contract} not found in API response, using default specs")
|
||||
|
||||
logger.info(f"Returning specs for contracts: {list(specs_dict.keys())}")
|
||||
logger.debug(f"Final specs: {specs_dict}")
|
||||
return specs_dict
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to fetch contract specs: {e}", exc_info=True)
|
||||
logger.error(f"Failed to fetch futures contract specs: {e}", exc_info=True)
|
||||
return specs_dict
|
||||
|
||||
class BybitMdProvider(MdProvider):
|
||||
@@ -113,7 +148,7 @@ class BybitMdProvider(MdProvider):
|
||||
contracts = list(
|
||||
chain(*[map(lambda f: f"{f}{c}", self.futures) for c in self.currencies])
|
||||
)
|
||||
contract_specs = fetch_contract_specs(contracts, use_testnet=self.use_testnet)
|
||||
contract_specs = fetch_futures_specs(contracts, use_testnet=self.use_testnet)
|
||||
sec_list = PySecurityList()
|
||||
|
||||
for contract in contracts:
|
||||
|
||||
Reference in new issue
Block a user