Recover stuck opening and harden LIVE open/close reconcile.
Stamp open intent, recover opening from exchange option/perp state, skip resell/reopen when already flat, and persist Binance margin mode. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -2,42 +2,48 @@
|
||||
|
||||
## 范围
|
||||
|
||||
- 开仓 / 平仓 / 到期双腿平仓 / 弃期权只平永续
|
||||
- LIVE 执行器(OKX / 币安)本地账本与交易所一致性
|
||||
- 手动开平仓 API 与策略引擎并发
|
||||
- 鉴权密钥、资金可开判定、平仓盈亏查询
|
||||
- 开仓 / 平仓 / 到期双腿 / 弃期权 / stuck `opening` 恢复
|
||||
- LIVE OKX / 币安执行器与本地账本一致性
|
||||
- 交易所对账(永续 + 期权)
|
||||
|
||||
## 结论摘要(含第二轮复审)
|
||||
## 结论摘要(三轮)
|
||||
|
||||
| 严重度 | 问题 | 处置 |
|
||||
|--------|------|------|
|
||||
| Critical | 到期双平:`_mark` 后仍二次入账期权 | **已修**(第一轮) |
|
||||
| Critical | 弃期权 `apply_cash` 无 `allow_negative` | **已修**(第一轮) |
|
||||
| Critical | OKX abandon:双腿已平期权落 pending 后仍记 residual → 到期再结期权双计 | **已修**(第二轮):pending 则续 `close_group` |
|
||||
| High | 平仓用当前保证金模式 | **已修**(第一轮,OKX) |
|
||||
| High | 手动开平无引擎锁 | **已修**(第一轮) |
|
||||
| High | `_wait_fill` 超时把部分成交当全成 | **已修**(第二轮):超时仅接受 `filled` |
|
||||
| High | residual 结算无 `allow_negative`(LIVE) | **已修**(第二轮) |
|
||||
| High | `opening` 卡住无恢复 | **部分**:紧急全平明确告警;不自动清槽(防裸仓清槽) |
|
||||
| Med | positions-history / AUTH / 币安可开误调 OKX | **已修**(第一轮) |
|
||||
| Med | 币安 `orderId` 误匹配卡 opening | **已修**(第二轮):仅 `orderId=` |
|
||||
| Critical | 到期双平期权二次入账 | **已修** |
|
||||
| Critical | abandon 账本拒记 / 假 residual 双计 | **已修** |
|
||||
| Critical | 币安 `orderId=` 保留 opening 实际不匹配 → 可能重复开 | **已修**:错误文案带 `orderId=` |
|
||||
| Critical | `opening` 无元数据,崩溃后无法恢复 | **已修**:`stamp_opening_intent` + `recover_opening` |
|
||||
| High | 对账只查永续 | **已修**:期权仓位 + flat 时任意期权残留 |
|
||||
| High | pending 时永续已空仍用 DB 数量下单 | **已修**:`allow_db_fallback=False` → 直接 finalize |
|
||||
| High | 期权已空仍再卖 / 未 mark | **已修**:查仓跳过再卖;有 close fill 不二次入账 |
|
||||
| High | 永续开仓异常时可能已成交仍回滚期权 | **已修**:先查永续仓再决定 |
|
||||
| High | `_wait_fill` 部分成交当全成 | **已修** |
|
||||
| High | residual LIVE 无 `allow_negative` | **已修** |
|
||||
| Med | 币安缺保证金模式 | **已修**:落库 + `set_margin_type` |
|
||||
|
||||
## 第二轮仍残留(已知)
|
||||
## 开仓状态机(自检)
|
||||
|
||||
- 开仓两腿成交后、写 DB 前进程崩溃 → 交易所满仓、本地 `opening`(需人工对账)。
|
||||
- 启动对账主要看永续,不查期权裸仓。
|
||||
- 币安未持久化/使用 `perp_margin_mode`;可开资金未接币安余额。
|
||||
- LIVE 仅拒绝默认 `AUTH_SECRET`,弱自定义密钥不拦截。
|
||||
1. `claim_open_slot` → `opening`
|
||||
2. `stamp_opening_intent`(写入 group_id / option_inst_id / 数量)
|
||||
3. 下期权 → 再 stamp 成交均价
|
||||
4. 下永续;失败则查永续仓:有仓保留 opening;无仓则回滚期权,回滚失败 → `half_open`
|
||||
5. 成功 → `open`;启动/tick/`close_group` 遇 `opening` → `recover_opening`
|
||||
|
||||
## 开/平仓自检要点
|
||||
## 平仓状态机(自检)
|
||||
|
||||
1. 期权先平并 `_mark_option_closed_perp_pending`,再平永续;finalize 跳过期权二次入账。
|
||||
2. abandon:若已 pending,只续平永续,**禁止**再插 residual。
|
||||
3. LIVE 成交后本地账本一律允许透支镜像。
|
||||
4. 手动开平与引擎共用 `_lock`。
|
||||
1. 卖期权(或到期本地结算)→ `_mark_option_closed_perp_pending`(只一次)
|
||||
2. 平永续;交易所已空则不下单,直接 finalize
|
||||
3. abandon:若已 pending,只续平永续,不记 residual
|
||||
4. finalize 在 pending 时 `skip_option_cash`
|
||||
|
||||
## 仍需人工场景(极少)
|
||||
|
||||
- `opening` 无 `option_inst_id` 且交易所有不明期权仓:禁止自动清槽,企微告警
|
||||
- 提升为 `open` 时永续入场价用指数近似(本地镜像;实盘盈亏仍可走交易所对账)
|
||||
|
||||
## 涉及文件
|
||||
|
||||
- `backend/app/live/executor.py` / `binance_executor.py` / `okx_trade.py`
|
||||
- `backend/app/sim/matcher.py` / `strategy/engine.py`
|
||||
- `backend/app/api/sim.py` / `settings.py` / `main.py`
|
||||
- `backend/app/live/reconcile.py`(核心恢复/对账)
|
||||
- `executor.py` / `binance_executor.py` / `okx_trade.py` / `binance_trade.py`
|
||||
- `strategy/engine.py` / `sim/matcher.py`
|
||||
|
||||
@@ -5,6 +5,22 @@
|
||||
|
||||
---
|
||||
|
||||
## 2026-07-29 — 第三轮:opening 恢复 / 期权对账 / 平仓兜底
|
||||
|
||||
### 变更
|
||||
|
||||
1. 开仓意图 `stamp_opening_intent`:崩溃后仍带 `option_inst_id` 可恢复。
|
||||
2. `recover_opening`:空仓清槽 / 仅期权→half_open+repair / 双边→提升 open;启动与 tick / 紧急全平触发。
|
||||
3. 开仓前对账增加期权仓(含 flat 时任意期权残留)。
|
||||
4. 平仓:期权已空跳过再卖;有 close fill 不二次入账;pending 且永续已空不下单。
|
||||
5. 永续开仓异常先查仓再决定是否回滚期权;币安错误带 `orderId=`;币安保证金模式落库并 `set_margin_type`。
|
||||
|
||||
### 审计
|
||||
|
||||
详见 [`docs/审计说明-2026-07-29-开平仓与实盘安全.md`](./审计说明-2026-07-29-开平仓与实盘安全.md)。
|
||||
|
||||
---
|
||||
|
||||
## 2026-07-29 — 第二轮审计:abandon residual / 部分成交 / residual 账本
|
||||
|
||||
### 变更
|
||||
|
||||
Reference in New Issue
Block a user