"use client"; import { useState, useEffect } from "react"; import { authFetch } 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 }); } // eslint-disable-next-line @typescript-eslint/no-explicit-any function parseFactors(raw: any) { if (!raw) return null; if (typeof raw === "string") { try { return JSON.parse(raw); } catch { return null; } } return raw; } interface Props { strategyId: string; symbol: string; } type FilterResult = "all" | "win" | "loss"; type FilterSymbol = "all" | string; // ─── 控制面板(策略启停)───────────────────────────────────────── function ControlPanel({ strategyId }: { strategyId: string }) { const [status, setStatus] = useState(null); const [saving, setSaving] = useState(false); useEffect(() => { (async () => { try { const r = await authFetch(`/api/strategies/${strategyId}`); if (r.ok) { const j = await r.json(); setStatus(j.status); } } catch {} })(); }, [strategyId]); const toggle = async () => { setSaving(true); const newStatus = status === "running" ? "paused" : "running"; try { const r = await authFetch(`/api/strategies/${strategyId}`, { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ status: newStatus }), }); if (r.ok) setStatus(newStatus); } catch {} finally { setSaving(false); } }; if (!status) return null; return (
{status === "running" ? "🟢 运行中" : "⚪ 已暂停"}
); } // ─── 总览卡片 ──────────────────────────────────────────────────── function SummaryCards({ strategyId }: { strategyId: string }) { // eslint-disable-next-line @typescript-eslint/no-explicit-any const [data, setData] = useState(null); useEffect(() => { const f = async () => { try { const r = await authFetch(`/api/paper/summary?strategy_id=${strategyId}`); if (r.ok) setData(await r.json()); } catch {} }; f(); const iv = setInterval(f, 10000); return () => clearInterval(iv); }, [strategyId]); if (!data) return
加载中...
; return (
{[ { 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 }) => (

{label}

{value}

{sub}

))}
); } // ─── 当前持仓 ──────────────────────────────────────────────────── function ActivePositions({ strategyId }: { strategyId: string }) { // eslint-disable-next-line @typescript-eslint/no-explicit-any const [positions, setPositions] = useState([]); const [wsPrices, setWsPrices] = useState>({}); const RISK_USD = 200; // 1R = 200 USDT useEffect(() => { const f = async () => { try { const r = await authFetch(`/api/paper/positions?strategy_id=${strategyId}`); if (r.ok) setPositions((await r.json()).data || []); } catch {} }; f(); const iv = setInterval(f, 10000); return () => clearInterval(iv); }, [strategyId]); useEffect(() => { const streams = ["btcusdt", "ethusdt", "xrpusdt", "solusdt"].map(s => `${s}@aggTrade`).join("/"); const ws = new WebSocket(`wss://fstream.binance.com/stream?streams=${streams}`); // eslint-disable-next-line @typescript-eslint/no-explicit-any 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
暂无活跃持仓
; return (

当前持仓 ● 实时

{/* eslint-disable-next-line @typescript-eslint/no-explicit-any */} {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 * RISK_USD; return (
{p.direction === "LONG" ? "🟢" : "🔴"} {sym} {p.direction} 评分{p.score}
= 0 ? "text-emerald-600" : "text-red-500"}`}>{unrealR >= 0 ? "+" : ""}{unrealR.toFixed(2)}R = 0 ? "text-emerald-500" : "text-red-400"}`}>${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)}
入场: {p.entry_ts ? bjt(p.entry_ts) : "-"}
); })}
); } // ─── 权益曲线 ──────────────────────────────────────────────────── function EquityCurve({ strategyId }: { strategyId: string }) { // eslint-disable-next-line @typescript-eslint/no-explicit-any const [data, setData] = useState([]); useEffect(() => { const f = async () => { try { const r = await authFetch(`/api/paper/equity-curve?strategy_id=${strategyId}`); if (r.ok) setData((await r.json()).data || []); } catch {} }; f(); const iv = setInterval(f, 30000); return () => clearInterval(iv); }, [strategyId]); return (

权益曲线

{data.length < 2 ?
数据积累中...
: (
bjt(Number(v))} tick={{ fontSize: 10 }} /> `${v}R`} /> bjt(Number(v))} formatter={(v: unknown) => [`${v}R`, "累计PnL"]} />
)}
); } // ─── 历史交易 ──────────────────────────────────────────────────── function TradeHistory({ strategyId }: { strategyId: string }) { // eslint-disable-next-line @typescript-eslint/no-explicit-any const [trades, setTrades] = useState([]); const [filterResult, setFilterResult] = useState("all"); const [filterSym, setFilterSym] = useState("all"); useEffect(() => { const f = async () => { try { const r = await authFetch(`/api/paper/trades?strategy_id=${strategyId}&result=${filterResult}&symbol=${filterSym}&limit=50`); if (r.ok) setTrades((await r.json()).data || []); } catch {} }; f(); const iv = setInterval(f, 10000); return () => clearInterval(iv); }, [strategyId, filterResult, filterSym]); const fmtTime = (ms: number) => ms ? bjt(ms) : "-"; const STATUS_LABEL: Record = { tp: "止盈", sl: "止损", sl_be: "保本", timeout: "超时", signal_flip: "翻转" }; const STATUS_COLOR: Record = { tp: "bg-emerald-100 text-emerald-700", sl: "bg-red-100 text-red-700", sl_be: "bg-amber-100 text-amber-700", signal_flip: "bg-purple-100 text-purple-700", timeout: "bg-slate-100 text-slate-600" }; return (

历史交易

{(["all", "BTC", "ETH", "XRP", "SOL"] as FilterSymbol[]).map(s => ( ))} | {(["all", "win", "loss"] as FilterResult[]).map(r => ( ))}
{trades.length === 0 ?
暂无交易记录
: ( {/* eslint-disable-next-line @typescript-eslint/no-explicit-any */} {trades.map((t: any) => { const holdMin = t.exit_ts && t.entry_ts ? Math.round((t.exit_ts - t.entry_ts) / 60000) : 0; return ( ); })}
币种 方向 入场价 出场价 PnL(R) 状态 分数 入场时间 出场时间 持仓
{t.symbol?.replace("USDT", "")} {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)} {STATUS_LABEL[t.status] || t.status} {t.score} {fmtTime(t.entry_ts)} {fmtTime(t.exit_ts)} {holdMin}m
)}
); } // ─── 详细统计 ──────────────────────────────────────────────────── function StatsPanel({ strategyId }: { strategyId: string }) { // eslint-disable-next-line @typescript-eslint/no-explicit-any const [data, setData] = useState(null); const [tab, setTab] = useState("ALL"); useEffect(() => { const f = async () => { try { const r = await authFetch(`/api/paper/stats?strategy_id=${strategyId}`); if (r.ok) setData(await r.json()); } catch {} }; f(); const iv = setInterval(f, 30000); return () => clearInterval(iv); }, [strategyId]); if (!data || data.error) return (

详细统计

等待交易记录积累...
); const coinTabs = ["ALL", "BTC", "ETH", "XRP", "SOL"]; const st = tab === "ALL" ? data : (data.by_symbol?.[tab] || null); return (

详细统计

{coinTabs.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}笔)

) :
该币种暂无数据
}
); } // ─── 主组件 ────────────────────────────────────────────────────── export default function PaperGeneric({ strategyId, symbol }: Props) { return (

📈 模拟盘

{symbol.replace("USDT", "")} · strategy_id: {strategyId.slice(0, 8)}...

); }