Show premium paid on options position card and parse strike/type from instId when OKX omits them.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-08 10:40:07 +08:00
parent 43fa1ad1f3
commit 75a64c5d24
5 changed files with 64 additions and 3 deletions
+25 -2
View File
@@ -216,6 +216,17 @@ def inst_family_from_inst_id(inst_id: str) -> str | None:
return "-".join(parts[:-3])
def option_fields_from_inst_id(inst_id: str) -> tuple[str | None, float | None]:
"""从 instId 解析 optType 与 strike,如 ETH-USD_UM-260709-1700-P。"""
parts = (inst_id or "").strip().split("-")
if len(parts) < 2:
return None, None
tail = parts[-1].upper()
opt_type = tail if tail in ("C", "P") else None
strike = _safe_float(parts[-2]) if len(parts) >= 2 else None
return opt_type, strike
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:
@@ -778,6 +789,7 @@ def format_position_row(pos: dict[str, Any], ct_mult: float = 0.01) -> dict[str,
close_breakeven_idx,
expiry_breakeven_px,
idx_distance_to_be,
total_premium,
)
sheets = _safe_float(pos.get("pos")) or 0.0
@@ -786,8 +798,18 @@ def format_position_row(pos: dict[str, Any], ct_mult: float = 0.01) -> dict[str,
upl = _safe_float(pos.get("upl"))
upl_ratio = _safe_float(pos.get("uplRatio"))
idx_px = _safe_float(pos.get("idxPx"))
inst_id = str(pos.get("instId") or "")
opt_type = pos.get("optType")
strike = _safe_float(pos.get("stk"))
parsed_type, parsed_strike = option_fields_from_inst_id(inst_id)
if not opt_type:
opt_type = parsed_type
if strike is None:
strike = parsed_strike
eth_amount = round(abs(sheets) * ct_mult, 8)
premium_paid = (
round(total_premium(avg, eth_amount), 4) if avg is not None and eth_amount > 0 else None
)
delta_pa = _safe_float(pos.get("deltaPA"))
expiry_be = expiry_breakeven_px(
opt_type=str(opt_type or ""),
@@ -805,12 +827,13 @@ def format_position_row(pos: dict[str, Any], ct_mult: float = 0.01) -> dict[str,
ct_mult=ct_mult,
)
return {
"inst_id": pos.get("instId"),
"inst_id": inst_id or pos.get("instId"),
"pos": sheets,
"eth_amount": round(abs(sheets) * ct_mult, 8),
"eth_amount": eth_amount,
"avg_px": avg,
"mark_px": mark,
"idx_px": idx_px,
"premium_paid": premium_paid,
"upl": upl,
"upl_ratio_pct": round(upl_ratio * 100, 2) if upl_ratio is not None else None,
"exp_time": pos.get("expTime"),