FIX: Reduced REST calls.
This commit is contained in:
1 parent
0cb3cadc11
commit
3739532947
1 file changed
+16
-20
@@ -175,29 +175,25 @@ def get_expiration(contract: Dict) -> str:
|
||||
return PERP_EXP_STRING # Default for perpetuals
|
||||
|
||||
def get_futures_contracts(base_symbols: list, use_testnet: bool = True) -> list:
|
||||
"""Query Bybit API for linear instruments and return full security names for given base coins, filtering for LinearFutures and LinearPerpetual."""
|
||||
"""Query Bybit API for all linear instruments and filter by base symbols and contract type."""
|
||||
futures_contracts = []
|
||||
host = "api-testnet.bybit.com" if use_testnet else "api.bybit.com"
|
||||
url = f"https://{host}/v5/market/instruments-info"
|
||||
url = f"https://{host}/v5/market/instruments-info?category=linear&limit=1000"
|
||||
|
||||
logger.info(f"Querying futures contracts for base symbols: {base_symbols}")
|
||||
for symbol in base_symbols:
|
||||
params = {
|
||||
"category": "linear",
|
||||
"baseCoin": symbol
|
||||
}
|
||||
logger.info(f"Querying for baseCoin={symbol}: {url}")
|
||||
response = requests.get(url, params=params, timeout=5)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
if data.get("retCode") == 0 and data.get("result", {}).get("list"):
|
||||
for item in data.get("result", {}).get("list", []):
|
||||
contract_type = item.get("contractType")
|
||||
if contract_type in ["LinearFutures", "LinearPerpetual"]:
|
||||
futures_contracts.append(item.get("symbol"))
|
||||
logger.info(f"Found {len(futures_contracts)} compatible instruments for {symbol}")
|
||||
else:
|
||||
logger.warning(f"No instruments found for baseCoin={symbol}: {data.get('retMsg', 'Unknown error')}")
|
||||
logger.info(f"Querying all linear instruments: {url}")
|
||||
response = requests.get(url, timeout=5)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
if data.get("retCode") == 0 and data.get("result", {}).get("list"):
|
||||
logger.info(f"Retrieved {len(data['result']['list'])} linear instruments")
|
||||
for item in data.get("result", {}).get("list", []):
|
||||
base_coin = item.get("baseCoin")
|
||||
contract_type = item.get("contractType")
|
||||
if base_coin in base_symbols and contract_type in ["LinearFutures", "LinearPerpetual"]:
|
||||
futures_contracts.append(item.get("symbol"))
|
||||
logger.info(f"Filtered to {len(futures_contracts)} compatible instruments for base symbols {base_symbols}")
|
||||
else:
|
||||
logger.warning(f"Failed to retrieve instruments: {data.get('retMsg', 'Unknown error')}")
|
||||
|
||||
return sorted(list(set(futures_contracts))) # Remove duplicates and sort
|
||||
|
||||
|
||||
Reference in new issue
Block a user