ADD: GATEIO md_provider with dynamic contract specs 1 REST call per Contract
This commit is contained in:
1 parent
f353a8e9fd
commit
ae776cf75f
1 file changed
+52
-23
@@ -1,20 +1,15 @@
|
||||
##############specs dynamic 1 at a time
|
||||
import json
|
||||
import time
|
||||
import logging
|
||||
import sys
|
||||
import threading
|
||||
import requests # Added for REST API calls
|
||||
from websocket import create_connection, WebSocketConnectionClosedException
|
||||
from typing import Optional, Any
|
||||
import python_md_module as pymd
|
||||
from config import WS_URL, CONTRACTS
|
||||
from py_md_utils import (
|
||||
get_contract_spec,
|
||||
get_price_info,
|
||||
get_quantity_info,
|
||||
get_security,
|
||||
get_book_side,
|
||||
)
|
||||
|
||||
from py_md_utils import get_contract_spec, get_price_info, get_quantity_info, get_security, get_book_side
|
||||
|
||||
# Logging setup
|
||||
LOG_PATH = "/home/bts/sparkdata/data/gateio_md_provider.log"
|
||||
@@ -23,15 +18,42 @@ logging.basicConfig(
|
||||
level=logging.DEBUG,
|
||||
format="%(asctime)s|%(levelname)s|%(message)s",
|
||||
)
|
||||
console = logging.StreamHandler(sys.stdout)#sends logs to the terminal too
|
||||
console.setLevel(logging.INFO)#only sends info and higher (np debug)
|
||||
formatter = logging.Formatter("%(asctime)s|%(levelname)s|%(message)s")#format for terminal logs
|
||||
console.setFormatter(formatter)#attaching to console handler to root logger, so both are written
|
||||
console = logging.StreamHandler(sys.stdout)
|
||||
console.setLevel(logging.INFO)
|
||||
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."""
|
||||
default_specs = {
|
||||
"tick_unit": 1,
|
||||
"tick_base": 10, # 0.1 USDT
|
||||
"cash_per_point": 0.1,
|
||||
"qty_tick_unit": 1,
|
||||
"qty_tick_base": 10000 # 0.0001 BTC
|
||||
}
|
||||
try:
|
||||
url = f"https://api.gateio.ws/api/v4/futures/usdt/contracts/{contract}"
|
||||
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
|
||||
}
|
||||
except Exception as e:
|
||||
logging.error(f"Failed to fetch specs for {contract}: {e}", exc_info=True)
|
||||
return default_specs
|
||||
|
||||
class GateIoMdProvider(pymd.MdProvider):
|
||||
def __init__(self):
|
||||
@@ -39,6 +61,7 @@ class GateIoMdProvider(pymd.MdProvider):
|
||||
self.ws = None
|
||||
self.stop_event = threading.Event()
|
||||
self.sink = None
|
||||
self.ping_thread = None
|
||||
|
||||
def send_ping(self):
|
||||
"""Send ping messages every 15 seconds to keep connection alive."""
|
||||
@@ -64,20 +87,27 @@ class GateIoMdProvider(pymd.MdProvider):
|
||||
self.ws.settimeout(5)
|
||||
logging.info("Connected.")
|
||||
|
||||
|
||||
# Start ping thread
|
||||
self.ping_thread = threading.Thread(target=self.send_ping, daemon=True)
|
||||
self.ping_thread.start()
|
||||
logging.info("Started ping thread")
|
||||
|
||||
|
||||
# 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)
|
||||
spec = get_contract_spec(
|
||||
price_info=get_price_info(tick_unit=1, tick_base=10, cash_per_point=0.1), # BTC_USDT: 0.1 USDT tick
|
||||
qty_info=get_quantity_info(tick_unit=1, tick_base=10000), # 1 contract = 0.0001 BTC
|
||||
price_info=get_price_info(
|
||||
tick_unit=specs["tick_unit"],
|
||||
tick_base=specs["tick_base"],
|
||||
cash_per_point=specs["cash_per_point"]
|
||||
),
|
||||
qty_info=get_quantity_info(
|
||||
tick_unit=specs["qty_tick_unit"],
|
||||
tick_base=specs["qty_tick_base"]
|
||||
)
|
||||
)
|
||||
sec = get_security(
|
||||
venue="GateIO",
|
||||
@@ -91,14 +121,14 @@ class GateIoMdProvider(pymd.MdProvider):
|
||||
spec=spec,
|
||||
)
|
||||
sec_list.append(sec)
|
||||
logging.info(f"Created security: {contract}")
|
||||
logging.info(f"Created security: {contract} with specs: {specs}")
|
||||
sink.on_securities(sec_list)
|
||||
logging.info("Sent securities list--sink.on_securities called.")
|
||||
|
||||
# 2) Subscribe to order books
|
||||
for contract in CONTRACTS:
|
||||
sub_msg = {
|
||||
"time": int(time.time() * 1000), # Milliseconds
|
||||
"time": int(time.time() * 1000),
|
||||
"channel": "futures.order_book",
|
||||
"event": "subscribe",
|
||||
"payload": [contract, "5", "0"]
|
||||
@@ -135,7 +165,7 @@ class GateIoMdProvider(pymd.MdProvider):
|
||||
# Handle order book updates
|
||||
if channel == "futures.order_book" and event in ["all", "update"]:
|
||||
logging.info(f"Handling '{event}' event")
|
||||
payload = data.get("result", {})#result has the t, contract, id, bids, asks and l
|
||||
payload = data.get("result", {})
|
||||
if isinstance(payload, dict) and ("bids" in payload or "asks" in payload):
|
||||
contract = payload.get("contract", "<no-contract>")
|
||||
bids = [(float(l["p"]), float(l["s"])) for l in payload.get("bids", [])]
|
||||
@@ -181,4 +211,3 @@ class GateIoMdProvider(pymd.MdProvider):
|
||||
|
||||
self.sink = None
|
||||
logging.info("✔ Stopped GateIoMdProvider.")
|
||||
|
||||
Reference in new issue
Block a user