划转默认折叠,修复币本位到期平衡计算

币本位权利金为币报价,到期平衡按 OKX 结算公式 K/(1±p) 计算;链/持仓/跨式平衡带同步修正。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-08-23 08:15:33 +08:00
parent aa14688a7e
commit 57cca5554e
5 changed files with 103 additions and 13 deletions
+50 -6
View File
@@ -438,10 +438,20 @@ def expiry_breakeven_from_ask(
strike: float | None,
ask_px: float | None,
mark_px: float | None = None,
quote_in_coin: bool | None = None,
inst_id: str | None = None,
margin_mode: str | None = None,
) -> float | None:
"""买入前预估到期平衡:权利金按卖一;无卖一时回退标记价."""
prem = ask_px if ask_px is not None and ask_px > 0 else mark_px
return expiry_breakeven_px(opt_type=opt_type, strike=strike, avg_px=prem)
return expiry_breakeven_px(
opt_type=opt_type,
strike=strike,
avg_px=prem,
quote_in_coin=quote_in_coin,
inst_id=inst_id,
margin_mode=margin_mode,
)
def expiry_breakeven_px(
@@ -450,17 +460,39 @@ def expiry_breakeven_px(
strike: float | None,
avg_px: float | None,
be_px_api: float | None = None,
quote_in_coin: bool | None = None,
inst_id: str | None = None,
margin_mode: str | None = None,
) -> float | None:
"""到期平衡点:持有至到期时标的指数盈亏为 0 的价格.优先 OKX bePx."""
if be_px_api is not None and be_px_api > 0:
return round(float(be_px_api), 2)
if strike is None or avg_px is None:
return None
try:
k = float(strike)
p = float(avg_px)
except (TypeError, ValueError):
return None
if p <= 0:
return None
o = (opt_type or "").upper()
coin = _quote_in_coin_from_context(
quote_in_coin=quote_in_coin, inst_id=inst_id, margin_mode=margin_mode
)
if coin:
# 币本位:权利金为币报价;到期结算 payoff 亦为币 → K/(1±p)
if o == "C":
if p >= 1:
return None
return round(k / (1 - p), 2)
if o == "P":
return round(k / (1 + p), 2)
return None
if o == "C":
return round(strike + avg_px, 2)
return round(k + p, 2)
if o == "P":
return round(strike - avg_px, 2)
return round(k - p, 2)
return None
@@ -617,12 +649,24 @@ def straddle_premium_total(
def straddle_breakeven_band(
strike: float | None,
combined_ask_per_unit: float | None,
combined_ask_per_unit: float | None = None,
*,
call_ask: float | None = None,
put_ask: float | None = None,
quote_in_coin: bool = False,
) -> tuple[float | None, float | None]:
"""跨式到期平衡带:下平衡 ~ 上平衡(按双卖一报价和)."""
if strike is None or combined_ask_per_unit is None:
"""跨式到期平衡带:下平衡 ~ 上平衡."""
if strike is None:
return None, None
k = float(strike)
if quote_in_coin:
pc = _safe_px(call_ask)
pp = _safe_px(put_ask)
if pc is None or pp is None or pc <= 0 or pp <= 0 or pc >= 1:
return None, None
return round(k / (1 + pp), 2), round(k / (1 - pc), 2)
if combined_ask_per_unit is None:
return None, None
d = float(combined_ask_per_unit)
return round(k - d, 2), round(k + d, 2)