Add HEDGE_PO_MODE instance modes with shared sim/live path.
Lock one mode per instance/subaccount, cross-asset BTC option/ETH perp sizing, combo-U exit, and ship docs/tests/audit. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
#!/usr/bin/env python3
|
||||
"""审计永期实例模式配置与纯函数口径(不连交易所下单).
|
||||
|
||||
用法:
|
||||
python scripts/audit_hedge_po_mode.py
|
||||
HEDGE_PO_MODE=btc_eth python scripts/audit_hedge_po_mode.py
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
# 仓库根
|
||||
_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
|
||||
if _ROOT not in sys.path:
|
||||
sys.path.insert(0, _ROOT)
|
||||
|
||||
|
||||
def main() -> int:
|
||||
from lib.hedge_plan.hedge_po_mode_lib import (
|
||||
apply_mode_to_start_body,
|
||||
clamp_hedge_po_mode_on_startup,
|
||||
combo_net_usdt,
|
||||
public_mode_payload,
|
||||
size_cross_notional,
|
||||
validate_body_matches_mode,
|
||||
)
|
||||
|
||||
print("=== HEDGE_PO_MODE 审计 ===")
|
||||
summary = clamp_hedge_po_mode_on_startup()
|
||||
payload = public_mode_payload(summary["mode"])
|
||||
ok = True
|
||||
|
||||
def check(name: str, cond: bool, detail: str = "") -> None:
|
||||
nonlocal ok
|
||||
mark = "PASS" if cond else "FAIL"
|
||||
if not cond:
|
||||
ok = False
|
||||
print(f"[{mark}] {name}" + (f" — {detail}" if detail else ""))
|
||||
|
||||
check("启动钳制", summary.get("ok") is True, str(summary.get("mode")))
|
||||
check("期权/永续标的已填", bool(payload.get("option_underlying") and payload.get("perp_underlying")))
|
||||
check("出场强制 profit", payload.get("exit_mode") == "profit")
|
||||
check("以期权为主", payload.get("option_primary") is True)
|
||||
check("实例规则文案", "子账户" in str(payload.get("instance_rule") or ""))
|
||||
|
||||
if payload.get("po_mode") == "btc_eth":
|
||||
check("跨币期权=BTC", payload.get("option_underlying") == "BTC")
|
||||
check("跨币永续=ETH", payload.get("perp_underlying") == "ETH")
|
||||
k = float(payload.get("cross_notional_k") or 0)
|
||||
check("K 在合理区间", 0.05 <= k <= 5.0, f"k={k}")
|
||||
sz = size_cross_notional(
|
||||
option_coin_qty=0.1,
|
||||
option_index=100_000,
|
||||
perp_index=2_500,
|
||||
contract_size=0.01,
|
||||
notional_k=k,
|
||||
)
|
||||
check("跨币定仓样例", bool(sz.get("ok")), str(sz.get("contracts")))
|
||||
expect = 0.1 * 100_000 * k / 2_500 / 0.01
|
||||
check("定仓≈名义公式", abs(float(sz["contracts"]) - expect) < 1e-6, f"{sz['contracts']} vs {expect}")
|
||||
|
||||
body = apply_mode_to_start_body(
|
||||
{
|
||||
"direction": "long",
|
||||
"opt_inst_id": f"{payload['option_underlying']}-USD-260831-1000-C",
|
||||
"exchange_symbol": f"{payload['perp_underlying']}/USDT:USDT",
|
||||
"profit_target_usdt": 10,
|
||||
"sheets": 1,
|
||||
"contracts": 1,
|
||||
"option_perp_ratio": payload.get("option_perp_ratio"),
|
||||
}
|
||||
)
|
||||
check("body 钳制后校验通过", validate_body_matches_mode(body) is None)
|
||||
|
||||
# 组合 U:币*指数 + 永续U
|
||||
fake_net = {"opt_net": 0.01, "perp_net": 5.0, "net": 5.01}
|
||||
u = combo_net_usdt(fake_net, option_index=100_000)
|
||||
check("组合U不混单位", u == 1005.0, f"got {u}")
|
||||
|
||||
if summary.get("warnings"):
|
||||
print("--- warnings ---")
|
||||
for w in summary["warnings"]:
|
||||
print(" ", w)
|
||||
|
||||
print("===", "全部通过" if ok else "存在失败", "===")
|
||||
return 0 if ok else 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user