Add option-option hedge mode with SIM/LIVE parity.

Mutual hedge_mode, amplitude OTM selection, 1:1 risk sizing, win-leg/full close, dual audits and docs.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-08-07 16:01:16 +08:00
parent 15fe2f72dc
commit ec244c63c6
22 changed files with 2640 additions and 83 deletions
+46 -8
View File
@@ -38,20 +38,51 @@ def size_and_gate(
index_px: float,
option_ask: float,
db: Database | None = None,
call_ask: float | None = None,
put_ask: float | None = None,
hedge_mode: str | None = None,
) -> OpenPrepResult:
"""
选约成功后:写入以损定仓 → 交易账户兑 USDC → 资金门。
资金门 fail-closed:异常 / can_open 非 True 一律拦截。
"""
database = db or get_db()
mode = str(hedge_mode or "").strip().lower()
if not mode:
try:
from ..config import get_settings
from ..sim.ledger import Ledger
s = get_settings()
mode = str(
Ledger(database).get_setting_str("hedge_mode", s.hedge_mode)
or s.hedge_mode
).strip().lower()
except Exception:
mode = "perp_option"
try:
rs = apply_risk_sizing_to_ledger(
index_px=float(index_px),
option_ask=float(option_ask),
db=database,
)
if not rs.ok:
return OpenPrepResult(ok=False, detail=rs.detail, sizing_detail=rs.detail)
if mode == "option_option":
from .risk_sizing import apply_oo_sizing_to_ledger
if call_ask is None or put_ask is None:
return OpenPrepResult(ok=False, detail="期期定仓缺少 call/put 卖一")
rs = apply_oo_sizing_to_ledger(
call_ask=float(call_ask),
put_ask=float(put_ask),
index_px=float(index_px),
db=database,
)
if not rs.ok:
return OpenPrepResult(ok=False, detail=rs.detail, sizing_detail=rs.detail)
else:
rs = apply_risk_sizing_to_ledger(
index_px=float(index_px),
option_ask=float(option_ask),
db=database,
)
if not rs.ok:
return OpenPrepResult(ok=False, detail=rs.detail, sizing_detail=rs.detail)
except Exception as e:
logger.exception("risk sizing failed in open pipeline")
return OpenPrepResult(ok=False, detail=f"以损定仓计算异常:{e}")
@@ -66,6 +97,8 @@ def size_and_gate(
cap=assess_open_capacity(
database,
option_ask=float(option_ask),
call_ask=call_ask,
put_ask=put_ask,
),
force=False,
)
@@ -77,7 +110,12 @@ def size_and_gate(
convert_detail = "自动兑 USDC 异常(已记日志)"
try:
cap = assess_open_capacity(database, option_ask=float(option_ask))
cap = assess_open_capacity(
database,
option_ask=float(option_ask),
call_ask=call_ask,
put_ask=put_ask,
)
except Exception as e:
logger.exception("open capacity assess failed")
return OpenPrepResult(