"use client"; import { useState, useEffect, Suspense } from "react"; import { useSearchParams } from "next/navigation"; 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; } type StrategyFilter = "all" | "v51_baseline" | "v52_8signals"; const STRATEGY_TABS: { value: StrategyFilter; label: string; hint: string }[] = [ { value: "all", label: "全部", hint: "总览" }, { value: "v51_baseline", label: "V5.1 模拟盘", hint: "经典五层" }, { value: "v52_8signals", label: "V5.2 模拟盘", hint: "8信号 + FR/Liq" }, ]; function normalizeStrategy(strategy: string | null | undefined): StrategyFilter { if (strategy === "v52_8signals") return "v52_8signals"; if (strategy === "v51_baseline") return "v51_baseline"; return "v51_baseline"; } function strategyName(strategy: string | null | undefined) { const normalized = normalizeStrategy(strategy); if (normalized === "v52_8signals") return "V5.2"; return "V5.1"; } function strategyBadgeClass(strategy: string | null | undefined) { return normalizeStrategy(strategy) === "v52_8signals" ? "bg-emerald-100 text-emerald-700 border border-emerald-200" : "bg-slate-200 text-slate-700 border border-slate-300"; } function strategyBadgeText(strategy: string | null | undefined) { return normalizeStrategy(strategy) === "v52_8signals" ? "✨ V5.2" : "V5.1"; } function strategyTabDescription(strategy: StrategyFilter) { if (strategy === "all") return "全部策略合并视图"; if (strategy === "v52_8signals") return "仅展示 V5.2 数据(含 FR / Liq)"; return "仅展示 V5.1 数据"; } // ─── 控制面板(开关+配置)────────────────────────────────────── function ControlPanel() { const [config, setConfig] = useState(null); const [saving, setSaving] = useState(false); useEffect(() => { const f = async () => { try { const r = await authFetch("/api/paper/config"); if (r.ok) setConfig(await r.json()); } catch {} }; f(); }, []); 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) => j.config)); } catch { } finally { setSaving(false); } }; if (!config) return null; return (
{config.enabled ? "🟢 运行中" : "⚪ 已停止"}
初始资金: ${config.initial_balance?.toLocaleString()} 单笔风险: {(config.risk_per_trade * 100).toFixed(0)}% 最大持仓: {config.max_positions}
); } // ─── 总览面板 ──────────────────────────────────────────────────── function SummaryCards({ strategy }: { strategy: StrategyFilter }) { const [data, setData] = useState(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); }, [strategy]); if (!data) return
加载中...
; return (

当前资金

= 10000 ? "text-emerald-600" : "text-red-500"}`}> ${data.balance?.toLocaleString()}

总盈亏(R)

= 0 ? "text-emerald-600" : "text-red-500"}`}> {data.total_pnl >= 0 ? "+" : ""} {data.total_pnl}R

= 0 ? "text-emerald-500" : "text-red-400"}`}> {data.total_pnl_usdt >= 0 ? "+" : ""}${data.total_pnl_usdt}

胜率

{data.win_rate}%

总交易

{data.total_trades}

持仓中

{data.active_positions}

盈亏比(PF)

{data.profit_factor}

运行

{data.start_time ? "运行中 ✅" : "等待首笔"}

); } // ─── 最新信号状态 ──────────────────────────────────────────────── const COINS = ["BTCUSDT", "ETHUSDT", "XRPUSDT", "SOLUSDT"]; function LatestSignals() { const [signals, setSignals] = useState>({}); useEffect(() => { const f = async () => { for (const sym of COINS) { try { const r = await authFetch(`/api/signals/signal-history?symbol=${sym.replace("USDT", "")}&limit=1`); if (r.ok) { const j = await r.json(); if (j.data && j.data.length > 0) { setSignals((prev) => ({ ...prev, [sym]: j.data[0] })); } } } catch {} } }; f(); const iv = setInterval(f, 15000); return () => clearInterval(iv); }, []); return (

最新信号

{COINS.map((sym) => { const s = signals[sym]; const coin = sym.replace("USDT", ""); const ago = s?.ts ? Math.round((Date.now() - s.ts) / 60000) : null; return (
{coin} {s?.signal ? ( <> {s.signal === "LONG" ? "🟢" : "🔴"} {s.signal} {s.score}分 ) : ( ⚪ 无信号 )}
{ago !== null && {ago < 60 ? `${ago}m前` : `${Math.round(ago / 60)}h前`}}
); })}
); } // ─── 当前持仓 ──────────────────────────────────────────────────── function ActivePositions({ strategy }: { strategy: StrategyFilter }) { const [positions, setPositions] = useState([]); const [wsPrices, setWsPrices] = useState>({}); useEffect(() => { const f = async () => { try { const r = await authFetch(`/api/paper/positions?strategy=${strategy}`); if (r.ok) { const j = await r.json(); setPositions(j.data || []); } } catch {} }; f(); const iv = setInterval(f, 10000); return () => clearInterval(iv); }, [strategy]); 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 (
{strategy === "all" ? "暂无活跃持仓" : `${strategyName(strategy)} 暂无活跃持仓`}
); return (

当前持仓 ● 实时

{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 factors = parseFactors(p.score_factors); const frScore = factors?.funding_rate?.score ?? 0; const liqScore = factors?.liquidation?.score ?? 0; const entry = p.entry_price || 0; const atr = p.atr_at_entry || 1; const riskDist = 2.0 * 0.7 * atr; const fullR = riskDist > 0 ? (p.direction === "LONG" ? (currentPrice - entry) / riskDist : (entry - currentPrice) / riskDist) : 0; const tp1R = riskDist > 0 ? (p.direction === "LONG" ? ((p.tp1_price || 0) - entry) / riskDist : (entry - (p.tp1_price || 0)) / riskDist) : 0; const unrealR = p.tp1_hit ? 0.5 * tp1R + 0.5 * fullR : fullR; const unrealUsdt = unrealR * 200; const isV52 = normalizeStrategy(p.strategy) === "v52_8signals"; return (
{p.direction === "LONG" ? "🟢" : "🔴"} {sym} {p.direction} {strategyBadgeText(p.strategy)} 评分{p.score} · {p.tier === "heavy" ? "加仓" : p.tier === "standard" ? "标准" : "轻仓"} {isV52 && ( FR {frScore >= 0 ? "+" : ""}{frScore} · Liq {liqScore >= 0 ? "+" : ""}{liqScore} )}
= 0 ? "text-emerald-600" : "text-red-500"}`}> {unrealR >= 0 ? "+" : ""} {unrealR.toFixed(2)}R = 0 ? "text-emerald-500" : "text-red-400"}`}> ({unrealUsdt >= 0 ? "+" : ""}${unrealUsdt.toFixed(0)}) {holdMin}m
入场: ${fmtPrice(p.entry_price)} 现价: ${currentPrice ? fmtPrice(currentPrice) : "-"} TP1: ${fmtPrice(p.tp1_price)}{p.tp1_hit ? " ✅" : ""} TP2: ${fmtPrice(p.tp2_price)} SL: ${fmtPrice(p.sl_price)} {!isV52 && FR/Liq 仅 V5.2 显示}
{isV52 && (
{isV52 &&
FR {frScore >= 0 ? "+" : ""}{frScore} · Liq {liqScore >= 0 ? "+" : ""}{liqScore}
}
)}
); })}
); } // ─── 权益曲线 ──────────────────────────────────────────────────── function EquityCurve({ strategy }: { strategy: StrategyFilter }) { const [data, setData] = useState([]); useEffect(() => { const f = async () => { try { const r = await authFetch(`/api/paper/equity-curve?strategy=${strategy}`); if (r.ok) { const j = await r.json(); setData(j.data || []); } } catch {} }; f(); const iv = setInterval(f, 30000); return () => clearInterval(iv); }, [strategy]); return (

权益曲线 (累计PnL)

{data.length < 2 ? (
{strategy === "all" ? "暂无足够历史数据" : `${strategyName(strategy)} 暂无足够历史数据`}
) : (
bjt(v)} tick={{ fontSize: 10 }} /> `${v}R`} /> bjt(Number(v))} formatter={(v: any) => [`${v}R`, "累计PnL"]} />
)}
); } // ─── 历史交易列表 ──────────────────────────────────────────────── type FilterSymbol = "all" | "BTC" | "ETH" | "XRP" | "SOL"; type FilterResult = "all" | "win" | "loss"; function TradeHistory({ strategy }: { strategy: StrategyFilter }) { const [trades, setTrades] = useState([]); const [symbol, setSymbol] = useState("all"); const [result, setResult] = useState("all"); useEffect(() => { const f = async () => { try { const r = await authFetch(`/api/paper/trades?symbol=${symbol}&result=${result}&strategy=${strategy}&limit=50`); if (r.ok) { const j = await r.json(); setTrades(j.data || []); } } catch {} }; f(); const iv = setInterval(f, 10000); return () => clearInterval(iv); }, [symbol, result, strategy]); return (

历史交易

{strategy === "all" ? "全部策略" : `${strategyBadgeText(strategy)} 视图`} | {(["all", "BTC", "ETH", "XRP", "SOL"] as FilterSymbol[]).map((s) => ( ))} | {(["all", "win", "loss"] as FilterResult[]).map((r) => ( ))}
{trades.length === 0 ? (
暂无交易记录
) : ( {trades.map((t: any) => { const holdMin = t.exit_ts && t.entry_ts ? Math.round((t.exit_ts - t.entry_ts) / 60000) : 0; const factors = parseFactors(t.score_factors); const frScore = factors?.funding_rate?.score ?? 0; const liqScore = factors?.liquidation?.score ?? 0; const isV52 = normalizeStrategy(t.strategy) === "v52_8signals"; return ( ); })}
币种 策略 方向 入场 出场 PnL(R) 状态 分数 时间
{t.symbol?.replace("USDT", "")} {strategyBadgeText(t.strategy)} {t.direction === "LONG" ? "🟢" : "🔴"} {t.direction} {fmtPrice(t.entry_price)} {t.exit_price ? fmtPrice(t.exit_price) : "-"} 0 ? "text-emerald-600" : t.pnl_r < 0 ? "text-red-500" : "text-slate-500"}`}> {t.pnl_r > 0 ? "+" : ""} {t.pnl_r?.toFixed(2)} {t.status === "tp" ? "止盈" : t.status === "sl" ? "止损" : t.status === "sl_be" ? "保本" : t.status === "timeout" ? "超时" : t.status === "signal_flip" ? "翻转" : t.status}
{t.score}{isV52 && (frScore !== 0 || liqScore !== 0) ? ({frScore > 0 ? "+" : ""}{frScore}/{liqScore > 0 ? "+" : ""}{liqScore}) : ""}
{holdMin}m
)}
); } // ─── 统计面板 ──────────────────────────────────────────────────── function StatsPanel({ strategy }: { strategy: StrategyFilter }) { const [data, setData] = useState(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); }, [strategy]); useEffect(() => { setTab("ALL"); }, [strategy]); if (!data || data.error) { return (

详细统计

该视图暂无统计数据
); } const tabs = ["ALL", "BTC", "ETH", "XRP", "SOL"]; const st = tab === "ALL" ? data : (data.by_symbol?.[tab] || null); return (

详细统计

{strategy !== "all" && {strategyBadgeText(strategy)}} {tabs.map((t) => ( ))}
{st ? (
胜率

{st.win_rate}%

盈亏比

{st.win_loss_ratio}

平均盈利

+{st.avg_win}R

平均亏损

-{st.avg_loss}R

最大回撤

{st.mdd}R

夏普比率

{st.sharpe}

总盈亏

= 0 ? "text-emerald-600" : "text-red-500"}`}> {(st.total_pnl ?? 0) >= 0 ? "+" : ""} {st.total_pnl ?? "-"}R

总笔数

{st.total ?? data.total}

做多胜率

{st.long_win_rate}% ({st.long_count}笔)

做空胜率

{st.short_win_rate}% ({st.short_count}笔)

{tab === "ALL" && data.by_tier && Object.entries(data.by_tier).map(([t, v]: [string, any]) => (
{t === "heavy" ? "加仓档" : t === "standard" ? "标准档" : "轻仓档"}

{v.win_rate}% ({v.total}笔)

))}
) : (
该币种暂无数据
)}
); } // ─── 主页面 ────────────────────────────────────────────────────── function PaperTradingPageInner() { const { isLoggedIn, loading } = useAuth(); const searchParams = useSearchParams(); const urlStrategy = searchParams.get("strategy"); const [strategyTab, setStrategyTab] = useState(() => normalizeStrategy(urlStrategy)); // URL参数变化时同步 useEffect(() => { if (urlStrategy) { setStrategyTab(normalizeStrategy(urlStrategy)); } }, [urlStrategy]); if (loading) return
加载中...
; if (!isLoggedIn) return (
🔒

请先登录查看模拟盘

登录
); return (

策略视图(顶部切换)

{STRATEGY_TABS.map((tab) => ( ))}

📊 模拟盘

V5.2策略AB测试 · 实时追踪 · 数据驱动优化 · {strategyTabDescription(strategyTab)}

); } export default function PaperTradingPage() { return ( 加载中...}> ); }