ec87cf2104
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>
3.2 KiB
3.2 KiB
审计说明 — 开/平仓逻辑与实盘安全(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,余额视为未知 |
开仓路径(自检)
claim_open_slot→opening占槽,防并发双开。- 先期权后永续;永续失败则卖回期权,卖回失败 →
half_open。 - 期权下单已返回
ordId但查单未确认:不释放opening,禁止再开,须核对交易所。 - LIVE 成交后本地账本一律
allow_negative,避免「交易所有仓、本地拒记」。
平仓路径(自检)
- 先平期权并
_mark_option_closed_perp_pending(写 fill + 入账),再平永续。 - 永续失败时状态已是
option_closed_perp_pending,重试只平永续,不再卖期权。 _finalize_dual_close在pending_perp_only时跳过期权二次入账(含 expiry)。- 弃期权:交易所平永续后本地必须入账成功(
allow_negative),再归档 residual。 - OKX 平永续
tdMode使用开仓时组上记录的perp_margin_mode。
安全
- Web 登录依赖
AUTH_SECRET;LIVE 禁止默认密钥启动。 - 策略/资金 API 均需登录;手动交易另需开关。
- 不在本文档记录任何真实密钥或口令。
残留 / 后续
- 币安 LIVE 交易账户余额接入后,再恢复精确「可开」判定。
opening残留需人工或后续对账任务清槽(现策略:宁卡不开重复仓)。- 期权腿交易所对账仍弱于永续(启动 reconcile 主要看永续)。
涉及文件
backend/app/live/executor.py/binance_executor.pybackend/app/live/okx_trade.pybackend/app/api/sim.py/settings.pybackend/app/main.pybackend/app/models/db.pybackend/app/strategy/open_capacity.py