Fix LIVE open/close double-book and security audit findings.
Prevent expiry dual-close from re-booking option cash, abandon ledger rejection, margin-mode mismatch, and manual close races; harden fill wait and refuse default AUTH_SECRET on LIVE. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
# 审计说明 — 开/平仓逻辑与实盘安全(2026-07-29)
|
||||
|
||||
## 范围
|
||||
|
||||
- 开仓 / 平仓 / 到期双腿平仓 / 弃期权只平永续
|
||||
- LIVE 执行器(OKX / 币安)本地账本与交易所一致性
|
||||
- 手动开平仓 API 与策略引擎并发
|
||||
- 鉴权密钥、资金可开判定、平仓盈亏查询
|
||||
|
||||
## 结论摘要
|
||||
|
||||
| 严重度 | 问题 | 处置 |
|
||||
|--------|------|------|
|
||||
| Critical | 到期双平:`_mark_option_closed_perp_pending` 后仍因 `not is_expiry` 二次入账期权现金/fill | **已修**:`skip_option_cash` / `option_fill_already_written` = `pending_perp_only` |
|
||||
| Critical | 弃期权路径:交易所已平永续,`apply_cash` 无 `allow_negative` → 本地可拒记卡仓 | **已修**:OKX/币安 abandon 均 `allow_negative=True` |
|
||||
| High | 平仓用「当前」永续保证金模式,持仓中可改设置导致模式错配 | **已修**:开仓写入 `groups.perp_margin_mode`;平仓读组记录;持仓中禁止改设置 |
|
||||
| High | 手动 `POST /close-group`(及手动开仓)未占引擎锁,可与自动 tick 竞态 | **已修**:`async with engine._lock` |
|
||||
| High | 期权成交等待超时 → 释放 opening,可能重复开 | **已修**:等成交通道加长+超时有成交则回填;含 `ordId` 失败保留 opening |
|
||||
| Med | `get_closed_perp_pnl_usdt` 构造了时间过滤 URL 却请求未过滤路径 | **已修**:优先带 after/before 查询,失败再兜底 |
|
||||
| Med | LIVE 默认可使用 `change-me-…` 鉴权密钥 | **已修**:LIVE 启动拒绝默认 `AUTH_SECRET` |
|
||||
| Med | 币安 LIVE 可开资金仍走 OKX 客户端 | **已修**:币安不再误调 OKX,余额视为未知 |
|
||||
|
||||
## 开仓路径(自检)
|
||||
|
||||
1. `claim_open_slot` → `opening` 占槽,防并发双开。
|
||||
2. 先期权后永续;永续失败则卖回期权,卖回失败 → `half_open`。
|
||||
3. 期权下单已返回 `ordId` 但查单未确认:不释放 `opening`,禁止再开,须核对交易所。
|
||||
4. LIVE 成交后本地账本一律 `allow_negative`,避免「交易所有仓、本地拒记」。
|
||||
|
||||
## 平仓路径(自检)
|
||||
|
||||
1. 先平期权并 `_mark_option_closed_perp_pending`(写 fill + 入账),再平永续。
|
||||
2. 永续失败时状态已是 `option_closed_perp_pending`,重试只平永续,不再卖期权。
|
||||
3. `_finalize_dual_close` 在 `pending_perp_only` 时跳过期权二次入账(**含 expiry**)。
|
||||
4. 弃期权:交易所平永续后本地必须入账成功(`allow_negative`),再归档 residual。
|
||||
5. OKX 平永续 `tdMode` 使用开仓时组上记录的 `perp_margin_mode`。
|
||||
|
||||
## 安全
|
||||
|
||||
- Web 登录依赖 `AUTH_SECRET`;LIVE 禁止默认密钥启动。
|
||||
- 策略/资金 API 均需登录;手动交易另需开关。
|
||||
- 不在本文档记录任何真实密钥或口令。
|
||||
|
||||
## 残留 / 后续
|
||||
|
||||
- 币安 LIVE 交易账户余额接入后,再恢复精确「可开」判定。
|
||||
- `opening` 残留需人工或后续对账任务清槽(现策略:宁卡不开重复仓)。
|
||||
- 期权腿交易所对账仍弱于永续(启动 reconcile 主要看永续)。
|
||||
|
||||
## 涉及文件
|
||||
|
||||
- `backend/app/live/executor.py` / `binance_executor.py`
|
||||
- `backend/app/live/okx_trade.py`
|
||||
- `backend/app/api/sim.py` / `settings.py`
|
||||
- `backend/app/main.py`
|
||||
- `backend/app/models/db.py`
|
||||
- `backend/app/strategy/open_capacity.py`
|
||||
@@ -5,6 +5,23 @@
|
||||
|
||||
---
|
||||
|
||||
## 2026-07-29 — 开平仓/实盘安全审计修复
|
||||
|
||||
### 变更
|
||||
|
||||
1. **Critical**:到期双平不再二次入账期权(`skip_option_cash` 跟 `pending_perp_only`)。
|
||||
2. **Critical**:弃期权平永续后本地账本强制 `allow_negative`,避免交易所已平、本地卡仓。
|
||||
3. 开仓写入 `groups.perp_margin_mode`;平仓用开仓时模式;持仓中禁止改保证金模式。
|
||||
4. 手动开/平仓走策略引擎锁,避免与自动 tick 竞态。
|
||||
5. OKX 等成交加长并超时有成交则回填;含 `ordId` 的未确认失败保留 `opening`。
|
||||
6. 修复 positions-history 盈亏查询未带时间过滤;LIVE 拒绝默认 `AUTH_SECRET`;币安可开不再误调 OKX 资金。
|
||||
|
||||
### 审计
|
||||
|
||||
详见 [`docs/审计说明-2026-07-29-开平仓与实盘安全.md`](./审计说明-2026-07-29-开平仓与实盘安全.md)。
|
||||
|
||||
---
|
||||
|
||||
## 2026-07-29 — 实盘资金读交易所;永续全仓/逐仓
|
||||
|
||||
### 变更
|
||||
|
||||
Reference in New Issue
Block a user