Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 890659f173 | |||
| ca499c6104 | |||
| 54f1857fa2 | |||
| 6f1ae14b3d | |||
| 29d59d6a53 |
@@ -4,6 +4,15 @@
|
||||
|
||||
「内照明心」页(`/archive`)用于 **复盘语录 + 交易记录回顾 + 按需 K 线**.左侧维护每日复盘语录(最多 100 条);右侧按日期区间列出开仓记录,展示区间统计,并可展开 K 线图表对照单笔交易.
|
||||
|
||||
顶栏有 **永续 / 期权** 品种切换:
|
||||
|
||||
| 品种 | 数据 | 说明 |
|
||||
|------|------|------|
|
||||
| **永续** | 三所 `trade_records` → `archive_trade_cache` | 含犯病标签、K 线 |
|
||||
| **期权** | OKX `options_review_trades` → `archive_options_trade_cache` | 独立 Tab;同步进中控库后离线可看;默认排除对冲腿 |
|
||||
|
||||
同步:「同步」按钮与后台 4h 任务会同时拉永续与期权(仅 `capabilities` 含 `options` 的账户).
|
||||
|
||||
与行情区 `hub_kline.db`(15 天滚动缓存)**完全独立**:档案库只增不删,从建档起永久保留.
|
||||
|
||||
## 页面布局
|
||||
|
||||
@@ -4,7 +4,8 @@
|
||||
|
||||
| 文档 | 实例 | 状态 |
|
||||
|------|------|------|
|
||||
| [交易执行手册-期权与Gate.md](../交易执行手册-期权与Gate.md) | 中控「策略说明」·执行手册 | 个人开单纪律 |
|
||||
| [交易执行手册-v2-期权与合约.md](../交易执行手册-v2-期权与合约.md) | 中控「策略说明」·执行手册v2 | **现行**:无对冲;1H→空间→结构→定损盈→期权/合约 |
|
||||
| [交易执行手册-期权与Gate.md](../交易执行手册-期权与Gate.md) | 中控「策略说明」·执行手册v1 | 含对冲;历史对照 |
|
||||
| [交易行为准则-开单三检.md](../交易行为准则-开单三检.md) | 中控「策略说明」·行为准则 | 开单前信号/流程/情绪三检 |
|
||||
| [binance-alt-trend-long.md](./binance-alt-trend-long.md) | 币安山寨·多头趋势 | v0.4 讨论稿 |
|
||||
| [okx-trend-both.md](./okx-trend-both.md) | OKX·多空趋势 | v0.4 讨论稿 |
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
"items": [
|
||||
"最核心、最明确的一个点位/结构确认已写清",
|
||||
"该确认本身足够清晰(不是靠一长串宏大叙事)",
|
||||
"已过方向 → 空间 → 值不值得(不够格则空仓)"
|
||||
"已过主链条:1H方向 → 空间 → 结构 → 定损盈 → 选工具(期权/合约,无对冲);不够格则空仓"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
{
|
||||
"exchange": "playbook_v2",
|
||||
"title": "执行手册 v2 开仓清单(无对冲)",
|
||||
"version": "v0.1",
|
||||
"groups": [
|
||||
{
|
||||
"title": "主链条",
|
||||
"items": [
|
||||
"1H 方向清楚(含明显 N 字);跟的是 1H 波段",
|
||||
"空间足够(支撑/阻力;至少约 ≥2%)",
|
||||
"结构已出现且量级够(约 8h+ / 48 根 15m)",
|
||||
"止损按模型:结构突破=外沿;假突破=针尖;止盈与 RR 已接受",
|
||||
"工具只在「期权 / 合约」中选择;未开对冲"
|
||||
]
|
||||
},
|
||||
{
|
||||
"title": "账户与仓位",
|
||||
"items": [
|
||||
"只动 OKX 期权或 Gate 合约;其它账户零操作",
|
||||
"期权:约 10U、一次一仓;合约:止损约 5U、本位置次数未超两次",
|
||||
"合计最坏风险可接受(约 ≤20U 量级)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"title": "离场与心态",
|
||||
"items": [
|
||||
"期权离场只认规则止盈或到期;开仓后中间不手平",
|
||||
"不是「今天也要开点期权」;过检才开,不过则空仓",
|
||||
"已过开单三检(信号 / 流程 / 情绪)"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
# 交易执行手册 v2(期权 / 合约 · 无对冲)
|
||||
|
||||
> 个人开单纪律第二版(2026-07-24 起)。
|
||||
> **相对 v1:去掉期期对冲 / 偏置对冲;工具只留期权与合约。**
|
||||
> 目标:少而精、珍惜机会、样本干净;**不保证收益**。
|
||||
> 旧版(含对冲)见 [交易执行手册-期权与Gate.md](./交易执行手册-期权与Gate.md)。
|
||||
> **开单前先过** [交易行为准则-开单三检.md](./交易行为准则-开单三检.md);本手册管怎么做单。
|
||||
|
||||
---
|
||||
|
||||
## 1. 主链条(强制)
|
||||
|
||||
```
|
||||
1H 方向 → 空间 → 结构 → 定损盈 → 选工具(期权 / 合约)
|
||||
```
|
||||
|
||||
任一步不过 → **空仓等待**,不为开单找理由。
|
||||
|
||||
| 步骤 | 做什么 | 否决 |
|
||||
|------|--------|------|
|
||||
| **1H 方向** | 趋势周期以 **1H** 为准;1H 上要有明显 **N 字**。跟 1H 波段,不跟 4H 打架硬做。例:4H 多、1H 空 → 做 1H 空头波段 | 1H 方向不清、无 N 字 |
|
||||
| **空间** | 做空看下方支撑,做多看上方阻力;至少约 **≥2%** 才值得谈(常期望更大空间,如 ~5%) | 空间不够、贴着墙 |
|
||||
| **结构** | 方向与空间过关后,在 **15m / 5m** 等结构;结构量级至少约 **8h+**(约 **48 根 15m**)。形态:收敛 / 两段式回调 / 箱体 / 假突破等 | 结构未出现、磨不够就抢跑 |
|
||||
| **定损盈** | 结构出现后定义止损、止盈,算盈亏比。结构突破 → 止损在 **结构外沿**;假突破 → 止损在 **假突破针尖** | 损盈说不清、RR 不接受 |
|
||||
| **选工具** | 只在上四步都齐之后选:**期权** 或 **合约**。波段有足够时间考虑,不急着下手 | 用对冲、或「每天都要开点期权」 |
|
||||
|
||||
**丢掉对冲。** 对冲易带来「有保护就能多做」的幻觉;本版不做期期对冲、不做偏置对冲壳。
|
||||
|
||||
---
|
||||
|
||||
## 2. 总原则
|
||||
|
||||
1. **工具只有期权与合约**;同一时段尽量只让一边「说话」。
|
||||
2. **看不懂不做**;过滤比频率重要。日更不是目标,过检才是。
|
||||
3. 动手前先过 **开单三检**(信号 → 流程 → 情绪);不过 → 空仓。
|
||||
4. 玩法必须走完主链条;不够格 → 空仓。
|
||||
5. 期权离场只认:**系统/规则止盈** 与 **到期**;**开仓后中间不手动平仓**(紧急例外不进策略样本)。
|
||||
6. 过程可控、结果随缘:用规则管仓位与次数,不追求每天打满。
|
||||
|
||||
---
|
||||
|
||||
## 3. 账户与分工
|
||||
|
||||
| 账户 | 角色 | 说明 |
|
||||
|------|------|------|
|
||||
| OKX 期权 | **主业之一** | 方向单(虚值等);**不做对冲腿** |
|
||||
| Gate 合约 | **主业之一** | 结构清楚时的波段;与期权尽量错开 |
|
||||
| 其它 | 暂不做 | 减少分心与样本污染 |
|
||||
|
||||
**到期选择(期权)**
|
||||
|
||||
- 方向单默认 **一天期**。
|
||||
- 尽量在 **北京时间下午 4 点后** 开 **次日到期**,覆盖较完整的美盘 + 亚盘 + 欧盘窗口。
|
||||
- 更长故事优先考虑合约,不强行拉长期权。
|
||||
|
||||
---
|
||||
|
||||
## 4. 入场逻辑(两类工具)
|
||||
|
||||
开仓前先判断:当前是 **买方向的期权表达**,还是 **合约波段**。
|
||||
|
||||
### 4.1 方向明确 · 结构到位 → 期权
|
||||
|
||||
- **条件**:主链条全部过关;常用结构突破或假突破模型在 15m/5m 成立。
|
||||
- **工具**:**一天期期权方向单**(空间够时优先考虑 **虚值**:同止损口径下盈亏比往往更高)。
|
||||
- **离场**:规则止盈或到期;不手平。
|
||||
- **默认**:先只开期权,不上合约。
|
||||
|
||||
### 4.2 结构到位 · 更适合合约 → 合约
|
||||
|
||||
- **条件**:主链条过关;位置极明确;同一位置机会计数见 Gate 纪律。
|
||||
- **工具**:Gate 合约波段;止损挂在模型对应位置(外沿 / 针尖)。
|
||||
- **独立假突破**(没有先开突破期权时):优先 **只做合约** 或 **空仓**,勿与「突破期权后再加仓」混用同一套仓。
|
||||
|
||||
### 4.3 明确不做
|
||||
|
||||
- 横盘「买波动」的 **期期对冲**(Call+Put)。
|
||||
- 任何「对冲壳 + 偏置」伪装成单边。
|
||||
- 为了「今天也开点期权」而破主链条。
|
||||
|
||||
---
|
||||
|
||||
## 5. 仓位与风险预算
|
||||
|
||||
**总资金参考:约 800U。**
|
||||
|
||||
| 项目 | 规则 |
|
||||
|------|------|
|
||||
| 单笔期权 | 约 **10U** 权利金预算;**一次只持有一个期权仓位** |
|
||||
| Gate 合约 | 日内保证金约 **50U**、约 **10 倍**;有单才用,无单为 0 |
|
||||
| 合约止损 | 一般约 **5U**;单笔最大亏损不超过约 **10U** |
|
||||
| 日损失心理框 | 期权+合约若都错:合计大约 **≤20U**;都对时期望可到 **40U+**(理想情形,非每日目标) |
|
||||
|
||||
相对 800U:单笔约 **1.25%** 量级;全错一天约 **2.5%** 量级——防守优先。
|
||||
|
||||
**叠加红线**
|
||||
|
||||
- 期权一仓 + 合约同日存在时,按合计风险接受最坏约 20U,且尽量少「同向双开」。
|
||||
- 不为「好像有保护」放大仓位(本版已无对冲保护叙事)。
|
||||
|
||||
---
|
||||
|
||||
## 6. 合约日纪律(Gate)
|
||||
|
||||
1. 只做 **很明确的位置**;不明确基本不做。
|
||||
2. 动手前想清:**如何进场**(假突破 / 结构突破)。
|
||||
3. **同一位置最多两次机会**:结构突破、假突破。
|
||||
4. **两次都错 → 当日不再做单**(即使后面更「看起来清楚」也留到明天)。
|
||||
5. 止损约 **5U**;波段规则开仓前想清。
|
||||
6. 离场以结构止盈/止损为准。
|
||||
|
||||
---
|
||||
|
||||
## 7. 期权日纪律(OKX)
|
||||
|
||||
1. **不手动平仓**;只等规则止盈或到期(紧急手平标记为非策略样本)。
|
||||
2. 一次一仓;约 10U 权利金。
|
||||
3. **不做对冲**;不做「每天默认开期权」。
|
||||
4. 结构突破 / 假突破用期权表达时,损位跟模型:外沿 / 针尖。
|
||||
5. 默认一天期;优先完整会话窗口再开。
|
||||
|
||||
---
|
||||
|
||||
## 8. 开仓前自检清单
|
||||
|
||||
- [ ] 今天是否只动「期权 / 合约」,其它账户零操作?是否 **未开对冲**?
|
||||
- [ ] **1H 方向**是否清楚(含 N 字)?
|
||||
- [ ] **空间**是否足够(支撑/阻力,至少约 ≥2%)?
|
||||
- [ ] **结构**是否出现且量级够(约 8h+ / 48×15m)?
|
||||
- [ ] **止损 / 止盈**是否按模型定好(外沿或针尖)?RR 是否接受?
|
||||
- [ ] **工具**选的是期权还是合约?理由是否写清?
|
||||
- [ ] 期权:止盈条件与「接受到期」是否写清?
|
||||
- [ ] 合约:本位置第几次机会?止损约 5U 设好了吗?今日两次是否已用完?
|
||||
|
||||
---
|
||||
|
||||
## 9. 一句话版本
|
||||
|
||||
> **1H 定方向 → 量空间 → 等够级别的结构 → 按模型定损盈 → 只在期权与合约里选工具;不对冲;期权不手平;一位置两次,错完收工;珍惜机会,日更不是目标。**
|
||||
|
||||
---
|
||||
|
||||
## 10. 修订记录
|
||||
|
||||
| 日期 | 说明 |
|
||||
|------|------|
|
||||
| 2026-07-24 | v2 初版:去掉对冲;主链条 1H→空间→结构→定损盈→期权/合约;吸收假突破针尖 / 结构外沿止损口径 |
|
||||
@@ -1,10 +1,10 @@
|
||||
# 交易执行手册(期权为主 · Gate 为辅)
|
||||
# 交易执行手册 v1(期权为主 · Gate 为辅 · 含对冲)
|
||||
|
||||
> 个人开单纪律与仓位规则(2026-07 起)。
|
||||
> 个人开单纪律与仓位规则(2026-07 起)。**本版保留对冲,仅作历史/对照。**
|
||||
> **现行主版本请用** [交易执行手册-v2-期权与合约.md](./交易执行手册-v2-期权与合约.md)(无对冲:1H→空间→结构→定损盈→期权/合约)。
|
||||
> 目标:少而精、可控回撤、样本干净;**不保证收益**。
|
||||
> 工具:OKX 期权(主)+ Gate 合约(辅);其它账户暂不做。
|
||||
> **开单前先过** [交易行为准则-开单三检.md](./交易行为准则-开单三检.md)(信号 / 流程 / 情绪);本手册管怎么做单。
|
||||
|
||||
---
|
||||
|
||||
## 1. 总原则
|
||||
@@ -146,3 +146,4 @@
|
||||
|------|------|
|
||||
| 2026-07-21 | 初版:根据实盘讨论整理(期权为主、Gate 为辅、仓位与日停手规则) |
|
||||
| 2026-07-23 | 挂钩开单三检行为准则 |
|
||||
| 2026-07-24 | 标注为 v1(含对冲);现行纪律迁至执行手册 v2 |
|
||||
|
||||
+2
-1
@@ -49,7 +49,7 @@
|
||||
| 点位 / 结构本身已经够清楚 | 「好像有戏」但确认点模糊 |
|
||||
| 只描述事实与系统条件 | 堆细节证明自己分析很厉害 |
|
||||
|
||||
对照执行手册时:先过 **方向 → 空间 → 值不值得**;不够格 → 空仓(见手册 §1、§3)。
|
||||
对照执行手册时:先过 **1H 方向 → 空间 → 结构 → 定损盈 → 选工具(期权/合约)**;不够格 → 空仓(见手册 v2)。
|
||||
|
||||
### 4.2 流程确认(Process Confirmation)
|
||||
|
||||
@@ -112,3 +112,4 @@
|
||||
| 日期 | 说明 |
|
||||
|------|------|
|
||||
| 2026-07-23 | 初级版:三检 + 总循环 + 红线;对齐 AI 复盘与本人总结 |
|
||||
| 2026-07-24 | 信号检对齐执行手册 v2 主链条(1H→空间→结构→定损盈→期权/合约) |
|
||||
|
||||
+7
-2
@@ -74,10 +74,15 @@ OKX_OPTIONS_API_PASSPHRASE=...
|
||||
|
||||
## 5. 微信提醒
|
||||
|
||||
当某笔持仓 **未实现盈亏 ≥ 已付权利金的 100%**(翻倍)时,会发 **一条** 企业微信提醒(同一笔只提醒一次).
|
||||
|
||||
需已配置 `WECHAT_WEBHOOK`.
|
||||
|
||||
| 场景 | 标题 | 说明 |
|
||||
|------|------|------|
|
||||
| **开仓** | 【OKX期权·开仓】 | 下单成功并写入本地后必发(幂等) |
|
||||
| **平仓** | 【OKX期权·平仓】 | 手动全平 / 目标位全平 / 到期或交易所平仓同步后必发(幂等) |
|
||||
| 浮盈翻倍 | 【OKX期权·翻倍提醒】 | 未实现盈亏 ≥ 已付权利金约 100%,同一笔只提醒一次 |
|
||||
| 挂单超时撤销 | 【OKX期权·挂单超时撤销】 | 平仓挂单超时被系统撤销 |
|
||||
|
||||
## 6. 与永续 / 对冲计划的关系
|
||||
|
||||
| | 永续(子账户) | 期权(主账户) |
|
||||
|
||||
@@ -672,6 +672,72 @@ def register_hub_routes(app):
|
||||
}
|
||||
)
|
||||
|
||||
@app.route("/api/hub/options/review/archive")
|
||||
@_hub_auth_required
|
||||
def api_hub_options_review_archive():
|
||||
"""中控期权档案:近 N 天已平仓复盘记录(默认排除对冲腿)."""
|
||||
from datetime import datetime, timedelta
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
from flask import current_app
|
||||
|
||||
from lib.options.options_review_lib import (
|
||||
compute_review_stats,
|
||||
ensure_local_review_synced,
|
||||
list_review_trades,
|
||||
)
|
||||
|
||||
c = _ctx()
|
||||
get_db = c.get("get_db")
|
||||
if not get_db:
|
||||
return jsonify({"ok": False, "msg": "HUB_CTX 缺少 get_db"}), 500
|
||||
try:
|
||||
days = int(request.args.get("days") or "365")
|
||||
except ValueError:
|
||||
days = 365
|
||||
days = max(1, min(days, 3650))
|
||||
try:
|
||||
limit = int(request.args.get("limit") or "2000")
|
||||
except ValueError:
|
||||
limit = 2000
|
||||
limit = max(1, min(limit, 5000))
|
||||
include_hedge_legs = str(request.args.get("include_hedge_legs") or "").strip() in (
|
||||
"1",
|
||||
"true",
|
||||
"yes",
|
||||
)
|
||||
tz = ZoneInfo("Asia/Shanghai")
|
||||
closed_from = (datetime.now(tz) - timedelta(days=days)).strftime("%Y-%m-%d")
|
||||
cfg = (current_app.extensions or {}).get("options_cfg") or {}
|
||||
ex = cfg.get("exchange_options")
|
||||
conn = get_db()
|
||||
try:
|
||||
ensure_local_review_synced(conn, ex=ex, backfill_exchange_pnl=bool(ex))
|
||||
trades = list_review_trades(
|
||||
conn,
|
||||
include_hedge_legs=include_hedge_legs,
|
||||
closed_from=closed_from,
|
||||
limit=limit,
|
||||
offset=0,
|
||||
)
|
||||
stats = compute_review_stats(
|
||||
conn,
|
||||
include_hedge_legs=include_hedge_legs,
|
||||
closed_from=closed_from,
|
||||
)
|
||||
finally:
|
||||
conn.close()
|
||||
return jsonify(
|
||||
{
|
||||
"ok": True,
|
||||
"days": days,
|
||||
"limit": limit,
|
||||
"product": "options",
|
||||
"trades": trades,
|
||||
"stats": stats,
|
||||
}
|
||||
)
|
||||
|
||||
@app.route("/api/hub/trades/today")
|
||||
@_hub_auth_required
|
||||
def api_hub_trades_today():
|
||||
|
||||
@@ -0,0 +1,599 @@
|
||||
"""中控期权档案:同步 OKX options_review_trades 到 hub_symbol_archive.db."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import time
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from lib.hub.hub_symbol_archive_lib import (
|
||||
TRADING_DAY_RESET_HOUR,
|
||||
_connect,
|
||||
default_db_path,
|
||||
init_db as init_perp_archive_db,
|
||||
ms_to_trading_day,
|
||||
parse_wall_clock_ms,
|
||||
resolve_period_bounds,
|
||||
trading_day_bounds_ms,
|
||||
)
|
||||
|
||||
|
||||
def _now_ms() -> int:
|
||||
return int(time.time() * 1000)
|
||||
|
||||
|
||||
def init_options_archive_db(db_path: Path | None = None) -> None:
|
||||
"""确保期权缓存表存在(与永续共用同一 SQLite)."""
|
||||
init_perp_archive_db(db_path)
|
||||
conn = _connect(db_path)
|
||||
try:
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS archive_options_trade_cache (
|
||||
exchange_key TEXT NOT NULL,
|
||||
history_key TEXT NOT NULL,
|
||||
source_type TEXT,
|
||||
underlying TEXT,
|
||||
opened_at TEXT,
|
||||
closed_at TEXT,
|
||||
opened_at_ms INTEGER,
|
||||
closed_at_ms INTEGER,
|
||||
hold_seconds INTEGER,
|
||||
realized_pnl_total REAL,
|
||||
status_raw TEXT,
|
||||
pos_id TEXT,
|
||||
inst_id TEXT,
|
||||
opt_type TEXT,
|
||||
strike REAL,
|
||||
exp_time TEXT,
|
||||
sheets INTEGER,
|
||||
open_avg REAL,
|
||||
close_avg REAL,
|
||||
premium_paid REAL,
|
||||
realized_pnl REAL,
|
||||
hedge_plan_id INTEGER,
|
||||
plan_close_reason TEXT,
|
||||
realized_pnl_perp REAL,
|
||||
realized_pnl_options REAL,
|
||||
premium_total REAL,
|
||||
direction TEXT,
|
||||
tp REAL,
|
||||
sl REAL,
|
||||
target_price REAL,
|
||||
target_price_up REAL,
|
||||
target_price_down REAL,
|
||||
legs_json TEXT,
|
||||
linked_hedge_plan_id INTEGER,
|
||||
excluded_as_hedge_leg INTEGER DEFAULT 0,
|
||||
strategy_tag TEXT,
|
||||
result_tag TEXT,
|
||||
reviewed INTEGER DEFAULT 0,
|
||||
source_label TEXT,
|
||||
payload_json TEXT NOT NULL,
|
||||
synced_at INTEGER NOT NULL,
|
||||
PRIMARY KEY (exchange_key, history_key)
|
||||
)
|
||||
"""
|
||||
)
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE INDEX IF NOT EXISTS idx_archive_options_closed
|
||||
ON archive_options_trade_cache (exchange_key, closed_at_ms)
|
||||
"""
|
||||
)
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def purge_stale_options_trades_cache(
|
||||
exchange_key: str,
|
||||
active_history_keys: list[str],
|
||||
*,
|
||||
db_path: Path | None = None,
|
||||
) -> int:
|
||||
init_options_archive_db(db_path)
|
||||
ex_k = (exchange_key or "").strip().lower()
|
||||
if not ex_k:
|
||||
return 0
|
||||
active = {str(k).strip() for k in (active_history_keys or []) if str(k).strip()}
|
||||
conn = _connect(db_path)
|
||||
try:
|
||||
rows = conn.execute(
|
||||
"SELECT history_key FROM archive_options_trade_cache WHERE exchange_key=?",
|
||||
(ex_k,),
|
||||
).fetchall()
|
||||
stale = [r["history_key"] for r in rows if r["history_key"] not in active]
|
||||
removed = 0
|
||||
for hk in stale:
|
||||
cur = conn.execute(
|
||||
"DELETE FROM archive_options_trade_cache WHERE exchange_key=? AND history_key=?",
|
||||
(ex_k, hk),
|
||||
)
|
||||
removed += int(cur.rowcount or 0)
|
||||
return removed
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def _optional_float(raw: Any) -> float | None:
|
||||
if raw in (None, ""):
|
||||
return None
|
||||
try:
|
||||
return float(raw)
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
|
||||
|
||||
def _optional_int(raw: Any) -> int | None:
|
||||
if raw in (None, ""):
|
||||
return None
|
||||
try:
|
||||
return int(raw)
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
|
||||
|
||||
def upsert_options_trades_cache(
|
||||
exchange_key: str,
|
||||
trades: list[dict[str, Any]],
|
||||
*,
|
||||
db_path: Path | None = None,
|
||||
prune_missing: bool = True,
|
||||
) -> dict[str, int]:
|
||||
init_options_archive_db(db_path)
|
||||
ex_k = (exchange_key or "").strip().lower()
|
||||
if not ex_k:
|
||||
return {"upserted": 0, "removed": 0}
|
||||
now = _now_ms()
|
||||
n = 0
|
||||
active_keys: list[str] = []
|
||||
conn = _connect(db_path)
|
||||
try:
|
||||
for t in trades or []:
|
||||
if not isinstance(t, dict):
|
||||
continue
|
||||
hk = str(t.get("history_key") or "").strip()
|
||||
if not hk:
|
||||
continue
|
||||
if int(t.get("excluded_as_hedge_leg") or 0):
|
||||
continue
|
||||
active_keys.append(hk)
|
||||
opened_at = t.get("opened_at")
|
||||
closed_at = t.get("closed_at")
|
||||
opened_ms = t.get("opened_at_ms") or parse_wall_clock_ms(opened_at)
|
||||
closed_ms = t.get("closed_at_ms") or parse_wall_clock_ms(closed_at)
|
||||
entry = t.get("entry") if isinstance(t.get("entry"), dict) else {}
|
||||
strategy_tag = t.get("strategy_tag") or (entry or {}).get("strategy_tag")
|
||||
result_tag = t.get("result_tag") or (entry or {}).get("result_tag")
|
||||
reviewed = 1 if t.get("reviewed") or entry else 0
|
||||
row = dict(t)
|
||||
row["exchange_key"] = ex_k
|
||||
payload = json.dumps(row, ensure_ascii=False, default=str)
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO archive_options_trade_cache (
|
||||
exchange_key, history_key, source_type, underlying,
|
||||
opened_at, closed_at, opened_at_ms, closed_at_ms, hold_seconds,
|
||||
realized_pnl_total, status_raw,
|
||||
pos_id, inst_id, opt_type, strike, exp_time, sheets,
|
||||
open_avg, close_avg, premium_paid, realized_pnl,
|
||||
hedge_plan_id, plan_close_reason, realized_pnl_perp, realized_pnl_options,
|
||||
premium_total, direction, tp, sl, target_price, target_price_up, target_price_down,
|
||||
legs_json, linked_hedge_plan_id, excluded_as_hedge_leg,
|
||||
strategy_tag, result_tag, reviewed, source_label,
|
||||
payload_json, synced_at
|
||||
) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
|
||||
ON CONFLICT(exchange_key, history_key) DO UPDATE SET
|
||||
source_type=excluded.source_type,
|
||||
underlying=excluded.underlying,
|
||||
opened_at=excluded.opened_at,
|
||||
closed_at=excluded.closed_at,
|
||||
opened_at_ms=excluded.opened_at_ms,
|
||||
closed_at_ms=excluded.closed_at_ms,
|
||||
hold_seconds=excluded.hold_seconds,
|
||||
realized_pnl_total=excluded.realized_pnl_total,
|
||||
status_raw=excluded.status_raw,
|
||||
pos_id=excluded.pos_id,
|
||||
inst_id=excluded.inst_id,
|
||||
opt_type=excluded.opt_type,
|
||||
strike=excluded.strike,
|
||||
exp_time=excluded.exp_time,
|
||||
sheets=excluded.sheets,
|
||||
open_avg=excluded.open_avg,
|
||||
close_avg=excluded.close_avg,
|
||||
premium_paid=excluded.premium_paid,
|
||||
realized_pnl=excluded.realized_pnl,
|
||||
hedge_plan_id=excluded.hedge_plan_id,
|
||||
plan_close_reason=excluded.plan_close_reason,
|
||||
realized_pnl_perp=excluded.realized_pnl_perp,
|
||||
realized_pnl_options=excluded.realized_pnl_options,
|
||||
premium_total=excluded.premium_total,
|
||||
direction=excluded.direction,
|
||||
tp=excluded.tp,
|
||||
sl=excluded.sl,
|
||||
target_price=excluded.target_price,
|
||||
target_price_up=excluded.target_price_up,
|
||||
target_price_down=excluded.target_price_down,
|
||||
legs_json=excluded.legs_json,
|
||||
linked_hedge_plan_id=excluded.linked_hedge_plan_id,
|
||||
excluded_as_hedge_leg=excluded.excluded_as_hedge_leg,
|
||||
strategy_tag=excluded.strategy_tag,
|
||||
result_tag=excluded.result_tag,
|
||||
reviewed=excluded.reviewed,
|
||||
source_label=excluded.source_label,
|
||||
payload_json=excluded.payload_json,
|
||||
synced_at=excluded.synced_at
|
||||
""",
|
||||
(
|
||||
ex_k,
|
||||
hk,
|
||||
t.get("source_type"),
|
||||
t.get("underlying"),
|
||||
opened_at,
|
||||
closed_at,
|
||||
int(opened_ms) if opened_ms else None,
|
||||
int(closed_ms) if closed_ms else None,
|
||||
_optional_int(t.get("hold_seconds")),
|
||||
float(t.get("realized_pnl_total") or t.get("realized_pnl") or 0),
|
||||
t.get("status_raw"),
|
||||
t.get("pos_id"),
|
||||
t.get("inst_id"),
|
||||
t.get("opt_type"),
|
||||
_optional_float(t.get("strike")),
|
||||
t.get("exp_time"),
|
||||
_optional_int(t.get("sheets")),
|
||||
_optional_float(t.get("open_avg")),
|
||||
_optional_float(t.get("close_avg")),
|
||||
_optional_float(t.get("premium_paid")),
|
||||
_optional_float(t.get("realized_pnl")),
|
||||
_optional_int(t.get("hedge_plan_id")),
|
||||
t.get("plan_close_reason"),
|
||||
_optional_float(t.get("realized_pnl_perp")),
|
||||
_optional_float(t.get("realized_pnl_options")),
|
||||
_optional_float(t.get("premium_total")),
|
||||
t.get("direction"),
|
||||
_optional_float(t.get("tp")),
|
||||
_optional_float(t.get("sl")),
|
||||
_optional_float(t.get("target_price")),
|
||||
_optional_float(t.get("target_price_up")),
|
||||
_optional_float(t.get("target_price_down")),
|
||||
t.get("legs_json")
|
||||
if isinstance(t.get("legs_json"), str)
|
||||
else (json.dumps(t.get("legs"), ensure_ascii=False) if t.get("legs") else None),
|
||||
_optional_int(t.get("linked_hedge_plan_id")),
|
||||
int(t.get("excluded_as_hedge_leg") or 0),
|
||||
strategy_tag,
|
||||
result_tag,
|
||||
reviewed,
|
||||
t.get("source_label"),
|
||||
payload,
|
||||
now,
|
||||
),
|
||||
)
|
||||
n += 1
|
||||
finally:
|
||||
conn.close()
|
||||
removed = 0
|
||||
if prune_missing:
|
||||
removed = purge_stale_options_trades_cache(ex_k, active_keys, db_path=db_path)
|
||||
return {"upserted": n, "removed": removed}
|
||||
|
||||
|
||||
def _options_row_to_dict(row: Any) -> dict[str, Any]:
|
||||
out: dict[str, Any] = dict(row)
|
||||
payload = {}
|
||||
raw = out.get("payload_json")
|
||||
if raw:
|
||||
try:
|
||||
payload = json.loads(raw) if isinstance(raw, str) else {}
|
||||
except (TypeError, ValueError, json.JSONDecodeError):
|
||||
payload = {}
|
||||
if isinstance(payload, dict):
|
||||
for k, v in payload.items():
|
||||
if k not in out or out.get(k) in (None, ""):
|
||||
out[k] = v
|
||||
pnl = float(out.get("realized_pnl_total") or out.get("realized_pnl") or 0)
|
||||
out["realized_pnl_total"] = pnl
|
||||
out["pnl_amount"] = pnl # 复用永续统计/日历字段名
|
||||
hold_sec = out.get("hold_seconds")
|
||||
if hold_sec is not None:
|
||||
try:
|
||||
out["hold_minutes"] = round(float(hold_sec) / 60.0, 2)
|
||||
except (TypeError, ValueError):
|
||||
pass
|
||||
if not out.get("opened_at_ms") and out.get("opened_at"):
|
||||
ms = parse_wall_clock_ms(out.get("opened_at"))
|
||||
if ms:
|
||||
out["opened_at_ms"] = int(ms)
|
||||
if not out.get("closed_at_ms") and out.get("closed_at"):
|
||||
ms = parse_wall_clock_ms(out.get("closed_at"))
|
||||
if ms:
|
||||
out["closed_at_ms"] = int(ms)
|
||||
out["trade_id"] = out.get("history_key")
|
||||
out["id"] = out.get("history_key")
|
||||
out["symbol"] = out.get("inst_id") or out.get("underlying") or ""
|
||||
return out
|
||||
|
||||
|
||||
def _empty_options_stats() -> dict[str, Any]:
|
||||
return {
|
||||
"open_count": 0,
|
||||
"sick_count": 0,
|
||||
"sick_pct": 0.0,
|
||||
"pnl_total": 0.0,
|
||||
"pnl_ex_sick": 0.0,
|
||||
"win_count": 0,
|
||||
"loss_count": 0,
|
||||
"avg_win": 0.0,
|
||||
"avg_loss": 0.0,
|
||||
"max_win": 0.0,
|
||||
"max_loss": 0.0,
|
||||
"win_rate": 0.0,
|
||||
"profit_loss_ratio": 0.0,
|
||||
"turnover_total": 0.0,
|
||||
"commission_total": 0.0,
|
||||
"premium_total": 0.0,
|
||||
"by_exchange": {},
|
||||
"by_source_type": {},
|
||||
}
|
||||
|
||||
|
||||
def _compute_options_period_stats(trade_rows: list[dict[str, Any]]) -> dict[str, Any]:
|
||||
st = _empty_options_stats()
|
||||
wins: list[float] = []
|
||||
losses: list[float] = []
|
||||
by_ex: dict[str, dict[str, Any]] = {}
|
||||
by_src: dict[str, dict[str, Any]] = {}
|
||||
|
||||
def bucket() -> dict[str, Any]:
|
||||
return {
|
||||
"open_count": 0,
|
||||
"pnl_total": 0.0,
|
||||
"win_count": 0,
|
||||
"loss_count": 0,
|
||||
"premium_total": 0.0,
|
||||
}
|
||||
|
||||
for td in trade_rows:
|
||||
pnl = float(td.get("pnl_amount") or td.get("realized_pnl_total") or 0)
|
||||
ex = str(td.get("exchange_key") or "okx")
|
||||
src = str(td.get("source_type") or td.get("source_label") or "?")
|
||||
prem = float(td.get("premium_total") or td.get("premium_paid") or 0)
|
||||
st["open_count"] += 1
|
||||
st["pnl_total"] += pnl
|
||||
st["premium_total"] += prem
|
||||
if pnl > 0.0001:
|
||||
st["win_count"] += 1
|
||||
wins.append(pnl)
|
||||
elif pnl < -0.0001:
|
||||
st["loss_count"] += 1
|
||||
losses.append(pnl)
|
||||
if ex not in by_ex:
|
||||
by_ex[ex] = bucket()
|
||||
by_ex[ex]["open_count"] += 1
|
||||
by_ex[ex]["pnl_total"] += pnl
|
||||
by_ex[ex]["premium_total"] += prem
|
||||
if pnl > 0.0001:
|
||||
by_ex[ex]["win_count"] += 1
|
||||
elif pnl < -0.0001:
|
||||
by_ex[ex]["loss_count"] += 1
|
||||
if src not in by_src:
|
||||
by_src[src] = bucket()
|
||||
by_src[src]["open_count"] += 1
|
||||
by_src[src]["pnl_total"] += pnl
|
||||
|
||||
total = int(st["open_count"] or 0)
|
||||
st["pnl_ex_sick"] = round(float(st["pnl_total"]), 4)
|
||||
st["pnl_total"] = round(float(st["pnl_total"]), 4)
|
||||
st["premium_total"] = round(float(st["premium_total"]), 4)
|
||||
st["avg_win"] = round(sum(wins) / len(wins), 4) if wins else 0.0
|
||||
st["avg_loss"] = round(sum(losses) / len(losses), 4) if losses else 0.0
|
||||
st["max_win"] = round(max(wins), 4) if wins else 0.0
|
||||
st["max_loss"] = round(min(losses), 4) if losses else 0.0
|
||||
st["win_rate"] = round(st["win_count"] / total * 100, 1) if total else 0.0
|
||||
if wins and losses and abs(st["avg_loss"]) > 1e-9:
|
||||
st["profit_loss_ratio"] = round(abs(st["avg_win"] / st["avg_loss"]), 2)
|
||||
for ex, b in by_ex.items():
|
||||
b["pnl_total"] = round(float(b["pnl_total"]), 4)
|
||||
b["premium_total"] = round(float(b["premium_total"]), 4)
|
||||
b["sick_count"] = 0
|
||||
b["sick_pct"] = 0.0
|
||||
b["pnl_ex_sick"] = b["pnl_total"]
|
||||
b["avg_win"] = 0.0
|
||||
b["avg_loss"] = 0.0
|
||||
b["max_win"] = 0.0
|
||||
b["max_loss"] = 0.0
|
||||
b["win_rate"] = (
|
||||
round(b["win_count"] / b["open_count"] * 100, 1) if b["open_count"] else 0.0
|
||||
)
|
||||
b["profit_loss_ratio"] = 0.0
|
||||
b["turnover_total"] = 0.0
|
||||
b["commission_total"] = 0.0
|
||||
for src, b in by_src.items():
|
||||
b["pnl_total"] = round(float(b["pnl_total"]), 4)
|
||||
st["by_exchange"] = by_ex
|
||||
st["by_source_type"] = by_src
|
||||
return st
|
||||
|
||||
|
||||
def list_daily_options_trades(
|
||||
trading_day: str = "",
|
||||
*,
|
||||
period: str = "",
|
||||
date_from: str = "",
|
||||
date_to: str = "",
|
||||
exchange_key: str = "",
|
||||
filter_profit: bool = False,
|
||||
filter_loss: bool = False,
|
||||
search: str = "",
|
||||
source_type: str = "",
|
||||
db_path: Path | None = None,
|
||||
) -> dict[str, Any]:
|
||||
init_options_archive_db(db_path)
|
||||
p = (period or "today").strip().lower() or "today"
|
||||
start_ms, end_ms, df, dt, period_label = resolve_period_bounds(
|
||||
period=p,
|
||||
trading_day=trading_day,
|
||||
date_from=date_from,
|
||||
date_to=date_to,
|
||||
)
|
||||
ex_filter = (exchange_key or "").strip().lower()
|
||||
src_filter = (source_type or "").strip().lower()
|
||||
conn = _connect(db_path)
|
||||
try:
|
||||
params: list[Any] = [start_ms, end_ms]
|
||||
where = "closed_at_ms IS NOT NULL AND closed_at_ms >= ? AND closed_at_ms < ?"
|
||||
where += " AND COALESCE(excluded_as_hedge_leg,0)=0"
|
||||
if ex_filter:
|
||||
where += " AND exchange_key=?"
|
||||
params.append(ex_filter)
|
||||
if src_filter:
|
||||
where += " AND LOWER(COALESCE(source_type,''))=?"
|
||||
params.append(src_filter)
|
||||
rows = conn.execute(
|
||||
f"""
|
||||
SELECT * FROM archive_options_trade_cache
|
||||
WHERE {where}
|
||||
ORDER BY closed_at_ms DESC, history_key DESC
|
||||
""",
|
||||
params,
|
||||
).fetchall()
|
||||
trades: list[dict[str, Any]] = []
|
||||
q = (search or "").strip().lower()
|
||||
for r in rows:
|
||||
td = _options_row_to_dict(r)
|
||||
pnl = float(td.get("pnl_amount") or 0)
|
||||
if filter_profit and pnl <= 0.0001:
|
||||
continue
|
||||
if filter_loss and pnl >= -0.0001:
|
||||
continue
|
||||
if q:
|
||||
blob = " ".join(
|
||||
str(td.get(k) or "")
|
||||
for k in (
|
||||
"underlying",
|
||||
"inst_id",
|
||||
"exchange_key",
|
||||
"source_type",
|
||||
"source_label",
|
||||
"opt_type",
|
||||
"strategy_tag",
|
||||
"result_tag",
|
||||
"direction",
|
||||
)
|
||||
).lower()
|
||||
if q not in blob:
|
||||
continue
|
||||
trades.append(td)
|
||||
return {
|
||||
"period": p,
|
||||
"period_label": period_label,
|
||||
"trading_day": dt,
|
||||
"date_from": df,
|
||||
"date_to": dt,
|
||||
"product": "options",
|
||||
"trades": trades,
|
||||
"stats": _compute_options_period_stats(trades),
|
||||
}
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def list_archive_options_calendar(
|
||||
year: int,
|
||||
month: int,
|
||||
*,
|
||||
exchange_key: str = "",
|
||||
db_path: Path | None = None,
|
||||
reset_hour: int = TRADING_DAY_RESET_HOUR,
|
||||
) -> dict[str, Any]:
|
||||
init_options_archive_db(db_path)
|
||||
y = int(year)
|
||||
m = int(month)
|
||||
if m < 1 or m > 12:
|
||||
raise ValueError("month 无效")
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
first = f"{y:04d}-{m:02d}-01"
|
||||
if m == 12:
|
||||
next_first = datetime(y + 1, 1, 1)
|
||||
else:
|
||||
next_first = datetime(y, m + 1, 1)
|
||||
last = (next_first - timedelta(days=1)).strftime("%Y-%m-%d")
|
||||
start_ms, _ = trading_day_bounds_ms(first, reset_hour=reset_hour)
|
||||
_, end_ms = trading_day_bounds_ms(last, reset_hour=reset_hour)
|
||||
ex_filter = (exchange_key or "").strip().lower()
|
||||
conn = _connect(db_path)
|
||||
try:
|
||||
params: list[Any] = [start_ms, end_ms]
|
||||
where = (
|
||||
"closed_at_ms IS NOT NULL AND closed_at_ms >= ? AND closed_at_ms < ?"
|
||||
" AND COALESCE(excluded_as_hedge_leg,0)=0"
|
||||
)
|
||||
if ex_filter:
|
||||
where += " AND exchange_key=?"
|
||||
params.append(ex_filter)
|
||||
rows = conn.execute(
|
||||
f"SELECT * FROM archive_options_trade_cache WHERE {where}",
|
||||
params,
|
||||
).fetchall()
|
||||
days: dict[str, dict[str, Any]] = {}
|
||||
for r in rows:
|
||||
td = _options_row_to_dict(r)
|
||||
closed_ms = td.get("closed_at_ms") or parse_wall_clock_ms(td.get("closed_at"))
|
||||
if not closed_ms:
|
||||
continue
|
||||
day = ms_to_trading_day(int(closed_ms), reset_hour=reset_hour)
|
||||
if not day or day < first or day > last:
|
||||
continue
|
||||
bucket = days.setdefault(
|
||||
day,
|
||||
{
|
||||
"trading_day": day,
|
||||
"open_count": 0,
|
||||
"sick_count": 0,
|
||||
"pnl_total": 0.0,
|
||||
"turnover_total": 0.0,
|
||||
"commission_total": 0.0,
|
||||
"has_sick": False,
|
||||
},
|
||||
)
|
||||
bucket["open_count"] += 1
|
||||
bucket["pnl_total"] += float(td.get("pnl_amount") or 0)
|
||||
for d in days.values():
|
||||
d["pnl_total"] = round(float(d["pnl_total"]), 4)
|
||||
month_pnl = sum(float(d["pnl_total"]) for d in days.values())
|
||||
month_count = sum(int(d["open_count"]) for d in days.values())
|
||||
return {
|
||||
"year": y,
|
||||
"month": m,
|
||||
"date_from": first,
|
||||
"date_to": last,
|
||||
"product": "options",
|
||||
"days": days,
|
||||
"month_pnl_total": round(month_pnl, 4),
|
||||
"month_open_count": month_count,
|
||||
}
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def sync_options_exchange_archive(
|
||||
exchange_key: str,
|
||||
trades: list[dict[str, Any]],
|
||||
*,
|
||||
db_path: Path | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""仅缓存期权交易,不做 K 线."""
|
||||
r = upsert_options_trades_cache(
|
||||
exchange_key, trades, db_path=db_path, prune_missing=True
|
||||
)
|
||||
return {
|
||||
"ok": True,
|
||||
"exchange_key": (exchange_key or "").strip().lower(),
|
||||
"product": "options",
|
||||
"trades_upserted": r.get("upserted", 0),
|
||||
"trades_removed": r.get("removed", 0),
|
||||
"trade_count": len(trades or []),
|
||||
}
|
||||
@@ -10,12 +10,24 @@ from typing import Any
|
||||
|
||||
from lib.paths import REPO_ROOT
|
||||
|
||||
STRATEGY_EXCHANGES: tuple[str, ...] = ("playbook", "behavior", "binance", "okx", "gate")
|
||||
STRATEGY_EXCHANGES: tuple[str, ...] = (
|
||||
"playbook_v2",
|
||||
"playbook",
|
||||
"behavior",
|
||||
"binance",
|
||||
"okx",
|
||||
"gate",
|
||||
)
|
||||
|
||||
STRATEGY_META: dict[str, dict[str, str]] = {
|
||||
"playbook_v2": {
|
||||
"label": "执行手册v2",
|
||||
"title": "交易执行手册 v2(期权 / 合约 · 无对冲)",
|
||||
"md_rel": "docs/交易执行手册-v2-期权与合约.md",
|
||||
},
|
||||
"playbook": {
|
||||
"label": "执行手册",
|
||||
"title": "交易执行手册(期权为主 · Gate 为辅)",
|
||||
"label": "执行手册v1",
|
||||
"title": "交易执行手册 v1(期权为主 · Gate 为辅 · 含对冲)",
|
||||
# 相对仓库根;其余条目用 md_file 相对 docs/strategy
|
||||
"md_rel": "docs/交易执行手册-期权与Gate.md",
|
||||
},
|
||||
@@ -224,11 +236,30 @@ def load_strategy_payload(exchange_key: str) -> dict[str, Any]:
|
||||
}
|
||||
|
||||
|
||||
def strategy_meta_payload() -> dict[str, Any]:
|
||||
tabs = [
|
||||
{"key": k, "label": STRATEGY_META[k]["label"], "title": STRATEGY_META[k]["title"]}
|
||||
for k in STRATEGY_EXCHANGES
|
||||
]
|
||||
_STRATEGY_TAB_DISPLAY_PREF: dict[str, str] = {
|
||||
"playbook_v2": "show_strategy_playbook_v2",
|
||||
"playbook": "show_strategy_playbook",
|
||||
"behavior": "show_strategy_behavior",
|
||||
"binance": "show_strategy_binance",
|
||||
"okx": "show_strategy_okx",
|
||||
"gate": "show_strategy_gate",
|
||||
}
|
||||
|
||||
|
||||
def strategy_meta_payload(display: dict[str, Any] | None = None) -> dict[str, Any]:
|
||||
prefs = display if isinstance(display, dict) else {}
|
||||
tabs = []
|
||||
for k in STRATEGY_EXCHANGES:
|
||||
pref_key = _STRATEGY_TAB_DISPLAY_PREF.get(k)
|
||||
if pref_key and prefs.get(pref_key) is False:
|
||||
continue
|
||||
tabs.append(
|
||||
{
|
||||
"key": k,
|
||||
"label": STRATEGY_META[k]["label"],
|
||||
"title": STRATEGY_META[k]["title"],
|
||||
}
|
||||
)
|
||||
return {"ok": True, "exchanges": tabs}
|
||||
|
||||
|
||||
|
||||
@@ -9,6 +9,8 @@ DISPLAY_RUNTIME_PREFIX = "display."
|
||||
|
||||
DEFAULT_INSTANCE_DISPLAY: dict[str, bool] = {
|
||||
"show_nav_dashboard": False,
|
||||
"show_nav_key_monitor": True,
|
||||
"show_nav_trade": True,
|
||||
"show_nav_strategy": True,
|
||||
"show_nav_strategy_records": True,
|
||||
"show_nav_records": True,
|
||||
@@ -28,6 +30,8 @@ DEFAULT_INSTANCE_DISPLAY: dict[str, bool] = {
|
||||
|
||||
DISPLAY_LABELS: dict[str, str] = {
|
||||
"show_nav_dashboard": "数据看板",
|
||||
"show_nav_key_monitor": "关键位监控",
|
||||
"show_nav_trade": "实盘下单",
|
||||
"show_nav_strategy": "策略交易",
|
||||
"show_nav_strategy_records": "策略交易记录",
|
||||
"show_nav_records": "交易记录与复盘",
|
||||
@@ -47,6 +51,8 @@ DISPLAY_LABELS: dict[str, str] = {
|
||||
|
||||
NAV_TAB_ALLOWED: dict[str, str] = {
|
||||
"dashboard": "show_nav_dashboard",
|
||||
"key_monitor": "show_nav_key_monitor",
|
||||
"trade": "show_nav_trade",
|
||||
"strategy": "show_nav_strategy",
|
||||
"strategy_records": "show_nav_strategy_records",
|
||||
"records": "show_nav_records",
|
||||
@@ -110,6 +116,8 @@ def tab_allowed(tab: str, display: Optional[dict[str, bool]] = None) -> bool:
|
||||
def display_meta_for_ui() -> list[dict[str, Any]]:
|
||||
nav_keys = [
|
||||
"show_nav_dashboard",
|
||||
"show_nav_key_monitor",
|
||||
"show_nav_trade",
|
||||
"show_nav_strategy",
|
||||
"show_nav_strategy_records",
|
||||
"show_nav_records",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{# 系统设置 · 导航显示开关(SSR 预渲染,保存仍走 API) #}
|
||||
<div class="settings-tab-inner" id="display-prefs-card">
|
||||
<h2>导航显示</h2>
|
||||
<p class="settings-env-hint">以下开关控制顶栏导航与系统设置内区块是否显示,保存后立即生效.关键位监控,实盘下单,系统设置为固定项.</p>
|
||||
<p class="settings-env-hint">以下开关控制顶栏导航与系统设置内区块是否显示,保存后立即生效.系统设置为固定项.</p>
|
||||
<div id="display-prefs-form" class="display-prefs-form" data-prefs-ssr="1">
|
||||
{% if display_meta %}
|
||||
{% for group in display_meta %}
|
||||
|
||||
@@ -31,8 +31,8 @@
|
||||
</div>
|
||||
<nav class="top-nav embed-top-nav" aria-label="实例导航">
|
||||
<a href="/dashboard" data-embed-tab="dashboard" class="{% if initial_tab == 'dashboard' %}active{% endif %}"{% if not display.show_nav_dashboard %} style="display:none"{% endif %}>数据看板</a>
|
||||
<a href="/key_monitor" data-embed-tab="key_monitor" class="{% if initial_tab == 'key_monitor' %}active{% endif %}">关键位监控</a>
|
||||
<a href="/trade" data-embed-tab="trade" class="{% if initial_tab == 'trade' %}active{% endif %}">实盘下单</a>
|
||||
<a href="/key_monitor" data-embed-tab="key_monitor" class="{% if initial_tab == 'key_monitor' %}active{% endif %}"{% if not display.show_nav_key_monitor %} style="display:none"{% endif %}>关键位监控</a>
|
||||
<a href="/trade" data-embed-tab="trade" class="{% if initial_tab == 'trade' %}active{% endif %}"{% if not display.show_nav_trade %} style="display:none"{% endif %}>实盘下单</a>
|
||||
{% if not intraday_discipline and display.show_nav_strategy %}
|
||||
<a href="/strategy" data-embed-tab="strategy" class="{% if initial_tab == 'strategy' %}active{% endif %}">策略交易</a>
|
||||
{% endif %}
|
||||
|
||||
@@ -118,8 +118,8 @@
|
||||
</div>
|
||||
<div class="top-nav">
|
||||
<a href="/dashboard" data-embed-tab="dashboard" class="{% if page == 'dashboard' %}active{% endif %}"{% if not display.show_nav_dashboard %} style="display:none"{% endif %}>数据看板</a>
|
||||
<a href="/key_monitor" class="{% if page == 'key_monitor' %}active{% endif %}">关键位监控</a>
|
||||
<a href="/trade" class="{% if page == 'trade' %}active{% endif %}">实盘下单</a>
|
||||
<a href="/key_monitor" class="{% if page == 'key_monitor' %}active{% endif %}"{% if not display.show_nav_key_monitor %} style="display:none"{% endif %}>关键位监控</a>
|
||||
<a href="/trade" class="{% if page == 'trade' %}active{% endif %}"{% if not display.show_nav_trade %} style="display:none"{% endif %}>实盘下单</a>
|
||||
{% if not intraday_discipline and display.show_nav_strategy %}
|
||||
<a href="/strategy" class="{% if page in ('strategy', 'strategy_trend', 'strategy_roll') %}active{% endif %}">策略交易</a>
|
||||
{% endif %}
|
||||
|
||||
@@ -95,6 +95,14 @@ def init_options_tables(conn: sqlite3.Connection) -> None:
|
||||
ON options_target_monitors(status)
|
||||
"""
|
||||
)
|
||||
for ddl in (
|
||||
"ALTER TABLE options_trades ADD COLUMN wechat_open_sent INTEGER DEFAULT 0",
|
||||
"ALTER TABLE options_trades ADD COLUMN wechat_close_sent INTEGER DEFAULT 0",
|
||||
):
|
||||
try:
|
||||
conn.execute(ddl)
|
||||
except Exception:
|
||||
pass
|
||||
init_options_review_tables(conn)
|
||||
|
||||
|
||||
|
||||
@@ -237,6 +237,7 @@ def sync_open_options_trades(
|
||||
*,
|
||||
live_inst_ids: set[str],
|
||||
fetch_history_fn: Callable[[str], list[dict[str, Any]]],
|
||||
notify_cfg: dict[str, Any] | None = None,
|
||||
) -> int:
|
||||
"""
|
||||
交易所已无持仓时,将本地 open 记录同步为 closed.
|
||||
@@ -319,6 +320,24 @@ def sync_open_options_trades(
|
||||
),
|
||||
)
|
||||
updated += 1
|
||||
if notify_cfg is not None:
|
||||
try:
|
||||
from lib.options.options_notify_lib import notify_options_close
|
||||
|
||||
reason = "到期结算" if close_reason == "expired" else "交易所平仓"
|
||||
notify_options_close(
|
||||
notify_cfg,
|
||||
conn,
|
||||
inst_id=inst_id,
|
||||
reason=reason,
|
||||
trade_id=int(row["id"]),
|
||||
premium_paid=paid,
|
||||
premium_received=prem_recv,
|
||||
realized_pnl=realized_pnl,
|
||||
close_quote=close_quote,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return updated
|
||||
|
||||
|
||||
@@ -414,6 +433,7 @@ def options_monitor_loop(
|
||||
close_fn=target_close_fn,
|
||||
send_wechat=send_wechat,
|
||||
account_label=account_label,
|
||||
cfg={"send_wechat": send_wechat, "account_label": account_label},
|
||||
)
|
||||
if sync_trades_fn is not None:
|
||||
sync_trades_fn(conn)
|
||||
|
||||
@@ -0,0 +1,330 @@
|
||||
"""OKX 期权开仓/平仓企业微信推送(必发,幂等落库标记)."""
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlite3
|
||||
from typing import Any, Callable, Optional
|
||||
|
||||
|
||||
def _fmt(v: Any, d: int = 4) -> str:
|
||||
try:
|
||||
if v is None or v == "":
|
||||
return "—"
|
||||
return f"{float(v):.{d}f}"
|
||||
except (TypeError, ValueError):
|
||||
return str(v)
|
||||
|
||||
|
||||
def _opt_type_label(opt_type: Any) -> str:
|
||||
t = str(opt_type or "").strip().upper()
|
||||
if t in ("C", "CALL"):
|
||||
return "Call"
|
||||
if t in ("P", "PUT"):
|
||||
return "Put"
|
||||
return t or "—"
|
||||
|
||||
|
||||
def ensure_options_notify_columns(conn: sqlite3.Connection) -> None:
|
||||
for ddl in (
|
||||
"ALTER TABLE options_trades ADD COLUMN wechat_open_sent INTEGER DEFAULT 0",
|
||||
"ALTER TABLE options_trades ADD COLUMN wechat_close_sent INTEGER DEFAULT 0",
|
||||
):
|
||||
try:
|
||||
conn.execute(ddl)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def notify_options_send(cfg: dict[str, Any], content: str) -> bool:
|
||||
send: Optional[Callable[[str], Any]] = cfg.get("send_wechat")
|
||||
if not callable(send):
|
||||
return False
|
||||
try:
|
||||
send(content)
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def build_options_open_message(
|
||||
*,
|
||||
account_label: str,
|
||||
inst_id: str,
|
||||
underlying: str = "",
|
||||
opt_type: Any = None,
|
||||
sheets: Any = None,
|
||||
premium_paid: Any = None,
|
||||
open_quote: Any = None,
|
||||
target_index: Any = None,
|
||||
signal_note: str = "",
|
||||
trade_id: Any = None,
|
||||
) -> str:
|
||||
lines = [
|
||||
"【OKX期权·开仓】",
|
||||
f"账户:{account_label or 'OKX期权'}",
|
||||
]
|
||||
if trade_id is not None:
|
||||
lines.append(f"本地单号:#{trade_id}")
|
||||
lines.extend(
|
||||
[
|
||||
f"合约:{inst_id}",
|
||||
f"标的:{(underlying or '—')} · {_opt_type_label(opt_type)}",
|
||||
f"张数:{sheets if sheets is not None else '—'}",
|
||||
f"开仓报价:{_fmt(open_quote)} USDC",
|
||||
f"权利金:{_fmt(premium_paid)} USDC",
|
||||
]
|
||||
)
|
||||
if target_index is not None and str(target_index).strip() != "":
|
||||
try:
|
||||
lines.append(f"目标指数:{float(target_index):g}")
|
||||
except (TypeError, ValueError):
|
||||
lines.append(f"目标指数:{target_index}")
|
||||
if signal_note:
|
||||
lines.append(f"备注:{signal_note[:200]}")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def build_options_close_message(
|
||||
*,
|
||||
account_label: str,
|
||||
inst_id: str,
|
||||
reason: str = "",
|
||||
underlying: str = "",
|
||||
opt_type: Any = None,
|
||||
sheets: Any = None,
|
||||
premium_paid: Any = None,
|
||||
premium_received: Any = None,
|
||||
realized_pnl: Any = None,
|
||||
close_quote: Any = None,
|
||||
target_index: Any = None,
|
||||
trigger_idx: Any = None,
|
||||
trade_id: Any = None,
|
||||
) -> str:
|
||||
lines = [
|
||||
"【OKX期权·平仓】",
|
||||
f"账户:{account_label or 'OKX期权'}",
|
||||
]
|
||||
if trade_id is not None:
|
||||
lines.append(f"本地单号:#{trade_id}")
|
||||
lines.extend(
|
||||
[
|
||||
f"合约:{inst_id}",
|
||||
f"标的:{(underlying or '—')} · {_opt_type_label(opt_type)}",
|
||||
f"原因:{(reason or '平仓').strip()}",
|
||||
f"张数:{sheets if sheets is not None else '—'}",
|
||||
f"平仓报价:{_fmt(close_quote)} USDC",
|
||||
f"已付/收回:{_fmt(premium_paid)} / {_fmt(premium_received)} USDC",
|
||||
f"实现盈亏:{_fmt(realized_pnl, 4)} USDC",
|
||||
]
|
||||
)
|
||||
if target_index is not None and str(target_index).strip() != "":
|
||||
try:
|
||||
lines.append(f"目标指数:{float(target_index):g}")
|
||||
except (TypeError, ValueError):
|
||||
lines.append(f"目标指数:{target_index}")
|
||||
if trigger_idx is not None and str(trigger_idx).strip() != "":
|
||||
try:
|
||||
lines.append(f"触发指数:{float(trigger_idx):g}")
|
||||
except (TypeError, ValueError):
|
||||
lines.append(f"触发指数:{trigger_idx}")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def notify_options_open(
|
||||
cfg: dict[str, Any],
|
||||
conn: sqlite3.Connection | None,
|
||||
*,
|
||||
trade_id: int | None,
|
||||
inst_id: str,
|
||||
underlying: str = "",
|
||||
opt_type: Any = None,
|
||||
sheets: Any = None,
|
||||
premium_paid: Any = None,
|
||||
open_quote: Any = None,
|
||||
target_index: Any = None,
|
||||
signal_note: str = "",
|
||||
) -> bool:
|
||||
ensure_options_notify_columns(conn) if conn is not None else None
|
||||
if conn is not None and trade_id is not None:
|
||||
row = conn.execute(
|
||||
"SELECT wechat_open_sent FROM options_trades WHERE id=?",
|
||||
(int(trade_id),),
|
||||
).fetchone()
|
||||
if row and int(row["wechat_open_sent"] or 0):
|
||||
return False
|
||||
msg = build_options_open_message(
|
||||
account_label=str(cfg.get("account_label") or "OKX期权"),
|
||||
inst_id=inst_id,
|
||||
underlying=underlying,
|
||||
opt_type=opt_type,
|
||||
sheets=sheets,
|
||||
premium_paid=premium_paid,
|
||||
open_quote=open_quote,
|
||||
target_index=target_index,
|
||||
signal_note=signal_note,
|
||||
trade_id=trade_id,
|
||||
)
|
||||
ok = notify_options_send(cfg, msg)
|
||||
if ok and conn is not None and trade_id is not None:
|
||||
conn.execute(
|
||||
"UPDATE options_trades SET wechat_open_sent=1 WHERE id=?",
|
||||
(int(trade_id),),
|
||||
)
|
||||
try:
|
||||
conn.commit()
|
||||
except Exception:
|
||||
pass
|
||||
return ok
|
||||
|
||||
|
||||
def _load_trade_row(conn: sqlite3.Connection, trade_id: int) -> dict[str, Any] | None:
|
||||
row = conn.execute("SELECT * FROM options_trades WHERE id=?", (int(trade_id),)).fetchone()
|
||||
return dict(row) if row else None
|
||||
|
||||
|
||||
def notify_options_close(
|
||||
cfg: dict[str, Any],
|
||||
conn: sqlite3.Connection | None,
|
||||
*,
|
||||
inst_id: str,
|
||||
reason: str = "平仓",
|
||||
trade_id: int | None = None,
|
||||
underlying: str = "",
|
||||
opt_type: Any = None,
|
||||
sheets: Any = None,
|
||||
premium_paid: Any = None,
|
||||
premium_received: Any = None,
|
||||
realized_pnl: Any = None,
|
||||
close_quote: Any = None,
|
||||
target_index: Any = None,
|
||||
trigger_idx: Any = None,
|
||||
force: bool = False,
|
||||
) -> bool:
|
||||
"""平仓必发.默认按 trade_id / 同合约未标记行幂等."""
|
||||
if conn is not None:
|
||||
ensure_options_notify_columns(conn)
|
||||
rows: list[dict[str, Any]] = []
|
||||
if conn is not None and trade_id is not None:
|
||||
r = _load_trade_row(conn, int(trade_id))
|
||||
if r:
|
||||
rows = [r]
|
||||
elif conn is not None and inst_id:
|
||||
q = conn.execute(
|
||||
"""
|
||||
SELECT * FROM options_trades
|
||||
WHERE inst_id=? AND status='closed'
|
||||
AND COALESCE(wechat_close_sent,0)=0
|
||||
ORDER BY id DESC
|
||||
LIMIT 20
|
||||
""",
|
||||
(inst_id,),
|
||||
).fetchall()
|
||||
rows = [dict(x) for x in q]
|
||||
if not rows and force:
|
||||
q2 = conn.execute(
|
||||
"""
|
||||
SELECT * FROM options_trades
|
||||
WHERE inst_id=? AND status='closed'
|
||||
ORDER BY id DESC LIMIT 1
|
||||
""",
|
||||
(inst_id,),
|
||||
).fetchone()
|
||||
if q2:
|
||||
rows = [dict(q2)]
|
||||
|
||||
if rows:
|
||||
# 同次平仓可能多腿:合并一条推送,逐条标记
|
||||
total_paid = sum(float(r.get("premium_paid") or 0) for r in rows)
|
||||
total_recv = sum(float(r.get("premium_received") or 0) for r in rows if r.get("premium_received") is not None)
|
||||
pnls = [float(r["realized_pnl"]) for r in rows if r.get("realized_pnl") is not None]
|
||||
total_pnl = sum(pnls) if pnls else None
|
||||
if total_pnl is None and (premium_received is not None or realized_pnl is not None):
|
||||
total_pnl = realized_pnl
|
||||
total_recv = premium_received if premium_received is not None else total_recv
|
||||
total_paid = premium_paid if premium_paid is not None else total_paid
|
||||
head = rows[0]
|
||||
pending = [r for r in rows if not int(r.get("wechat_close_sent") or 0)]
|
||||
if not pending and not force:
|
||||
return False
|
||||
msg = build_options_close_message(
|
||||
account_label=str(cfg.get("account_label") or "OKX期权"),
|
||||
inst_id=inst_id or str(head.get("inst_id") or ""),
|
||||
reason=reason,
|
||||
underlying=underlying or str(head.get("underlying") or ""),
|
||||
opt_type=opt_type or head.get("opt_type"),
|
||||
sheets=sheets if sheets is not None else sum(int(r.get("sheets") or 0) for r in rows),
|
||||
premium_paid=total_paid,
|
||||
premium_received=total_recv if rows else premium_received,
|
||||
realized_pnl=total_pnl,
|
||||
close_quote=close_quote if close_quote is not None else head.get("close_quote"),
|
||||
target_index=target_index,
|
||||
trigger_idx=trigger_idx,
|
||||
trade_id=head.get("id") if len(rows) == 1 else None,
|
||||
)
|
||||
ok = notify_options_send(cfg, msg)
|
||||
if ok and conn is not None:
|
||||
for r in pending or rows:
|
||||
conn.execute(
|
||||
"UPDATE options_trades SET wechat_close_sent=1 WHERE id=?",
|
||||
(int(r["id"]),),
|
||||
)
|
||||
try:
|
||||
conn.commit()
|
||||
except Exception:
|
||||
pass
|
||||
return ok
|
||||
|
||||
# 无库行时仍发一条(尽量不丢提醒)
|
||||
msg = build_options_close_message(
|
||||
account_label=str(cfg.get("account_label") or "OKX期权"),
|
||||
inst_id=inst_id,
|
||||
reason=reason,
|
||||
underlying=underlying,
|
||||
opt_type=opt_type,
|
||||
sheets=sheets,
|
||||
premium_paid=premium_paid,
|
||||
premium_received=premium_received,
|
||||
realized_pnl=realized_pnl,
|
||||
close_quote=close_quote,
|
||||
target_index=target_index,
|
||||
trigger_idx=trigger_idx,
|
||||
trade_id=trade_id,
|
||||
)
|
||||
return notify_options_send(cfg, msg)
|
||||
|
||||
|
||||
def notify_options_close_trade_ids(
|
||||
cfg: dict[str, Any],
|
||||
conn: sqlite3.Connection,
|
||||
trade_ids: list[int],
|
||||
*,
|
||||
reason: str,
|
||||
) -> bool:
|
||||
ids = [int(x) for x in trade_ids if x is not None]
|
||||
if not ids:
|
||||
return False
|
||||
ensure_options_notify_columns(conn)
|
||||
placeholders = ",".join("?" for _ in ids)
|
||||
rows = conn.execute(
|
||||
f"""
|
||||
SELECT * FROM options_trades
|
||||
WHERE id IN ({placeholders}) AND COALESCE(wechat_close_sent,0)=0
|
||||
""",
|
||||
ids,
|
||||
).fetchall()
|
||||
if not rows:
|
||||
return False
|
||||
first = dict(rows[0])
|
||||
return notify_options_close(
|
||||
cfg,
|
||||
conn,
|
||||
inst_id=str(first.get("inst_id") or ""),
|
||||
reason=reason,
|
||||
trade_id=int(first["id"]) if len(rows) == 1 else None,
|
||||
underlying=str(first.get("underlying") or ""),
|
||||
opt_type=first.get("opt_type"),
|
||||
sheets=sum(int(r["sheets"] or 0) for r in rows),
|
||||
premium_paid=sum(float(r["premium_paid"] or 0) for r in rows),
|
||||
premium_received=sum(float(r["premium_received"] or 0) for r in rows if r["premium_received"] is not None),
|
||||
realized_pnl=sum(float(r["realized_pnl"]) for r in rows if r["realized_pnl"] is not None),
|
||||
close_quote=first.get("close_quote"),
|
||||
)
|
||||
@@ -640,11 +640,15 @@ def register_options_routes(app: Flask, cfg: dict[str, Any]) -> None:
|
||||
conn = cfg["get_db"]()
|
||||
trade_id = None
|
||||
target_mon = None
|
||||
open_underlying = ""
|
||||
open_opt_type = None
|
||||
try:
|
||||
init_options_tables(conn)
|
||||
meta = q.get("meta") or {}
|
||||
u = str(meta.get("uly") or inst_id).split("-")[0]
|
||||
opt_type = meta.get("optType")
|
||||
open_underlying = u
|
||||
open_opt_type = opt_type
|
||||
cur = conn.execute(
|
||||
"""
|
||||
INSERT INTO options_trades
|
||||
@@ -683,9 +687,30 @@ def register_options_routes(app: Flask, cfg: dict[str, Any]) -> None:
|
||||
finally:
|
||||
conn.close()
|
||||
from lib.exchange.okx_options_lib import invalidate_option_positions_cache
|
||||
from lib.options.options_notify_lib import notify_options_open
|
||||
|
||||
invalidate_option_positions_cache()
|
||||
_sync_options_trades(cfg, force=True)
|
||||
try:
|
||||
conn_n = cfg["get_db"]()
|
||||
try:
|
||||
notify_options_open(
|
||||
cfg,
|
||||
conn_n,
|
||||
trade_id=trade_id,
|
||||
inst_id=inst_id,
|
||||
underlying=open_underlying,
|
||||
opt_type=open_opt_type,
|
||||
sheets=sheets,
|
||||
premium_paid=sizing.get("total_premium"),
|
||||
open_quote=float(ask) if ask is not None else None,
|
||||
target_index=target_index,
|
||||
signal_note=signal_note,
|
||||
)
|
||||
finally:
|
||||
conn_n.close()
|
||||
except Exception:
|
||||
pass
|
||||
return jsonify(
|
||||
{
|
||||
"ok": True,
|
||||
@@ -938,11 +963,21 @@ def register_options_routes(app: Flask, cfg: dict[str, Any]) -> None:
|
||||
if result.get("fully_closed"):
|
||||
try:
|
||||
from lib.options.options_target_lib import cancel_target_monitor
|
||||
from lib.options.options_notify_lib import notify_options_close
|
||||
|
||||
conn2 = cfg["get_db"]()
|
||||
try:
|
||||
cancel_target_monitor(conn2, inst_id=inst_id)
|
||||
conn2.commit()
|
||||
notify_options_close(
|
||||
cfg,
|
||||
conn2,
|
||||
inst_id=inst_id,
|
||||
reason="手动平仓",
|
||||
sheets=result.get("submitted_sheets"),
|
||||
premium_received=result.get("premium_received"),
|
||||
close_quote=result.get("locked_bid_px") or result.get("bid"),
|
||||
)
|
||||
finally:
|
||||
conn2.close()
|
||||
except Exception:
|
||||
@@ -1253,6 +1288,7 @@ def _start_monitor_thread(app: Flask, cfg: dict[str, Any]) -> None:
|
||||
conn,
|
||||
live_inst_ids=live_ids,
|
||||
fetch_history_fn=lambda inst_id: fetch_option_position_history(ex, inst_id),
|
||||
notify_cfg=cfg,
|
||||
)
|
||||
|
||||
def _target_close(inst_id: str) -> dict[str, Any]:
|
||||
|
||||
@@ -292,6 +292,7 @@ def close_option_by_bid_depth(
|
||||
|
||||
|
||||
def _notify_target_close(
|
||||
cfg: dict[str, Any] | None,
|
||||
send_wechat: Callable[[str], None] | None,
|
||||
*,
|
||||
account_label: str,
|
||||
@@ -299,7 +300,28 @@ def _notify_target_close(
|
||||
target: float,
|
||||
idx: float,
|
||||
result: dict[str, Any],
|
||||
conn: Any = None,
|
||||
) -> None:
|
||||
"""目标位平仓推送:优先走统一平仓必发(幂等);无 cfg 时回退旧文案."""
|
||||
if result.get("fully_closed") or result.get("already_flat"):
|
||||
if cfg is not None:
|
||||
try:
|
||||
from lib.options.options_notify_lib import notify_options_close
|
||||
|
||||
notify_options_close(
|
||||
cfg,
|
||||
conn,
|
||||
inst_id=inst_id,
|
||||
reason="目标位平仓",
|
||||
sheets=result.get("submitted_sheets"),
|
||||
premium_received=result.get("premium_received"),
|
||||
close_quote=result.get("locked_bid_px") or result.get("bid"),
|
||||
target_index=target,
|
||||
trigger_idx=idx,
|
||||
)
|
||||
return
|
||||
except Exception:
|
||||
pass
|
||||
if not send_wechat:
|
||||
return
|
||||
try:
|
||||
@@ -313,6 +335,7 @@ def _notify_target_close(
|
||||
f"触发指数:{idx:g}",
|
||||
f"提交张数:{result.get('submitted_sheets') or '—'}",
|
||||
f"预估收回:{result.get('premium_received') if result.get('premium_received') is not None else '—'} USDC",
|
||||
f"状态:{'已全平' if (result.get('fully_closed') or result.get('already_flat')) else '挂单中/部分'}",
|
||||
]
|
||||
)
|
||||
)
|
||||
@@ -339,6 +362,7 @@ def run_options_target_closes(
|
||||
index_fn: Callable[[dict[str, Any]], float | None] | None = None,
|
||||
send_wechat: Callable[[str], None] | None = None,
|
||||
account_label: str = "OKX期权",
|
||||
cfg: dict[str, Any] | None = None,
|
||||
) -> int:
|
||||
"""
|
||||
扫描 active 目标委托;指数到位后限价平仓.
|
||||
@@ -433,11 +457,13 @@ def run_options_target_closes(
|
||||
_commit_monitor(conn)
|
||||
triggered += 1
|
||||
_notify_target_close(
|
||||
cfg,
|
||||
send_wechat,
|
||||
account_label=account_label,
|
||||
inst_id=inst_id,
|
||||
target=target,
|
||||
idx=idx,
|
||||
result=result,
|
||||
conn=conn,
|
||||
)
|
||||
return triggered
|
||||
|
||||
+150
-20
@@ -79,6 +79,12 @@ from lib.hub.hub_symbol_archive_lib import (
|
||||
update_review_quote,
|
||||
upsert_trade_overlay,
|
||||
)
|
||||
from lib.hub.hub_options_archive_lib import (
|
||||
init_options_archive_db,
|
||||
list_archive_options_calendar,
|
||||
list_daily_options_trades,
|
||||
sync_options_exchange_archive,
|
||||
)
|
||||
from lib.hub.hub_entry_plan_lib import (
|
||||
compute_entry_plan_stats,
|
||||
create_entry_plan,
|
||||
@@ -355,9 +361,11 @@ def _schedule_board_refresh() -> None:
|
||||
async def _run_archive_sync_once() -> dict:
|
||||
global _last_archive_sync
|
||||
init_archive_db()
|
||||
init_options_archive_db()
|
||||
settings = load_settings()
|
||||
targets = enabled_exchanges(settings)
|
||||
results: list[dict] = []
|
||||
options_results: list[dict] = []
|
||||
for ex in targets:
|
||||
ex_key = str(ex.get("key") or "").strip().lower()
|
||||
if not ex_key:
|
||||
@@ -390,34 +398,71 @@ async def _run_archive_sync_once() -> dict:
|
||||
"msg": msg,
|
||||
}
|
||||
)
|
||||
else:
|
||||
trades = trades_resp.get("trades") or []
|
||||
for t in trades:
|
||||
if isinstance(t, dict):
|
||||
t["exchange_key"] = ex_key
|
||||
|
||||
def remote_fetch(**kwargs):
|
||||
return _fetch_instance_ohlcv_sync(
|
||||
ex,
|
||||
symbol=kwargs.get("symbol") or "",
|
||||
timeframe=kwargs.get("timeframe") or "5m",
|
||||
since_ms=kwargs.get("since_ms"),
|
||||
limit=int(kwargs.get("limit") or 500),
|
||||
)
|
||||
|
||||
r = await asyncio.to_thread(
|
||||
sync_exchange_symbol_archives,
|
||||
ex_key,
|
||||
trades,
|
||||
remote_fetch,
|
||||
)
|
||||
r["name"] = ex.get("name")
|
||||
r["trade_count"] = len(trades)
|
||||
results.append(r)
|
||||
|
||||
caps = [str(x).lower() for x in (ex.get("capabilities") or [])]
|
||||
if "options" not in caps:
|
||||
continue
|
||||
trades = trades_resp.get("trades") or []
|
||||
for t in trades:
|
||||
opt_resp = await asyncio.to_thread(
|
||||
_fetch_instance_options_review_archive_sync,
|
||||
ex,
|
||||
days=ARCHIVE_TRADE_DAYS,
|
||||
limit=ARCHIVE_TRADE_LIMIT,
|
||||
)
|
||||
if not opt_resp.get("ok"):
|
||||
options_results.append(
|
||||
{
|
||||
"exchange_key": ex_key,
|
||||
"name": ex.get("name"),
|
||||
"ok": False,
|
||||
"status": opt_resp.get("status"),
|
||||
"msg": opt_resp.get("msg")
|
||||
or opt_resp.get("error")
|
||||
or opt_resp.get("detail")
|
||||
or "拉取期权复盘失败",
|
||||
"product": "options",
|
||||
}
|
||||
)
|
||||
continue
|
||||
opt_trades = opt_resp.get("trades") or []
|
||||
for t in opt_trades:
|
||||
if isinstance(t, dict):
|
||||
t["exchange_key"] = ex_key
|
||||
|
||||
def remote_fetch(**kwargs):
|
||||
return _fetch_instance_ohlcv_sync(
|
||||
ex,
|
||||
symbol=kwargs.get("symbol") or "",
|
||||
timeframe=kwargs.get("timeframe") or "5m",
|
||||
since_ms=kwargs.get("since_ms"),
|
||||
limit=int(kwargs.get("limit") or 500),
|
||||
)
|
||||
|
||||
r = await asyncio.to_thread(
|
||||
sync_exchange_symbol_archives,
|
||||
orow = await asyncio.to_thread(
|
||||
sync_options_exchange_archive,
|
||||
ex_key,
|
||||
trades,
|
||||
remote_fetch,
|
||||
opt_trades,
|
||||
)
|
||||
r["name"] = ex.get("name")
|
||||
r["trade_count"] = len(trades)
|
||||
results.append(r)
|
||||
orow["name"] = ex.get("name")
|
||||
options_results.append(orow)
|
||||
out = {
|
||||
"ok": True,
|
||||
"exchanges": len(targets),
|
||||
"results": results,
|
||||
"options_results": options_results,
|
||||
"updated_at": __import__("datetime").datetime.now().isoformat(timespec="seconds"),
|
||||
}
|
||||
_last_archive_sync = out
|
||||
@@ -1119,6 +1164,16 @@ class SettingsDisplayBody(BaseModel):
|
||||
show_nav_amp_stats: bool = True
|
||||
show_nav_help: bool = True
|
||||
show_nav_logs: bool = True
|
||||
show_monitor_binance: bool = True
|
||||
show_monitor_okx_perp: bool = True
|
||||
show_monitor_okx_options: bool = True
|
||||
show_monitor_gate: bool = True
|
||||
show_strategy_playbook_v2: bool = True
|
||||
show_strategy_playbook: bool = True
|
||||
show_strategy_behavior: bool = True
|
||||
show_strategy_binance: bool = True
|
||||
show_strategy_okx: bool = True
|
||||
show_strategy_gate: bool = True
|
||||
|
||||
|
||||
class SupervisorSettingsBody(BaseModel):
|
||||
@@ -1355,6 +1410,34 @@ def _fetch_instance_trades_archive_sync(
|
||||
return {"ok": False, "msg": str(e)}
|
||||
|
||||
|
||||
def _fetch_instance_options_review_archive_sync(
|
||||
ex: dict,
|
||||
*,
|
||||
days: int = 365,
|
||||
limit: int = 2000,
|
||||
) -> dict:
|
||||
base = (ex.get("flask_url") or "").rstrip("/")
|
||||
if not base:
|
||||
return {"ok": False, "msg": "未配置 flask_url"}
|
||||
params = {"days": str(int(days)), "limit": str(int(limit))}
|
||||
url = f"{base}/api/hub/options/review/archive?{urlencode(params)}"
|
||||
try:
|
||||
with httpx.Client(timeout=max(HUB_FLASK_TIMEOUT, 120.0)) as client:
|
||||
r = client.get(url, headers=_hub_headers())
|
||||
if r.status_code >= 400:
|
||||
parsed = _parse_http_json_body(r)
|
||||
parsed.setdefault("ok", False)
|
||||
parsed.setdefault("status", r.status_code)
|
||||
return parsed
|
||||
data = r.json() if r.content else {}
|
||||
if isinstance(data, dict):
|
||||
data.setdefault("ok", True)
|
||||
return data
|
||||
return {"ok": False, "msg": "无效 JSON"}
|
||||
except Exception as e:
|
||||
return {"ok": False, "msg": str(e)}
|
||||
|
||||
|
||||
def _fetch_instance_ohlcv_sync(
|
||||
ex: dict,
|
||||
*,
|
||||
@@ -3135,6 +3218,52 @@ def api_archive_calendar(
|
||||
return {"ok": True, **payload}
|
||||
|
||||
|
||||
@app.get("/api/archive/options/daily-trades")
|
||||
def api_archive_options_daily_trades(
|
||||
period: str = "",
|
||||
trading_day: str = "",
|
||||
date_from: str = "",
|
||||
date_to: str = "",
|
||||
exchange_key: str = "",
|
||||
filter_profit: str = "",
|
||||
filter_loss: str = "",
|
||||
search: str = "",
|
||||
source_type: str = "",
|
||||
):
|
||||
init_options_archive_db()
|
||||
payload = list_daily_options_trades(
|
||||
trading_day=trading_day,
|
||||
period=period or "today",
|
||||
date_from=date_from,
|
||||
date_to=date_to,
|
||||
exchange_key=exchange_key,
|
||||
filter_profit=(filter_profit or "").lower() in ("1", "true", "yes", "on"),
|
||||
filter_loss=(filter_loss or "").lower() in ("1", "true", "yes", "on"),
|
||||
search=search,
|
||||
source_type=source_type,
|
||||
)
|
||||
return {"ok": True, **payload}
|
||||
|
||||
|
||||
@app.get("/api/archive/options/calendar")
|
||||
def api_archive_options_calendar(
|
||||
year: int = 0,
|
||||
month: int = 0,
|
||||
exchange_key: str = "",
|
||||
):
|
||||
init_options_archive_db()
|
||||
if year <= 0 or month <= 0:
|
||||
td = today_trading_day()
|
||||
parts = td.split("-")
|
||||
year = int(parts[0])
|
||||
month = int(parts[1])
|
||||
try:
|
||||
payload = list_archive_options_calendar(year, month, exchange_key=exchange_key)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e)) from e
|
||||
return {"ok": True, **payload}
|
||||
|
||||
|
||||
@app.get("/api/archive/quotes")
|
||||
def api_archive_quotes():
|
||||
init_archive_db()
|
||||
@@ -3348,7 +3477,8 @@ async def api_archive_sync():
|
||||
|
||||
@app.get("/api/strategy/meta")
|
||||
def api_strategy_meta():
|
||||
return strategy_meta_payload()
|
||||
display = (load_settings() or {}).get("display") or {}
|
||||
return strategy_meta_payload(display)
|
||||
|
||||
|
||||
@app.get("/api/help/meta")
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
"""交易教练用的执行手册短摘要(来源 docs/交易执行手册-期权与Gate.md)."""
|
||||
"""交易教练用的执行手册短摘要(现行 v2:无对冲)."""
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
@@ -6,19 +6,21 @@ from pathlib import Path
|
||||
from lib.paths import REPO_ROOT
|
||||
|
||||
# 控制 token:保持简短;手册大改时同步修订本摘要.
|
||||
_PLAYBOOK_BRIEF = """【用户策略执行手册·摘要】(来源:docs/交易执行手册-期权与Gate.md + docs/交易行为准则-开单三检.md)
|
||||
开单防火墙(强制):信号判断(核心点位是否清晰)→流程确认(资金/单笔敞口超限则暂停)→情绪自检(符合系统才做;怕踏空/回本/证明自己→放弃)。三检不过不开。成败先看三检是否跑完,不看这笔盈亏。
|
||||
一句话:横盘对冲(可偏置实值);突破用一天期权;假破确认后小仓合约加强;先过方向/空间/值不值得;期权不手平;一位置两次,错完收工;单笔小亏、组合回撤可控.
|
||||
分工:OKX 期权=主业;Gate 合约=辅业;其它账户暂不做.同一时段尽量只让一边说话.
|
||||
入场三类:①横盘较久→期期对冲(一天 Call+Put,总权利金约10U),期间一般不开 Gate;②方向/空间/值不值得过关且结构突破→一天期权方向单,默认不上合约;③已有突破期权后出现反向假破确认→Gate 小仓加强(加重暴露,按一笔故事控风险).
|
||||
仓位(总资约800U):单笔期权约10U且一次一仓;期期对冲合计约10U;Gate 保证金约50U×约10x,止损约5U,单笔最亏约≤10U;日最坏约≤20U.
|
||||
期权纪律:不手动平仓,只认规则止盈或到期(紧急手平非策略样本);默认一天期,尽量北京时间16:00后开次日到期.
|
||||
Gate 纪律:只做很明确位置;同一位置最多两次机会(结构突破/假突破);两次都错→当日收工.
|
||||
教练用法:对照上述纪律讨论执行与心态;开单前优先提醒三检;勿另造策略或鼓励期权手平/超仓."""
|
||||
_PLAYBOOK_BRIEF = """【用户策略执行手册·摘要】(来源:docs/交易执行手册-v2-期权与合约.md + docs/交易行为准则-开单三检.md)
|
||||
开单防火墙(强制):信号判断→流程确认→情绪自检。三检不过不开。成败先看三检是否跑完。
|
||||
主链条(强制):1H方向(含N字)→空间(支撑/阻力,至少约≥2%)→结构(约8h+/48×15m:收敛/两段回调/箱体/假突破等)→定损盈(结构突破=外沿;假突破=针尖)→选工具(只剩期权或合约)。
|
||||
丢掉对冲:不做期期对冲/偏置对冲;对冲易产生「有保护就能多做」的幻觉。日更不是目标,过检才开。
|
||||
一句话:1H定方向→量空间→等够结构→按模型定损盈→只在期权与合约里选;期权不手平;Gate一位置两次,错完收工;珍惜机会。
|
||||
分工:OKX期权与Gate合约;其它账户暂不做。同一时段尽量只让一边说话。
|
||||
入场:①主链条过关→一天期期权方向单(空间够可优先虚值);②极明确位置→Gate合约;独立假破优先只做合约或空仓。明确不做横盘双买对冲。
|
||||
仓位(总资约800U):单笔期权约10U且一次一仓;Gate保证金约50U×约10x,止损约5U,单笔最亏约≤10U;日最坏约≤20U。
|
||||
期权纪律:开仓后中间不手动平仓,只认规则止盈或到期;默认一天期,尽量北京时间16:00后开次日到期。
|
||||
Gate纪律:只做很明确位置;同一位置最多两次(结构突破/假突破);两次都错→当日收工。
|
||||
教练用法:对照上述纪律讨论执行与心态;开单前优先提醒主链条与三检;勿另造策略或鼓励对冲/期权手平/超仓/每天默认开期权。"""
|
||||
|
||||
|
||||
def playbook_md_path() -> Path:
|
||||
return REPO_ROOT / "docs" / "交易执行手册-期权与Gate.md"
|
||||
return REPO_ROOT / "docs" / "交易执行手册-v2-期权与合约.md"
|
||||
|
||||
|
||||
def format_playbook_brief_for_chat(max_chars: int = 1200) -> str:
|
||||
|
||||
@@ -34,6 +34,18 @@ DEFAULT_DISPLAY = {
|
||||
"show_nav_amp_stats": True,
|
||||
"show_nav_help": True,
|
||||
"show_nav_logs": True,
|
||||
# 监控区卡片(仅隐藏界面,不关闭账户)
|
||||
"show_monitor_binance": True,
|
||||
"show_monitor_okx_perp": True,
|
||||
"show_monitor_okx_options": True,
|
||||
"show_monitor_gate": True,
|
||||
# 策略说明页签
|
||||
"show_strategy_playbook_v2": True,
|
||||
"show_strategy_playbook": True,
|
||||
"show_strategy_behavior": True,
|
||||
"show_strategy_binance": True,
|
||||
"show_strategy_okx": True,
|
||||
"show_strategy_gate": True,
|
||||
}
|
||||
|
||||
DEFAULT_EXCHANGES = [
|
||||
|
||||
+155
-115
@@ -3501,6 +3501,18 @@ button.btn-sm {
|
||||
margin-top: 8px;
|
||||
}
|
||||
|
||||
.settings-display-subtitle {
|
||||
margin: 16px 0 6px;
|
||||
font-size: 0.82rem;
|
||||
font-weight: 600;
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
.settings-display-subtitle + .settings-display-hint {
|
||||
margin-top: 0;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.settings-display-hint {
|
||||
margin: 8px 0 0;
|
||||
font-size: 0.78rem;
|
||||
@@ -8045,6 +8057,34 @@ body.funds-fullscreen-open {
|
||||
gap: 12px;
|
||||
align-items: stretch;
|
||||
}
|
||||
.archive-product-tabs {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 8px;
|
||||
margin: 0 0 10px;
|
||||
}
|
||||
.archive-product-tab {
|
||||
border: 1px solid var(--border-soft);
|
||||
background: transparent;
|
||||
color: inherit;
|
||||
padding: 8px 18px;
|
||||
border-radius: 999px;
|
||||
cursor: pointer;
|
||||
font-family: var(--font);
|
||||
font-size: 0.88rem;
|
||||
font-weight: 600;
|
||||
}
|
||||
.archive-product-tab.is-active {
|
||||
background: rgba(16, 185, 129, 0.2);
|
||||
border-color: rgba(16, 185, 129, 0.55);
|
||||
color: var(--text);
|
||||
}
|
||||
body.archive-product-options .archive-toolbar-desktop[data-archive-perp-only],
|
||||
body.archive-product-options #archive-btn-chart-toggle,
|
||||
body.archive-product-options #archive-filter-sick,
|
||||
body.archive-product-options #archive-tab-viz {
|
||||
display: none !important;
|
||||
}
|
||||
.archive-content-tabs {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
@@ -11008,118 +11048,118 @@ html[data-theme="light"] .hub-logs-card-hint {
|
||||
.amp-form { grid-template-columns: 1fr 1fr; }
|
||||
.amp-actions { grid-column: 1 / -1; }
|
||||
}
|
||||
|
||||
/* --- strategy compare --- */
|
||||
#page-compare .toolbar {
|
||||
padding: 12px 16px;
|
||||
margin-bottom: 14px;
|
||||
}
|
||||
.cmp-form { display: flex; flex-direction: column; gap: 14px; margin-bottom: 16px; }
|
||||
.cmp-form .card,
|
||||
.cmp-common-card,
|
||||
.cmp-sum-card,
|
||||
.cmp-rec-card {
|
||||
padding: 18px 20px;
|
||||
}
|
||||
.cmp-common-card h2,
|
||||
.cmp-form .card h2 {
|
||||
margin: 0 0 14px;
|
||||
font-size: 15px;
|
||||
}
|
||||
.cmp-subhead {
|
||||
margin: 16px 0 10px;
|
||||
font-size: 13px;
|
||||
color: var(--muted);
|
||||
font-weight: 600;
|
||||
}
|
||||
.cmp-form-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(3, minmax(0, 1fr));
|
||||
gap: 14px 16px;
|
||||
}
|
||||
.cmp-field {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 6px;
|
||||
font-size: 12px;
|
||||
color: var(--muted);
|
||||
}
|
||||
.cmp-field input,
|
||||
.cmp-field select {
|
||||
background: var(--inset-surface);
|
||||
border: 1px solid var(--border-soft);
|
||||
border-radius: 8px;
|
||||
color: var(--text);
|
||||
padding: 9px 12px;
|
||||
font-size: 13px;
|
||||
}
|
||||
.cmp-input-cols {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||
gap: 14px;
|
||||
}
|
||||
.cmp-summary {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(3, minmax(0, 1fr));
|
||||
gap: 14px;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
.cmp-sum-card h3 { margin: 0 0 12px; font-size: 14px; }
|
||||
.cmp-sum-row {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
gap: 12px;
|
||||
font-size: 12px;
|
||||
margin: 6px 0;
|
||||
color: var(--muted);
|
||||
}
|
||||
.cmp-sum-row strong { color: var(--text); font-weight: 600; }
|
||||
.cmp-muted { color: var(--muted); font-size: 12px; margin: 0; }
|
||||
.cmp-table-wrap { margin-bottom: 16px; }
|
||||
.cmp-table-scroll { overflow-x: auto; }
|
||||
.cmp-table {
|
||||
width: 100%;
|
||||
border-collapse: separate;
|
||||
border-spacing: 0;
|
||||
font-size: 13px;
|
||||
background: var(--panel);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: var(--radius);
|
||||
overflow: hidden;
|
||||
}
|
||||
.cmp-table th,
|
||||
.cmp-table td {
|
||||
border-bottom: 1px solid var(--border-soft);
|
||||
padding: 14px 16px;
|
||||
vertical-align: top;
|
||||
text-align: left;
|
||||
}
|
||||
.cmp-table th:first-child,
|
||||
.cmp-table td:first-child { width: 22%; }
|
||||
.cmp-table tr:last-child td { border-bottom: none; }
|
||||
.cmp-cell-note {
|
||||
margin-top: 6px;
|
||||
font-size: 11px;
|
||||
color: var(--muted);
|
||||
line-height: 1.4;
|
||||
}
|
||||
.cmp-pnl-pos { color: var(--green); font-weight: 600; }
|
||||
.cmp-pnl-neg { color: var(--red); font-weight: 600; }
|
||||
.cmp-rec-head { font-size: 16px; margin-bottom: 8px; }
|
||||
.cmp-rec-reason { margin: 0 0 10px; color: var(--muted); font-size: 13px; }
|
||||
.cmp-rec-list { margin: 0; padding-left: 20px; font-size: 13px; line-height: 1.55; }
|
||||
.cmp-warn { margin-top: 12px; font-size: 12px; color: var(--warn, #e6a23c); }
|
||||
.cmp-foot-note { margin: 12px 0 0; font-size: 11px; color: var(--muted); }
|
||||
@media (max-width: 900px) {
|
||||
.cmp-form .card,
|
||||
.cmp-common-card,
|
||||
.cmp-sum-card,
|
||||
.cmp-rec-card {
|
||||
padding: 16px;
|
||||
}
|
||||
.cmp-form-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
|
||||
.cmp-input-cols,
|
||||
.cmp-summary { grid-template-columns: 1fr; }
|
||||
.cmp-table th,
|
||||
.cmp-table td { padding: 12px 14px; }
|
||||
}
|
||||
|
||||
/* --- strategy compare --- */
|
||||
#page-compare .toolbar {
|
||||
padding: 12px 16px;
|
||||
margin-bottom: 14px;
|
||||
}
|
||||
.cmp-form { display: flex; flex-direction: column; gap: 14px; margin-bottom: 16px; }
|
||||
.cmp-form .card,
|
||||
.cmp-common-card,
|
||||
.cmp-sum-card,
|
||||
.cmp-rec-card {
|
||||
padding: 18px 20px;
|
||||
}
|
||||
.cmp-common-card h2,
|
||||
.cmp-form .card h2 {
|
||||
margin: 0 0 14px;
|
||||
font-size: 15px;
|
||||
}
|
||||
.cmp-subhead {
|
||||
margin: 16px 0 10px;
|
||||
font-size: 13px;
|
||||
color: var(--muted);
|
||||
font-weight: 600;
|
||||
}
|
||||
.cmp-form-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(3, minmax(0, 1fr));
|
||||
gap: 14px 16px;
|
||||
}
|
||||
.cmp-field {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 6px;
|
||||
font-size: 12px;
|
||||
color: var(--muted);
|
||||
}
|
||||
.cmp-field input,
|
||||
.cmp-field select {
|
||||
background: var(--inset-surface);
|
||||
border: 1px solid var(--border-soft);
|
||||
border-radius: 8px;
|
||||
color: var(--text);
|
||||
padding: 9px 12px;
|
||||
font-size: 13px;
|
||||
}
|
||||
.cmp-input-cols {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||
gap: 14px;
|
||||
}
|
||||
.cmp-summary {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(3, minmax(0, 1fr));
|
||||
gap: 14px;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
.cmp-sum-card h3 { margin: 0 0 12px; font-size: 14px; }
|
||||
.cmp-sum-row {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
gap: 12px;
|
||||
font-size: 12px;
|
||||
margin: 6px 0;
|
||||
color: var(--muted);
|
||||
}
|
||||
.cmp-sum-row strong { color: var(--text); font-weight: 600; }
|
||||
.cmp-muted { color: var(--muted); font-size: 12px; margin: 0; }
|
||||
.cmp-table-wrap { margin-bottom: 16px; }
|
||||
.cmp-table-scroll { overflow-x: auto; }
|
||||
.cmp-table {
|
||||
width: 100%;
|
||||
border-collapse: separate;
|
||||
border-spacing: 0;
|
||||
font-size: 13px;
|
||||
background: var(--panel);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: var(--radius);
|
||||
overflow: hidden;
|
||||
}
|
||||
.cmp-table th,
|
||||
.cmp-table td {
|
||||
border-bottom: 1px solid var(--border-soft);
|
||||
padding: 14px 16px;
|
||||
vertical-align: top;
|
||||
text-align: left;
|
||||
}
|
||||
.cmp-table th:first-child,
|
||||
.cmp-table td:first-child { width: 22%; }
|
||||
.cmp-table tr:last-child td { border-bottom: none; }
|
||||
.cmp-cell-note {
|
||||
margin-top: 6px;
|
||||
font-size: 11px;
|
||||
color: var(--muted);
|
||||
line-height: 1.4;
|
||||
}
|
||||
.cmp-pnl-pos { color: var(--green); font-weight: 600; }
|
||||
.cmp-pnl-neg { color: var(--red); font-weight: 600; }
|
||||
.cmp-rec-head { font-size: 16px; margin-bottom: 8px; }
|
||||
.cmp-rec-reason { margin: 0 0 10px; color: var(--muted); font-size: 13px; }
|
||||
.cmp-rec-list { margin: 0; padding-left: 20px; font-size: 13px; line-height: 1.55; }
|
||||
.cmp-warn { margin-top: 12px; font-size: 12px; color: var(--warn, #e6a23c); }
|
||||
.cmp-foot-note { margin: 12px 0 0; font-size: 11px; color: var(--muted); }
|
||||
@media (max-width: 900px) {
|
||||
.cmp-form .card,
|
||||
.cmp-common-card,
|
||||
.cmp-sum-card,
|
||||
.cmp-rec-card {
|
||||
padding: 16px;
|
||||
}
|
||||
.cmp-form-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
|
||||
.cmp-input-cols,
|
||||
.cmp-summary { grid-template-columns: 1fr; }
|
||||
.cmp-table th,
|
||||
.cmp-table td { padding: 12px 14px; }
|
||||
}
|
||||
|
||||
@@ -9,12 +9,42 @@
|
||||
return !!d[key];
|
||||
}
|
||||
|
||||
window.hubDisplayPref = displayPref;
|
||||
|
||||
function showAccountPnlPref() {
|
||||
return displayPref("show_account_pnl", true);
|
||||
}
|
||||
|
||||
window.hubShowAccountPnlPref = showAccountPnlPref;
|
||||
|
||||
function showMonitorBinancePref() {
|
||||
return displayPref("show_monitor_binance", true);
|
||||
}
|
||||
|
||||
function showMonitorOkxPerpPref() {
|
||||
return displayPref("show_monitor_okx_perp", true);
|
||||
}
|
||||
|
||||
function showMonitorOkxOptionsPref() {
|
||||
return displayPref("show_monitor_okx_options", true);
|
||||
}
|
||||
|
||||
function showMonitorGatePref() {
|
||||
return displayPref("show_monitor_gate", true);
|
||||
}
|
||||
|
||||
function monitorExchangeKeyVisible(key) {
|
||||
const k = String(key || "").toLowerCase();
|
||||
if (k === "binance") return showMonitorBinancePref();
|
||||
if (k === "gate") return showMonitorGatePref();
|
||||
if (k === "okx") return showMonitorOkxPerpPref() || showMonitorOkxOptionsPref();
|
||||
return true;
|
||||
}
|
||||
|
||||
function filterVisibleMonitorRows(rows) {
|
||||
return (rows || []).filter((r) => monitorExchangeKeyVisible(r && r.key));
|
||||
}
|
||||
|
||||
function showNavFundsPref() {
|
||||
return displayPref("show_nav_funds", true);
|
||||
}
|
||||
@@ -157,32 +187,33 @@
|
||||
|
||||
function syncDisplayPrefsUI(data) {
|
||||
const d = (data && data.display) || {};
|
||||
const pnlCb = document.getElementById("pref-show-account-pnl");
|
||||
const fundsCb = document.getElementById("pref-show-nav-funds");
|
||||
const dashCb = document.getElementById("pref-show-nav-dashboard");
|
||||
const planCb = document.getElementById("pref-show-nav-plan");
|
||||
const archiveCb = document.getElementById("pref-show-nav-archive");
|
||||
const quotesCb = document.getElementById("pref-show-nav-quotes");
|
||||
const aiCb = document.getElementById("pref-show-nav-ai");
|
||||
const calcCb = document.getElementById("pref-show-nav-calculator");
|
||||
const compareCb = document.getElementById("pref-show-nav-compare");
|
||||
const strategyCb = document.getElementById("pref-show-nav-strategy");
|
||||
const ampCb = document.getElementById("pref-show-nav-amp-stats");
|
||||
const helpCb = document.getElementById("pref-show-nav-help");
|
||||
const logsCb = document.getElementById("pref-show-nav-logs");
|
||||
if (pnlCb) pnlCb.checked = d.show_account_pnl !== false;
|
||||
if (fundsCb) fundsCb.checked = d.show_nav_funds !== false;
|
||||
if (dashCb) dashCb.checked = d.show_nav_dashboard !== false;
|
||||
if (planCb) planCb.checked = d.show_nav_plan !== false;
|
||||
if (archiveCb) archiveCb.checked = d.show_nav_archive !== false;
|
||||
if (quotesCb) quotesCb.checked = d.show_nav_quotes !== false;
|
||||
if (aiCb) aiCb.checked = d.show_nav_ai !== false;
|
||||
if (calcCb) calcCb.checked = d.show_nav_calculator !== false;
|
||||
if (compareCb) compareCb.checked = d.show_nav_compare !== false;
|
||||
if (strategyCb) strategyCb.checked = d.show_nav_strategy !== false;
|
||||
if (ampCb) ampCb.checked = d.show_nav_amp_stats !== false;
|
||||
if (helpCb) helpCb.checked = d.show_nav_help !== false;
|
||||
if (logsCb) logsCb.checked = d.show_nav_logs !== false;
|
||||
const setChk = (id, key) => {
|
||||
const el = document.getElementById(id);
|
||||
if (el) el.checked = d[key] !== false;
|
||||
};
|
||||
setChk("pref-show-account-pnl", "show_account_pnl");
|
||||
setChk("pref-show-nav-funds", "show_nav_funds");
|
||||
setChk("pref-show-nav-dashboard", "show_nav_dashboard");
|
||||
setChk("pref-show-nav-plan", "show_nav_plan");
|
||||
setChk("pref-show-nav-archive", "show_nav_archive");
|
||||
setChk("pref-show-nav-quotes", "show_nav_quotes");
|
||||
setChk("pref-show-nav-ai", "show_nav_ai");
|
||||
setChk("pref-show-nav-calculator", "show_nav_calculator");
|
||||
setChk("pref-show-nav-compare", "show_nav_compare");
|
||||
setChk("pref-show-nav-strategy", "show_nav_strategy");
|
||||
setChk("pref-show-nav-amp-stats", "show_nav_amp_stats");
|
||||
setChk("pref-show-nav-help", "show_nav_help");
|
||||
setChk("pref-show-nav-logs", "show_nav_logs");
|
||||
setChk("pref-show-monitor-binance", "show_monitor_binance");
|
||||
setChk("pref-show-monitor-okx-perp", "show_monitor_okx_perp");
|
||||
setChk("pref-show-monitor-okx-options", "show_monitor_okx_options");
|
||||
setChk("pref-show-monitor-gate", "show_monitor_gate");
|
||||
setChk("pref-show-strategy-playbook-v2", "show_strategy_playbook_v2");
|
||||
setChk("pref-show-strategy-playbook", "show_strategy_playbook");
|
||||
setChk("pref-show-strategy-behavior", "show_strategy_behavior");
|
||||
setChk("pref-show-strategy-binance", "show_strategy_binance");
|
||||
setChk("pref-show-strategy-okx", "show_strategy_okx");
|
||||
setChk("pref-show-strategy-gate", "show_strategy_gate");
|
||||
syncNavVisibility(data);
|
||||
}
|
||||
|
||||
@@ -1569,7 +1600,7 @@
|
||||
if (upd) upd.textContent = txt;
|
||||
if (updSum) updSum.textContent = txt;
|
||||
}
|
||||
updateMonitorAlertSummary(rows || []);
|
||||
updateMonitorAlertSummary(filterVisibleMonitorRows(rows || []));
|
||||
void refreshMacroRiskBanner(rows || []);
|
||||
renderMonitorGrid(rows || []);
|
||||
}
|
||||
@@ -2080,19 +2111,20 @@
|
||||
if (lastMonitorRows.length && nowMobile !== wasMobile) {
|
||||
wasMobile = nowMobile;
|
||||
renderMonitorGrid(lastMonitorRows);
|
||||
updateMonitorAlertSummary(lastMonitorRows);
|
||||
updateMonitorAlertSummary(filterVisibleMonitorRows(lastMonitorRows));
|
||||
syncHubMobileTabActive(currentPage());
|
||||
return;
|
||||
}
|
||||
wasMobile = nowMobile;
|
||||
const box = document.getElementById("monitor-grid");
|
||||
if (box && lastMonitorRows.length) {
|
||||
const split = monitorOptionsSplitActive(lastMonitorRows);
|
||||
syncMonitorGridColumns(box, lastMonitorRows.length + (lastMonitorTotals ? 1 : 0), {
|
||||
const visible = filterVisibleMonitorRows(lastMonitorRows);
|
||||
const split = monitorOptionsSplitActive(visible);
|
||||
syncMonitorGridColumns(box, visible.length + (lastMonitorTotals ? 1 : 0), {
|
||||
statsFirst: !!lastMonitorTotals && !split,
|
||||
optionsSplit: split,
|
||||
});
|
||||
updateMonitorAlertSummary(lastMonitorRows);
|
||||
updateMonitorAlertSummary(visible);
|
||||
}
|
||||
syncHubMobileTabActive(currentPage());
|
||||
}, 120);
|
||||
@@ -2486,11 +2518,12 @@
|
||||
const fs = document.getElementById("exchange-fullscreen");
|
||||
const fsInner = document.getElementById("exchange-fullscreen-inner");
|
||||
if (!box) return;
|
||||
if (expandedExchangeId && !rows.some((r) => String(r.id) === String(expandedExchangeId))) {
|
||||
const visibleSource = filterVisibleMonitorRows(rows);
|
||||
if (expandedExchangeId && !visibleSource.some((r) => String(r.id) === String(expandedExchangeId))) {
|
||||
closeExchangeFullscreen();
|
||||
}
|
||||
const mobileTiles = isMobileLayout() && !expandedExchangeId;
|
||||
const displayRows = mobileTiles ? sortRowsForMobileDashboard(rows) : rows;
|
||||
const displayRows = mobileTiles ? sortRowsForMobileDashboard(visibleSource) : visibleSource;
|
||||
const optionsSplit = monitorOptionsSplitActive(displayRows);
|
||||
monitorGridOptionsSplit = optionsSplit;
|
||||
const showStatsCard = !expandedExchangeId;
|
||||
@@ -2503,20 +2536,33 @@
|
||||
let cardsHtml = "";
|
||||
if (optionsSplit) {
|
||||
const okxRow = displayRows.find((r) => rowHasOptionsLayout(r));
|
||||
const otherRows = displayRows.filter((r) => !rowHasOptionsLayout(r));
|
||||
const ph =
|
||||
'<div class="card card-monitor-split-side card-monitor-placeholder" aria-hidden="true"></div>';
|
||||
/* 平铺 2×2 顺序:永续|币安 / 期权|Gate —— 同行左右同高,多仓时该行一起长高 */
|
||||
const cells = [
|
||||
okxRow ? renderMonitorCard(okxRow, { okxPart: "perp", splitSide: true }) : ph,
|
||||
otherRows[0] ? renderMonitorCard(otherRows[0], { splitSide: true }) : ph,
|
||||
okxRow ? renderMonitorCard(okxRow, { okxPart: "options", splitSide: true }) : ph,
|
||||
otherRows[1] ? renderMonitorCard(otherRows[1], { splitSide: true }) : ph,
|
||||
];
|
||||
for (let i = 2; i < otherRows.length; i++) {
|
||||
cells.push(renderMonitorCard(otherRows[i], { splitSide: true }));
|
||||
const otherByKey = {};
|
||||
displayRows
|
||||
.filter((r) => !rowHasOptionsLayout(r))
|
||||
.forEach((r) => {
|
||||
otherByKey[String(r.key || "").toLowerCase()] = r;
|
||||
});
|
||||
/* 平铺顺序尽量保持:永续|币安 / 期权|Gate;隐藏项不占位 */
|
||||
const cells = [];
|
||||
if (okxRow && showMonitorOkxPerpPref()) {
|
||||
cells.push(renderMonitorCard(okxRow, { okxPart: "perp", splitSide: true }));
|
||||
}
|
||||
cardsHtml = `<div class="monitor-split-body monitor-split-2x2">${cells.join("")}</div>`;
|
||||
if (otherByKey.binance && showMonitorBinancePref()) {
|
||||
cells.push(renderMonitorCard(otherByKey.binance, { splitSide: true }));
|
||||
}
|
||||
if (okxRow && showMonitorOkxOptionsPref()) {
|
||||
cells.push(renderMonitorCard(okxRow, { okxPart: "options", splitSide: true }));
|
||||
}
|
||||
if (otherByKey.gate && showMonitorGatePref()) {
|
||||
cells.push(renderMonitorCard(otherByKey.gate, { splitSide: true }));
|
||||
}
|
||||
Object.keys(otherByKey).forEach((k) => {
|
||||
if (k === "binance" || k === "gate") return;
|
||||
cells.push(renderMonitorCard(otherByKey[k], { splitSide: true }));
|
||||
});
|
||||
cardsHtml = cells.length
|
||||
? `<div class="monitor-split-body monitor-split-2x2">${cells.join("")}</div>`
|
||||
: "";
|
||||
} else {
|
||||
cardsHtml =
|
||||
displayRows
|
||||
@@ -2545,7 +2591,7 @@
|
||||
}
|
||||
|
||||
if (expandedExchangeId && fs && fsInner) {
|
||||
const row = rows.find((r) => String(r.id) === String(expandedExchangeId));
|
||||
const row = visibleSource.find((r) => String(r.id) === String(expandedExchangeId));
|
||||
if (row) {
|
||||
try {
|
||||
fsInner.innerHTML = renderFullscreenExchange(row);
|
||||
@@ -3900,7 +3946,8 @@
|
||||
|
||||
function monitorOptionsSplitActive(rows) {
|
||||
if (isMobileLayout() || expandedExchangeId) return false;
|
||||
return (rows || []).some((r) => rowHasOptionsLayout(r));
|
||||
if (!(rows || []).some((r) => rowHasOptionsLayout(r))) return false;
|
||||
return showMonitorOkxPerpPref() || showMonitorOkxOptionsPref();
|
||||
}
|
||||
|
||||
function renderPerpetualInnerCard(row, ag, pos, orders, trends, tickMap, intraday) {
|
||||
@@ -5112,19 +5159,10 @@
|
||||
|
||||
function collectSettingsFromUI() {
|
||||
const rows = [...document.querySelectorAll("#settings-list .settings-card")];
|
||||
const pnlCb = document.getElementById("pref-show-account-pnl");
|
||||
const fundsCb = document.getElementById("pref-show-nav-funds");
|
||||
const dashCb = document.getElementById("pref-show-nav-dashboard");
|
||||
const planCb = document.getElementById("pref-show-nav-plan");
|
||||
const archiveCb = document.getElementById("pref-show-nav-archive");
|
||||
const quotesCb = document.getElementById("pref-show-nav-quotes");
|
||||
const aiCb = document.getElementById("pref-show-nav-ai");
|
||||
const calcCb = document.getElementById("pref-show-nav-calculator");
|
||||
const compareCb = document.getElementById("pref-show-nav-compare");
|
||||
const strategyCb = document.getElementById("pref-show-nav-strategy");
|
||||
const ampCb = document.getElementById("pref-show-nav-amp-stats");
|
||||
const helpCb = document.getElementById("pref-show-nav-help");
|
||||
const logsCb = document.getElementById("pref-show-nav-logs");
|
||||
const chk = (id) => {
|
||||
const el = document.getElementById(id);
|
||||
return el ? !!el.checked : true;
|
||||
};
|
||||
const supEnabled = document.getElementById("supervisor-enabled");
|
||||
const supProg = document.getElementById("supervisor-wechat-program");
|
||||
const supWebhook = document.getElementById("supervisor-wechat-webhook");
|
||||
@@ -5137,19 +5175,29 @@
|
||||
return {
|
||||
version: 1,
|
||||
display: {
|
||||
show_account_pnl: pnlCb ? !!pnlCb.checked : true,
|
||||
show_nav_funds: fundsCb ? !!fundsCb.checked : true,
|
||||
show_nav_dashboard: dashCb ? !!dashCb.checked : true,
|
||||
show_nav_plan: planCb ? !!planCb.checked : true,
|
||||
show_nav_archive: archiveCb ? !!archiveCb.checked : true,
|
||||
show_nav_quotes: quotesCb ? !!quotesCb.checked : true,
|
||||
show_nav_ai: aiCb ? !!aiCb.checked : true,
|
||||
show_nav_calculator: calcCb ? !!calcCb.checked : true,
|
||||
show_nav_compare: compareCb ? !!compareCb.checked : true,
|
||||
show_nav_strategy: strategyCb ? !!strategyCb.checked : true,
|
||||
show_nav_amp_stats: ampCb ? !!ampCb.checked : true,
|
||||
show_nav_help: helpCb ? !!helpCb.checked : true,
|
||||
show_nav_logs: logsCb ? !!logsCb.checked : true,
|
||||
show_account_pnl: chk("pref-show-account-pnl"),
|
||||
show_nav_funds: chk("pref-show-nav-funds"),
|
||||
show_nav_dashboard: chk("pref-show-nav-dashboard"),
|
||||
show_nav_plan: chk("pref-show-nav-plan"),
|
||||
show_nav_archive: chk("pref-show-nav-archive"),
|
||||
show_nav_quotes: chk("pref-show-nav-quotes"),
|
||||
show_nav_ai: chk("pref-show-nav-ai"),
|
||||
show_nav_calculator: chk("pref-show-nav-calculator"),
|
||||
show_nav_compare: chk("pref-show-nav-compare"),
|
||||
show_nav_strategy: chk("pref-show-nav-strategy"),
|
||||
show_nav_amp_stats: chk("pref-show-nav-amp-stats"),
|
||||
show_nav_help: chk("pref-show-nav-help"),
|
||||
show_nav_logs: chk("pref-show-nav-logs"),
|
||||
show_monitor_binance: chk("pref-show-monitor-binance"),
|
||||
show_monitor_okx_perp: chk("pref-show-monitor-okx-perp"),
|
||||
show_monitor_okx_options: chk("pref-show-monitor-okx-options"),
|
||||
show_monitor_gate: chk("pref-show-monitor-gate"),
|
||||
show_strategy_playbook_v2: chk("pref-show-strategy-playbook-v2"),
|
||||
show_strategy_playbook: chk("pref-show-strategy-playbook"),
|
||||
show_strategy_behavior: chk("pref-show-strategy-behavior"),
|
||||
show_strategy_binance: chk("pref-show-strategy-binance"),
|
||||
show_strategy_okx: chk("pref-show-strategy-okx"),
|
||||
show_strategy_gate: chk("pref-show-strategy-gate"),
|
||||
},
|
||||
supervisor: {
|
||||
enabled: supEnabled ? !!supEnabled.checked : true,
|
||||
@@ -5210,6 +5258,9 @@
|
||||
if (window.hubDashboardPage && window.hubDashboardPage.refresh) {
|
||||
window.hubDashboardPage.refresh();
|
||||
}
|
||||
if (window.hubStrategyPage && typeof window.hubStrategyPage.reloadMeta === "function") {
|
||||
window.hubStrategyPage.reloadMeta();
|
||||
}
|
||||
if (!pageNavAllowed(currentPage())) {
|
||||
history.replaceState({}, "", "/monitor");
|
||||
setActiveNav();
|
||||
|
||||
@@ -35,6 +35,7 @@
|
||||
const elQuoteContent = document.getElementById("archive-quote-content");
|
||||
const elQuoteSubmit = document.getElementById("archive-quote-submit");
|
||||
const elContentTabs = document.getElementById("archive-content-tabs");
|
||||
const elProductTabs = document.getElementById("archive-product-tabs");
|
||||
const elPanelViz = document.getElementById("archive-panel-viz");
|
||||
const elPanelCalendar = document.getElementById("archive-panel-calendar");
|
||||
const elPanelTrades = document.getElementById("archive-panel-trades");
|
||||
@@ -76,6 +77,7 @@
|
||||
let selectedQuoteId = null;
|
||||
let editingQuoteId = null;
|
||||
let archiveContentTab = "trades";
|
||||
let archiveProduct = "perp";
|
||||
let quoteDayTrades = [];
|
||||
let quoteDayTradesDay = "";
|
||||
let quoteDayTradesReq = 0;
|
||||
@@ -416,6 +418,44 @@
|
||||
syncPeriodUI();
|
||||
}
|
||||
|
||||
function isOptionsProduct() {
|
||||
return archiveProduct === "options";
|
||||
}
|
||||
|
||||
function syncProductUI() {
|
||||
document.body.classList.toggle("archive-product-options", isOptionsProduct());
|
||||
if (elProductTabs) {
|
||||
elProductTabs.querySelectorAll(".archive-product-tab").forEach(function (btn) {
|
||||
const on = btn.getAttribute("data-archive-product") === archiveProduct;
|
||||
btn.classList.toggle("is-active", on);
|
||||
btn.setAttribute("aria-selected", on ? "true" : "false");
|
||||
});
|
||||
}
|
||||
if (isOptionsProduct()) {
|
||||
setChartOpen(false);
|
||||
if (archiveContentTab === "viz") setArchiveContentTab("trades");
|
||||
}
|
||||
}
|
||||
|
||||
function setArchiveProduct(product) {
|
||||
const next = product === "options" ? "options" : "perp";
|
||||
if (next === archiveProduct) return;
|
||||
archiveProduct = next;
|
||||
selected = null;
|
||||
selectedTradeKey = null;
|
||||
syncProductUI();
|
||||
void loadDailyTrades();
|
||||
void loadCalendar();
|
||||
}
|
||||
|
||||
function dailyTradesApiPath() {
|
||||
return isOptionsProduct() ? "/api/archive/options/daily-trades" : "/api/archive/daily-trades";
|
||||
}
|
||||
|
||||
function calendarApiPath() {
|
||||
return isOptionsProduct() ? "/api/archive/options/calendar" : "/api/archive/calendar";
|
||||
}
|
||||
|
||||
function queryDailyParams() {
|
||||
const q = new URLSearchParams();
|
||||
q.set("period", periodMode);
|
||||
@@ -430,7 +470,7 @@
|
||||
if (ex) q.set("exchange_key", ex);
|
||||
if (elFilterProfit && elFilterProfit.checked) q.set("filter_profit", "1");
|
||||
if (elFilterLoss && elFilterLoss.checked) q.set("filter_loss", "1");
|
||||
if (elFilterSick && elFilterSick.checked) q.set("filter_sick", "1");
|
||||
if (!isOptionsProduct() && elFilterSick && elFilterSick.checked) q.set("filter_sick", "1");
|
||||
if (elSearch && elSearch.value.trim()) q.set("search", elSearch.value.trim());
|
||||
return q.toString();
|
||||
}
|
||||
@@ -554,7 +594,7 @@
|
||||
return q;
|
||||
},
|
||||
fetchFn: async function (q) {
|
||||
const r = await apiFetch("/api/archive/calendar?" + q.toString());
|
||||
const r = await apiFetch(calendarApiPath() + "?" + q.toString());
|
||||
return r.json();
|
||||
},
|
||||
parseResponse: function (data) {
|
||||
@@ -1089,7 +1129,7 @@
|
||||
elQuoteDayTradesBody.innerHTML = '<p class="archive-empty">加载当日已平仓…</p>';
|
||||
if (elQuoteDayTradesMeta) elQuoteDayTradesMeta.textContent = day;
|
||||
try {
|
||||
const r = await apiFetch("/api/archive/daily-trades?" + q.toString());
|
||||
const r = await apiFetch(dailyTradesApiPath() + "?" + q.toString());
|
||||
const j = await r.json();
|
||||
if (req !== quoteDayTradesReq) return;
|
||||
if (!r.ok) {
|
||||
@@ -1827,6 +1867,80 @@
|
||||
return;
|
||||
}
|
||||
const pageRows = pagedDailyTrades();
|
||||
if (isOptionsProduct()) {
|
||||
elTrades.innerHTML =
|
||||
'<table class="archive-trades-table"><thead><tr>' +
|
||||
"<th>交易所</th><th>标的</th><th>合约/来源</th><th>开仓时间</th><th>平仓时间</th><th>持仓</th>" +
|
||||
"<th>类型</th><th>策略</th><th>盈亏</th><th>权利金</th><th>复盘</th>" +
|
||||
"</tr></thead><tbody>" +
|
||||
pageRows
|
||||
.map(function (t) {
|
||||
const rowKey = tradeRowKey(t);
|
||||
const active = rowKey && rowKey === selectedTradeKey ? " is-active" : "";
|
||||
const holdMin =
|
||||
t.hold_minutes != null
|
||||
? t.hold_minutes
|
||||
: t.hold_seconds != null
|
||||
? Number(t.hold_seconds) / 60
|
||||
: null;
|
||||
const optLabel =
|
||||
t.source_label ||
|
||||
t.source_type ||
|
||||
(t.opt_type === "C" || t.opt_type === "CALL"
|
||||
? "Call"
|
||||
: t.opt_type === "P" || t.opt_type === "PUT"
|
||||
? "Put"
|
||||
: "—");
|
||||
const pnl = t.pnl_amount != null ? t.pnl_amount : t.realized_pnl_total;
|
||||
return (
|
||||
'<tr class="archive-trade-row' +
|
||||
active +
|
||||
'" data-key="' +
|
||||
esc(rowKey) +
|
||||
'">' +
|
||||
"<td>" +
|
||||
esc(tradeRowExchange(t)) +
|
||||
"</td>" +
|
||||
'<td class="archive-symbol">' +
|
||||
esc(t.underlying || "—") +
|
||||
"</td>" +
|
||||
"<td>" +
|
||||
esc(t.inst_id || t.source_label || "—") +
|
||||
"</td>" +
|
||||
'<td class="archive-dt">' +
|
||||
fmtDt(t.opened_at) +
|
||||
"</td>" +
|
||||
'<td class="archive-dt">' +
|
||||
fmtDt(t.closed_at) +
|
||||
"</td>" +
|
||||
'<td class="archive-hold">' +
|
||||
fmtDurationMinutes(holdMin) +
|
||||
"</td>" +
|
||||
"<td>" +
|
||||
esc(optLabel) +
|
||||
"</td>" +
|
||||
"<td>" +
|
||||
esc(t.strategy_tag || "—") +
|
||||
"</td>" +
|
||||
'<td class="' +
|
||||
pnlClass(pnl) +
|
||||
'">' +
|
||||
fmtPnl(pnl) +
|
||||
"</td>" +
|
||||
"<td>" +
|
||||
fmtVolStat(t.premium_total != null ? t.premium_total : t.premium_paid) +
|
||||
"</td>" +
|
||||
"<td>" +
|
||||
(t.reviewed ? "已复盘" : "—") +
|
||||
"</td>" +
|
||||
"</tr>"
|
||||
);
|
||||
})
|
||||
.join("") +
|
||||
"</tbody></table>";
|
||||
updateTradesPager();
|
||||
return;
|
||||
}
|
||||
elTrades.innerHTML =
|
||||
'<table class="archive-trades-table"><thead><tr>' +
|
||||
"<th>交易所</th><th>合约</th><th>开仓类型</th><th>开仓时间</th><th>平仓时间</th><th>持仓时长</th>" +
|
||||
@@ -2051,7 +2165,7 @@
|
||||
|
||||
async function loadDailyTrades() {
|
||||
setStatus("加载交易记录…");
|
||||
const r = await apiFetch("/api/archive/daily-trades?" + queryDailyParams());
|
||||
const r = await apiFetch(dailyTradesApiPath() + "?" + queryDailyParams());
|
||||
const j = await r.json();
|
||||
if (!r.ok) {
|
||||
setStatus(j.detail || "加载失败");
|
||||
@@ -2080,7 +2194,8 @@
|
||||
void loadCalendar();
|
||||
if (archiveContentTab === "quotes") void loadQuoteDayTrades();
|
||||
setStatus(
|
||||
(periodLabel || tradingDay || "当日") +
|
||||
(isOptionsProduct() ? "期权 · " : "永续 · ") +
|
||||
(periodLabel || tradingDay || "当日") +
|
||||
" · 列表 " +
|
||||
dailyTrades.length +
|
||||
" 笔 · " +
|
||||
@@ -2105,6 +2220,7 @@
|
||||
|
||||
function formatSyncSummary(j) {
|
||||
const results = j.results || [];
|
||||
const optResults = j.options_results || [];
|
||||
const okN = results.filter(function (x) {
|
||||
return x.ok !== false;
|
||||
}).length;
|
||||
@@ -2118,6 +2234,19 @@
|
||||
parts.push(line);
|
||||
}
|
||||
});
|
||||
optResults.forEach(function (row) {
|
||||
const label = (row.exchange_key || row.name || "?") + "期权";
|
||||
if (row.ok === false) parts.push(label + " 失败: " + (row.msg || "未知错误"));
|
||||
else {
|
||||
let line =
|
||||
label +
|
||||
" " +
|
||||
(row.trade_count != null ? row.trade_count : row.trades_upserted || 0) +
|
||||
" 笔";
|
||||
if (row.trades_removed > 0) line += " 清" + row.trades_removed;
|
||||
parts.push(line);
|
||||
}
|
||||
});
|
||||
return parts.join(" · ");
|
||||
}
|
||||
|
||||
@@ -2217,6 +2346,13 @@
|
||||
setArchiveContentTab(btn.getAttribute("data-archive-tab") || "trades");
|
||||
});
|
||||
}
|
||||
if (elProductTabs) {
|
||||
elProductTabs.addEventListener("click", function (ev) {
|
||||
const btn = ev.target.closest(".archive-product-tab");
|
||||
if (!btn) return;
|
||||
setArchiveProduct(btn.getAttribute("data-archive-product") || "perp");
|
||||
});
|
||||
}
|
||||
if (elTfTabs) {
|
||||
elTfTabs.addEventListener("click", function (ev) {
|
||||
const btn = ev.target.closest(".archive-tf-btn");
|
||||
@@ -2249,6 +2385,7 @@
|
||||
syncPeriodUI();
|
||||
syncTradesLayout();
|
||||
bindEvents();
|
||||
syncProductUI();
|
||||
setArchiveContentTab("trades");
|
||||
inited = true;
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
|
||||
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;500;600&family=Orbitron:wght@500;600;700&display=swap" rel="stylesheet" media="print" onload="this.media='all'" />
|
||||
<noscript><link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;500;600&family=Orbitron:wght@500;600;700&display=swap" rel="stylesheet" /></noscript>
|
||||
<link rel="stylesheet" href="/assets/app.css?v=20260723-cmp-pad" />
|
||||
<link rel="stylesheet" href="/assets/app.css?v=20260724-opt-archive" />
|
||||
<link rel="stylesheet" href="/assets/trade_stats_calendar.css?v=4" />
|
||||
<link rel="stylesheet" href="/assets/account_risk_badge.css?v=4" />
|
||||
<script src="/assets/account_risk_badge.js?v=4"></script>
|
||||
@@ -451,7 +451,11 @@
|
||||
<div id="page-archive" class="page hidden">
|
||||
<div class="page-head">
|
||||
<h1><span class="head-tag">IN</span> 内照明心</h1>
|
||||
<p class="page-desc">交易记录 · 交易日历 · 图表概览 · 复盘语录</p>
|
||||
<p class="page-desc">永续 / 期权交易记录 · 交易日历 · 图表概览 · 复盘语录</p>
|
||||
</div>
|
||||
<div class="archive-product-tabs" id="archive-product-tabs" role="tablist" aria-label="品种">
|
||||
<button type="button" class="archive-product-tab is-active" role="tab" aria-selected="true" data-archive-product="perp">永续</button>
|
||||
<button type="button" class="archive-product-tab" role="tab" aria-selected="false" data-archive-product="options">期权</button>
|
||||
</div>
|
||||
<div class="archive-toolbar toolbar">
|
||||
<label class="chk-label archive-toolbar-desktop"><input type="checkbox" id="archive-filter-profit" /> 盈利单</label>
|
||||
@@ -1081,7 +1085,7 @@
|
||||
<div class="page-head strategy-page-head">
|
||||
<div>
|
||||
<h1><span class="head-tag">STR</span> 策略说明</h1>
|
||||
<p class="page-desc">执行手册 · 三所策略正文(带目录) · 执行清单(打印对照)</p>
|
||||
<p class="page-desc">执行手册v2(无对冲)· 行为准则 · 三所策略正文 · 执行清单</p>
|
||||
</div>
|
||||
<div class="strategy-page-actions no-print">
|
||||
<button type="button" id="strategy-btn-download" class="ghost">下载 HTML</button>
|
||||
@@ -1382,6 +1386,50 @@
|
||||
<input type="checkbox" id="pref-show-nav-logs" checked />
|
||||
顶栏显示「系统日志」
|
||||
</label>
|
||||
<p class="settings-display-subtitle">监控区卡片</p>
|
||||
<p class="settings-display-hint">仅隐藏监控区界面卡片,不关闭账户与后台拉取.例:只做 OKX 期权与 Gate 时可关掉币安与 OKX 永续.</p>
|
||||
<label class="chk-label settings-display-chk">
|
||||
<input type="checkbox" id="pref-show-monitor-binance" checked />
|
||||
监控区显示「币安」
|
||||
</label>
|
||||
<label class="chk-label settings-display-chk">
|
||||
<input type="checkbox" id="pref-show-monitor-okx-perp" checked />
|
||||
监控区显示「OKX 永续」
|
||||
</label>
|
||||
<label class="chk-label settings-display-chk">
|
||||
<input type="checkbox" id="pref-show-monitor-okx-options" checked />
|
||||
监控区显示「OKX 期权」
|
||||
</label>
|
||||
<label class="chk-label settings-display-chk">
|
||||
<input type="checkbox" id="pref-show-monitor-gate" checked />
|
||||
监控区显示「Gate」
|
||||
</label>
|
||||
<p class="settings-display-subtitle">策略说明页签</p>
|
||||
<p class="settings-display-hint">关闭后该页签从策略说明中消失;顶栏「策略说明」入口仍由上方导航开关控制.</p>
|
||||
<label class="chk-label settings-display-chk">
|
||||
<input type="checkbox" id="pref-show-strategy-playbook-v2" checked />
|
||||
策略说明显示「执行手册v2」
|
||||
</label>
|
||||
<label class="chk-label settings-display-chk">
|
||||
<input type="checkbox" id="pref-show-strategy-playbook" checked />
|
||||
策略说明显示「执行手册v1」
|
||||
</label>
|
||||
<label class="chk-label settings-display-chk">
|
||||
<input type="checkbox" id="pref-show-strategy-behavior" checked />
|
||||
策略说明显示「行为准则」
|
||||
</label>
|
||||
<label class="chk-label settings-display-chk">
|
||||
<input type="checkbox" id="pref-show-strategy-binance" checked />
|
||||
策略说明显示「币安」
|
||||
</label>
|
||||
<label class="chk-label settings-display-chk">
|
||||
<input type="checkbox" id="pref-show-strategy-okx" checked />
|
||||
策略说明显示「OKX」
|
||||
</label>
|
||||
<label class="chk-label settings-display-chk">
|
||||
<input type="checkbox" id="pref-show-strategy-gate" checked />
|
||||
策略说明显示「Gate」
|
||||
</label>
|
||||
<p class="settings-display-hint">保存至 hub_settings.json,换浏览器同样生效.关闭导航后对应页面将不可从顶栏进入,直接访问 URL 会跳回监控区.</p>
|
||||
</section>
|
||||
|
||||
@@ -1629,11 +1677,11 @@
|
||||
<script src="/assets/calculator.js?v=20260715-calc-tabs"></script>
|
||||
<script src="/assets/compare.js?v=20260723-compare"></script>
|
||||
<script src="/assets/trade_stats_calendar.js?v=3"></script>
|
||||
<script src="/assets/archive.js?v=20260717-archive-cal-chart"></script>
|
||||
<script src="/assets/archive.js?v=20260724-opt-archive"></script>
|
||||
<script src="/assets/quotes.js?v=20260717-quotes-feed"></script>
|
||||
<script src="/assets/funds.js?v=20260717-funds-scroll-fix"></script>
|
||||
<script src="/assets/dashboard.js?v=20260723-hide-pnl"></script>
|
||||
<script src="/assets/strategy.js?v=9"></script>
|
||||
<script src="/assets/strategy.js?v=11"></script>
|
||||
<script src="/assets/amp_stats.js?v=5"></script>
|
||||
<script src="/assets/help.js?v=1"></script>
|
||||
<script src="/assets/logs.js?v=1"></script>
|
||||
@@ -1642,6 +1690,6 @@
|
||||
<script src="/assets/options_expiry_countdown.js?v=1"></script>
|
||||
<script src="/assets/options_position_cards.js?v=3"></script>
|
||||
<script src="/assets/backup.js?v=1"></script>
|
||||
<script src="/assets/app.js?v=20260723-compare"></script>
|
||||
<script src="/assets/app.js?v=20260724-display-hide"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
const btnPrintChecklistInline = document.getElementById("strategy-btn-print-checklist-inline");
|
||||
const btnDownload = document.getElementById("strategy-btn-download");
|
||||
|
||||
let activeKey = "playbook";
|
||||
let activeKey = "playbook_v2";
|
||||
let activeView = "doc";
|
||||
let tabsMeta = [];
|
||||
let cache = {};
|
||||
@@ -74,6 +74,14 @@
|
||||
|
||||
function renderExchangeTabs() {
|
||||
if (!tabsEl) return;
|
||||
if (!tabsMeta.length) {
|
||||
tabsEl.innerHTML = "";
|
||||
if (statusEl) statusEl.textContent = "当前无可显示的策略页签(可在系统设置·显示与导航中开启)";
|
||||
if (docBody) docBody.innerHTML = "";
|
||||
if (docToc) docToc.innerHTML = "";
|
||||
if (checklistBody) checklistBody.innerHTML = "";
|
||||
return;
|
||||
}
|
||||
tabsEl.innerHTML = tabsMeta
|
||||
.map(
|
||||
(t) =>
|
||||
@@ -274,6 +282,15 @@
|
||||
renderExchangeTabs();
|
||||
}
|
||||
|
||||
async function reloadMeta() {
|
||||
try {
|
||||
await loadMeta();
|
||||
if (tabsMeta.length) await loadExchange(activeKey);
|
||||
} catch (e) {
|
||||
if (statusEl) statusEl.textContent = String(e);
|
||||
}
|
||||
}
|
||||
|
||||
async function printSection(mode) {
|
||||
const part = mode === "checklist" ? "checklist" : "doc";
|
||||
const url = `/api/strategy/${encodeURIComponent(activeKey)}/print?part=${encodeURIComponent(part)}`;
|
||||
@@ -334,7 +351,8 @@
|
||||
setView(activeView);
|
||||
try {
|
||||
await loadMeta();
|
||||
await loadExchange(activeKey);
|
||||
if (tabsMeta.length) await loadExchange(activeKey);
|
||||
else if (statusEl) statusEl.textContent = "当前无可显示的策略页签(可在系统设置·显示与导航中开启)";
|
||||
} catch (e) {
|
||||
if (statusEl) statusEl.textContent = String(e);
|
||||
}
|
||||
@@ -347,5 +365,5 @@
|
||||
}
|
||||
}
|
||||
|
||||
window.hubStrategyPage = { init, destroy };
|
||||
window.hubStrategyPage = { init, destroy, reloadMeta };
|
||||
})();
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
"""期权档案缓存 upsert / 列表 / 日历."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from lib.hub.hub_options_archive_lib import (
|
||||
init_options_archive_db,
|
||||
list_archive_options_calendar,
|
||||
list_daily_options_trades,
|
||||
upsert_options_trades_cache,
|
||||
)
|
||||
|
||||
|
||||
class TestHubOptionsArchive(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self._td = tempfile.TemporaryDirectory()
|
||||
self.db = Path(self._td.name) / "hub_symbol_archive.db"
|
||||
init_options_archive_db(self.db)
|
||||
|
||||
def tearDown(self) -> None:
|
||||
self._td.cleanup()
|
||||
|
||||
def test_upsert_and_list_daily(self) -> None:
|
||||
trades = [
|
||||
{
|
||||
"history_key": "local_opt:1",
|
||||
"source_type": "option_spot",
|
||||
"source_label": "纯期权",
|
||||
"underlying": "ETH",
|
||||
"inst_id": "ETH-USD-250725-3200-C",
|
||||
"opt_type": "C",
|
||||
"opened_at": "2026-07-20 10:00:00",
|
||||
"closed_at": "2026-07-20 16:00:00",
|
||||
"hold_seconds": 21600,
|
||||
"realized_pnl_total": 12.5,
|
||||
"premium_paid": 8.0,
|
||||
"reviewed": True,
|
||||
"strategy_tag": "假突破",
|
||||
},
|
||||
{
|
||||
"history_key": "local_opt:2",
|
||||
"source_type": "option_spot",
|
||||
"underlying": "ETH",
|
||||
"opened_at": "2026-07-19 10:00:00",
|
||||
"closed_at": "2026-07-19 12:00:00",
|
||||
"realized_pnl_total": -3.0,
|
||||
"excluded_as_hedge_leg": 1,
|
||||
},
|
||||
]
|
||||
r = upsert_options_trades_cache("okx", trades, db_path=self.db)
|
||||
self.assertEqual(r["upserted"], 1)
|
||||
payload = list_daily_options_trades(
|
||||
"2026-07-20",
|
||||
period="today",
|
||||
db_path=self.db,
|
||||
)
|
||||
self.assertEqual(len(payload["trades"]), 1)
|
||||
self.assertEqual(payload["trades"][0]["history_key"], "local_opt:1")
|
||||
self.assertAlmostEqual(payload["stats"]["pnl_total"], 12.5)
|
||||
cal = list_archive_options_calendar(2026, 7, db_path=self.db)
|
||||
self.assertIn("2026-07-20", cal["days"])
|
||||
self.assertEqual(cal["days"]["2026-07-20"]["open_count"], 1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -21,6 +21,19 @@ class TestInstanceDisplayPrefs(unittest.TestCase):
|
||||
prefs = normalize_display_prefs({"show_nav_stats": False})
|
||||
self.assertFalse(tab_allowed("stats", prefs))
|
||||
self.assertTrue(tab_allowed("trade", prefs))
|
||||
self.assertTrue(tab_allowed("key_monitor", prefs))
|
||||
|
||||
def test_key_monitor_and_trade_nav_can_hide(self):
|
||||
prefs = normalize_display_prefs(
|
||||
{"show_nav_key_monitor": False, "show_nav_trade": False}
|
||||
)
|
||||
self.assertFalse(tab_allowed("key_monitor", prefs))
|
||||
self.assertFalse(tab_allowed("trade", prefs))
|
||||
on = normalize_display_prefs({})
|
||||
self.assertTrue(on["show_nav_key_monitor"])
|
||||
self.assertTrue(on["show_nav_trade"])
|
||||
self.assertTrue(tab_allowed("key_monitor", on))
|
||||
self.assertTrue(tab_allowed("trade", on))
|
||||
|
||||
def test_dashboard_nav_default_off(self):
|
||||
prefs = normalize_display_prefs({})
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
"""期权开平仓微信文案."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
|
||||
from lib.options.options_notify_lib import (
|
||||
build_options_close_message,
|
||||
build_options_open_message,
|
||||
)
|
||||
|
||||
|
||||
class TestOptionsNotify(unittest.TestCase):
|
||||
def test_open_close_messages(self) -> None:
|
||||
open_msg = build_options_open_message(
|
||||
account_label="OKX期权",
|
||||
inst_id="ETH-USD-250725-3200-C",
|
||||
underlying="ETH",
|
||||
opt_type="C",
|
||||
sheets=2,
|
||||
premium_paid=8.5,
|
||||
open_quote=0.01,
|
||||
target_index=3400,
|
||||
signal_note="假突破",
|
||||
trade_id=12,
|
||||
)
|
||||
self.assertIn("【OKX期权·开仓】", open_msg)
|
||||
self.assertIn("ETH-USD-250725-3200-C", open_msg)
|
||||
self.assertIn("目标指数:3400", open_msg)
|
||||
|
||||
close_msg = build_options_close_message(
|
||||
account_label="OKX期权",
|
||||
inst_id="ETH-USD-250725-3200-C",
|
||||
reason="手动平仓",
|
||||
underlying="ETH",
|
||||
opt_type="C",
|
||||
sheets=2,
|
||||
premium_paid=8.5,
|
||||
premium_received=12.0,
|
||||
realized_pnl=3.5,
|
||||
)
|
||||
self.assertIn("【OKX期权·平仓】", close_msg)
|
||||
self.assertIn("手动平仓", close_msg)
|
||||
self.assertIn("3.5000", close_msg)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user