修复币本位期权残档判定:内在价值按币报价(S-K)/S,避免与美元点差混比误杀有效买一

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-08-20 16:21:54 +08:00
parent b0c331aa26
commit a7bec5e121
6 changed files with 115 additions and 20 deletions
+7 -1
View File
@@ -58,7 +58,13 @@ def _pos_close_refs(ex: Any, pos: dict[str, Any], quote: dict[str, Any] | None =
if strike is None:
strike = ps
idx = _safe_float(pos.get("idxPx")) or _safe_float((quote or {}).get("index_px"))
return close_ref_prices(mark_px=mark, opt_type=str(opt_type or ""), strike=strike, index_px=idx)
return close_ref_prices(
mark_px=mark,
opt_type=str(opt_type or ""),
strike=strike,
index_px=idx,
inst_id=inst_id,
)
def _avail_sheets(pos: dict[str, Any]) -> int:
+2
View File
@@ -40,6 +40,8 @@ def attach_close_preview(
row.get("opt_type") or row.get("optType"),
_safe_float(row.get("strike") or row.get("stk")),
_safe_float(row.get("idx_px") or row.get("idxPx")),
inst_id=inst_id,
margin_mode=row.get("margin_mode"),
)
# 与实盘一致:只按买一估算本轮可平
preview = estimate_close_by_bids(
+64 -7
View File
@@ -64,7 +64,46 @@ def _safe_px(v: Any) -> float | None:
return x if x > 0 else None
def intrinsic_px_per_unit(opt_type: str | None, strike: float | None, index_px: float | None) -> float | None:
def _quote_in_coin_from_context(
*,
quote_in_coin: bool | None = None,
inst_id: str | None = None,
margin_mode: str | None = None,
) -> bool:
"""币本位(ETH-USD/BTC-USD)权利金按币报价;USDC(USD_UM)按美元点差."""
if quote_in_coin is not None:
return bool(quote_in_coin)
if inst_id:
try:
from lib.options.options_margin_mode_lib import MODE_COIN, margin_mode_from_inst_id
return margin_mode_from_inst_id(inst_id) == MODE_COIN
except Exception:
pass
if margin_mode is not None:
try:
from lib.options.options_margin_mode_lib import is_coin_margin_mode
return is_coin_margin_mode(margin_mode)
except Exception:
return str(margin_mode).strip().lower() in ("coin", "coin_margin", "crypto")
return False
def intrinsic_px_per_unit(
opt_type: str | None,
strike: float | None,
index_px: float | None,
*,
quote_in_coin: bool | None = None,
inst_id: str | None = None,
margin_mode: str | None = None,
) -> float | None:
"""
与盘口同单位的内在价值(每 1 标的).
- USDC / USD_UM: 美元点差 max(0, SK) / max(0, KS)
- 币本位 ETH-USD / BTC-USD: 币报价 max(0, SK)/S / max(0, KS)/S
"""
o = (opt_type or "").strip().upper()
if strike is None or index_px is None:
return None
@@ -74,10 +113,18 @@ def intrinsic_px_per_unit(opt_type: str | None, strike: float | None, index_px:
except (TypeError, ValueError):
return None
if o == "C" and idx > k:
return idx - k
if o == "P" and idx < k:
return k - idx
return None
points = idx - k
elif o == "P" and idx < k:
points = k - idx
else:
return None
if _quote_in_coin_from_context(
quote_in_coin=quote_in_coin, inst_id=inst_id, margin_mode=margin_mode
):
if idx <= 0:
return None
return points / idx
return points
def is_stub_bid_px(
@@ -128,9 +175,19 @@ def close_ref_prices(
opt_type: str | None = None,
strike: float | None = None,
index_px: float | None = None,
quote_in_coin: bool | None = None,
inst_id: str | None = None,
margin_mode: str | None = None,
) -> tuple[float | None, float | None]:
"""返回 (mark_px, intrinsic_px) 供残档判断."""
return _safe_px(mark_px), intrinsic_px_per_unit(opt_type, strike, index_px)
"""返回 (mark_px, intrinsic_px) 供残档判断;intrinsic 与盘口同单位."""
return _safe_px(mark_px), intrinsic_px_per_unit(
opt_type,
strike,
index_px,
quote_in_coin=quote_in_coin,
inst_id=inst_id,
margin_mode=margin_mode,
)
def filter_bids_for_close(
+7 -1
View File
@@ -33,7 +33,13 @@ def _pos_close_refs(ex: Any, pos: dict[str, Any], quote: dict[str, Any] | None =
if strike is None:
strike = ps
idx = _safe_float(pos.get("idxPx")) or _safe_float((quote or {}).get("index_px"))
return close_ref_prices(mark_px=mark, opt_type=str(opt_type or ""), strike=strike, index_px=idx)
return close_ref_prices(
mark_px=mark,
opt_type=str(opt_type or ""),
strike=strike,
index_px=idx,
inst_id=inst_id,
)
def ensure_target_tables(conn: sqlite3.Connection) -> None: