Compare commits
No commits in common. "b1ed55382c0ccaf790354b84ace53bbc6d304cb0" and "2d64a5524e0a44d88d7ce088d100b8a05299b916" have entirely different histories.
b1ed55382c
...
2d64a5524e
204
backend/main.py
204
backend/main.py
@ -1975,207 +1975,3 @@ async def live_config_update(request: Request, user: dict = Depends(get_current_
|
||||
)
|
||||
updated.append(key)
|
||||
return {"updated": updated}
|
||||
|
||||
|
||||
# ─────────────────────────────────────────────
|
||||
# 策略广场 API
|
||||
# ─────────────────────────────────────────────
|
||||
|
||||
import json as _json
|
||||
import os as _os
|
||||
import statistics as _statistics
|
||||
|
||||
_STRATEGY_META = {
|
||||
"v53": {
|
||||
"display_name": "V5.3 标准版",
|
||||
"cvd_windows": "30m / 4h",
|
||||
"description": "标准版:30分钟+4小时CVD双轨,适配1小时信号周期",
|
||||
"initial_balance": 10000,
|
||||
},
|
||||
"v53_fast": {
|
||||
"display_name": "V5.3 Fast版",
|
||||
"cvd_windows": "5m / 30m",
|
||||
"description": "快速版:5分钟+30分钟CVD双轨,捕捉短期动量",
|
||||
"initial_balance": 10000,
|
||||
},
|
||||
"v53_middle": {
|
||||
"display_name": "V5.3 Middle版",
|
||||
"cvd_windows": "15m / 1h",
|
||||
"description": "中速版:15分钟+1小时CVD双轨,平衡噪音与时效",
|
||||
"initial_balance": 10000,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
async def _get_strategy_status(strategy_id: str) -> str:
|
||||
"""根据 paper_config 和最新心跳判断策略状态"""
|
||||
config_path = _os.path.join(_os.path.dirname(__file__), "paper_config.json")
|
||||
try:
|
||||
with open(config_path) as f:
|
||||
config = _json.load(f)
|
||||
enabled = strategy_id in config.get("enabled_strategies", [])
|
||||
except Exception:
|
||||
enabled = False
|
||||
|
||||
if not enabled:
|
||||
return "paused"
|
||||
|
||||
# 检查最近5分钟内是否有心跳
|
||||
cutoff = int((__import__("time").time() - 300) * 1000)
|
||||
row = await async_fetch(
|
||||
"SELECT ts FROM signal_indicators WHERE strategy=$1 AND ts > $2 ORDER BY ts DESC LIMIT 1",
|
||||
strategy_id, cutoff
|
||||
)
|
||||
if row:
|
||||
return "running"
|
||||
return "error"
|
||||
|
||||
|
||||
@app.get("/api/strategy-plaza")
|
||||
async def strategy_plaza(user: dict = Depends(get_current_user)):
|
||||
"""策略广场总览:返回所有策略卡片数据"""
|
||||
now_ms = int(__import__("time").time() * 1000)
|
||||
cutoff_24h = now_ms - 86400000
|
||||
|
||||
results = []
|
||||
for sid, meta in _STRATEGY_META.items():
|
||||
# 累计统计
|
||||
rows = await async_fetch(
|
||||
"SELECT pnl_r, entry_ts, exit_ts FROM paper_trades "
|
||||
"WHERE strategy=$1 AND exit_ts IS NOT NULL",
|
||||
sid
|
||||
)
|
||||
# 活跃持仓
|
||||
open_rows = await async_fetch(
|
||||
"SELECT COUNT(*) as cnt FROM paper_trades WHERE strategy=$1 AND exit_ts IS NULL",
|
||||
sid
|
||||
)
|
||||
open_positions = int(open_rows[0]["cnt"]) if open_rows else 0
|
||||
|
||||
# 24h 统计
|
||||
rows_24h = [r for r in rows if (r["exit_ts"] or 0) >= cutoff_24h]
|
||||
|
||||
pnl_rs = [float(r["pnl_r"]) for r in rows]
|
||||
wins = [p for p in pnl_rs if p > 0]
|
||||
losses = [p for p in pnl_rs if p <= 0]
|
||||
net_r = round(sum(pnl_rs), 3)
|
||||
net_usdt = round(net_r * 200, 0)
|
||||
|
||||
pnl_rs_24h = [float(r["pnl_r"]) for r in rows_24h]
|
||||
pnl_r_24h = round(sum(pnl_rs_24h), 3)
|
||||
pnl_usdt_24h = round(pnl_r_24h * 200, 0)
|
||||
|
||||
std_r = round(_statistics.stdev(pnl_rs), 3) if len(pnl_rs) > 1 else 0.0
|
||||
|
||||
started_at = min(r["entry_ts"] for r in rows) if rows else now_ms
|
||||
last_trade_at = max(r["exit_ts"] for r in rows if r["exit_ts"]) if rows else None
|
||||
|
||||
status = await _get_strategy_status(sid)
|
||||
|
||||
results.append({
|
||||
"id": sid,
|
||||
"display_name": meta["display_name"],
|
||||
"status": status,
|
||||
"started_at": started_at,
|
||||
"initial_balance": meta["initial_balance"],
|
||||
"current_balance": meta["initial_balance"] + int(net_usdt),
|
||||
"net_usdt": int(net_usdt),
|
||||
"net_r": net_r,
|
||||
"trade_count": len(pnl_rs),
|
||||
"win_rate": round(len(wins) / len(pnl_rs) * 100, 1) if pnl_rs else 0.0,
|
||||
"avg_win_r": round(sum(wins) / len(wins), 3) if wins else 0.0,
|
||||
"avg_loss_r": round(sum(losses) / len(losses), 3) if losses else 0.0,
|
||||
"open_positions": open_positions,
|
||||
"pnl_usdt_24h": int(pnl_usdt_24h),
|
||||
"pnl_r_24h": pnl_r_24h,
|
||||
"std_r": std_r,
|
||||
"last_trade_at": last_trade_at,
|
||||
})
|
||||
|
||||
return {"strategies": results}
|
||||
|
||||
|
||||
@app.get("/api/strategy-plaza/{strategy_id}/summary")
|
||||
async def strategy_plaza_summary(strategy_id: str, user: dict = Depends(get_current_user)):
|
||||
"""策略详情 summary:卡片数据 + 详情字段"""
|
||||
if strategy_id not in _STRATEGY_META:
|
||||
from fastapi import HTTPException
|
||||
raise HTTPException(status_code=404, detail="Strategy not found")
|
||||
|
||||
# 先拿广场数据
|
||||
plaza_data = await strategy_plaza(user)
|
||||
card = next((s for s in plaza_data["strategies"] if s["id"] == strategy_id), None)
|
||||
if not card:
|
||||
from fastapi import HTTPException
|
||||
raise HTTPException(status_code=404, detail="Strategy not found")
|
||||
|
||||
meta = _STRATEGY_META[strategy_id]
|
||||
|
||||
# 读策略 JSON 获取权重和阈值
|
||||
strategy_file = _os.path.join(_os.path.dirname(__file__), "strategies", f"{strategy_id}.json")
|
||||
weights = {}
|
||||
thresholds = {}
|
||||
symbols = []
|
||||
try:
|
||||
with open(strategy_file) as f:
|
||||
cfg = _json.load(f)
|
||||
weights = {
|
||||
"direction": cfg.get("direction_weight", 55),
|
||||
"crowding": cfg.get("crowding_weight", 25),
|
||||
"environment": cfg.get("environment_weight", 15),
|
||||
"auxiliary": cfg.get("auxiliary_weight", 5),
|
||||
}
|
||||
thresholds = {
|
||||
"signal_threshold": cfg.get("threshold", 75),
|
||||
"flip_threshold": cfg.get("flip_threshold", 85),
|
||||
}
|
||||
symbols = list(cfg.get("symbol_gates", {}).keys())
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return {
|
||||
**card,
|
||||
"cvd_windows": meta["cvd_windows"],
|
||||
"description": meta["description"],
|
||||
"symbols": symbols,
|
||||
"weights": weights,
|
||||
"thresholds": thresholds,
|
||||
}
|
||||
|
||||
|
||||
@app.get("/api/strategy-plaza/{strategy_id}/signals")
|
||||
async def strategy_plaza_signals(
|
||||
strategy_id: str,
|
||||
limit: int = 50,
|
||||
user: dict = Depends(get_current_user)
|
||||
):
|
||||
"""策略信号列表(复用现有逻辑,加 strategy 过滤)"""
|
||||
if strategy_id not in _STRATEGY_META:
|
||||
from fastapi import HTTPException
|
||||
raise HTTPException(status_code=404, detail="Strategy not found")
|
||||
rows = await async_fetch(
|
||||
"SELECT ts, symbol, score, signal, price, factors, cvd_5m, cvd_15m, cvd_30m, cvd_1h, cvd_4h, atr_value "
|
||||
"FROM signal_indicators WHERE strategy=$1 ORDER BY ts DESC LIMIT $2",
|
||||
strategy_id, limit
|
||||
)
|
||||
return {"signals": [dict(r) for r in rows]}
|
||||
|
||||
|
||||
@app.get("/api/strategy-plaza/{strategy_id}/trades")
|
||||
async def strategy_plaza_trades(
|
||||
strategy_id: str,
|
||||
limit: int = 50,
|
||||
user: dict = Depends(get_current_user)
|
||||
):
|
||||
"""策略交易记录(复用现有逻辑,加 strategy 过滤)"""
|
||||
if strategy_id not in _STRATEGY_META:
|
||||
from fastapi import HTTPException
|
||||
raise HTTPException(status_code=404, detail="Strategy not found")
|
||||
rows = await async_fetch(
|
||||
"SELECT id, symbol, direction, score, tier, entry_price, exit_price, "
|
||||
"tp1_price, tp2_price, sl_price, tp1_hit, pnl_r, risk_distance, "
|
||||
"entry_ts, exit_ts, status, strategy "
|
||||
"FROM paper_trades WHERE strategy=$1 ORDER BY entry_ts DESC LIMIT $2",
|
||||
strategy_id, limit
|
||||
)
|
||||
return {"trades": [dict(r) for r in rows]}
|
||||
|
||||
@ -1,10 +1,6 @@
|
||||
{
|
||||
"enabled": true,
|
||||
"enabled_strategies": [
|
||||
"v53",
|
||||
"v53_fast",
|
||||
"v53_middle"
|
||||
],
|
||||
"enabled_strategies": ["v53", "v53_fast"],
|
||||
"initial_balance": 10000,
|
||||
"risk_per_trade": 0.02,
|
||||
"max_positions": 4,
|
||||
|
||||
@ -42,7 +42,7 @@ logger = logging.getLogger("signal-engine")
|
||||
SYMBOLS = ["BTCUSDT", "ETHUSDT", "XRPUSDT", "SOLUSDT"]
|
||||
LOOP_INTERVAL = 15 # 秒(从5改15,CPU降60%,信号质量无影响)
|
||||
STRATEGY_DIR = os.path.join(os.path.dirname(__file__), "strategies")
|
||||
DEFAULT_STRATEGY_FILES = ["v51_baseline.json", "v52_8signals.json", "v53.json", "v53_fast.json", "v53_middle.json"]
|
||||
DEFAULT_STRATEGY_FILES = ["v51_baseline.json", "v52_8signals.json", "v53.json", "v53_fast.json"]
|
||||
|
||||
|
||||
def load_strategy_configs() -> list[dict]:
|
||||
|
||||
@ -1,49 +0,0 @@
|
||||
{
|
||||
"name": "v53_middle",
|
||||
"version": "5.3",
|
||||
"description": "V5.3 Middle版(BTC/ETH/XRP/SOL): CVD 15m/1h 窗口,适合1h信号时框",
|
||||
"threshold": 75,
|
||||
"flip_threshold": 85,
|
||||
"cvd_fast_window": "15m",
|
||||
"cvd_slow_window": "1h",
|
||||
"weights": {
|
||||
"direction": 55,
|
||||
"crowding": 25,
|
||||
"environment": 15,
|
||||
"auxiliary": 5
|
||||
},
|
||||
"tp_sl": {
|
||||
"sl_multiplier": 2.0,
|
||||
"tp1_multiplier": 1.5,
|
||||
"tp2_multiplier": 3.0,
|
||||
"tp_maker": true
|
||||
},
|
||||
"symbols": ["BTCUSDT", "ETHUSDT", "XRPUSDT", "SOLUSDT"],
|
||||
"symbol_gates": {
|
||||
"BTCUSDT": {
|
||||
"min_vol_threshold": 0.002,
|
||||
"whale_threshold_usd": 100000,
|
||||
"whale_flow_threshold_pct": 0.5,
|
||||
"obi_veto_threshold": 0.30,
|
||||
"spot_perp_divergence_veto": 0.003
|
||||
},
|
||||
"ETHUSDT": {
|
||||
"min_vol_threshold": 0.003,
|
||||
"whale_threshold_usd": 50000,
|
||||
"obi_veto_threshold": 0.35,
|
||||
"spot_perp_divergence_veto": 0.005
|
||||
},
|
||||
"SOLUSDT": {
|
||||
"min_vol_threshold": 0.004,
|
||||
"whale_threshold_usd": 20000,
|
||||
"obi_veto_threshold": 0.45,
|
||||
"spot_perp_divergence_veto": 0.008
|
||||
},
|
||||
"XRPUSDT": {
|
||||
"min_vol_threshold": 0.0025,
|
||||
"whale_threshold_usd": 30000,
|
||||
"obi_veto_threshold": 0.40,
|
||||
"spot_perp_divergence_veto": 0.006
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,203 +0,0 @@
|
||||
# 策略广场 数据合约文档
|
||||
|
||||
> 版本:v1.0
|
||||
> 日期:2026-03-07
|
||||
> 状态:待范总确认 ✅
|
||||
> 作者:露露 + 小范
|
||||
> 分支:`feature/strategy-plaza`
|
||||
|
||||
---
|
||||
|
||||
## 1. 功能概述
|
||||
|
||||
**策略广场**(Strategy Plaza)将现有的 signals-v53 / paper-v53 / signals-v53fast 等分散页面整合为统一入口:
|
||||
|
||||
- 总览页:策略卡片列表,展示每个策略的核心指标,30 秒自动刷新
|
||||
- 详情页:点击卡片进入,顶部 Tab 切换「信号引擎」和「模拟盘」视图
|
||||
|
||||
---
|
||||
|
||||
## 2. 前端路由
|
||||
|
||||
| 路由 | 说明 |
|
||||
|------|------|
|
||||
| `/strategy-plaza` | 策略广场总览(卡片列表) |
|
||||
| `/strategy-plaza/[id]` | 策略详情页,默认「信号引擎」tab |
|
||||
| `/strategy-plaza/[id]?tab=paper` | 策略详情页「模拟盘」tab |
|
||||
|
||||
**侧边栏变更:**
|
||||
- 新增「策略广场」单一入口
|
||||
- 原 `signals-v53` / `paper-v53` / `signals-v53fast` / `paper-v53fast` / `signals-v53middle` / `paper-v53middle` 页面:**保留但从侧边栏隐藏**(路由仍可访问)
|
||||
|
||||
---
|
||||
|
||||
## 3. API 设计
|
||||
|
||||
### 3.1 `GET /api/strategy-plaza`
|
||||
|
||||
返回所有策略的卡片摘要数据。
|
||||
|
||||
**Response:**
|
||||
```json
|
||||
{
|
||||
"strategies": [
|
||||
{
|
||||
"id": "v53",
|
||||
"display_name": "V5.3 标准版",
|
||||
"status": "running",
|
||||
"started_at": 1741234567000,
|
||||
"initial_balance": 10000,
|
||||
"current_balance": 8693,
|
||||
"net_usdt": -1307,
|
||||
"net_r": -6.535,
|
||||
"trade_count": 63,
|
||||
"win_rate": 49.2,
|
||||
"avg_win_r": 0.533,
|
||||
"avg_loss_r": -0.721,
|
||||
"open_positions": 0,
|
||||
"pnl_usdt_24h": -320,
|
||||
"pnl_r_24h": -1.6,
|
||||
"std_r": 0.9,
|
||||
"last_trade_at": 1741367890000
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
**字段说明:**
|
||||
|
||||
| 字段 | 类型 | 说明 |
|
||||
|------|------|------|
|
||||
| `id` | string | 策略唯一标识,与 DB strategy 字段一致 |
|
||||
| `display_name` | string | 展示名称 |
|
||||
| `status` | string | `running` / `paused` / `error` |
|
||||
| `started_at` | number (ms) | 策略启动时间(暂用 paper_trades 第一条 entry_ts,后续补 strategy_meta 表) |
|
||||
| `initial_balance` | number | 初始余额 USDT,固定 10000 |
|
||||
| `current_balance` | number | 当前余额 = initial_balance + net_usdt |
|
||||
| `net_usdt` | number | 累计盈亏 USDT = SUM(pnl_r) × 200 |
|
||||
| `net_r` | number | 累计净 R |
|
||||
| `trade_count` | number | 已出场交易数 |
|
||||
| `win_rate` | number | 胜率 % |
|
||||
| `avg_win_r` | number | 平均赢单 R |
|
||||
| `avg_loss_r` | number | 平均亏单 R(负数) |
|
||||
| `open_positions` | number | 当前活跃持仓数(exit_ts IS NULL) |
|
||||
| `pnl_usdt_24h` | number | 最近 24h 盈亏 USDT |
|
||||
| `pnl_r_24h` | number | 最近 24h 净 R |
|
||||
| `std_r` | number | 所有已出场交易的 pnl_r 标准差(风险感知) |
|
||||
| `last_trade_at` | number (ms) | 最近一笔成交的 exit_ts |
|
||||
|
||||
**status 判断逻辑:**
|
||||
- `running`:paper_config 中 enabled=true 且最近 signal_indicators 记录 < 5 分钟
|
||||
- `paused`:paper_config 中 enabled=false
|
||||
- `error`:paper_config 中 enabled=true 但 signal_indicators 最新记录 > 5 分钟
|
||||
|
||||
---
|
||||
|
||||
### 3.2 `GET /api/strategy-plaza/[id]/summary`
|
||||
|
||||
返回单个策略的完整摘要,包含卡片字段 + 详情字段。
|
||||
|
||||
**Response(在 3.1 基础上增加):**
|
||||
```json
|
||||
{
|
||||
"id": "v53",
|
||||
"display_name": "V5.3 标准版",
|
||||
"cvd_windows": "30m / 4h",
|
||||
"description": "标准版:30分钟+4小时CVD双轨,适配1小时信号周期",
|
||||
"symbols": ["BTCUSDT", "ETHUSDT", "SOLUSDT", "XRPUSDT"],
|
||||
"weights": {
|
||||
"direction": 55,
|
||||
"crowding": 25,
|
||||
"environment": 15,
|
||||
"auxiliary": 5
|
||||
},
|
||||
"thresholds": {
|
||||
"signal_threshold": 75,
|
||||
"flip_threshold": 85
|
||||
},
|
||||
"...所有 3.1 字段..."
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 3.3 `GET /api/strategy-plaza/[id]/signals`
|
||||
|
||||
复用现有 `/api/signals` 逻辑,增加 `strategy` 过滤。接口参数和返回格式与现有保持一致。
|
||||
|
||||
---
|
||||
|
||||
### 3.4 `GET /api/strategy-plaza/[id]/trades`
|
||||
|
||||
复用现有 `/api/paper-trades` 逻辑,增加 `strategy` 过滤。接口参数和返回格式与现有保持一致。
|
||||
|
||||
---
|
||||
|
||||
## 4. 数据来源映射
|
||||
|
||||
| 字段 | 数据来源 |
|
||||
|------|---------|
|
||||
| `net_usdt`, `net_r`, `trade_count`, `win_rate`, `avg_win_r`, `avg_loss_r` | `paper_trades` WHERE strategy=id AND exit_ts IS NOT NULL |
|
||||
| `open_positions` | `paper_trades` WHERE strategy=id AND exit_ts IS NULL |
|
||||
| `pnl_usdt_24h`, `pnl_r_24h` | `paper_trades` WHERE strategy=id AND exit_ts > NOW()-24h |
|
||||
| `std_r` | STDDEV(pnl_r) FROM paper_trades WHERE strategy=id AND exit_ts IS NOT NULL |
|
||||
| `started_at` | MIN(entry_ts) FROM paper_trades WHERE strategy=id |
|
||||
| `last_trade_at` | MAX(exit_ts) FROM paper_trades WHERE strategy=id AND exit_ts IS NOT NULL |
|
||||
| `status` | paper_config.json + signal_indicators 最新记录时间 |
|
||||
| `cvd_windows`, `weights`, `thresholds` | backend/strategies/[id].json |
|
||||
|
||||
---
|
||||
|
||||
## 5. 前端组件规划
|
||||
|
||||
### 5.1 总览页组件
|
||||
|
||||
```
|
||||
StrategyPlaza
|
||||
└── StrategyCardGrid
|
||||
└── StrategyCard (×N)
|
||||
├── 策略名 + status badge (running/paused/error)
|
||||
├── 运行时长 (now - started_at)
|
||||
├── 当前余额 / 初始余额
|
||||
├── 净盈亏 USDT + 净R(带颜色)
|
||||
├── 胜率
|
||||
├── 最近24h盈亏(小字)
|
||||
└── 点击 → /strategy-plaza/[id]
|
||||
```
|
||||
|
||||
### 5.2 详情页组件
|
||||
|
||||
```
|
||||
StrategyDetail
|
||||
├── 顶部:策略名 + status + 运行时长
|
||||
├── Tab 切换:[信号引擎] [模拟盘]
|
||||
│
|
||||
├── Tab: 信号引擎
|
||||
│ └── 复用 SignalsV53Page 内容
|
||||
│
|
||||
└── Tab: 模拟盘
|
||||
└── 复用 PaperV53Page 内容
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 6. 实现计划
|
||||
|
||||
| 阶段 | 内容 | 负责 |
|
||||
|------|------|------|
|
||||
| P0 | 后端 API `/api/strategy-plaza` | 露露 |
|
||||
| P1 | 后端 API `/api/strategy-plaza/[id]/summary` | 露露 |
|
||||
| P2 | 前端总览页(StrategyCard × 3) | 露露 |
|
||||
| P3 | 前端详情页(Tab + 复用现有组件) | 露露 |
|
||||
| P4 | 侧边栏整合(新增入口,隐藏旧页面) | 露露 |
|
||||
| Review | 代码审阅 + 逻辑验证 | 小范 |
|
||||
|
||||
> 开发前等范总确认数据结构,不提前动代码。
|
||||
|
||||
---
|
||||
|
||||
## 7. 变更记录
|
||||
|
||||
| 版本 | 日期 | 内容 |
|
||||
|------|------|------|
|
||||
| v1.0 | 2026-03-07 | 初版,露露起草 + 小范审阅 |
|
||||
@ -1,265 +0,0 @@
|
||||
# V5.4 Strategy Factory 需求文档
|
||||
|
||||
**版本**:v1.0
|
||||
**日期**:2026-03-11
|
||||
**作者**:露露
|
||||
**状态**:待范总 + 小范 Review
|
||||
|
||||
---
|
||||
|
||||
## 1. 背景与目标
|
||||
|
||||
当前系统(V5.3)使用单体 `signal_engine.py`,所有策略逻辑耦合在一起,存在以下问题:
|
||||
|
||||
- 修改任意策略参数需重启整个引擎,中断数据采集
|
||||
- 不同策略无法独立运行和对比,A/B 测试成本高
|
||||
- 参数配置分散在 JSON 文件中,无法通过前端界面管理
|
||||
- 无法按币种独立优化权重
|
||||
|
||||
V5.4 目标:构建 **Strategy Factory(策略工厂)**,将信号引擎解耦为数据总线 + 独立策略 Worker,支持前端可视化管理策略生命周期和参数配置。
|
||||
|
||||
---
|
||||
|
||||
## 2. 核心架构
|
||||
|
||||
### 2.1 整体架构
|
||||
|
||||
```
|
||||
Signal Engine(数据总线)
|
||||
├── 采集原始数据(aggTrades / OBI / 清算 / 市场数据)
|
||||
├── 计算基础 Feature(CVD / ATR / VWAP / whale flow / OBI / spot-perp div)
|
||||
└── 广播 feature_event(每15秒一次)
|
||||
|
||||
Strategy Workers(策略工厂)
|
||||
├── Worker-1:我的BTC策略01(BTCUSDT,asyncio协程)
|
||||
├── Worker-2:我的ETH策略01(ETHUSDT,asyncio协程)
|
||||
├── Worker-N:...
|
||||
└── 每个 Worker 订阅 feature_event,独立打分、开仓、管仓
|
||||
```
|
||||
|
||||
### 2.2 关键设计原则
|
||||
|
||||
- **同一进程 + asyncio 协程**:所有 Worker 共享同一 Python 进程,feature_event 内存传递,省资源
|
||||
- **独立资金池**:每个 Worker 有独立的 paper trading 余额,互不影响
|
||||
- **15秒内热生效**:前端修改参数后,Worker 在下一个评估周期(≤15秒)自动从 DB 读取新参数
|
||||
- **配置存 DB**:所有策略配置存入数据库,JSON 文件废弃
|
||||
- **直接切换**:V5.4 上线后直接替换 V5.3 单体,不并行
|
||||
|
||||
---
|
||||
|
||||
## 3. 策略生命周期
|
||||
|
||||
### 3.1 状态定义
|
||||
|
||||
```
|
||||
created(已创建)→ running(运行中)→ paused(已暂停)→ running
|
||||
↓
|
||||
deprecated(已废弃)→ running(重新启用)
|
||||
```
|
||||
|
||||
- **只有「废弃」,没有「删除」**
|
||||
- 废弃的策略数据永久保留,可在废弃列表中检索
|
||||
- 废弃策略可重新启用,继续使用原有余额和历史数据
|
||||
|
||||
### 3.2 策略标识
|
||||
|
||||
- 用户填写**显示名称**(自由命名,如"我的BTC激进策略")
|
||||
- 后台自动生成**UUID**作为唯一标识,用户不感知
|
||||
- `paper_trades` 等表通过 strategy_id(UUID)关联
|
||||
|
||||
### 3.3 余额管理
|
||||
|
||||
- 创建时设置初始资金(默认 10,000 USDT)
|
||||
- 支持**追加余额**:追加后,`initial_balance` 同步增加,`current_balance` 同步增加
|
||||
- 废弃后重新启用,继续使用废弃时的余额和历史数据
|
||||
|
||||
---
|
||||
|
||||
## 4. 策略配置参数
|
||||
|
||||
每个策略实例(每个币种独立)包含以下可配置参数,均存入数据库,前端可编辑。
|
||||
|
||||
### 4.1 基础信息
|
||||
|
||||
| 参数 | 说明 | 类型 | 默认值 | 范围 |
|
||||
|------|------|------|--------|------|
|
||||
| display_name | 策略显示名称 | string | — | 1-50字符 |
|
||||
| symbol | 交易对 | enum | BTCUSDT | BTCUSDT / ETHUSDT / SOLUSDT / XRPUSDT |
|
||||
| direction | 交易方向 | enum | both | long_only / short_only / both |
|
||||
| initial_balance | 初始资金(USDT) | float | 10000 | 1000-1000000 |
|
||||
|
||||
### 4.2 CVD 窗口配置
|
||||
|
||||
| 参数 | 说明 | 类型 | 默认值 | 可选项 |
|
||||
|------|------|------|--------|--------|
|
||||
| cvd_fast_window | 快线CVD窗口 | enum | 30m | 5m / 15m / 30m |
|
||||
| cvd_slow_window | 慢线CVD窗口 | enum | 4h | 1h / 4h |
|
||||
|
||||
### 4.3 四层权重(合计必须 = 100)
|
||||
|
||||
| 参数 | 说明 | 默认值 | 范围 |
|
||||
|------|------|--------|------|
|
||||
| weight_direction | 方向得分权重 | 55 | 10-80 |
|
||||
| weight_env | 环境得分权重 | 25 | 5-60 |
|
||||
| weight_aux | 辅助因子权重 | 15 | 0-40 |
|
||||
| weight_momentum | 动量权重 | 5 | 0-20 |
|
||||
|
||||
> 前端校验:四项之和必须 = 100,否则不允许保存
|
||||
|
||||
### 4.4 入场阈值
|
||||
|
||||
| 参数 | 说明 | 默认值 | 范围 |
|
||||
|------|------|--------|------|
|
||||
| entry_score | 入场最低总分 | 75 | 60-95 |
|
||||
|
||||
### 4.5 四道 Gate(过滤门)
|
||||
|
||||
每道 Gate 有独立开关和阈值:
|
||||
|
||||
| Gate | 说明 | 开关默认 | 阈值参数 | 默认值 | 范围 |
|
||||
|------|------|----------|----------|--------|------|
|
||||
| gate_obi | 订单簿失衡门 | ON | obi_threshold | 0.3 | 0.1-0.9 |
|
||||
| gate_whale_cvd | 大单CVD门 | ON | whale_cvd_threshold | 0.0 | -1.0-1.0 |
|
||||
| gate_vol_atr | 波动率ATR门 | ON | atr_percentile_min | 20 | 5-80 |
|
||||
| gate_spot_perp | 现货/永续溢价门 | OFF | spot_perp_threshold | 0.002 | 0.0005-0.01 |
|
||||
|
||||
### 4.6 风控参数
|
||||
|
||||
| 参数 | 说明 | 默认值 | 范围 |
|
||||
|------|------|--------|------|
|
||||
| sl_atr_multiplier | SL宽度(×ATR) | 1.5 | 0.5-3.0 |
|
||||
| tp1_ratio | TP1(×RD) | 0.75 | 0.3-2.0 |
|
||||
| tp2_ratio | TP2(×RD) | 1.5 | 0.5-4.0 |
|
||||
| timeout_minutes | 持仓超时(分钟) | 240 | 30-1440 |
|
||||
| flip_threshold | 反转平仓阈值(分) | 80 | 60-95 |
|
||||
|
||||
---
|
||||
|
||||
## 5. 前端功能需求
|
||||
|
||||
### 5.1 策略广场列表页(已有基础,新增以下)
|
||||
|
||||
- **右上角「+ 新增策略」按钮**:点击进入参数配置创建界面
|
||||
- **每个卡片新增「调整参数」按钮**:点击进入参数配置编辑界面(预填当前参数)
|
||||
- **每个卡片新增「废弃」按钮**:点击弹出二次确认,确认后策略进入废弃状态
|
||||
- **每个卡片新增「追加余额」功能**:输入追加金额,确认后更新余额
|
||||
|
||||
### 5.2 参数配置界面(新建/编辑共用)
|
||||
|
||||
- 基础信息:名称、币种、交易方向、初始资金
|
||||
- CVD窗口:快线/慢线各独立选择
|
||||
- 四层权重:滑块或数字输入,实时显示合计,合计≠100时禁止保存
|
||||
- 四道Gate:开关 + 阈值输入,各有说明文字和合理范围提示
|
||||
- 风控参数:数字输入,有最小/最大值限制
|
||||
- 底部:「保存并启动」(新建)/ 「保存」(编辑)按钮
|
||||
|
||||
### 5.3 策略详情页(已有基础,新增以下)
|
||||
|
||||
- 新增第三个 Tab:**「参数配置」**
|
||||
- 展示当前所有参数,可点击编辑跳转到编辑界面
|
||||
|
||||
### 5.4 侧边栏新增入口
|
||||
|
||||
- **「废弃策略」**:点击进入废弃策略列表
|
||||
- 展示格式与正常卡片相同,额外显示废弃时间
|
||||
- 每个废弃策略有「重新启用」按钮
|
||||
- 重新启用后恢复至运行中状态,继续原余额和数据
|
||||
|
||||
### 5.5 权限
|
||||
|
||||
- 所有页面需登录才能访问(复用现有 JWT)
|
||||
- 登录后有全部操作权限,无角色区分
|
||||
|
||||
---
|
||||
|
||||
## 6. 后端架构需求
|
||||
|
||||
### 6.1 数据库新增表
|
||||
|
||||
**`strategies` 表**(策略配置主表):
|
||||
```sql
|
||||
strategy_id UUID PRIMARY KEY
|
||||
display_name TEXT NOT NULL
|
||||
symbol TEXT NOT NULL
|
||||
direction TEXT NOT NULL DEFAULT 'both'
|
||||
status TEXT NOT NULL DEFAULT 'running' -- running/paused/deprecated
|
||||
initial_balance FLOAT NOT NULL DEFAULT 10000
|
||||
current_balance FLOAT NOT NULL DEFAULT 10000
|
||||
cvd_fast_window TEXT NOT NULL DEFAULT '30m'
|
||||
cvd_slow_window TEXT NOT NULL DEFAULT '4h'
|
||||
weight_direction INT NOT NULL DEFAULT 55
|
||||
weight_env INT NOT NULL DEFAULT 25
|
||||
weight_aux INT NOT NULL DEFAULT 15
|
||||
weight_momentum INT NOT NULL DEFAULT 5
|
||||
entry_score INT NOT NULL DEFAULT 75
|
||||
gate_obi_enabled BOOL NOT NULL DEFAULT TRUE
|
||||
obi_threshold FLOAT NOT NULL DEFAULT 0.3
|
||||
gate_whale_enabled BOOL NOT NULL DEFAULT TRUE
|
||||
whale_cvd_threshold FLOAT NOT NULL DEFAULT 0.0
|
||||
gate_vol_enabled BOOL NOT NULL DEFAULT TRUE
|
||||
atr_percentile_min INT NOT NULL DEFAULT 20
|
||||
gate_spot_perp_enabled BOOL NOT NULL DEFAULT FALSE
|
||||
spot_perp_threshold FLOAT NOT NULL DEFAULT 0.002
|
||||
sl_atr_multiplier FLOAT NOT NULL DEFAULT 1.5
|
||||
tp1_ratio FLOAT NOT NULL DEFAULT 0.75
|
||||
tp2_ratio FLOAT NOT NULL DEFAULT 1.5
|
||||
timeout_minutes INT NOT NULL DEFAULT 240
|
||||
flip_threshold INT NOT NULL DEFAULT 80
|
||||
deprecated_at TIMESTAMP
|
||||
created_at TIMESTAMP DEFAULT NOW()
|
||||
updated_at TIMESTAMP DEFAULT NOW()
|
||||
```
|
||||
|
||||
### 6.2 现有表调整
|
||||
|
||||
- `paper_trades`:`strategy` 字段改为存 `strategy_id`(UUID),兼容现有数据(v53/v53_middle/v53_fast 保留字符串形式)
|
||||
- `signal_indicators`:同上
|
||||
|
||||
### 6.3 API 新增端点
|
||||
|
||||
```
|
||||
POST /api/strategies 创建策略
|
||||
GET /api/strategies 获取所有策略列表(含废弃)
|
||||
GET /api/strategies/{id} 获取单个策略详情
|
||||
PATCH /api/strategies/{id} 更新策略参数
|
||||
POST /api/strategies/{id}/pause 暂停策略
|
||||
POST /api/strategies/{id}/resume 恢复策略
|
||||
POST /api/strategies/{id}/deprecate 废弃策略
|
||||
POST /api/strategies/{id}/restore 重新启用
|
||||
POST /api/strategies/{id}/add-balance 追加余额
|
||||
```
|
||||
|
||||
### 6.4 Signal Engine 改造
|
||||
|
||||
- Signal Engine 只负责 feature 计算和广播,不再包含评分/开仓逻辑
|
||||
- 每个 Strategy Worker 作为 asyncio 协程,订阅 feature_event
|
||||
- Worker 启动时从 DB 读取配置,每15秒评估时重新读取(捕获参数变更)
|
||||
|
||||
---
|
||||
|
||||
## 7. 迁移计划
|
||||
|
||||
V5.4 上线时:
|
||||
1. 将现有 v53、v53_middle、v53_fast 三个策略迁移为 `strategies` 表中的三条记录
|
||||
2. 历史 `paper_trades` 数据通过 strategy 名称映射到对应 strategy_id
|
||||
3. 直接切换,不保留 V5.3 单体并行
|
||||
|
||||
---
|
||||
|
||||
## 8. 不在本期范围内
|
||||
|
||||
- 实盘交易(本期只做 paper trading)
|
||||
- 多用户/多账户体系
|
||||
- 策略算法类型选择(本期只支持四层评分算法)
|
||||
- 自动化参数优化(Optuna 集成)
|
||||
|
||||
---
|
||||
|
||||
## 9. Review 检查清单
|
||||
|
||||
- [ ] 范总确认需求无遗漏
|
||||
- [ ] 小范审阅数据结构合理性
|
||||
- [ ] 确认 `strategies` 表字段完整性
|
||||
- [ ] 确认 API 端点覆盖所有前端操作
|
||||
- [ ] 确认迁移方案不丢失历史数据
|
||||
- [ ] 需求文档 Review 通过后,再开始写数据合约文档
|
||||
@ -1,405 +0,0 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect } from "react";
|
||||
import Link from "next/link";
|
||||
import { authFetch, useAuth } from "@/lib/auth";
|
||||
import { XAxis, YAxis, Tooltip, ResponsiveContainer, ReferenceLine, Area, AreaChart } from "recharts";
|
||||
|
||||
function bjt(ms: number) {
|
||||
const d = new Date(ms + 8 * 3600 * 1000);
|
||||
return `${String(d.getUTCMonth() + 1).padStart(2, "0")}-${String(d.getUTCDate()).padStart(2, "0")} ${String(d.getUTCHours()).padStart(2, "0")}:${String(d.getUTCMinutes()).padStart(2, "0")}`;
|
||||
}
|
||||
|
||||
function fmtPrice(p: number) {
|
||||
return p < 100 ? p.toFixed(4) : p.toLocaleString("en-US", { minimumFractionDigits: 1, maximumFractionDigits: 1 });
|
||||
}
|
||||
|
||||
function parseFactors(raw: any) {
|
||||
if (!raw) return null;
|
||||
if (typeof raw === "string") { try { return JSON.parse(raw); } catch { return null; } }
|
||||
return raw;
|
||||
}
|
||||
|
||||
const STRATEGY = "v53_middle";
|
||||
const ALL_COINS = ["BTCUSDT", "ETHUSDT", "XRPUSDT", "SOLUSDT"];
|
||||
|
||||
// ─── 最新信号 ────────────────────────────────────────────────────
|
||||
|
||||
function LatestSignals() {
|
||||
const [signals, setSignals] = useState<Record<string, any>>({});
|
||||
useEffect(() => {
|
||||
const f = async () => {
|
||||
for (const sym of ALL_COINS) {
|
||||
const coin = sym.replace("USDT", "");
|
||||
try {
|
||||
const r = await authFetch(`/api/signals/signal-history?symbol=${coin}&limit=1&strategy=${STRATEGY}`);
|
||||
if (r.ok) { const j = await r.json(); if (j.data?.length > 0) setSignals(prev => ({ ...prev, [sym]: j.data[0] })); }
|
||||
} catch {}
|
||||
}
|
||||
};
|
||||
f(); const iv = setInterval(f, 15000); return () => clearInterval(iv);
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div className="rounded-xl border border-slate-200 bg-white overflow-hidden">
|
||||
<div className="px-3 py-2 border-b border-slate-100 flex items-center justify-between">
|
||||
<h3 className="font-semibold text-slate-800 text-xs">最新信号</h3>
|
||||
<span className="px-2 py-0.5 rounded text-[10px] font-semibold bg-blue-100 text-blue-700 border border-blue-200">v53</span>
|
||||
</div>
|
||||
<div className="divide-y divide-slate-50">
|
||||
{ALL_COINS.map(sym => {
|
||||
const s = signals[sym];
|
||||
const coin = sym.replace("USDT", "");
|
||||
const ago = s?.ts ? Math.round((Date.now() - s.ts) / 60000) : null;
|
||||
const fc = s?.factors;
|
||||
const gatePassed = fc?.gate_passed ?? true;
|
||||
return (
|
||||
<div key={sym} className="px-3 py-2">
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="font-mono text-xs font-bold text-slate-700 w-8">{coin}</span>
|
||||
{s?.signal ? (
|
||||
<>
|
||||
<span className={`text-xs font-bold ${s.signal === "LONG" ? "text-emerald-600" : "text-red-500"}`}>
|
||||
{s.signal === "LONG" ? "🟢" : "🔴"} {s.signal}
|
||||
</span>
|
||||
<span className="font-mono text-xs font-bold text-slate-800">{s.score}分</span>
|
||||
</>
|
||||
) : <span className="text-[10px] text-slate-400">暂无信号</span>}
|
||||
</div>
|
||||
{ago !== null && <span className="text-[10px] text-slate-400">{ago < 60 ? `${ago}m前` : `${Math.round(ago/60)}h前`}</span>}
|
||||
</div>
|
||||
{fc && (
|
||||
<div className="flex gap-1 mt-1 flex-wrap">
|
||||
<span className={`text-[9px] px-1 py-0.5 rounded ${gatePassed ? "bg-emerald-100 text-emerald-700" : "bg-red-100 text-red-600"}`}>
|
||||
{gatePassed ? "✅" : "❌"} {fc.gate_block || "Gate"}
|
||||
</span>
|
||||
<span className="text-[9px] px-1 py-0.5 rounded bg-blue-50 text-blue-700">方向{fc.direction?.score ?? 0}/55</span>
|
||||
<span className="text-[9px] px-1 py-0.5 rounded bg-violet-50 text-violet-700">拥挤{fc.crowding?.score ?? 0}/25</span>
|
||||
<span className="text-[9px] px-1 py-0.5 rounded bg-emerald-50 text-emerald-700">环境{fc.environment?.score ?? 0}/15</span>
|
||||
<span className="text-[9px] px-1 py-0.5 rounded bg-slate-100 text-slate-600">辅助{fc.auxiliary?.score ?? 0}/5</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ─── 控制面板 ────────────────────────────────────────────────────
|
||||
|
||||
function ControlPanel() {
|
||||
const [config, setConfig] = useState<any>(null);
|
||||
const [saving, setSaving] = useState(false);
|
||||
useEffect(() => {
|
||||
(async () => { try { const r = await authFetch("/api/paper/config"); if (r.ok) setConfig(await r.json()); } catch {} })();
|
||||
}, []);
|
||||
const toggle = async () => {
|
||||
setSaving(true);
|
||||
try {
|
||||
const r = await authFetch("/api/paper/config", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ enabled: !config.enabled }) });
|
||||
if (r.ok) setConfig(await r.json().then((j: any) => j.config));
|
||||
} catch {} finally { setSaving(false); }
|
||||
};
|
||||
if (!config) return null;
|
||||
return (
|
||||
<div className={`rounded-xl border-2 ${config.enabled ? "border-emerald-400 bg-emerald-50" : "border-slate-200 bg-white"} px-3 py-2 flex items-center justify-between`}>
|
||||
<div className="flex items-center gap-3">
|
||||
<button onClick={toggle} disabled={saving}
|
||||
className={`px-3 py-1.5 rounded-lg text-xs font-bold transition-all ${config.enabled ? "bg-red-500 text-white hover:bg-red-600" : "bg-emerald-500 text-white hover:bg-emerald-600"}`}>
|
||||
{saving ? "..." : config.enabled ? "⏹ 停止" : "▶️ 启动"}
|
||||
</button>
|
||||
<span className={`text-xs font-medium ${config.enabled ? "text-emerald-700" : "text-slate-500"}`}>{config.enabled ? "🟢 运行中" : "⚪ 已停止"}</span>
|
||||
</div>
|
||||
<div className="flex gap-4 text-[10px] text-slate-500">
|
||||
<span>初始: ${config.initial_balance?.toLocaleString()}</span>
|
||||
<span>风险: {(config.risk_per_trade * 100).toFixed(0)}%</span>
|
||||
<span>最大: {config.max_positions}仓</span>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ─── 总览 ────────────────────────────────────────────────────────
|
||||
|
||||
function SummaryCards() {
|
||||
const [data, setData] = useState<any>(null);
|
||||
useEffect(() => {
|
||||
const f = async () => { try { const r = await authFetch(`/api/paper/summary?strategy=${STRATEGY}`); if (r.ok) setData(await r.json()); } catch {} };
|
||||
f(); const iv = setInterval(f, 10000); return () => clearInterval(iv);
|
||||
}, []);
|
||||
if (!data) return <div className="text-center text-slate-400 text-sm py-4">加载中...</div>;
|
||||
return (
|
||||
<div className="grid grid-cols-3 lg:grid-cols-6 gap-1.5">
|
||||
{[
|
||||
{ label: "总盈亏(R)", value: `${data.total_pnl >= 0 ? "+" : ""}${data.total_pnl}R`, sub: `${data.total_pnl_usdt >= 0 ? "+" : ""}$${data.total_pnl_usdt}`, color: data.total_pnl >= 0 ? "text-emerald-600" : "text-red-500" },
|
||||
{ label: "胜率", value: `${data.win_rate}%`, sub: `共${data.total_trades}笔`, color: "text-slate-800" },
|
||||
{ label: "持仓中", value: data.active_positions, sub: "活跃仓位", color: "text-blue-600" },
|
||||
{ label: "盈亏比", value: data.profit_factor, sub: "PF", color: "text-slate-800" },
|
||||
{ label: "当前资金", value: `$${data.balance?.toLocaleString()}`, sub: "虚拟余额", color: data.balance >= 10000 ? "text-emerald-600" : "text-red-500" },
|
||||
{ label: "状态", value: data.start_time ? "运行中 ✅" : "等待首笔", sub: "accumulating", color: "text-slate-600" },
|
||||
].map(({ label, value, sub, color }) => (
|
||||
<div key={label} className="bg-white rounded-lg border border-slate-200 px-2.5 py-2">
|
||||
<p className="text-[10px] text-slate-400">{label}</p>
|
||||
<p className={`font-mono font-bold text-base ${color}`}>{value}</p>
|
||||
<p className="text-[10px] text-slate-400">{sub}</p>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ─── 当前持仓 ────────────────────────────────────────────────────
|
||||
|
||||
function ActivePositions() {
|
||||
const [positions, setPositions] = useState<any[]>([]);
|
||||
const [wsPrices, setWsPrices] = useState<Record<string, number>>({});
|
||||
const [paperRiskUsd, setPaperRiskUsd] = useState(200);
|
||||
useEffect(() => {
|
||||
(async () => { try { const r = await authFetch("/api/paper/config"); if (r.ok) { const cfg = await r.json(); setPaperRiskUsd((cfg.initial_balance||10000)*(cfg.risk_per_trade||0.02)); } } catch {} })();
|
||||
}, []);
|
||||
useEffect(() => {
|
||||
const f = async () => { try { const r = await authFetch(`/api/paper/positions?strategy=${STRATEGY}`); if (r.ok) setPositions((await r.json()).data||[]); } catch {} };
|
||||
f(); const iv = setInterval(f, 10000); return () => clearInterval(iv);
|
||||
}, []);
|
||||
useEffect(() => {
|
||||
const streams = ["btcusdt","ethusdt","xrpusdt","solusdt"].map(s=>`${s}@aggTrade`).join("/");
|
||||
const ws = new WebSocket(`wss://fstream.binance.com/stream?streams=${streams}`);
|
||||
ws.onmessage = (e) => { try { const msg=JSON.parse(e.data); if(msg.data){const sym=msg.data.s;const price=parseFloat(msg.data.p);if(sym&&price>0)setWsPrices(prev=>({...prev,[sym]:price}));} } catch {} };
|
||||
return () => ws.close();
|
||||
}, []);
|
||||
if (positions.length === 0) return <div className="rounded-xl border border-slate-200 bg-white px-3 py-4 text-center text-slate-400 text-sm">v53 暂无活跃持仓</div>;
|
||||
return (
|
||||
<div className="rounded-xl border border-slate-200 bg-white overflow-hidden">
|
||||
<div className="px-3 py-2 border-b border-slate-100"><h3 className="font-semibold text-slate-800 text-xs">当前持仓 <span className="text-[10px] text-emerald-500 font-normal">● 实时</span></h3></div>
|
||||
<div className="divide-y divide-slate-100">
|
||||
{positions.map((p: any) => {
|
||||
const sym = p.symbol?.replace("USDT","") || "";
|
||||
const holdMin = Math.round((Date.now()-p.entry_ts)/60000);
|
||||
const currentPrice = wsPrices[p.symbol]||p.current_price||0;
|
||||
const entry = p.entry_price||0;
|
||||
const riskDist = p.risk_distance||Math.abs(entry-(p.sl_price||entry))||1;
|
||||
const tp1R = riskDist>0?(p.direction==="LONG"?((p.tp1_price||0)-entry)/riskDist:(entry-(p.tp1_price||0))/riskDist):0;
|
||||
const fullR = riskDist>0?(p.direction==="LONG"?(currentPrice-entry)/riskDist:(entry-currentPrice)/riskDist):0;
|
||||
const unrealR = p.tp1_hit?0.5*tp1R+0.5*fullR:fullR;
|
||||
const unrealUsdt = unrealR*paperRiskUsd;
|
||||
const fc = parseFactors(p.score_factors);
|
||||
const track = fc?.track||(p.symbol==="BTCUSDT"?"BTC":"ALT");
|
||||
return (
|
||||
<div key={p.id} className="px-3 py-2 bg-emerald-50/60">
|
||||
<div className="flex items-center justify-between gap-2">
|
||||
<div className="flex items-center gap-2 flex-wrap">
|
||||
<span className={`text-xs font-bold ${p.direction==="LONG"?"text-emerald-600":"text-red-500"}`}>{p.direction==="LONG"?"🟢":"🔴"} {sym} {p.direction}</span>
|
||||
<span className={`px-1.5 py-0.5 rounded text-[10px] font-semibold ${track==="BTC"?"bg-amber-100 text-amber-700":"bg-purple-100 text-purple-700"}`}>{track}</span>
|
||||
<span className="text-[10px] text-slate-500">评分{p.score}</span>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<span className={`font-mono text-sm font-bold ${unrealR>=0?"text-emerald-600":"text-red-500"}`}>{unrealR>=0?"+":""}{unrealR.toFixed(2)}R</span>
|
||||
<span className="text-[10px] text-slate-400">{holdMin}m</span>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex gap-3 mt-1 text-[10px] font-mono text-slate-600 flex-wrap">
|
||||
<span>入: ${fmtPrice(p.entry_price)}</span>
|
||||
<span className="text-blue-600">现: ${currentPrice?fmtPrice(currentPrice):"-"}</span>
|
||||
<span className="text-emerald-600">TP1: ${fmtPrice(p.tp1_price)}{p.tp1_hit?" ✅":""}</span>
|
||||
<span className="text-emerald-600">TP2: ${fmtPrice(p.tp2_price)}</span>
|
||||
<span className="text-red-500">SL: ${fmtPrice(p.sl_price)}</span>
|
||||
</div>
|
||||
<div className="mt-1 text-[9px] text-slate-400">
|
||||
入场时间: {p.entry_ts ? new Date(p.entry_ts).toLocaleString("zh-CN", {hour12:false, month:"2-digit", day:"2-digit", hour:"2-digit", minute:"2-digit", second:"2-digit"} as any) : "-"}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ─── 权益曲线 ────────────────────────────────────────────────────
|
||||
|
||||
function EquityCurve() {
|
||||
const [data, setData] = useState<any[]>([]);
|
||||
useEffect(() => {
|
||||
const f = async () => { try { const r = await authFetch(`/api/paper/equity-curve?strategy=${STRATEGY}`); if (r.ok) setData((await r.json()).data||[]); } catch {} };
|
||||
f(); const iv = setInterval(f, 30000); return () => clearInterval(iv);
|
||||
}, []);
|
||||
return (
|
||||
<div className="rounded-xl border border-slate-200 bg-white overflow-hidden">
|
||||
<div className="px-3 py-2 border-b border-slate-100"><h3 className="font-semibold text-slate-800 text-xs">权益曲线</h3></div>
|
||||
{data.length < 2 ? <div className="px-3 py-6 text-center text-xs text-slate-400">数据积累中...</div> : (
|
||||
<div className="p-2" style={{ height: 200 }}>
|
||||
<ResponsiveContainer width="100%" height="100%">
|
||||
<AreaChart data={data}>
|
||||
<XAxis dataKey="ts" tickFormatter={(v) => bjt(v)} tick={{ fontSize: 10 }} />
|
||||
<YAxis tick={{ fontSize: 10 }} tickFormatter={(v) => `${v}R`} />
|
||||
<Tooltip labelFormatter={(v) => bjt(Number(v))} formatter={(v: any) => [`${v}R`, "累计PnL"]} />
|
||||
<ReferenceLine y={0} stroke="#94a3b8" strokeDasharray="3 3" />
|
||||
<Area type="monotone" dataKey="pnl" stroke="#10b981" fill="#d1fae5" strokeWidth={2} />
|
||||
</AreaChart>
|
||||
</ResponsiveContainer>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ─── 历史交易 ────────────────────────────────────────────────────
|
||||
|
||||
type FilterSymbol = "all" | "BTC" | "ETH" | "XRP" | "SOL";
|
||||
type FilterResult = "all" | "win" | "loss";
|
||||
|
||||
function TradeHistory() {
|
||||
const [trades, setTrades] = useState<any[]>([]);
|
||||
const [symbol, setSymbol] = useState<FilterSymbol>("all");
|
||||
const [result, setResult] = useState<FilterResult>("all");
|
||||
useEffect(() => {
|
||||
const f = async () => { try { const r = await authFetch(`/api/paper/trades?symbol=${symbol}&result=${result}&strategy=${STRATEGY}&limit=50`); if (r.ok) setTrades((await r.json()).data||[]); } catch {} };
|
||||
f(); const iv = setInterval(f, 10000); return () => clearInterval(iv);
|
||||
}, [symbol, result]);
|
||||
return (
|
||||
<div className="rounded-xl border border-slate-200 bg-white overflow-hidden">
|
||||
<div className="px-3 py-2 border-b border-slate-100 flex items-center justify-between flex-wrap gap-1">
|
||||
<h3 className="font-semibold text-slate-800 text-xs">历史交易</h3>
|
||||
<div className="flex items-center gap-1 flex-wrap">
|
||||
{(["all","BTC","ETH","XRP","SOL"] as FilterSymbol[]).map(s => (
|
||||
<button key={s} onClick={() => setSymbol(s)} className={`px-2 py-0.5 rounded text-[10px] ${symbol===s?"bg-slate-800 text-white":"text-slate-500 hover:bg-slate-100"}`}>{s==="all"?"全部":s}</button>
|
||||
))}
|
||||
<span className="text-slate-300">|</span>
|
||||
{(["all","win","loss"] as FilterResult[]).map(r => (
|
||||
<button key={r} onClick={() => setResult(r)} className={`px-2 py-0.5 rounded text-[10px] ${result===r?"bg-slate-800 text-white":"text-slate-500 hover:bg-slate-100"}`}>{r==="all"?"全部":r==="win"?"盈利":"亏损"}</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
<div className="max-h-64 overflow-y-auto">
|
||||
{trades.length === 0 ? <div className="text-center text-slate-400 text-sm py-6">暂无交易记录</div> : (
|
||||
<table className="w-full text-[11px]">
|
||||
<thead className="bg-slate-50 sticky top-0">
|
||||
<tr className="text-slate-500">
|
||||
<th className="px-2 py-1.5 text-left font-medium">币种</th>
|
||||
<th className="px-2 py-1.5 text-left font-medium">方向</th>
|
||||
<th className="px-2 py-1.5 text-right font-medium">入场价</th>
|
||||
<th className="px-2 py-1.5 text-right font-medium">出场价</th>
|
||||
<th className="px-2 py-1.5 text-right font-medium">PnL(R)</th>
|
||||
<th className="px-2 py-1.5 text-center font-medium">状态</th>
|
||||
<th className="px-2 py-1.5 text-right font-medium">分数</th>
|
||||
<th className="px-2 py-1.5 text-right font-medium">入场时间</th>
|
||||
<th className="px-2 py-1.5 text-right font-medium">出场时间</th>
|
||||
<th className="px-2 py-1.5 text-right font-medium">持仓</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y divide-slate-50">
|
||||
{trades.map((t: any) => {
|
||||
const holdMin = t.exit_ts&&t.entry_ts?Math.round((t.exit_ts-t.entry_ts)/60000):0;
|
||||
const fc = parseFactors(t.score_factors);
|
||||
const track = fc?.track||(t.symbol==="BTCUSDT"?"BTC":"ALT");
|
||||
const fmtTime = (ms: number) => ms ? new Date(ms).toLocaleString("zh-CN", {hour12:false, month:"2-digit", day:"2-digit", hour:"2-digit", minute:"2-digit"} as any) : "-";
|
||||
return (
|
||||
<tr key={t.id} className="hover:bg-slate-50">
|
||||
<td className="px-2 py-1.5 font-mono">{t.symbol?.replace("USDT","")}<span className={`ml-1 text-[9px] px-1 rounded ${track==="BTC"?"bg-amber-100 text-amber-700":"bg-purple-100 text-purple-700"}`}>{track}</span></td>
|
||||
<td className={`px-2 py-1.5 font-bold ${t.direction==="LONG"?"text-emerald-600":"text-red-500"}`}>{t.direction==="LONG"?"🟢":"🔴"} {t.direction}</td>
|
||||
<td className="px-2 py-1.5 text-right font-mono">{fmtPrice(t.entry_price)}</td>
|
||||
<td className="px-2 py-1.5 text-right font-mono">{t.exit_price?fmtPrice(t.exit_price):"-"}</td>
|
||||
<td className={`px-2 py-1.5 text-right font-mono font-bold ${t.pnl_r>0?"text-emerald-600":t.pnl_r<0?"text-red-500":"text-slate-500"}`}>{t.pnl_r>0?"+":""}{t.pnl_r?.toFixed(2)}</td>
|
||||
<td className="px-2 py-1.5 text-center"><span className={`px-1 py-0.5 rounded text-[9px] ${t.status==="tp"?"bg-emerald-100 text-emerald-700":t.status==="sl"?"bg-red-100 text-red-700":t.status==="sl_be"?"bg-amber-100 text-amber-700":t.status==="signal_flip"?"bg-purple-100 text-purple-700":"bg-slate-100 text-slate-600"}`}>{t.status==="tp"?"止盈":t.status==="sl"?"止损":t.status==="sl_be"?"保本":t.status==="timeout"?"超时":t.status==="signal_flip"?"翻转":t.status}</span></td>
|
||||
<td className="px-2 py-1.5 text-right font-mono">{t.score}</td>
|
||||
<td className="px-2 py-1.5 text-right text-slate-400 whitespace-nowrap">{fmtTime(t.entry_ts)}</td>
|
||||
<td className="px-2 py-1.5 text-right text-slate-400 whitespace-nowrap">{fmtTime(t.exit_ts)}</td>
|
||||
<td className="px-2 py-1.5 text-right text-slate-400">{holdMin}m</td>
|
||||
</tr>
|
||||
);
|
||||
})}
|
||||
</tbody>
|
||||
</table>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ─── 统计面板 ────────────────────────────────────────────────────
|
||||
|
||||
function StatsPanel() {
|
||||
const [data, setData] = useState<any>(null);
|
||||
const [tab, setTab] = useState("ALL");
|
||||
useEffect(() => {
|
||||
const f = async () => { try { const r = await authFetch(`/api/paper/stats?strategy=${STRATEGY}`); if (r.ok) setData(await r.json()); } catch {} };
|
||||
f(); const iv = setInterval(f, 30000); return () => clearInterval(iv);
|
||||
}, []);
|
||||
if (!data || data.error) return (
|
||||
<div className="rounded-xl border border-slate-200 bg-white overflow-hidden">
|
||||
<div className="px-3 py-2 border-b border-slate-100"><h3 className="font-semibold text-slate-800 text-xs">详细统计</h3></div>
|
||||
<div className="p-3 text-xs text-slate-400">等待交易记录积累...</div>
|
||||
</div>
|
||||
);
|
||||
const coinTabs = ["ALL","BTC","ETH","XRP","SOL"];
|
||||
const st = tab==="ALL"?data:(data.by_symbol?.[tab]||null);
|
||||
return (
|
||||
<div className="rounded-xl border border-slate-200 bg-white overflow-hidden">
|
||||
<div className="px-3 py-2 border-b border-slate-100 flex items-center justify-between flex-wrap gap-1">
|
||||
<h3 className="font-semibold text-slate-800 text-xs">详细统计</h3>
|
||||
<div className="flex items-center gap-1">
|
||||
{coinTabs.map(t => (
|
||||
<button key={t} onClick={() => setTab(t)} className={`px-2 py-0.5 rounded text-[10px] font-medium transition-colors ${tab===t?"bg-slate-800 text-white":"bg-slate-100 text-slate-500 hover:bg-slate-200"}`}>{t==="ALL"?"总计":t}</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
{st ? (
|
||||
<div className="p-3">
|
||||
<div className="grid grid-cols-2 lg:grid-cols-4 gap-2 text-xs">
|
||||
<div><span className="text-slate-400">胜率</span><p className="font-mono font-bold">{st.win_rate}%</p></div>
|
||||
<div><span className="text-slate-400">盈亏比</span><p className="font-mono font-bold">{st.win_loss_ratio}</p></div>
|
||||
<div><span className="text-slate-400">平均盈利</span><p className="font-mono font-bold text-emerald-600">+{st.avg_win}R</p></div>
|
||||
<div><span className="text-slate-400">平均亏损</span><p className="font-mono font-bold text-red-500">-{st.avg_loss}R</p></div>
|
||||
<div><span className="text-slate-400">最大回撤</span><p className="font-mono font-bold">{st.mdd}R</p></div>
|
||||
<div><span className="text-slate-400">夏普比率</span><p className="font-mono font-bold">{st.sharpe}</p></div>
|
||||
<div><span className="text-slate-400">总盈亏</span><p className={`font-mono font-bold ${(st.total_pnl??0)>=0?"text-emerald-600":"text-red-500"}`}>{(st.total_pnl??0)>=0?"+":""}{st.total_pnl??"-"}R</p></div>
|
||||
<div><span className="text-slate-400">总笔数</span><p className="font-mono font-bold">{st.total??data.total}</p></div>
|
||||
<div><span className="text-slate-400">做多胜率</span><p className="font-mono">{st.long_win_rate}% ({st.long_count}笔)</p></div>
|
||||
<div><span className="text-slate-400">做空胜率</span><p className="font-mono">{st.short_win_rate}% ({st.short_count}笔)</p></div>
|
||||
</div>
|
||||
</div>
|
||||
) : <div className="p-3 text-xs text-slate-400">该币种暂无数据</div>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ─── 主页面 ──────────────────────────────────────────────────────
|
||||
|
||||
export default function PaperTradingV53Page() {
|
||||
const { isLoggedIn, loading } = useAuth();
|
||||
if (loading) return <div className="text-center text-slate-400 py-8">加载中...</div>;
|
||||
if (!isLoggedIn) return (
|
||||
<div className="flex flex-col items-center justify-center h-64 gap-4">
|
||||
<div className="text-5xl">🔒</div>
|
||||
<p className="text-slate-600 font-medium">请先登录查看模拟盘</p>
|
||||
<Link href="/login" className="bg-blue-600 text-white px-4 py-2 rounded-lg text-sm">登录</Link>
|
||||
</div>
|
||||
);
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
{/* Fast 实验版标识条 */}
|
||||
<div className="rounded-lg bg-gradient-to-r from-orange-500 to-amber-400 px-3 py-1.5 flex items-center justify-between">
|
||||
<span className="text-white text-xs font-bold">🚀 V5.3 Middle — 实验变体 A/B对照</span>
|
||||
<div className="flex gap-2 text-white text-[10px] font-medium">
|
||||
<span className="bg-white/20 px-2 py-0.5 rounded">CVD 5m/30m</span>
|
||||
<span className="bg-white/20 px-2 py-0.5 rounded">OBI+加分</span>
|
||||
<span className="bg-white/20 px-2 py-0.5 rounded">accel独立触发</span>
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<h1 className="text-lg font-bold text-slate-900">🚀 模拟盘 V5.3 Middle</h1>
|
||||
<p className="text-[10px] text-slate-500">实验变体 v53_middle · BTC/ETH/XRP/SOL · CVD 5m/30m · OBI正向加分 · 与 V5.3 同起点对照</p>
|
||||
</div>
|
||||
<ControlPanel />
|
||||
<SummaryCards />
|
||||
<LatestSignals />
|
||||
<ActivePositions />
|
||||
<EquityCurve />
|
||||
<TradeHistory />
|
||||
<StatsPanel />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@ -1,580 +0,0 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState, useCallback } from "react";
|
||||
import { authFetch } from "@/lib/auth";
|
||||
import { useAuth } from "@/lib/auth";
|
||||
import Link from "next/link";
|
||||
import {
|
||||
ComposedChart, Area, Line, XAxis, YAxis, Tooltip, ResponsiveContainer,
|
||||
ReferenceLine, CartesianGrid, Legend
|
||||
} from "recharts";
|
||||
|
||||
type Symbol = "BTC" | "ETH" | "XRP" | "SOL";
|
||||
|
||||
interface IndicatorRow {
|
||||
ts: number;
|
||||
cvd_fast: number;
|
||||
cvd_mid: number;
|
||||
cvd_day: number;
|
||||
atr_5m: number;
|
||||
vwap_30m: number;
|
||||
price: number;
|
||||
score: number;
|
||||
signal: string | null;
|
||||
}
|
||||
|
||||
interface LatestIndicator {
|
||||
ts: number;
|
||||
cvd_fast: number;
|
||||
cvd_mid: number;
|
||||
cvd_day: number;
|
||||
cvd_fast_slope: number;
|
||||
atr_5m: number;
|
||||
atr_percentile: number;
|
||||
vwap_30m: number;
|
||||
price: number;
|
||||
p95_qty: number;
|
||||
p99_qty: number;
|
||||
score: number;
|
||||
display_score?: number; // v53_btc: alt_score_ref(参考分)
|
||||
gate_passed?: boolean; // v53_btc顶层字段
|
||||
signal: string | null;
|
||||
tier?: "light" | "standard" | "heavy" | null;
|
||||
factors?: {
|
||||
track?: string;
|
||||
direction?: { score?: number; max?: number; cvd_resonance?: number; p99_flow?: number; accel_bonus?: number; accel_independent_score?: number };
|
||||
crowding?: { score?: number; max?: number; lsr_contrarian?: number; top_trader_position?: number };
|
||||
environment?: { score?: number; max?: number; obi_bonus?: number; oi_base?: number };
|
||||
auxiliary?: { score?: number; max?: number; coinbase_premium?: number };
|
||||
// BTC gate fields
|
||||
gate_passed?: boolean;
|
||||
block_reason?: string; // BTC用
|
||||
gate_block?: string; // ALT用
|
||||
obi_raw?: number;
|
||||
spot_perp_div?: number;
|
||||
whale_cvd_ratio?: number;
|
||||
atr_pct_price?: number;
|
||||
alt_score_ref?: number;
|
||||
} | null;
|
||||
}
|
||||
|
||||
const WINDOWS = [
|
||||
{ label: "1h", value: 60 },
|
||||
{ label: "4h", value: 240 },
|
||||
{ label: "12h", value: 720 },
|
||||
{ label: "24h", value: 1440 },
|
||||
];
|
||||
|
||||
function bjtStr(ms: number) {
|
||||
const d = new Date(ms + 8 * 3600 * 1000);
|
||||
return `${String(d.getUTCHours()).padStart(2, "0")}:${String(d.getUTCMinutes()).padStart(2, "0")}`;
|
||||
}
|
||||
|
||||
function bjtFull(ms: number) {
|
||||
const d = new Date(ms + 8 * 3600 * 1000);
|
||||
return `${String(d.getUTCMonth() + 1).padStart(2, "0")}-${String(d.getUTCDate()).padStart(2, "0")} ${String(d.getUTCHours()).padStart(2, "0")}:${String(d.getUTCMinutes()).padStart(2, "0")}:${String(d.getUTCSeconds()).padStart(2, "0")}`;
|
||||
}
|
||||
|
||||
function fmt(v: number, decimals = 1): string {
|
||||
if (Math.abs(v) >= 1000000) return `${(v / 1000000).toFixed(1)}M`;
|
||||
if (Math.abs(v) >= 1000) return `${(v / 1000).toFixed(1)}K`;
|
||||
return v.toFixed(decimals);
|
||||
}
|
||||
|
||||
function LayerScore({ label, score, max, colorClass }: { label: string; score: number; max: number; colorClass: string }) {
|
||||
const ratio = Math.max(0, Math.min((score / max) * 100, 100));
|
||||
return (
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="text-[10px] text-slate-500 w-6 shrink-0">{label}</span>
|
||||
<div className="flex-1 h-1.5 rounded-full bg-slate-100 overflow-hidden">
|
||||
<div className={`h-full ${colorClass}`} style={{ width: `${ratio}%` }} />
|
||||
</div>
|
||||
<span className="text-[10px] font-mono text-slate-600 w-8 text-right">{score}/{max}</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ─── ALT Gate 状态卡片 ──────────────────────────────────────────
|
||||
|
||||
const ALT_GATE_THRESHOLDS: Record<string, { vol: string; obi: string; spd: string; whale: string }> = {
|
||||
ETH: { vol: "0.3%", obi: "0.35", spd: "0.5%", whale: "$50k" },
|
||||
XRP: { vol: "0.25%", obi: "0.40", spd: "0.6%", whale: "$30k" },
|
||||
SOL: { vol: "0.4%", obi: "0.45", spd: "0.8%", whale: "$20k" },
|
||||
};
|
||||
|
||||
function ALTGateCard({ symbol, factors }: { symbol: Symbol; factors: LatestIndicator["factors"] }) {
|
||||
if (!factors || symbol === "BTC") return null;
|
||||
const thresholds = ALT_GATE_THRESHOLDS[symbol] ?? ALT_GATE_THRESHOLDS["ETH"];
|
||||
const passed = factors.gate_passed ?? true;
|
||||
const blockReason = factors.gate_block;
|
||||
return (
|
||||
<div className={`rounded-xl border px-3 py-2 mt-2 ${passed ? "border-purple-200 bg-purple-50" : "border-red-200 bg-red-50"}`}>
|
||||
<div className="flex items-center justify-between mb-1.5">
|
||||
<p className="text-[10px] font-semibold text-purple-800">🔒 {symbol} Gate-Control</p>
|
||||
<span className={`text-[10px] font-bold px-2 py-0.5 rounded ${passed ? "bg-emerald-100 text-emerald-700" : "bg-red-100 text-red-600"}`}>
|
||||
{passed ? "✅ Gate通过" : "❌ 否决"}
|
||||
</span>
|
||||
</div>
|
||||
<div className="grid grid-cols-4 gap-1.5">
|
||||
<div className="bg-white rounded px-2 py-1">
|
||||
<p className="text-[10px] text-slate-400">波动率</p>
|
||||
<p className="text-xs font-mono text-slate-800">{((factors.atr_pct_price ?? 0) * 100).toFixed(3)}%</p>
|
||||
<p className="text-[9px] text-slate-400">需 ≥{thresholds.vol}</p>
|
||||
</div>
|
||||
<div className="bg-white rounded px-2 py-1">
|
||||
<p className="text-[10px] text-slate-400">OBI</p>
|
||||
<p className={`text-xs font-mono ${(factors.obi_raw ?? 0) >= 0 ? "text-emerald-600" : "text-red-500"}`}>
|
||||
{((factors.obi_raw ?? 0) * 100).toFixed(2)}%
|
||||
</p>
|
||||
<p className="text-[9px] text-slate-400">否决±{thresholds.obi}</p>
|
||||
</div>
|
||||
<div className="bg-white rounded px-2 py-1">
|
||||
<p className="text-[10px] text-slate-400">期现背离</p>
|
||||
<p className={`text-xs font-mono ${(factors.spot_perp_div ?? 0) >= 0 ? "text-emerald-600" : "text-red-500"}`}>
|
||||
{((factors.spot_perp_div ?? 0) * 10000).toFixed(2)}bps
|
||||
</p>
|
||||
<p className="text-[9px] text-slate-400">否决±{thresholds.spd}</p>
|
||||
</div>
|
||||
<div className="bg-white rounded px-2 py-1">
|
||||
<p className="text-[10px] text-slate-400">鲸鱼阈值</p>
|
||||
<p className="text-xs font-mono text-slate-800">{thresholds.whale}</p>
|
||||
<p className="text-[9px] text-slate-400">大单门槛</p>
|
||||
</div>
|
||||
</div>
|
||||
{blockReason && (
|
||||
<p className="text-[10px] text-red-600 mt-1.5 bg-red-50 rounded px-2 py-1">
|
||||
否决原因: <span className="font-mono">{blockReason}</span>
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ─── BTC Gate 状态卡片 ───────────────────────────────────────────
|
||||
|
||||
function BTCGateCard({ factors }: { factors: LatestIndicator["factors"] }) {
|
||||
if (!factors) return null;
|
||||
return (
|
||||
<div className="rounded-xl border border-amber-200 bg-amber-50 px-3 py-2 mt-2">
|
||||
<div className="flex items-center justify-between mb-1.5">
|
||||
<p className="text-[10px] font-semibold text-amber-800">⚡ BTC Gate-Control</p>
|
||||
<span className={`text-[10px] font-bold px-2 py-0.5 rounded ${factors.gate_passed ? "bg-emerald-100 text-emerald-700" : "bg-red-100 text-red-600"}`}>
|
||||
{factors.gate_passed ? "✅ Gate通过" : "❌ 否决"}
|
||||
</span>
|
||||
</div>
|
||||
<div className="grid grid-cols-4 gap-1.5">
|
||||
<div className="bg-white rounded px-2 py-1">
|
||||
<p className="text-[10px] text-slate-400">波动率</p>
|
||||
<p className="text-xs font-mono text-slate-800">{((factors.atr_pct_price ?? 0) * 100).toFixed(3)}%</p>
|
||||
<p className="text-[9px] text-slate-400">需 ≥0.2%</p>
|
||||
</div>
|
||||
<div className="bg-white rounded px-2 py-1">
|
||||
<p className="text-[10px] text-slate-400">OBI</p>
|
||||
<p className={`text-xs font-mono ${(factors.obi_raw ?? 0) >= 0 ? "text-emerald-600" : "text-red-500"}`}>
|
||||
{((factors.obi_raw ?? 0) * 100).toFixed(2)}%
|
||||
</p>
|
||||
<p className="text-[9px] text-slate-400">盘口失衡</p>
|
||||
</div>
|
||||
<div className="bg-white rounded px-2 py-1">
|
||||
<p className="text-[10px] text-slate-400">期现背离</p>
|
||||
<p className={`text-xs font-mono ${(factors.spot_perp_div ?? 0) >= 0 ? "text-emerald-600" : "text-red-500"}`}>
|
||||
{((factors.spot_perp_div ?? 0) * 10000).toFixed(2)}bps
|
||||
</p>
|
||||
<p className="text-[9px] text-slate-400">spot-perp</p>
|
||||
</div>
|
||||
<div className="bg-white rounded px-2 py-1">
|
||||
<p className="text-[10px] text-slate-400">巨鲸CVD</p>
|
||||
<p className={`text-xs font-mono ${(factors.whale_cvd_ratio ?? 0) >= 0 ? "text-emerald-600" : "text-red-500"}`}>
|
||||
{((factors.whale_cvd_ratio ?? 0) * 100).toFixed(2)}%
|
||||
</p>
|
||||
<p className="text-[9px] text-slate-400">>$100k</p>
|
||||
</div>
|
||||
</div>
|
||||
{factors.block_reason && (
|
||||
<p className="text-[10px] text-red-600 mt-1.5 bg-red-50 rounded px-2 py-1">
|
||||
否决原因: <span className="font-mono">{factors.block_reason}</span>
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ─── 实时指标卡片 ────────────────────────────────────────────────
|
||||
|
||||
function IndicatorCards({ symbol }: { symbol: Symbol }) {
|
||||
const [data, setData] = useState<LatestIndicator | null>(null);
|
||||
const strategy = "v53_middle";
|
||||
|
||||
useEffect(() => {
|
||||
const fetch = async () => {
|
||||
try {
|
||||
const res = await authFetch(`/api/signals/latest?strategy=${strategy}`);
|
||||
if (!res.ok) return;
|
||||
const json = await res.json();
|
||||
setData(json[symbol] || null);
|
||||
} catch {}
|
||||
};
|
||||
fetch();
|
||||
const iv = setInterval(fetch, 5000);
|
||||
return () => clearInterval(iv);
|
||||
}, [symbol, strategy]);
|
||||
|
||||
if (!data) return <div className="text-center text-slate-400 text-sm py-4">等待指标数据...</div>;
|
||||
|
||||
const isBTC = symbol === "BTC";
|
||||
const priceVsVwap = data.price > data.vwap_30m ? "上方" : "下方";
|
||||
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
{/* CVD三轨 */}
|
||||
<div className="grid grid-cols-3 gap-1.5">
|
||||
<div className="bg-white rounded-lg border border-slate-200 px-2.5 py-2">
|
||||
<p className="text-[10px] text-slate-400">CVD_fast (5m实算★)</p>
|
||||
<p className={`font-mono font-bold text-sm ${data.cvd_fast >= 0 ? "text-emerald-600" : "text-red-500"}`}>
|
||||
{fmt(data.cvd_fast)}
|
||||
</p>
|
||||
<p className="text-[10px] text-slate-400">
|
||||
斜率: <span className={data.cvd_fast_slope >= 0 ? "text-emerald-600" : "text-red-500"}>
|
||||
{data.cvd_fast_slope >= 0 ? "↑" : "↓"}{fmt(Math.abs(data.cvd_fast_slope))}
|
||||
</span>
|
||||
</p>
|
||||
</div>
|
||||
<div className="bg-white rounded-lg border border-slate-200 px-2.5 py-2">
|
||||
<p className="text-[10px] text-slate-400">CVD_mid (30m实算★)</p>
|
||||
<p className={`font-mono font-bold text-sm ${data.cvd_mid >= 0 ? "text-emerald-600" : "text-red-500"}`}>
|
||||
{fmt(data.cvd_mid)}
|
||||
</p>
|
||||
<p className="text-[10px] text-slate-400">{data.cvd_mid > 0 ? "多" : "空"}头占优</p>
|
||||
</div>
|
||||
<div className="bg-white rounded-lg border border-slate-200 px-2.5 py-2">
|
||||
<p className="text-[10px] text-slate-400">CVD共振</p>
|
||||
<p className={`font-mono font-bold text-sm ${data.cvd_fast >= 0 && data.cvd_mid >= 0 ? "text-emerald-600" : data.cvd_fast < 0 && data.cvd_mid < 0 ? "text-red-500" : "text-slate-400"}`}>
|
||||
{data.cvd_fast >= 0 && data.cvd_mid >= 0 ? "✅ 多头共振" : data.cvd_fast < 0 && data.cvd_mid < 0 ? "✅ 空头共振" : "⚠️ 分歧"}
|
||||
</p>
|
||||
<p className="text-[10px] text-slate-400">V5.3核心信号</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* ATR + VWAP */}
|
||||
<div className="grid grid-cols-4 gap-1.5">
|
||||
<div className="bg-white rounded-lg border border-slate-200 px-2.5 py-2">
|
||||
<p className="text-[10px] text-slate-400">ATR</p>
|
||||
<p className="font-mono font-semibold text-sm text-slate-800">${fmt(data.atr_5m, 2)}</p>
|
||||
<p className="text-[10px]">
|
||||
<span className={data.atr_percentile > 60 ? "text-amber-600 font-semibold" : "text-slate-400"}>
|
||||
{data.atr_percentile.toFixed(0)}%{data.atr_percentile > 60 ? "🔥" : ""}
|
||||
</span>
|
||||
</p>
|
||||
</div>
|
||||
<div className="bg-white rounded-lg border border-slate-200 px-2.5 py-2">
|
||||
<p className="text-[10px] text-slate-400">VWAP</p>
|
||||
<p className="font-mono font-semibold text-sm text-slate-800">${data.vwap_30m.toLocaleString("en-US", { maximumFractionDigits: 1 })}</p>
|
||||
<p className="text-[10px]">
|
||||
价格在<span className={data.price > data.vwap_30m ? "text-emerald-600" : "text-red-500"}>{priceVsVwap}</span>
|
||||
</p>
|
||||
</div>
|
||||
<div className="bg-white rounded-lg border border-slate-200 px-2.5 py-2">
|
||||
<p className="text-[10px] text-slate-400">P95</p>
|
||||
<p className="font-mono font-semibold text-sm text-slate-800">{data.p95_qty.toFixed(4)}</p>
|
||||
<p className="text-[10px] text-slate-400">大单阈值</p>
|
||||
</div>
|
||||
<div className="bg-white rounded-lg border border-slate-200 px-2.5 py-2">
|
||||
<p className="text-[10px] text-slate-400">P99</p>
|
||||
<p className="font-mono font-semibold text-sm text-amber-600">{data.p99_qty.toFixed(4)}</p>
|
||||
<p className="text-[10px] text-slate-400">超大单</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 信号状态 */}
|
||||
<div className={`rounded-xl border px-3 py-2.5 ${
|
||||
data.signal === "LONG" ? "border-emerald-300 bg-emerald-50" :
|
||||
data.signal === "SHORT" ? "border-red-300 bg-red-50" :
|
||||
"border-slate-200 bg-slate-50"
|
||||
}`}>
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<p className="text-[10px] text-slate-500">
|
||||
{isBTC ? "BTC Gate-Control" : "ALT 四层评分"}
|
||||
{" · "}{"v53"}
|
||||
</p>
|
||||
<p className={`font-bold text-base ${
|
||||
data.signal === "LONG" ? "text-emerald-700" :
|
||||
data.signal === "SHORT" ? "text-red-600" :
|
||||
"text-slate-400"
|
||||
}`}>
|
||||
{data.signal === "LONG" ? "🟢 做多" : data.signal === "SHORT" ? "🔴 做空" : "⚪ 无信号"}
|
||||
</p>
|
||||
</div>
|
||||
<div className="text-right">
|
||||
{isBTC ? (
|
||||
<>
|
||||
<p className="font-mono font-bold text-lg text-slate-800">
|
||||
{data.display_score ?? data.factors?.alt_score_ref ?? data.score}/100
|
||||
<span className="text-[10px] font-normal text-slate-400 ml-1">参考分</span>
|
||||
</p>
|
||||
<p className="text-[10px] text-slate-500">
|
||||
{(data.gate_passed ?? data.factors?.gate_passed) ? (data.tier === "standard" ? "标准" : "不开仓") : "Gate否决"}
|
||||
</p>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<p className="font-mono font-bold text-lg text-slate-800">{data.score}/100</p>
|
||||
<p className="text-[10px] text-slate-500">{data.tier === "heavy" ? "加仓" : data.tier === "standard" ? "标准" : "不开仓"}</p>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 四层分数 — ALT和BTC都显示 */}
|
||||
<div className="mt-2 space-y-1">
|
||||
<LayerScore label="方向" score={data.factors?.direction?.score ?? 0} max={55} colorClass="bg-blue-600" />
|
||||
{data.factors?.direction?.accel_independent_score != null && data.factors.direction.accel_independent_score > 0 && (
|
||||
<p className="text-[9px] text-orange-600 pl-1">⚡ accel独立触发 +{data.factors.direction.accel_independent_score}</p>
|
||||
)}
|
||||
<LayerScore label="拥挤" score={data.factors?.crowding?.score ?? 0} max={25} colorClass="bg-violet-600" />
|
||||
<LayerScore label="环境" score={data.factors?.environment?.score ?? 0} max={15} colorClass="bg-emerald-600" />
|
||||
{(data.factors?.environment?.obi_bonus ?? 0) > 0 && (
|
||||
<p className="text-[9px] text-cyan-600 pl-1">📊 OBI正向 +{data.factors?.environment?.obi_bonus}</p>
|
||||
)}
|
||||
<LayerScore label="辅助" score={data.factors?.auxiliary?.score ?? 0} max={5} colorClass="bg-slate-500" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* ALT Gate 卡片 */}
|
||||
{!isBTC && data.factors && <ALTGateCard symbol={symbol} factors={data.factors} />}
|
||||
|
||||
{/* BTC Gate 卡片 */}
|
||||
{isBTC && data.factors && <BTCGateCard factors={data.factors} />}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ─── 信号历史 ────────────────────────────────────────────────────
|
||||
|
||||
interface SignalRecord {
|
||||
ts: number;
|
||||
score: number;
|
||||
signal: string;
|
||||
}
|
||||
|
||||
function SignalHistory({ symbol }: { symbol: Symbol }) {
|
||||
const [data, setData] = useState<SignalRecord[]>([]);
|
||||
const strategy = "v53_middle";
|
||||
|
||||
useEffect(() => {
|
||||
const fetchData = async () => {
|
||||
try {
|
||||
const res = await authFetch(`/api/signals/signal-history?symbol=${symbol}&limit=20&strategy=${strategy}`);
|
||||
if (!res.ok) return;
|
||||
const json = await res.json();
|
||||
setData(json.data || []);
|
||||
} catch {}
|
||||
};
|
||||
fetchData();
|
||||
const iv = setInterval(fetchData, 15000);
|
||||
return () => clearInterval(iv);
|
||||
}, [symbol, strategy]);
|
||||
|
||||
if (data.length === 0) return null;
|
||||
|
||||
return (
|
||||
<div className="rounded-xl border border-slate-200 bg-white shadow-sm overflow-hidden">
|
||||
<div className="px-3 py-2 border-b border-slate-100">
|
||||
<h3 className="font-semibold text-slate-800 text-xs">最近信号 ({strategy})</h3>
|
||||
</div>
|
||||
<div className="divide-y divide-slate-100 max-h-48 overflow-y-auto">
|
||||
{data.map((s, i) => (
|
||||
<div key={i} className="px-3 py-1.5 flex items-center justify-between">
|
||||
<div className="flex items-center gap-2">
|
||||
<span className={`text-xs font-bold ${s.signal === "LONG" ? "text-emerald-600" : "text-red-500"}`}>
|
||||
{s.signal === "LONG" ? "🟢 LONG" : "🔴 SHORT"}
|
||||
</span>
|
||||
<span className="text-[10px] text-slate-400">{bjtFull(s.ts)}</span>
|
||||
</div>
|
||||
<div className="flex items-center gap-1.5">
|
||||
<span className="font-mono text-xs text-slate-700">{s.score}</span>
|
||||
<span className={`text-[10px] px-1 py-0.5 rounded ${
|
||||
s.score >= 85 ? "bg-red-100 text-red-700" :
|
||||
s.score >= 75 ? "bg-blue-100 text-blue-700" :
|
||||
"bg-slate-100 text-slate-600"
|
||||
}`}>
|
||||
{s.score >= 85 ? "加仓" : s.score >= 75 ? "标准" : "不开仓"}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ─── CVD图表 ────────────────────────────────────────────────────
|
||||
|
||||
function CVDChart({ symbol, minutes }: { symbol: Symbol; minutes: number }) {
|
||||
const [data, setData] = useState<IndicatorRow[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const strategy = "v53_middle";
|
||||
|
||||
const fetchData = useCallback(async (silent = false) => {
|
||||
try {
|
||||
const res = await authFetch(`/api/signals/indicators?symbol=${symbol}&minutes=${minutes}&strategy=${strategy}`);
|
||||
if (!res.ok) return;
|
||||
const json = await res.json();
|
||||
setData(json.data || []);
|
||||
if (!silent) setLoading(false);
|
||||
} catch {}
|
||||
}, [symbol, minutes, strategy]);
|
||||
|
||||
useEffect(() => {
|
||||
setLoading(true);
|
||||
fetchData();
|
||||
const iv = setInterval(() => fetchData(true), 30000);
|
||||
return () => clearInterval(iv);
|
||||
}, [fetchData]);
|
||||
|
||||
const chartData = data.map(d => ({
|
||||
time: bjtStr(d.ts),
|
||||
fast: parseFloat(d.cvd_fast?.toFixed(2) || "0"),
|
||||
mid: parseFloat(d.cvd_mid?.toFixed(2) || "0"),
|
||||
price: d.price,
|
||||
}));
|
||||
|
||||
const prices = chartData.map(d => d.price).filter(v => v > 0);
|
||||
const pMin = prices.length ? Math.min(...prices) : 0;
|
||||
const pMax = prices.length ? Math.max(...prices) : 0;
|
||||
const pPad = (pMax - pMin) * 0.3 || pMax * 0.001;
|
||||
|
||||
if (loading) return <div className="flex items-center justify-center h-48 text-slate-400 text-sm">加载指标数据...</div>;
|
||||
if (data.length === 0) return <div className="flex items-center justify-center h-48 text-slate-400 text-sm">暂无 V5.3 指标数据,signal-engine 需运行积累</div>;
|
||||
|
||||
return (
|
||||
<ResponsiveContainer width="100%" height={220}>
|
||||
<ComposedChart data={chartData} margin={{ top: 4, right: 60, bottom: 0, left: 8 }}>
|
||||
<CartesianGrid strokeDasharray="3 3" stroke="#f1f5f9" />
|
||||
<XAxis dataKey="time" tick={{ fill: "#94a3b8", fontSize: 10 }} tickLine={false} interval="preserveStartEnd" />
|
||||
<YAxis yAxisId="cvd" tick={{ fill: "#94a3b8", fontSize: 10 }} tickLine={false} axisLine={false} width={55} />
|
||||
<YAxis yAxisId="price" orientation="right" tick={{ fill: "#f59e0b", fontSize: 10 }} tickLine={false} axisLine={false} width={65}
|
||||
domain={[Math.floor(pMin - pPad), Math.ceil(pMax + pPad)]}
|
||||
tickFormatter={(v: number) => v >= 1000 ? `$${(v / 1000).toFixed(1)}k` : `$${v.toFixed(0)}`}
|
||||
/>
|
||||
<Tooltip
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
formatter={(v: any, name: any) => {
|
||||
if (name === "price") return [`$${Number(v).toLocaleString()}`, "币价"];
|
||||
if (name === "fast") return [fmt(Number(v)), "CVD_fast(5m实算)"];
|
||||
return [fmt(Number(v)), "CVD_mid(30m实算)"];
|
||||
}}
|
||||
contentStyle={{ background: "#fff", border: "1px solid #e2e8f0", borderRadius: 8, fontSize: 11 }}
|
||||
/>
|
||||
<Legend wrapperStyle={{ fontSize: 11 }} />
|
||||
<ReferenceLine yAxisId="cvd" y={0} stroke="#94a3b8" strokeDasharray="4 2" />
|
||||
<Area yAxisId="cvd" type="monotone" dataKey="fast" name="fast" stroke="#2563eb" fill="#eff6ff" strokeWidth={1.5} dot={false} connectNulls />
|
||||
<Line yAxisId="cvd" type="monotone" dataKey="mid" name="mid" stroke="#7c3aed" strokeWidth={1.5} dot={false} connectNulls strokeDasharray="6 3" />
|
||||
<Line yAxisId="price" type="monotone" dataKey="price" name="price" stroke="#f59e0b" strokeWidth={1.5} dot={false} connectNulls strokeDasharray="4 2" />
|
||||
</ComposedChart>
|
||||
</ResponsiveContainer>
|
||||
);
|
||||
}
|
||||
|
||||
// ─── 主页面 ──────────────────────────────────────────────────────
|
||||
|
||||
export default function SignalsV53Page() {
|
||||
const { isLoggedIn, loading } = useAuth();
|
||||
const [symbol, setSymbol] = useState<Symbol>("ETH");
|
||||
const [minutes, setMinutes] = useState(240);
|
||||
|
||||
if (loading) return <div className="flex items-center justify-center h-64 text-slate-400">加载中...</div>;
|
||||
if (!isLoggedIn) return (
|
||||
<div className="flex flex-col items-center justify-center h-64 gap-4">
|
||||
<div className="text-5xl">🔒</div>
|
||||
<p className="text-slate-600 font-medium">请先登录查看信号数据</p>
|
||||
<div className="flex gap-2">
|
||||
<Link href="/login" className="bg-blue-600 text-white px-4 py-2 rounded-lg text-sm">登录</Link>
|
||||
<Link href="/register" className="border border-slate-300 text-slate-600 px-4 py-2 rounded-lg text-sm">注册</Link>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
{/* Fast 实验版标识条 */}
|
||||
<div className="rounded-lg bg-gradient-to-r from-orange-500 to-amber-400 px-3 py-1.5 flex items-center justify-between">
|
||||
<span className="text-white text-xs font-bold">🚀 V5.3 Middle — 实验变体 A/B对照</span>
|
||||
<div className="flex gap-2 text-white text-[10px] font-medium">
|
||||
<span className="bg-white/20 px-2 py-0.5 rounded">CVD 5m/30m</span>
|
||||
<span className="bg-white/20 px-2 py-0.5 rounded">OBI+加分</span>
|
||||
<span className="bg-white/20 px-2 py-0.5 rounded">accel独立触发</span>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center justify-between flex-wrap gap-2">
|
||||
<div>
|
||||
<h1 className="text-lg font-bold text-slate-900">🚀 信号引擎 V5.3 Middle</h1>
|
||||
<p className="text-slate-500 text-[10px]">
|
||||
CVD 5m/30m · OBI正向加分 · accel独立触发 · 实验变体 ·
|
||||
{symbol === "BTC" ? " 🔵 BTC轨(gate-control)" : " 🟣 ALT轨(ETH/XRP/SOL)"}
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex gap-1">
|
||||
{(["BTC", "ETH", "XRP", "SOL"] as Symbol[]).map(s => (
|
||||
<button key={s} onClick={() => setSymbol(s)}
|
||||
className={`px-3 py-1 rounded-lg border text-xs font-medium transition-colors ${symbol === s ? (s === "BTC" ? "bg-amber-500 text-white border-amber-500" : "bg-blue-600 text-white border-blue-600") : "border-slate-200 text-slate-600 hover:border-blue-400"}`}>
|
||||
{s}{s === "BTC" ? " 🔵" : ""}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<IndicatorCards symbol={symbol} />
|
||||
<SignalHistory symbol={symbol} />
|
||||
|
||||
<div className="rounded-xl border border-slate-200 bg-white shadow-sm overflow-hidden">
|
||||
<div className="px-3 py-2 border-b border-slate-100 flex items-center justify-between flex-wrap gap-1">
|
||||
<div>
|
||||
<h3 className="font-semibold text-slate-800 text-xs">CVD三轨 + 币价</h3>
|
||||
<p className="text-[10px] text-slate-400">蓝=fast(DB存30m,实算5m★) · 紫=mid(DB存4h,实算30m★) · 橙=价格</p>
|
||||
</div>
|
||||
<div className="flex gap-1">
|
||||
{WINDOWS.map(w => (
|
||||
<button key={w.value} onClick={() => setMinutes(w.value)}
|
||||
className={`px-2 py-1 rounded border text-xs transition-colors ${minutes === w.value ? "bg-slate-800 text-white border-slate-800" : "border-slate-200 text-slate-500 hover:border-slate-400"}`}>
|
||||
{w.label}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
<div className="px-3 py-2">
|
||||
<CVDChart symbol={symbol} minutes={minutes} />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="rounded-xl border border-slate-200 bg-white shadow-sm overflow-hidden">
|
||||
<div className="px-3 py-2 border-b border-slate-100">
|
||||
<h3 className="font-semibold text-slate-800 text-xs">📖 V5.3 双轨信号说明</h3>
|
||||
</div>
|
||||
<div className="px-3 py-2 space-y-2 text-[11px] text-slate-600">
|
||||
<div className="p-2 bg-purple-50 rounded-lg border border-purple-100">
|
||||
<span className="font-bold text-purple-800">🟣 ALT轨(ETH/XRP/SOL)— 四层线性评分</span>
|
||||
<div className="mt-1 space-y-1">
|
||||
<p><span className="font-semibold">1️⃣ 方向层(55分)</span> — CVD共振30分(fast+mid同向)+ P99大单对齐20分 + 加速奖励5分。删除独立确认层,解决CVD双重计分问题。</p>
|
||||
<p><span className="font-semibold">2️⃣ 拥挤层(25分)</span> — LSR反向拥挤15分(散户过度拥挤=信号)+ 大户持仓方向10分。</p>
|
||||
<p><span className="font-semibold">3️⃣ 环境层(15分)</span> — OI变化率,新资金进场vs撤离,判断趋势持续性。</p>
|
||||
<p><span className="font-semibold">4️⃣ 辅助层(5分)</span> — Coinbase Premium,美系机构动向。</p>
|
||||
</div>
|
||||
</div>
|
||||
<div className="p-2 bg-amber-50 rounded-lg border border-amber-100">
|
||||
<span className="font-bold text-amber-800">🔵 BTC轨 — Gate-Control逻辑</span>
|
||||
<div className="mt-1 space-y-1">
|
||||
<p><span className="font-semibold">波动率门控</span>:ATR/Price ≥ 0.2%,低波动行情拒绝开仓</p>
|
||||
<p><span className="font-semibold">OBI否决</span>:订单簿失衡超阈值且与信号方向冲突时否决(实时100ms)</p>
|
||||
<p><span className="font-semibold">期现背离否决</span>:spot与perp价差超阈值时否决(实时1s)</p>
|
||||
<p><span className="font-semibold">巨鲸CVD</span>:>$100k成交额净CVD,15分钟滚动窗口实时计算</p>
|
||||
</div>
|
||||
</div>
|
||||
<div className="pt-1 border-t border-slate-100">
|
||||
<span className="text-blue-600 font-medium">档位:</span><75不开仓 · 75-84标准 · ≥85加仓 · 冷却10分钟
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@ -1,176 +0,0 @@
|
||||
"use client";
|
||||
|
||||
import { useParams, useSearchParams, useRouter } from "next/navigation";
|
||||
import { useEffect, useState, useCallback } from "react";
|
||||
import { authFetch } from "@/lib/auth";
|
||||
import Link from "next/link";
|
||||
import dynamic from "next/dynamic";
|
||||
import {
|
||||
ArrowLeft,
|
||||
CheckCircle,
|
||||
PauseCircle,
|
||||
AlertCircle,
|
||||
Clock,
|
||||
} from "lucide-react";
|
||||
|
||||
// ─── Dynamic imports for each strategy's pages ───────────────────
|
||||
const SignalsV53 = dynamic(() => import("@/app/signals-v53/page"), { ssr: false });
|
||||
const SignalsV53Fast = dynamic(() => import("@/app/signals-v53fast/page"), { ssr: false });
|
||||
const SignalsV53Middle = dynamic(() => import("@/app/signals-v53middle/page"), { ssr: false });
|
||||
const PaperV53 = dynamic(() => import("@/app/paper-v53/page"), { ssr: false });
|
||||
const PaperV53Fast = dynamic(() => import("@/app/paper-v53fast/page"), { ssr: false });
|
||||
const PaperV53Middle = dynamic(() => import("@/app/paper-v53middle/page"), { ssr: false });
|
||||
|
||||
// ─── Types ────────────────────────────────────────────────────────
|
||||
interface StrategySummary {
|
||||
id: string;
|
||||
display_name: string;
|
||||
status: string;
|
||||
started_at: number;
|
||||
initial_balance: number;
|
||||
current_balance: number;
|
||||
net_usdt: number;
|
||||
net_r: number;
|
||||
trade_count: number;
|
||||
win_rate: number;
|
||||
avg_win_r: number;
|
||||
avg_loss_r: number;
|
||||
open_positions: number;
|
||||
pnl_usdt_24h: number;
|
||||
pnl_r_24h: number;
|
||||
cvd_windows?: string;
|
||||
description?: string;
|
||||
}
|
||||
|
||||
// ─── Helpers ──────────────────────────────────────────────────────
|
||||
function fmtDur(ms: number) {
|
||||
const s = Math.floor((Date.now() - ms) / 1000);
|
||||
const d = Math.floor(s / 86400);
|
||||
const h = Math.floor((s % 86400) / 3600);
|
||||
const m = Math.floor((s % 3600) / 60);
|
||||
if (d > 0) return `${d}天${h}h`;
|
||||
if (h > 0) return `${h}h${m}m`;
|
||||
return `${m}m`;
|
||||
}
|
||||
|
||||
function StatusBadge({ status }: { status: string }) {
|
||||
if (status === "running") return <span className="flex items-center gap-1 text-xs text-emerald-400"><CheckCircle size={12} />运行中</span>;
|
||||
if (status === "paused") return <span className="flex items-center gap-1 text-xs text-yellow-400"><PauseCircle size={12} />已暂停</span>;
|
||||
return <span className="flex items-center gap-1 text-xs text-red-400"><AlertCircle size={12} />异常</span>;
|
||||
}
|
||||
|
||||
// ─── Content router ───────────────────────────────────────────────
|
||||
function SignalsContent({ strategyId }: { strategyId: string }) {
|
||||
if (strategyId === "v53") return <SignalsV53 />;
|
||||
if (strategyId === "v53_fast") return <SignalsV53Fast />;
|
||||
if (strategyId === "v53_middle") return <SignalsV53Middle />;
|
||||
return <div className="p-8 text-gray-400">未知策略: {strategyId}</div>;
|
||||
}
|
||||
|
||||
function PaperContent({ strategyId }: { strategyId: string }) {
|
||||
if (strategyId === "v53") return <PaperV53 />;
|
||||
if (strategyId === "v53_fast") return <PaperV53Fast />;
|
||||
if (strategyId === "v53_middle") return <PaperV53Middle />;
|
||||
return <div className="p-8 text-gray-400">未知策略: {strategyId}</div>;
|
||||
}
|
||||
|
||||
// ─── Main Page ────────────────────────────────────────────────────
|
||||
export default function StrategyDetailPage() {
|
||||
const params = useParams();
|
||||
const searchParams = useSearchParams();
|
||||
const router = useRouter();
|
||||
const strategyId = params?.id as string;
|
||||
const tab = searchParams?.get("tab") || "signals";
|
||||
|
||||
const [summary, setSummary] = useState<StrategySummary | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
const fetchSummary = useCallback(async () => {
|
||||
try {
|
||||
const r = await authFetch(`/api/strategy-plaza/${strategyId}/summary`);
|
||||
if (r.ok) {
|
||||
const d = await r.json();
|
||||
setSummary(d);
|
||||
}
|
||||
} catch {}
|
||||
setLoading(false);
|
||||
}, [strategyId]);
|
||||
|
||||
useEffect(() => {
|
||||
fetchSummary();
|
||||
const iv = setInterval(fetchSummary, 30000);
|
||||
return () => clearInterval(iv);
|
||||
}, [fetchSummary]);
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="flex items-center justify-center min-h-screen">
|
||||
<div className="text-gray-400 animate-pulse">加载中...</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const isProfit = (summary?.net_usdt ?? 0) >= 0;
|
||||
|
||||
return (
|
||||
<div className="p-4 max-w-full">
|
||||
{/* Back + Strategy Header */}
|
||||
<div className="flex items-center gap-3 mb-4">
|
||||
<Link href="/strategy-plaza" className="flex items-center gap-1 text-gray-400 hover:text-white text-sm transition-colors">
|
||||
<ArrowLeft size={16} />
|
||||
策略广场
|
||||
</Link>
|
||||
<span className="text-gray-600">/</span>
|
||||
<span className="text-white font-medium">{summary?.display_name ?? strategyId}</span>
|
||||
</div>
|
||||
|
||||
{/* Summary Bar */}
|
||||
{summary && (
|
||||
<div className="flex flex-wrap items-center gap-3 rounded-xl border border-slate-200 bg-white px-4 py-2.5 mb-4">
|
||||
<StatusBadge status={summary.status} />
|
||||
<span className="text-xs text-slate-400 flex items-center gap-1">
|
||||
<Clock size={10} />运行 {fmtDur(summary.started_at)}
|
||||
</span>
|
||||
{summary.cvd_windows && (
|
||||
<span className="text-xs text-blue-600 bg-blue-50 border border-blue-100 px-2 py-0.5 rounded">CVD {summary.cvd_windows}</span>
|
||||
)}
|
||||
<span className="ml-auto flex items-center gap-4 text-xs">
|
||||
<span className="text-slate-500">胜率 <span className={summary.win_rate >= 50 ? "text-emerald-600 font-bold" : "text-amber-600 font-bold"}>{summary.win_rate}%</span></span>
|
||||
<span className="text-slate-500">净R <span className={`font-bold ${isProfit ? "text-emerald-600" : "text-red-500"}`}>{summary.net_r >= 0 ? "+" : ""}{summary.net_r}R</span></span>
|
||||
<span className="text-slate-500">余额 <span className={`font-bold ${isProfit ? "text-emerald-600" : "text-red-500"}`}>{summary.current_balance.toLocaleString()} U</span></span>
|
||||
<span className="text-slate-500">24h <span className={`font-bold ${summary.pnl_usdt_24h >= 0 ? "text-emerald-600" : "text-red-500"}`}>{summary.pnl_usdt_24h >= 0 ? "+" : ""}{summary.pnl_usdt_24h} U</span></span>
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Tabs */}
|
||||
<div className="flex gap-2 mb-4">
|
||||
{[
|
||||
{ key: "signals", label: "📊 信号引擎" },
|
||||
{ key: "paper", label: "📈 模拟盘" },
|
||||
].map(({ key, label }) => (
|
||||
<button
|
||||
key={key}
|
||||
onClick={() => router.push(`/strategy-plaza/${strategyId}?tab=${key}`)}
|
||||
className={`px-4 py-1.5 rounded-lg text-xs font-medium border transition-colors ${
|
||||
tab === key
|
||||
? "bg-blue-600 text-white border-blue-600"
|
||||
: "bg-white text-slate-600 border-slate-200 hover:border-blue-300 hover:text-blue-600"
|
||||
}`}
|
||||
>
|
||||
{label}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{/* Content — direct render of existing pages */}
|
||||
<div>
|
||||
{tab === "signals" ? (
|
||||
<SignalsContent strategyId={strategyId} />
|
||||
) : (
|
||||
<PaperContent strategyId={strategyId} />
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@ -1,259 +0,0 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState, useCallback } from "react";
|
||||
import { authFetch } from "@/lib/auth";
|
||||
import { useAuth } from "@/lib/auth";
|
||||
import Link from "next/link";
|
||||
import {
|
||||
TrendingUp,
|
||||
TrendingDown,
|
||||
Clock,
|
||||
Activity,
|
||||
AlertCircle,
|
||||
CheckCircle,
|
||||
PauseCircle,
|
||||
} from "lucide-react";
|
||||
|
||||
interface StrategyCard {
|
||||
id: string;
|
||||
display_name: string;
|
||||
status: "running" | "paused" | "error";
|
||||
started_at: number;
|
||||
initial_balance: number;
|
||||
current_balance: number;
|
||||
net_usdt: number;
|
||||
net_r: number;
|
||||
trade_count: number;
|
||||
win_rate: number;
|
||||
avg_win_r: number;
|
||||
avg_loss_r: number;
|
||||
open_positions: number;
|
||||
pnl_usdt_24h: number;
|
||||
pnl_r_24h: number;
|
||||
std_r: number;
|
||||
last_trade_at: number | null;
|
||||
}
|
||||
|
||||
function formatDuration(ms: number): string {
|
||||
const totalSec = Math.floor((Date.now() - ms) / 1000);
|
||||
const d = Math.floor(totalSec / 86400);
|
||||
const h = Math.floor((totalSec % 86400) / 3600);
|
||||
const m = Math.floor((totalSec % 3600) / 60);
|
||||
if (d > 0) return `${d}天 ${h}小时`;
|
||||
if (h > 0) return `${h}小时 ${m}分`;
|
||||
return `${m}分钟`;
|
||||
}
|
||||
|
||||
function formatTime(ms: number | null): string {
|
||||
if (!ms) return "—";
|
||||
const d = new Date(ms);
|
||||
return d.toLocaleString("zh-CN", {
|
||||
timeZone: "Asia/Shanghai",
|
||||
month: "2-digit",
|
||||
day: "2-digit",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
hour12: false,
|
||||
});
|
||||
}
|
||||
|
||||
function StatusBadge({ status }: { status: string }) {
|
||||
if (status === "running") {
|
||||
return (
|
||||
<span className="flex items-center gap-1 text-xs text-emerald-600 font-medium">
|
||||
<CheckCircle size={11} className="text-emerald-500" />
|
||||
运行中
|
||||
</span>
|
||||
);
|
||||
}
|
||||
if (status === "paused") {
|
||||
return (
|
||||
<span className="flex items-center gap-1 text-xs text-amber-600 font-medium">
|
||||
<PauseCircle size={11} className="text-amber-500" />
|
||||
已暂停
|
||||
</span>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<span className="flex items-center gap-1 text-xs text-red-600 font-medium">
|
||||
<AlertCircle size={11} className="text-red-500" />
|
||||
异常
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
function StrategyCardComponent({ s }: { s: StrategyCard }) {
|
||||
const isProfit = s.net_usdt >= 0;
|
||||
const is24hProfit = s.pnl_usdt_24h >= 0;
|
||||
const balancePct = ((s.current_balance / s.initial_balance) * 100).toFixed(1);
|
||||
|
||||
return (
|
||||
<Link href={`/strategy-plaza/${s.id}`}>
|
||||
<div className="rounded-xl border border-slate-200 bg-white overflow-hidden hover:border-blue-300 hover:shadow-md transition-all cursor-pointer group">
|
||||
{/* Header */}
|
||||
<div className="px-4 py-3 border-b border-slate-100 flex items-center justify-between">
|
||||
<div className="flex items-center gap-2">
|
||||
<h3 className="font-semibold text-slate-800 text-sm group-hover:text-blue-700 transition-colors">
|
||||
{s.display_name}
|
||||
</h3>
|
||||
<StatusBadge status={s.status} />
|
||||
</div>
|
||||
<span className="text-[10px] text-slate-400 flex items-center gap-1">
|
||||
<Clock size={9} />
|
||||
{formatDuration(s.started_at)}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{/* Main PnL */}
|
||||
<div className="px-4 pt-3 pb-2">
|
||||
<div className="flex items-end justify-between mb-2">
|
||||
<div>
|
||||
<div className="text-[10px] text-slate-400 mb-0.5">当前余额</div>
|
||||
<div className="text-xl font-bold text-slate-800">
|
||||
{s.current_balance.toLocaleString()}
|
||||
<span className="text-xs font-normal text-slate-400 ml-1">USDT</span>
|
||||
</div>
|
||||
</div>
|
||||
<div className="text-right">
|
||||
<div className="text-[10px] text-slate-400 mb-0.5">累计盈亏</div>
|
||||
<div className={`text-lg font-bold ${isProfit ? "text-emerald-600" : "text-red-500"}`}>
|
||||
{isProfit ? "+" : ""}{s.net_usdt.toLocaleString()} U
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Balance Bar */}
|
||||
<div className="mb-3">
|
||||
<div className="flex justify-between text-[10px] text-slate-400 mb-1">
|
||||
<span>{balancePct}%</span>
|
||||
<span>{s.initial_balance.toLocaleString()} USDT 初始</span>
|
||||
</div>
|
||||
<div className="w-full bg-slate-100 rounded-full h-1.5">
|
||||
<div
|
||||
className={`h-1.5 rounded-full ${isProfit ? "bg-emerald-400" : "bg-red-400"}`}
|
||||
style={{ width: `${Math.min(100, Math.max(0, (s.current_balance / s.initial_balance) * 100))}%` }}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Stats Row */}
|
||||
<div className="grid grid-cols-3 gap-2 mb-3">
|
||||
<div className="bg-slate-50 rounded-lg p-2 text-center">
|
||||
<div className="text-[10px] text-slate-400">胜率</div>
|
||||
<div className={`text-sm font-bold ${s.win_rate >= 50 ? "text-emerald-600" : s.win_rate >= 45 ? "text-amber-600" : "text-red-500"}`}>
|
||||
{s.win_rate}%
|
||||
</div>
|
||||
</div>
|
||||
<div className="bg-slate-50 rounded-lg p-2 text-center">
|
||||
<div className="text-[10px] text-slate-400">净R</div>
|
||||
<div className={`text-sm font-bold ${s.net_r >= 0 ? "text-emerald-600" : "text-red-500"}`}>
|
||||
{s.net_r >= 0 ? "+" : ""}{s.net_r}R
|
||||
</div>
|
||||
</div>
|
||||
<div className="bg-slate-50 rounded-lg p-2 text-center">
|
||||
<div className="text-[10px] text-slate-400">交易数</div>
|
||||
<div className="text-sm font-bold text-slate-700">{s.trade_count}</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Avg win/loss */}
|
||||
<div className="flex gap-2 mb-3">
|
||||
<div className="flex-1 bg-emerald-50 rounded-lg px-2.5 py-1.5">
|
||||
<span className="text-[10px] text-emerald-600">平均赢</span>
|
||||
<span className="float-right text-[10px] font-bold text-emerald-600">+{s.avg_win_r}R</span>
|
||||
</div>
|
||||
<div className="flex-1 bg-red-50 rounded-lg px-2.5 py-1.5">
|
||||
<span className="text-[10px] text-red-500">平均亏</span>
|
||||
<span className="float-right text-[10px] font-bold text-red-500">{s.avg_loss_r}R</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Footer */}
|
||||
<div className="px-4 py-2.5 border-t border-slate-100 flex items-center justify-between bg-slate-50/60">
|
||||
<div className="flex items-center gap-1">
|
||||
{is24hProfit ? (
|
||||
<TrendingUp size={12} className="text-emerald-500" />
|
||||
) : (
|
||||
<TrendingDown size={12} className="text-red-500" />
|
||||
)}
|
||||
<span className={`text-[10px] font-medium ${is24hProfit ? "text-emerald-600" : "text-red-500"}`}>
|
||||
24h {is24hProfit ? "+" : ""}{s.pnl_usdt_24h.toLocaleString()} U
|
||||
</span>
|
||||
</div>
|
||||
<div className="flex items-center gap-1 text-[10px] text-slate-400">
|
||||
<Activity size={9} />
|
||||
{s.open_positions > 0 ? (
|
||||
<span className="text-amber-600 font-medium">{s.open_positions}仓持仓中</span>
|
||||
) : (
|
||||
<span>上次: {formatTime(s.last_trade_at)}</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</Link>
|
||||
);
|
||||
}
|
||||
|
||||
export default function StrategyPlazaPage() {
|
||||
useAuth();
|
||||
const [strategies, setStrategies] = useState<StrategyCard[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [lastUpdated, setLastUpdated] = useState<Date | null>(null);
|
||||
|
||||
const fetchData = useCallback(async () => {
|
||||
try {
|
||||
const res = await authFetch("/api/strategy-plaza");
|
||||
const data = await res.json();
|
||||
setStrategies(data.strategies || []);
|
||||
setLastUpdated(new Date());
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
fetchData();
|
||||
const interval = setInterval(fetchData, 30000);
|
||||
return () => clearInterval(interval);
|
||||
}, [fetchData]);
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="flex items-center justify-center min-h-64">
|
||||
<div className="text-slate-400 text-sm animate-pulse">加载中...</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="p-4 max-w-5xl mx-auto">
|
||||
{/* Header */}
|
||||
<div className="flex items-center justify-between mb-5">
|
||||
<div>
|
||||
<h1 className="text-lg font-bold text-slate-800">策略广场</h1>
|
||||
<p className="text-slate-500 text-xs mt-0.5">点击卡片查看信号引擎和模拟盘详情</p>
|
||||
</div>
|
||||
{lastUpdated && (
|
||||
<div className="text-[10px] text-slate-400 flex items-center gap-1">
|
||||
<span className="inline-block w-1.5 h-1.5 rounded-full bg-emerald-400 animate-pulse" />
|
||||
{lastUpdated.toLocaleTimeString("zh-CN", { timeZone: "Asia/Shanghai", hour12: false })}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Strategy Cards */}
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-3 gap-4">
|
||||
{strategies.map((s) => (
|
||||
<StrategyCardComponent key={s.id} s={s} />
|
||||
))}
|
||||
</div>
|
||||
|
||||
{strategies.length === 0 && (
|
||||
<div className="text-center text-slate-400 text-sm py-16">暂无策略数据</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@ -14,7 +14,10 @@ const navItems = [
|
||||
{ href: "/", label: "仪表盘", icon: LayoutDashboard },
|
||||
{ href: "/trades", label: "成交流", icon: Activity },
|
||||
{ href: "/live", label: "⚡ 实盘交易", icon: Bolt, section: "── 实盘 ──" },
|
||||
{ href: "/strategy-plaza", label: "策略广场", icon: Zap, section: "── 策略 ──" },
|
||||
{ href: "/signals-v53", label: "V5.3 信号引擎", icon: Zap, section: "── V5.3 ──" },
|
||||
{ href: "/paper-v53", label: "V5.3 模拟盘", icon: LineChart },
|
||||
{ href: "/signals-v53fast", label: "V5.3 Fast 信号", icon: Zap, section: "── V5.3 Fast ──" },
|
||||
{ href: "/paper-v53fast", label: "V5.3 Fast 模拟盘", icon: LineChart },
|
||||
{ href: "/server", label: "服务器", icon: Monitor },
|
||||
{ href: "/about", label: "说明", icon: Info },
|
||||
];
|
||||
|
||||
Loading…
Reference in New Issue
Block a user