ADD: GATEIO md_provider with dynamic contract specs using Batch REST call
This commit is contained in:
1 parent
ae776cf75f
commit
29c951fbca
1 file changed
+28
-24
@@ -1,10 +1,9 @@
|
||||
##############specs dynamic 1 at a time
|
||||
import json
|
||||
import time
|
||||
import logging
|
||||
import sys
|
||||
import threading
|
||||
import requests # Added for REST API calls
|
||||
import requests
|
||||
from websocket import create_connection, WebSocketConnectionClosedException
|
||||
from typing import Optional, Any
|
||||
import python_md_module as pymd
|
||||
@@ -24,36 +23,39 @@ formatter = logging.Formatter("%(asctime)s|%(levelname)s|%(message)s")
|
||||
console.setFormatter(formatter)
|
||||
logging.getLogger().addHandler(console)
|
||||
|
||||
def fetch_contract_specs(contract: str) -> dict:
|
||||
"""Fetch contract specifications from Gate.io REST API."""
|
||||
def fetch_contract_specs() -> dict:
|
||||
"""Fetch specs for all contracts in one request."""
|
||||
default_specs = {
|
||||
"tick_unit": 1,
|
||||
"tick_base": 10, # 0.1 USDT
|
||||
"tick_base": 10,
|
||||
"cash_per_point": 0.1,
|
||||
"qty_tick_unit": 1,
|
||||
"qty_tick_base": 10000 # 0.0001 BTC
|
||||
"qty_tick_base": 10000
|
||||
}
|
||||
specs_dict = {contract: default_specs for contract in CONTRACTS}
|
||||
try:
|
||||
url = f"https://api.gateio.ws/api/v4/futures/usdt/contracts/{contract}"
|
||||
url = "https://api.gateio.ws/api/v4/futures/usdt/contracts"
|
||||
response = requests.get(url, timeout=5)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
price_tick = float(data.get("price_tick", "0.1"))
|
||||
contract_size = float(data.get("contract_size", "0.0001"))
|
||||
# Calculate tick_base: price_tick = tick_unit / tick_base
|
||||
tick_base = int(1 / price_tick) if price_tick != 0 else 10
|
||||
# Calculate qty_tick_base: contract_size = qty_tick_unit / qty_tick_base
|
||||
qty_tick_base = int(1 / contract_size) if contract_size != 0 else 10000
|
||||
return {
|
||||
"tick_unit": 1,
|
||||
"tick_base": tick_base,
|
||||
"cash_per_point": price_tick, # Use price_tick as cash_per_point
|
||||
"qty_tick_unit": 1,
|
||||
"qty_tick_base": qty_tick_base
|
||||
}
|
||||
for item in data:
|
||||
contract = item.get("name")
|
||||
if contract in CONTRACTS:
|
||||
price_tick = float(item.get("price_tick", "0.1"))
|
||||
contract_size = float(item.get("contract_size", "0.0001"))
|
||||
tick_base = int(1 / price_tick) if price_tick != 0 else 10
|
||||
qty_tick_base = int(1 / contract_size) if contract_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
|
||||
}
|
||||
return specs_dict
|
||||
except Exception as e:
|
||||
logging.error(f"Failed to fetch specs for {contract}: {e}", exc_info=True)
|
||||
return default_specs
|
||||
logging.error(f"Failed to fetch batch specs: {e}", exc_info=True)
|
||||
return specs_dict
|
||||
|
||||
class GateIoMdProvider(pymd.MdProvider):
|
||||
def __init__(self):
|
||||
@@ -92,12 +94,14 @@ class GateIoMdProvider(pymd.MdProvider):
|
||||
self.ping_thread.start()
|
||||
logging.info("Started ping thread")
|
||||
|
||||
# Fetch all contract specs at once
|
||||
contract_specs = fetch_contract_specs()
|
||||
|
||||
# 1) Send security info
|
||||
sec_list = pymd.PySecurityList()
|
||||
for contract in CONTRACTS:
|
||||
asset, quote = contract.split("_")
|
||||
# Fetch contract specs dynamically
|
||||
specs = fetch_contract_specs(contract)
|
||||
specs = contract_specs[contract]
|
||||
spec = get_contract_spec(
|
||||
price_info=get_price_info(
|
||||
tick_unit=specs["tick_unit"],
|
||||
|
||||
Reference in new issue
Block a user