fix: OKX option quote requires instFamily with instId

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-07 08:58:31 +08:00
parent 05586242f0
commit e1c14977e6
2 changed files with 77 additions and 40 deletions
+71 -39
View File
@@ -90,6 +90,35 @@ def _pos_side_from_position(pos: dict[str, Any] | None) -> str | None:
return "net" return "net"
def inst_family_from_inst_id(inst_id: str) -> str | None:
"""从 instId 解析 instFamily,如 ETH-USD_UM-260707-1790-C → ETH-USD_UM。"""
parts = (inst_id or "").strip().split("-")
if len(parts) < 4:
return None
return "-".join(parts[:-3])
def fetch_option_instrument_meta(ex: ccxt.okx, inst_id: str) -> dict[str, Any] | None:
family = inst_family_from_inst_id(inst_id)
if not family:
return None
try:
rows = ex.public_get_public_instruments(
{"instType": "OPTION", "instFamily": family, "instId": inst_id}
).get("data") or []
if rows and isinstance(rows[0], dict):
return rows[0]
rows = ex.public_get_public_instruments(
{"instType": "OPTION", "instFamily": family}
).get("data") or []
for r in rows:
if isinstance(r, dict) and str(r.get("instId")) == inst_id:
return r
except Exception:
return None
return None
def _extract_ccy_balance(balance: dict[str, Any], ccy: str) -> float | None: def _extract_ccy_balance(balance: dict[str, Any], ccy: str) -> float | None:
ccy = (ccy or "").upper() ccy = (ccy or "").upper()
if not isinstance(balance, dict): if not isinstance(balance, dict):
@@ -244,45 +273,48 @@ def build_option_chain(
def quote_option_contract(ex: ccxt.okx, inst_id: str) -> dict[str, Any]: def quote_option_contract(ex: ccxt.okx, inst_id: str) -> dict[str, Any]:
meta_rows = ex.public_get_public_instruments( inst_id = (inst_id or "").strip()
{"instType": "OPTION", "instId": inst_id} if not inst_id:
).get("data") or [] return {"ok": False, "msg": "缺少 inst_id"}
if not meta_rows: try:
return {"ok": False, "msg": "合约不存在"} meta = fetch_option_instrument_meta(ex, inst_id)
meta = meta_rows[0] if not meta:
t_rows = ex.public_get_market_ticker({"instId": inst_id}).get("data") or [] return {"ok": False, "msg": "合约不存在"}
t = t_rows[0] if t_rows else {} t_rows = ex.public_get_market_ticker({"instId": inst_id}).get("data") or []
ask = _safe_float(t.get("askPx")) t = t_rows[0] if t_rows else {}
bid = _safe_float(t.get("bidPx")) ask = _safe_float(t.get("askPx"))
if ask is None or bid is None: bid = _safe_float(t.get("bidPx"))
book_bid, book_ask = _fetch_book_bid_ask(ex, inst_id) if ask is None or bid is None:
if ask is None: book_bid, book_ask = _fetch_book_bid_ask(ex, inst_id)
ask = book_ask if ask is None:
if bid is None: ask = book_ask
bid = book_bid if bid is None:
mark = _safe_float(t.get("markPx")) bid = book_bid
tick_sz = meta.get("tickSz") mark = _safe_float(t.get("markPx"))
if ask is None and mark is not None: tick_sz = meta.get("tickSz")
ask = round_option_px(mark, tick_sz, "buy") if ask is None and mark is not None:
if bid is None and mark is not None: ask = round_option_px(mark, tick_sz, "buy")
bid = round_option_px(mark, tick_sz, "sell") if bid is None and mark is not None:
uly = str(meta.get("uly") or "") bid = round_option_px(mark, tick_sz, "sell")
idx = fetch_index_price(ex, uly) uly = str(meta.get("uly") or "")
return { idx = fetch_index_price(ex, uly)
"ok": True, return {
"inst_id": inst_id, "ok": True,
"meta": meta, "inst_id": inst_id,
"ask": ask, "meta": meta,
"bid": bid, "ask": ask,
"mark": mark, "bid": bid,
"index_px": idx, "mark": mark,
"ct_mult": _safe_float(meta.get("ctMult")) or 0.01, "index_px": idx,
"min_sz": int(_safe_float(meta.get("minSz")) or 1), "ct_mult": _safe_float(meta.get("ctMult")) or 0.01,
"tick_sz": meta.get("tickSz"), "min_sz": int(_safe_float(meta.get("minSz")) or 1),
"strike": _safe_float(meta.get("stk")), "tick_sz": tick_sz,
"opt_type": meta.get("optType"), "strike": _safe_float(meta.get("stk")),
"exp_time": meta.get("expTime"), "opt_type": meta.get("optType"),
} "exp_time": meta.get("expTime"),
}
except Exception as e:
return {"ok": False, "msg": str(e)}
def place_option_limit_order( def place_option_limit_order(
+6 -1
View File
@@ -5,7 +5,12 @@ from lib.options.options_pricing_lib import (
sheets_from_eth_amount, sheets_from_eth_amount,
total_premium, total_premium,
) )
from lib.exchange.okx_options_lib import format_option_px, round_option_px from lib.exchange.okx_options_lib import format_option_px, inst_family_from_inst_id, round_option_px
def test_inst_family_from_inst_id():
assert inst_family_from_inst_id("ETH-USD_UM-260707-1790-C") == "ETH-USD_UM"
assert inst_family_from_inst_id("BTC-USD-260925-60000-C") == "BTC-USD"
def test_round_option_px(): def test_round_option_px():