Fix Binance option top-of-book via depth limit and ticker fallback.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-25 12:08:33 +08:00
parent 78fd046fb6
commit d701d30c04
4 changed files with 134 additions and 12 deletions
+30 -2
View File
@@ -84,8 +84,11 @@ class BinanceExchange:
ids = [i for i in inst_ids if i]
for inst in ids:
try:
bids, asks, ts = self.rest.fetch_books(inst, sz=5)
self.cache.upsert_book(inst, bids=bids, asks=asks, ts_ms=ts)
bids, asks, ts = self.rest.fetch_books(inst, sz=10)
if bids or asks:
self.cache.upsert_book(inst, bids=bids, asks=asks, ts_ms=ts)
else:
logger.warning("binance warm book empty: %s", inst)
except Exception as e:
logger.warning("binance warm book %s failed: %s", inst, e)
try:
@@ -111,7 +114,32 @@ class BinanceExchange:
def quote(self, inst_id: str) -> Quote | None:
return self.cache.get(inst_id)
def _refresh_quote_if_stale(self, inst_id: str) -> None:
if not inst_id:
return
q = self.cache.get(inst_id)
if q is not None and (q.bid is not None or q.ask is not None):
return
try:
bids, asks, ts = self.rest.fetch_books(inst_id, sz=10)
if bids or asks:
self.cache.upsert_book(inst_id, bids=bids, asks=asks, ts_ms=ts)
except Exception as e:
logger.debug("binance refresh quote %s: %s", inst_id, e)
def snapshot(self, perp_inst_id: str) -> MarketSnapshot:
# 期权 WS 偶发无推送时,用 REST/ticker 补买卖一,避免 UI 一直是 -
self._refresh_quote_if_stale(perp_inst_id)
pair = None
try:
# BookCache 无公开 getter;从 snapshot 前先按 pair 刷新
with self.cache._lock: # noqa: SLF001
pair = self.cache._pair
except Exception:
pair = None
if pair is not None:
self._refresh_quote_if_stale(pair.call_inst_id)
self._refresh_quote_if_stale(pair.put_inst_id)
return self.cache.snapshot(perp_inst_id)
def snapshot_dict(self, perp_inst_id: str) -> dict[str, Any]: