-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
386 lines (355 loc) · 17 KB
/
index.html
File metadata and controls
386 lines (355 loc) · 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Polymarket BTC Trading Dashboard</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://unpkg.com/react@18/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js@4"></script>
<script>
tailwind.config = {
darkMode: 'class',
theme: {
extend: {
colors: {
navy: { 800: '#1e293b', 900: '#0f172a', 950: '#020617' }
}
}
}
}
</script>
<style>
body { background: #0f172a; }
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: #1e293b; }
::-webkit-scrollbar-thumb { background: #475569; border-radius: 3px; }
.pulse-green { animation: pulseG 2s infinite; }
@keyframes pulseG { 0%,100% { opacity:1; } 50% { opacity:.5; } }
.pulse-red { animation: pulseR 1.5s infinite; }
@keyframes pulseR { 0%,100% { opacity:1; } 50% { opacity:.4; } }
</style>
</head>
<body class="dark min-h-screen text-slate-200">
<div id="root"></div>
<script type="text/babel">
const { useState, useEffect, useRef, useCallback } = React;
// ── Sample Data ──
const SAMPLE_TRADES = [
{ ts: '2026-03-15 11:55:00', dir: 'UP', amount: 5.00, result: 'won', pnl: 3.85, strategy: 'trend-follow' },
{ ts: '2026-03-15 11:50:00', dir: 'DOWN', amount: 5.00, result: 'lost', pnl: -5.00, strategy: 'overconfidence-reversal' },
{ ts: '2026-03-15 11:45:00', dir: 'UP', amount: 5.00, result: 'won', pnl: 4.20, strategy: 'trend-follow' },
{ ts: '2026-03-15 11:40:00', dir: 'UP', amount: 5.00, result: 'won', pnl: 3.50, strategy: 'low-vol-reversal' },
{ ts: '2026-03-15 11:35:00', dir: 'DOWN', amount: 5.00, result: 'won', pnl: 4.10, strategy: 'overconfidence-reversal' },
{ ts: '2026-03-15 11:30:00', dir: 'UP', amount: 5.00, result: 'lost', pnl: -5.00, strategy: 'trend-follow' },
{ ts: '2026-03-15 11:25:00', dir: 'DOWN', amount: 5.00, result: 'won', pnl: 3.90, strategy: 'low-vol-reversal' },
{ ts: '2026-03-15 11:20:00', dir: 'UP', amount: 5.00, result: 'won', pnl: 4.55, strategy: 'trend-follow' },
{ ts: '2026-03-15 11:15:00', dir: 'DOWN', amount: 5.00, result: 'pending', pnl: 0, strategy: 'overconfidence-reversal' },
{ ts: '2026-03-15 11:10:00', dir: 'UP', amount: 5.00, result: 'won', pnl: 3.70, strategy: 'trend-follow' },
{ ts: '2026-03-15 11:05:00', dir: 'UP', amount: 5.00, result: 'lost', pnl: -5.00, strategy: 'low-vol-reversal' },
{ ts: '2026-03-15 11:00:00', dir: 'DOWN', amount: 5.00, result: 'won', pnl: 4.00, strategy: 'overconfidence-reversal' },
{ ts: '2026-03-15 10:55:00', dir: 'UP', amount: 5.00, result: 'won', pnl: 3.65, strategy: 'trend-follow' },
{ ts: '2026-03-15 10:50:00', dir: 'DOWN', amount: 5.00, result: 'won', pnl: 4.30, strategy: 'trend-follow' },
{ ts: '2026-03-15 10:45:00', dir: 'UP', amount: 5.00, result: 'lost', pnl: -5.00, strategy: 'low-vol-reversal' },
];
// ── Detect if running locally (with API server) or on GitHub Pages ──
const IS_LOCAL = window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1';
const GITHUB_RAW_URL = 'https://raw.githubusercontent.com/ukgorclawbot-stack/trading-dashboard/main/trades.json';
// ── Stat helpers ──
function computeStats(trades) {
const settled = trades.filter(t => t.result === 'won' || t.result === 'lost');
const wins = settled.filter(t => t.result === 'won').length;
const totalPnl = trades.reduce((s, t) => s + t.pnl, 0);
let streak = 0, streakType = '';
for (const t of trades) {
if (t.result === 'pending') continue;
if (!streakType) { streakType = t.result; streak = 1; }
else if (t.result === streakType) streak++;
else break;
}
return {
total: trades.length,
winRate: settled.length ? ((wins / settled.length) * 100).toFixed(1) : '0.0',
totalPnl: totalPnl.toFixed(2),
streak: streak ? `${streak} ${streakType === 'won' ? 'W' : 'L'}` : '-'
};
}
// ── Components ──
function StatusDot({ running }) {
return (
<span className={`inline-block w-3 h-3 rounded-full mr-2 ${running ? 'bg-emerald-400 pulse-green' : 'bg-red-500 pulse-red'}`}/>
);
}
function StatCard({ label, value, sub, color }) {
return (
<div className="bg-slate-800/80 backdrop-blur rounded-xl p-4 border border-slate-700/50">
<div className="text-xs uppercase tracking-wider text-slate-400 mb-1">{label}</div>
<div className={`text-2xl font-bold ${color || 'text-white'}`}>{value}</div>
{sub && <div className="text-xs text-slate-500 mt-1">{sub}</div>}
</div>
);
}
function ResultBadge({ result }) {
const cls = {
won: 'bg-emerald-500/20 text-emerald-400 border-emerald-500/30',
lost: 'bg-red-500/20 text-red-400 border-red-500/30',
pending: 'bg-amber-500/20 text-amber-400 border-amber-500/30',
}[result] || 'bg-slate-600 text-slate-300';
return <span className={`text-xs px-2 py-0.5 rounded-full border ${cls}`}>{result}</span>;
}
function StrategyBadge({ strategy }) {
const cls = {
'trend-follow': 'text-blue-400',
'overconfidence-reversal': 'text-purple-400',
'low-vol-reversal': 'text-teal-400',
}[strategy] || 'text-slate-400';
return <span className={`text-xs ${cls}`}>{strategy}</span>;
}
function PnlChart({ trades }) {
const canvasRef = useRef(null);
const chartRef = useRef(null);
useEffect(() => {
if (!canvasRef.current) return;
const reversed = [...trades].reverse();
let cum = 0;
const data = reversed.map(t => { cum += t.pnl; return cum; });
const labels = reversed.map(t => t.ts.split(' ')[1]?.slice(0, 5) || '');
if (chartRef.current) chartRef.current.destroy();
const ctx = canvasRef.current.getContext('2d');
const gradient = ctx.createLinearGradient(0, 0, 0, 250);
gradient.addColorStop(0, 'rgba(52,211,153,0.3)');
gradient.addColorStop(1, 'rgba(52,211,153,0.0)');
chartRef.current = new Chart(ctx, {
type: 'line',
data: {
labels,
datasets: [{
label: 'Cumulative P&L (USDC)',
data,
borderColor: '#34d399',
backgroundColor: gradient,
fill: true,
tension: 0.3,
pointRadius: 3,
pointBackgroundColor: '#34d399',
borderWidth: 2,
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: { display: false },
tooltip: {
backgroundColor: '#1e293b',
borderColor: '#475569',
borderWidth: 1,
titleColor: '#e2e8f0',
bodyColor: '#94a3b8',
callbacks: { label: (c) => `P&L: ${c.parsed.y >= 0 ? '+' : ''}${c.parsed.y.toFixed(2)} USDC` }
}
},
scales: {
x: { grid: { color: '#1e293b' }, ticks: { color: '#64748b', font: { size: 10 } } },
y: {
grid: { color: '#1e293b' },
ticks: { color: '#64748b', font: { size: 10 }, callback: v => `${v >= 0 ? '+' : ''}${v}` }
}
}
}
});
return () => { if (chartRef.current) chartRef.current.destroy(); };
}, [trades]);
return (
<div className="bg-slate-800/80 backdrop-blur rounded-xl p-4 border border-slate-700/50">
<h2 className="text-sm font-semibold text-slate-300 mb-3">Cumulative P&L</h2>
<div style={{ height: '250px' }}><canvas ref={canvasRef}/></div>
</div>
);
}
// ── Main App ──
function App() {
const [btcPrice, setBtcPrice] = useState(null);
const [trades, setTrades] = useState(SAMPLE_TRADES);
const [balance, setBalance] = useState('247.35');
const [running, setRunning] = useState(true);
const [lastRefresh, setLastRefresh] = useState(new Date());
const [startTime] = useState(new Date(Date.now() - 2 * 60 * 60 * 1000)); // 2h ago
const [uptime, setUptime] = useState('');
const [marketWindow, setMarketWindow] = useState({ up: '0.52', down: '0.48' });
const fetchBtcPrice = useCallback(async () => {
try {
const r = await fetch('https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT');
const d = await r.json();
setBtcPrice(parseFloat(d.price).toLocaleString('en-US', { maximumFractionDigits: 2 }));
} catch { /* keep last price */ }
}, []);
const [apiConnected, setApiConnected] = useState(false);
const [dataSource, setDataSource] = useState('sample');
function applyTradeData(d, source) {
if (d.trades && d.trades.length) setTrades(d.trades);
if (d.balance) setBalance(d.balance);
if (d.running !== undefined) setRunning(d.running);
if (d.market) setMarketWindow(d.market);
setApiConnected(true);
setDataSource(source);
}
const fetchLogTrades = useCallback(async () => {
// Try local API first
if (IS_LOCAL) {
try {
const r = await fetch('/api/trades');
if (r.ok) {
applyTradeData(await r.json(), 'local');
return;
}
} catch { /* fall through */ }
}
// Try GitHub raw trades.json (for GitHub Pages or when local API is down)
try {
const r = await fetch(GITHUB_RAW_URL + '?t=' + Date.now()); // cache-bust
if (r.ok) {
applyTradeData(await r.json(), 'github');
return;
}
} catch { /* fall through */ }
// Fallback: keep sample data
setApiConnected(false);
setDataSource('sample');
}, []);
useEffect(() => {
fetchBtcPrice();
fetchLogTrades();
const iv = setInterval(() => {
fetchBtcPrice();
fetchLogTrades();
setLastRefresh(new Date());
}, 30000);
return () => clearInterval(iv);
}, [fetchBtcPrice, fetchLogTrades]);
useEffect(() => {
const iv = setInterval(() => {
const diff = Date.now() - startTime.getTime();
const h = Math.floor(diff / 3600000);
const m = Math.floor((diff % 3600000) / 60000);
const s = Math.floor((diff % 60000) / 1000);
setUptime(`${h}h ${m}m ${s}s`);
}, 1000);
return () => clearInterval(iv);
}, [startTime]);
const stats = computeStats(trades);
return (
<div className="max-w-6xl mx-auto px-4 py-6">
{/* Header */}
<div className="flex items-center justify-between mb-6">
<div>
<h1 className="text-xl font-bold text-white tracking-tight">
<span className="text-emerald-400">◈</span> Polymarket BTC 5-Min Dashboard
</h1>
<p className="text-xs text-slate-500 mt-0.5">Auto-refreshes every 30s</p>
</div>
<div className="text-right text-xs text-slate-500">
{dataSource === 'sample' && <div className="text-amber-400 mb-1">No data source — showing sample data</div>}
{dataSource === 'github' && <div className="text-blue-400 mb-1">Live data via GitHub</div>}
{dataSource === 'local' && <div className="text-emerald-400 mb-1">Live data via local API</div>}
Last refresh: {lastRefresh.toLocaleTimeString()}
</div>
</div>
{/* Live Status Panel */}
<div className="bg-slate-800/80 backdrop-blur rounded-xl p-4 border border-slate-700/50 mb-4">
<div className="grid grid-cols-2 md:grid-cols-5 gap-4">
<div>
<div className="text-xs text-slate-400 uppercase tracking-wider">BTC Price</div>
<div className="text-lg font-mono font-bold text-amber-400 mt-1">
${btcPrice || '—'}
</div>
</div>
<div>
<div className="text-xs text-slate-400 uppercase tracking-wider">Market Window</div>
<div className="mt-1 flex gap-2 text-sm font-mono">
<span className="text-emerald-400">UP {marketWindow.up}</span>
<span className="text-slate-600">|</span>
<span className="text-red-400">DN {marketWindow.down}</span>
</div>
</div>
<div>
<div className="text-xs text-slate-400 uppercase tracking-wider">Status</div>
<div className="mt-1 flex items-center text-sm font-semibold">
<StatusDot running={running}/>
<span className={running ? 'text-emerald-400' : 'text-red-400'}>
{running ? 'Running' : 'Stopped'}
</span>
</div>
</div>
<div>
<div className="text-xs text-slate-400 uppercase tracking-wider">Balance</div>
<div className="text-lg font-mono font-bold text-white mt-1">${balance}</div>
</div>
<div>
<div className="text-xs text-slate-400 uppercase tracking-wider">Uptime</div>
<div className="text-sm font-mono text-slate-300 mt-1">{uptime}</div>
</div>
</div>
</div>
{/* Stats Cards */}
<div className="grid grid-cols-2 md:grid-cols-4 gap-3 mb-4">
<StatCard label="Total Trades" value={stats.total} />
<StatCard label="Win Rate" value={`${stats.winRate}%`}
color={parseFloat(stats.winRate) >= 50 ? 'text-emerald-400' : 'text-red-400'} />
<StatCard label="Total P&L" value={`${parseFloat(stats.totalPnl) >= 0 ? '+' : ''}$${stats.totalPnl}`}
color={parseFloat(stats.totalPnl) >= 0 ? 'text-emerald-400' : 'text-red-400'} />
<StatCard label="Current Streak" value={stats.streak}
color={stats.streak.includes('W') ? 'text-emerald-400' : stats.streak.includes('L') ? 'text-red-400' : 'text-slate-300'} />
</div>
{/* Trade History */}
<div className="bg-slate-800/80 backdrop-blur rounded-xl border border-slate-700/50 mb-4 overflow-hidden">
<div className="px-4 py-3 border-b border-slate-700/50">
<h2 className="text-sm font-semibold text-slate-300">Trade History</h2>
</div>
<div className="overflow-x-auto">
<table className="w-full text-sm">
<thead>
<tr className="text-xs text-slate-500 uppercase tracking-wider border-b border-slate-700/30">
<th className="text-left px-4 py-2">Time</th>
<th className="text-left px-4 py-2">Dir</th>
<th className="text-right px-4 py-2">Amount</th>
<th className="text-center px-4 py-2">Result</th>
<th className="text-right px-4 py-2">P&L</th>
<th className="text-left px-4 py-2">Strategy</th>
</tr>
</thead>
<tbody>
{trades.map((t, i) => (
<tr key={i} className="border-b border-slate-700/20 hover:bg-slate-700/30 transition-colors">
<td className="px-4 py-2 font-mono text-xs text-slate-400">{t.ts}</td>
<td className="px-4 py-2">
<span className={`font-bold text-xs ${t.dir === 'UP' ? 'text-emerald-400' : 'text-red-400'}`}>
{t.dir === 'UP' ? '\u25B2' : '\u25BC'} {t.dir}
</span>
</td>
<td className="px-4 py-2 text-right font-mono text-xs">${t.amount.toFixed(2)}</td>
<td className="px-4 py-2 text-center"><ResultBadge result={t.result}/></td>
<td className={`px-4 py-2 text-right font-mono text-xs font-bold ${t.pnl > 0 ? 'text-emerald-400' : t.pnl < 0 ? 'text-red-400' : 'text-slate-500'}`}>
{t.pnl > 0 ? '+' : ''}{t.pnl.toFixed(2)}
</td>
<td className="px-4 py-2"><StrategyBadge strategy={t.strategy}/></td>
</tr>
))}
</tbody>
</table>
</div>
</div>
{/* P&L Chart */}
<PnlChart trades={trades}/>
<div className="text-center text-xs text-slate-600 mt-6 pb-4">
Polymarket BTC 5-Min Auto-Trader Dashboard
</div>
</div>
);
}
ReactDOM.createRoot(document.getElementById('root')).render(<App/>);
</script>
</body>
</html>