UPDATE: Added back Spots to Bybit MD.
This commit is contained in:
1 parent
217db51cbc
commit
1b5f1bfb27
1 file changed
+207
-65
@@ -27,12 +27,13 @@ import requests
|
||||
|
||||
logger = logging.getLogger("bybit_md_provider")
|
||||
|
||||
|
||||
def get_metadata(contracts: Iterable[str], use_testnet: bool = True) -> Dict[str, Dict]:
|
||||
"""Fetch metadata for all linear contracts and filter for specified contracts."""
|
||||
metadata_dict = {}
|
||||
try:
|
||||
host = "api-testnet.bybit.com" if use_testnet else "api.bybit.com"
|
||||
url = f"https://{host}/v5/market/instruments-info?category=linear&limit=1000"
|
||||
url = f"https://{host}/v5/market/instruments-info?category=linear&limit=600"
|
||||
logger.info(f"Querying Bybit API for metadata: {url}")
|
||||
response = requests.get(url, timeout=5)
|
||||
response.raise_for_status()
|
||||
@@ -150,7 +151,7 @@ def get_futures_contracts(base_symbols: list, use_testnet: bool = True) -> list:
|
||||
"""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?category=linear&limit=1000"
|
||||
url = f"https://{host}/v5/market/instruments-info?category=linear&limit=600"
|
||||
|
||||
logger.info(f"Querying all linear instruments: {url}")
|
||||
response = requests.get(url, timeout=5)
|
||||
@@ -169,11 +170,38 @@ def get_futures_contracts(base_symbols: list, use_testnet: bool = True) -> list:
|
||||
|
||||
return sorted(list(set(futures_contracts))) # Remove duplicates and sort
|
||||
|
||||
|
||||
|
||||
# Spot support starts here (updated)
|
||||
def get_spot_metadata(base_symbols: list, currencies: list, logger, use_testnet: bool = True) -> dict:
|
||||
"""Fetch metadata for spot contracts filtered by base symbols and supported currencies."""
|
||||
host = "api-testnet.bybit.com" if use_testnet else "api.bybit.com"
|
||||
url = f"https://{host}/v5/market/instruments-info?category=spot&limit=600"
|
||||
logger.info(f"Querying Bybit API for spot metadata with base symbols {base_symbols} and currencies {currencies}: {url}")
|
||||
response = requests.get(url, timeout=5)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
|
||||
spots_meta = {}
|
||||
if data.get("retCode") == 0:
|
||||
for item in data.get("result", {}).get("list", []):
|
||||
symbol = item.get("symbol")
|
||||
base_coin = item.get("baseCoin")
|
||||
quote_coin = item.get("quoteCoin")
|
||||
if base_coin in base_symbols and quote_coin in currencies:
|
||||
spots_meta[symbol] = item
|
||||
logger.info(f"Stored metadata for spot {symbol} with base {base_coin} and quote {quote_coin}")
|
||||
else:
|
||||
logger.error(f"Spot metadata API error: {data.get('retMsg', 'Unknown error')}")
|
||||
return spots_meta
|
||||
# Spot support ends here
|
||||
|
||||
class BybitMdProvider(MdProvider):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self._config: Optional[dict] = None
|
||||
self.ws_futures = None
|
||||
self.ws_spots = None # Add WebSocket for spots
|
||||
self.ws_thread: Optional[threading.Thread] = None
|
||||
self.bridge: Optional[MdBridge] = None
|
||||
self.stop_event = threading.Event()
|
||||
@@ -196,6 +224,12 @@ class BybitMdProvider(MdProvider):
|
||||
def futures(self):
|
||||
return self.config.get("futures", [])
|
||||
|
||||
# Spot support starts here
|
||||
@property
|
||||
def spots(self):
|
||||
return self.config.get("spots", [])
|
||||
# Spot support ends here
|
||||
|
||||
def start_impl(self, bridge: MdBridge):
|
||||
configure_logger(logger, "md", level="INFO", **self.config.get("logging", {})) # Enforce INFO level
|
||||
self.bridge = bridge
|
||||
@@ -208,88 +242,57 @@ class BybitMdProvider(MdProvider):
|
||||
# Fetch metadata for all specified futures contracts
|
||||
metadata = get_metadata(futures_contracts, self.use_testnet)
|
||||
logger.info(f"Fetched metadata for {len(metadata)} futures contracts")
|
||||
logger.info(f"metadata: {metadata}")
|
||||
|
||||
# Fetch specs
|
||||
futures_specs = fetch_futures_specs(futures_contracts, self.use_testnet)
|
||||
logger.info(f"Fetched futures specs for {len(futures_specs)} contracts")
|
||||
logger.info(f"futures_specs: {futures_specs}")
|
||||
|
||||
sec_list_futures = PySecurityList()
|
||||
|
||||
for contract in futures_contracts:
|
||||
meta = metadata.get(contract, {})
|
||||
logger.info(f"Metadata for {contract}: {meta}") # Log metadata to verify contents
|
||||
logger.info(f"Metadata for {contract}: {meta}")
|
||||
if not meta:
|
||||
logger.warning(f"No metadata found for contract: {contract}, using defaults")
|
||||
continue
|
||||
|
||||
try:
|
||||
# Extract details from metadata
|
||||
symbol = meta.get("symbol", contract)
|
||||
logger.info(f"Security creation for {contract}: symbol={symbol}")
|
||||
base_asset = meta.get("baseCoin")
|
||||
logger.info(f"Security creation for {contract}: base_asset from metadata={base_asset}")
|
||||
quote_currency = meta.get("quoteCoin")
|
||||
logger.info(f"Security creation for {contract}: quote_currency from metadata={quote_currency}")
|
||||
expiration = get_expiration(meta)
|
||||
logger.info(f"Security creation for {contract}: expiration={expiration}")
|
||||
|
||||
asset = base_asset
|
||||
logger.info(f"Security creation for {contract}: using asset={asset}")
|
||||
group = base_asset
|
||||
logger.info(f"Security creation for {contract}: using group={group}")
|
||||
currency = quote_currency
|
||||
logger.info(f"Security creation for {contract}: using currency={currency}")
|
||||
|
||||
specs = futures_specs.get(contract, {}).get("specs", {})
|
||||
if not specs:
|
||||
logger.warning(f"No specs found for contract: {contract}, using defaults")
|
||||
specs = {"tick_unit": 1, "tick_base": 10, "cash_per_point": 0.1, "qty_tick_unit": 1, "qty_tick_base": 10000}
|
||||
|
||||
try:
|
||||
qty_info = get_quantity_info(
|
||||
tick_unit=specs.get("qty_tick_unit", 1),
|
||||
tick_base=specs.get("qty_tick_base", 10000)
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get quantity info for futures contract: {contract}, error: {e}")
|
||||
continue
|
||||
|
||||
try:
|
||||
spec = get_contract_spec(
|
||||
price_info=get_price_info(
|
||||
tick_unit=specs.get("tick_unit", 1),
|
||||
tick_base=specs.get("tick_base", 10),
|
||||
cash_per_point=specs.get("cash_per_point", 0.1)
|
||||
),
|
||||
qty_info=qty_info
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get contract spec for futures contract: {contract}, error: {e}")
|
||||
continue
|
||||
|
||||
try:
|
||||
sec = get_security(
|
||||
venue="Bybit",
|
||||
name=symbol,
|
||||
asset=asset,
|
||||
group=group,
|
||||
currency=currency,
|
||||
expiration=expiration,
|
||||
option_kind=None,
|
||||
strike=None,
|
||||
spec=spec,
|
||||
)
|
||||
sec_list_futures.append(sec)
|
||||
logger.info(f"Created FUTURE security: name={symbol}, venue=Bybit, asset={asset}, group={group}, currency={currency}, expiration={expiration}, option_kind={None}, strike={None}, spec={specs}")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to create FUTURE security for contract: {contract}, error: {e}", exc_info=True)
|
||||
continue
|
||||
|
||||
qty_info = get_quantity_info(
|
||||
tick_unit=specs.get("qty_tick_unit", 1),
|
||||
tick_base=specs.get("qty_tick_base", 10000)
|
||||
)
|
||||
spec = get_contract_spec(
|
||||
price_info=get_price_info(
|
||||
tick_unit=specs.get("tick_unit", 1),
|
||||
tick_base=specs.get("tick_base", 10),
|
||||
cash_per_point=specs.get("cash_per_point", 1)
|
||||
),
|
||||
qty_info=qty_info
|
||||
)
|
||||
sec = get_security(
|
||||
venue="Bybit",
|
||||
name=symbol,
|
||||
asset=asset,
|
||||
group=group,
|
||||
currency=currency,
|
||||
expiration=expiration,
|
||||
option_kind=None,
|
||||
strike=None,
|
||||
spec=spec,
|
||||
)
|
||||
sec_list_futures.append(sec)
|
||||
logger.info(f"Created FUTURE security: name={symbol}, venue=Bybit, asset={asset}, group={group}, currency={currency}, expiration={expiration}, spec={specs}")
|
||||
except Exception as e:
|
||||
logger.error(f"Error processing metadata for {contract}: {e}", exc_info=True)
|
||||
continue
|
||||
logger.error(f"Failed to create FUTURE security for contract: {contract}, error: {e}", exc_info=True)
|
||||
|
||||
combined_sec_list = PySecurityList()
|
||||
for sec in sec_list_futures:
|
||||
@@ -297,7 +300,114 @@ class BybitMdProvider(MdProvider):
|
||||
bridge.on_securities(combined_sec_list)
|
||||
logger.info(f"Sent securities list: {len(sec_list_futures)} futures")
|
||||
|
||||
# Spot support starts here
|
||||
# Get spot metadata
|
||||
spots_meta = get_spot_metadata(self.spots, self.currencies, logger, self.use_testnet)
|
||||
logger.info(f"Fetched metadata for {len(spots_meta)} spot contracts")
|
||||
|
||||
# Filter spot contracts based on base symbols
|
||||
spot_contracts = [symbol for symbol, meta in spots_meta.items() if meta.get("baseCoin") in self.spots]
|
||||
logger.info(f"Filtered to {len(spot_contracts)} spot contracts for base symbols {self.spots}")
|
||||
|
||||
# Fetch spot specs, mirroring fetch_futures_specs logic
|
||||
default_specs = {
|
||||
"tick_unit": 1,
|
||||
"tick_base": 10,
|
||||
"cash_per_point": 1,
|
||||
"qty_tick_unit": 1,
|
||||
"qty_tick_base": 10000
|
||||
}
|
||||
spot_specs = {symbol: {"specs": default_specs} for symbol in spot_contracts}
|
||||
found_spot_contracts = set()
|
||||
|
||||
for symbol, meta in spots_meta.items():
|
||||
if symbol in spot_contracts:
|
||||
try:
|
||||
price_tick = float(meta.get("priceFilter", {}).get("tickSize", 0.1))
|
||||
lot_size = float(meta.get("lotSizeFilter", {}).get("qtyStep", 0.001))
|
||||
|
||||
if price_tick == 0 or lot_size == 0:
|
||||
logger.error(f"Invalid tick or lot size for {symbol}: tickSize={price_tick}, qtyStep={lot_size}")
|
||||
continue
|
||||
|
||||
tick_base = min(int(1 / price_tick), 1000000) if price_tick != 0 else 10
|
||||
if lot_size >= 1:
|
||||
qty_tick_unit = lot_size
|
||||
qty_tick_base = 1
|
||||
else:
|
||||
qty_tick_unit = 1
|
||||
qty_tick_base = min(int(1 / lot_size), 1000000) if lot_size != 0 else 10000
|
||||
|
||||
spot_specs[symbol] = {
|
||||
"specs": {
|
||||
"tick_unit": 1,
|
||||
"tick_base": tick_base,
|
||||
"cash_per_point": 1,
|
||||
"qty_tick_unit": qty_tick_unit,
|
||||
"qty_tick_base": qty_tick_base
|
||||
}
|
||||
}
|
||||
found_spot_contracts.add(symbol)
|
||||
logger.info(f"Fetched spot specs for {symbol}: {spot_specs[symbol]['specs']}")
|
||||
except (ValueError, TypeError) as e:
|
||||
logger.error(f"Failed to parse spot specs for {symbol}: {e}")
|
||||
continue
|
||||
|
||||
for symbol in spot_contracts:
|
||||
if symbol not in found_spot_contracts:
|
||||
logger.warning(f"Spot contract {symbol} not found in API response, using default specs")
|
||||
|
||||
logger.info(f"Returning specs for spot contracts: {list(spot_specs.keys())}")
|
||||
logger.debug(f"Final spot specs: {spot_specs}")
|
||||
|
||||
# Create spot securities
|
||||
sec_list_spots = PySecurityList()
|
||||
for symbol in spot_contracts:
|
||||
meta = spots_meta[symbol]
|
||||
logger.info(f"Security creation for {symbol}: {meta}")
|
||||
try:
|
||||
base_asset = meta.get("baseCoin")
|
||||
quote_currency = meta.get("quoteCoin")
|
||||
specs = spot_specs[symbol]["specs"]
|
||||
|
||||
qty_info = get_quantity_info(
|
||||
tick_unit=specs.get("qty_tick_unit", 1),
|
||||
tick_base=specs.get("qty_tick_base", 10000)
|
||||
)
|
||||
spec = get_contract_spec(
|
||||
price_info=get_price_info(
|
||||
tick_unit=specs.get("tick_unit", 1),
|
||||
tick_base=specs.get("tick_base", 10),
|
||||
cash_per_point=specs.get("cash_per_point", 1)
|
||||
),
|
||||
qty_info=qty_info
|
||||
)
|
||||
sec = get_security(
|
||||
venue="Bybit",
|
||||
name=symbol,
|
||||
asset=base_asset,
|
||||
group=base_asset,
|
||||
currency=quote_currency,
|
||||
expiration=None,
|
||||
option_kind=None,
|
||||
strike=None,
|
||||
spec=spec,
|
||||
)
|
||||
sec_list_spots.append(sec)
|
||||
logger.info(f"Created SPOT security: name={symbol}, asset={base_asset}, currency={quote_currency}")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to create SPOT security for {symbol}: {e}", exc_info=True)
|
||||
|
||||
# Combine spot and futures securities
|
||||
for sec in sec_list_spots:
|
||||
combined_sec_list.append(sec)
|
||||
bridge.on_securities(combined_sec_list)
|
||||
logger.info(f"Sent securities list: {len(sec_list_spots)} spots + {len(sec_list_futures)} futures")
|
||||
# Spot support ends here
|
||||
|
||||
|
||||
try:
|
||||
# WebSocket for futures
|
||||
self.ws_futures = WebSocket(
|
||||
testnet=self.use_testnet,
|
||||
channel_type="linear",
|
||||
@@ -314,9 +424,26 @@ class BybitMdProvider(MdProvider):
|
||||
logger.info(f"Subscribed to orderbook.50.{contract} (futures)")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to subscribe to orderbook for {contract} (futures): {e}")
|
||||
logger.info("Subscribed to orderbook channels on Bybit futures WS.")
|
||||
for t in threading.enumerate():
|
||||
logger.debug(f"Thread (start): Name={t.name}, Alive={t.is_alive()}, Daemon={t.daemon}, Target={t._target}")
|
||||
|
||||
# WebSocket for spots
|
||||
self.ws_spots = WebSocket(
|
||||
testnet=self.use_testnet,
|
||||
channel_type="spot",
|
||||
ping_interval=20,
|
||||
)
|
||||
logger.info("Connecting to Bybit WebSocket (spots)")
|
||||
for contract in spot_contracts:
|
||||
try:
|
||||
self.ws_spots.orderbook_stream(
|
||||
symbol=contract,
|
||||
depth=50,
|
||||
callback=lambda data: self._on_book_update(data, source="spots")
|
||||
)
|
||||
logger.info(f"Subscribed to orderbook.50.{contract} (spots)")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to subscribe to orderbook for {contract} (spots): {e}")
|
||||
|
||||
logger.info("Subscribed to orderbook channels on Bybit futures and spots WS.")
|
||||
while not self.stop_event.is_set():
|
||||
time.sleep(1)
|
||||
except KeyboardInterrupt:
|
||||
@@ -356,7 +483,7 @@ class BybitMdProvider(MdProvider):
|
||||
sorted_asks += [(np.float64('nan'), np.float64('nan'))]
|
||||
logger.info(f"📘 BOOK DATA for {contract} (source={source}): bids={sorted_bids} asks={sorted_asks}")
|
||||
book = PyBook()
|
||||
market_type = "FUTURE"
|
||||
market_type = "FUTURE" if source == "futures" else "SPOT" # Dynamically set market type
|
||||
logger.debug(f"Assigned market_type={market_type} for contract={contract} (source={source})")
|
||||
book.set_data(
|
||||
contract,
|
||||
@@ -391,7 +518,18 @@ class BybitMdProvider(MdProvider):
|
||||
t.join(timeout=1.0)
|
||||
except Exception as e:
|
||||
logger.error(f"Error while closing WebSocket (futures): {e}", exc_info=True)
|
||||
if self.ws_spots: # Add cleanup for spots WebSocket
|
||||
try:
|
||||
self.ws_spots.exit()
|
||||
logger.info("WebSocket (spots) exit called")
|
||||
for t in threading.enumerate():
|
||||
if t.name in ['Thread-3 (<lambda>)', 'Thread-4 (_send_ping)'] and t.is_alive():
|
||||
logger.info(f"Joining pybit thread: {t.name} (spots)")
|
||||
t.join(timeout=1.0)
|
||||
except Exception as e:
|
||||
logger.error(f"Error while closing WebSocket (spots): {e}", exc_info=True)
|
||||
self.ws_futures = None
|
||||
self.ws_spots = None
|
||||
if self.ws_thread:
|
||||
self.ws_thread.join()
|
||||
self.bridge = None
|
||||
@@ -409,6 +547,7 @@ if __name__ == "__main__":
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--currencies", nargs="+")
|
||||
ap.add_argument("--futures", nargs="+")
|
||||
ap.add_argument("--spots", nargs="+")
|
||||
log_levels = list(logging._nameToLevel)
|
||||
ap.add_argument("--console-level", choices=log_levels)
|
||||
ap.add_argument("--file-level", choices=log_levels)
|
||||
@@ -417,6 +556,7 @@ if __name__ == "__main__":
|
||||
ap.set_defaults(
|
||||
currencies=None,
|
||||
futures=None,
|
||||
spots=None,
|
||||
console_level=None,
|
||||
file_level=None,
|
||||
use_testnet=None,
|
||||
@@ -430,6 +570,8 @@ if __name__ == "__main__":
|
||||
config["currencies"] = args.currencies
|
||||
if args.futures:
|
||||
config["futures"] = args.futures
|
||||
if args.spots:
|
||||
config["spots"] = args.spots
|
||||
logging_overrides = {}
|
||||
if args.console_level:
|
||||
logging_overrides["console_level"] = args.console_level
|
||||
@@ -442,4 +584,4 @@ if __name__ == "__main__":
|
||||
md_bridge = FakeMdBridge(logger)
|
||||
md_pro.start(md_bridge)
|
||||
while True:
|
||||
time.sleep(1)
|
||||
time.sleep(1)
|
||||
Reference in new issue
Block a user