Add pre-buy expiry breakeven columns to options chain table.

Show estimated expiry balance and distance from index in the chain list and order panel using ask price before opening a position.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-07 16:21:22 +08:00
parent 3570a6900e
commit 99f13817f8
6 changed files with 96 additions and 8 deletions
+30 -4
View File
@@ -9,7 +9,13 @@ from typing import Any, Callable
import ccxt
from lib.options.options_pricing_lib import is_shallow_itm, option_moneyness, option_moneyness_label
from lib.options.options_pricing_lib import (
expiry_breakeven_from_ask,
idx_distance_to_be,
is_shallow_itm,
option_moneyness,
option_moneyness_label,
)
_OKX_OPTION_ERR_ZH: dict[str, str] = {
"51018": "期权账户不能持有净空头头寸",
@@ -331,8 +337,15 @@ def build_option_chain(
t = tickers.get(inst_id) or {}
ask = _safe_float(t.get("askPx"))
bid = _safe_float(t.get("bidPx"))
if ask is None and bid is None:
mark = _safe_float(t.get("markPx"))
if ask is None and bid is None and mark is None:
continue
expiry_be = expiry_breakeven_from_ask(
opt_type=opt_type,
strike=strike,
ask_px=ask,
mark_px=mark,
)
mny = option_moneyness(opt_type=opt_type, strike=strike, index_px=idx)
exp_key = str(exp_ms)
expiries.setdefault(exp_key, []).append(
@@ -343,6 +356,9 @@ def build_option_chain(
"exp_time": exp_ms,
"ask": ask,
"bid": bid,
"mark_px": mark,
"expiry_be_px": expiry_be,
"dist_expiry_be": idx_distance_to_be(idx, expiry_be),
"moneyness": mny,
"moneyness_label": option_moneyness_label(mny),
"ct_mult": _safe_float(meta.get("ctMult")) or 0.01,
@@ -383,6 +399,14 @@ def quote_option_contract(ex: ccxt.okx, inst_id: str) -> dict[str, Any]:
bid = round_option_px(mark, tick_sz, "sell")
uly = str(meta.get("uly") or "")
idx = fetch_index_price(ex, uly)
opt_type = meta.get("optType")
strike = _safe_float(meta.get("stk"))
expiry_be = expiry_breakeven_from_ask(
opt_type=str(opt_type or ""),
strike=strike,
ask_px=ask,
mark_px=mark,
)
return {
"ok": True,
"inst_id": inst_id,
@@ -391,11 +415,13 @@ def quote_option_contract(ex: ccxt.okx, inst_id: str) -> dict[str, Any]:
"bid": bid,
"mark": mark,
"index_px": idx,
"expiry_be_px": expiry_be,
"dist_expiry_be": idx_distance_to_be(idx, expiry_be),
"ct_mult": _safe_float(meta.get("ctMult")) or 0.01,
"min_sz": int(_safe_float(meta.get("minSz")) or 1),
"tick_sz": tick_sz,
"strike": _safe_float(meta.get("stk")),
"opt_type": meta.get("optType"),
"strike": strike,
"opt_type": opt_type,
"exp_time": meta.get("expTime"),
}
except Exception as e: