Fix strategy-logic P0s from audit: monitor false-flat, fill-confirmed open/close, mode gates.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-30 09:56:18 +08:00
parent fa7ff739a0
commit 4b4dca9e3c
15 changed files with 820 additions and 167 deletions
@@ -0,0 +1,80 @@
# 策略与逻辑审计修复报告
- 日期: 2026-07-30
- 范围: OKX 三选一模式 / 永期·期期对冲监控与开平 / 单独期权开平 / 互斥与复盘钳制
- 准则: 以资金与仓位正确性为准(假平仓、未成交落库、跨模式拆组等)
- 复审: 共 3 轮深度复审;最终 **剩余 P0 = 0**
## 修复总览
| 轮次 | 结果 |
|------|------|
| 初审 | 约 10 项 P0 + 多项 P1(监控假平、落库≠成交、模式互斥缺口等) |
| 复审 1 | 18/20 已修;发现 SL 待平可误判 TP、监控可重复启动、单独开仓仍可缩量 |
| 复审 2 | 上述 3 项已修;剩余若干 P1 |
| 复审 3 | P1 再收口(互斥/目标监控 fail-closed、already_flat 二次验仓、监控启动锁);**P0 清零** |
## 已修复关键项(原审计编号)
### 永期监控 / 对冲平仓
- **H1** `live is None` 不再当已平;仅 `live==0` 且过开仓宽限期后处理
- **H2** 止损强平失败不写 `closed`,写 `perp_sl_pending_opt` 并重试
- **H3** TP/SL 分类:歧义偏 SL;未知跳过;`*_pending_opt` 粘滞不再被 mark 反弹改判
- **H4** `_sell_option` 改为 `close_option_by_bid1`,要求 `fully_closed``already_flat` 二次验仓
- **H7** `partial` 计划纳入监控
- **H5** IOC 部分成交后尝试立刻平掉孤儿仓
- **H6** `/start` 进程内锁 + 闸门重检
- **H8** 服务端校验 long↔Put / short↔Call 与 TP/SL 几何
- 卖一深度不足时拒绝缩量成交(对冲买入)
### 单独期权
- **O1** 开仓 IOC + `wait_option_order_full_fill`,成交后再落 `open`
- **O2** 平仓后持仓 `None` 不标 `fully_closed`
- **O3** 禁止期权页 close/target 拆对冲腿;目标监控跳过托管合约
- **O4** 模式/互斥校验异常 fail-closed
- 开仓拒绝卖一深度不足时的静默缩量
- stub 买一路径不再撤掉他人挂单;门控通过改在下单接受后标记
### 三选一模式 / UI
- **M1** Jinja 去掉 `| default(true)`,避免 `False` 显示成 Tab
- **M2** 监控线程始终启动(单独期权也收口遗留计划)+ 单例锁
- **M3** env 展示 `OKX_TRADE_MODE``get_okx_trade_mode()` 一致
- 仪表盘始终展示进行中对冲;`complete-leg` 校验当前模式
- 复盘 API 按模式钳制 `source_type`
## 测试
```text
python -m unittest tests.test_hedge_po_monitor_safety tests.test_hedge_plan_orders \
tests.test_okx_trade_mode tests.test_hedge_options_exclusive \
tests.test_hedge_plan_end tests.test_hedge_partial_manual -v
→ OK (35)
```
新增: `tests/test_hedge_po_monitor_safety.py`(含 None 跳过、分类、SL sticky
## 残留非关键项(P1,可后续迭代)
1. 连续两次持仓列表均为空时,仍可能把「短暂漏仓」当成已平(对冲路径已有二次验仓;目标/手动路径仍单次)
2. 部分成交后若孤儿平仓也失败,需人工处理(已返回 `orphan_close`
3. 期权页对托管腿仍可能显示按钮,但 API 已拒绝
## 主要改动文件
- `lib/hedge_plan/hedge_plan_monitor_lib.py`
- `lib/hedge_plan/hedge_plan_orders_lib.py`
- `lib/hedge_plan/hedge_plan_register.py`
- `lib/hedge_plan/hedge_plan_db.py`
- `lib/hedge_plan/hedge_options_exclusive_lib.py`
- `lib/hedge_plan/templates/hedge_plan_panel.html`
- `lib/options/options_close_exec_lib.py`
- `lib/options/options_register.py`
- `lib/options/options_target_lib.py`
- `lib/options/options_review_register.py`
- `lib/env/env_ui_manifest.py`
- `lib/instance/instance_dashboard_lib.py`
- `tests/test_hedge_po_monitor_safety.py`
## 部署
见本轮 commit + `zk.hyf2.cc` `deploy/pull_and_restart.sh` 结果。