-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto-example.html
More file actions
555 lines (520 loc) · 18.9 KB
/
crypto-example.html
File metadata and controls
555 lines (520 loc) · 18.9 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FCS Crypto Real-time Demo</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
background: linear-gradient(135deg, #0f0c29 0%, #302b63 50%, #24243e 100%);
min-height: 100vh;
padding: 20px;
color: #fff;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
header {
text-align: center;
margin-bottom: 30px;
}
header h1 {
font-size: 2.5rem;
margin-bottom: 10px;
background: linear-gradient(90deg, #f7931a, #ffcc00);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
header p {
color: #a0a0c0;
font-size: 1.1rem;
}
.status-bar {
display: flex;
justify-content: center;
align-items: center;
gap: 20px;
margin-bottom: 30px;
flex-wrap: wrap;
}
.status {
display: flex;
align-items: center;
gap: 8px;
padding: 10px 20px;
background: rgba(255,255,255,0.05);
border-radius: 50px;
}
.status-dot {
width: 10px;
height: 10px;
border-radius: 50%;
background: #ff4757;
animation: pulse 2s infinite;
}
.status-dot.connected {
background: #00ff88;
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.5; }
}
.controls {
display: flex;
justify-content: center;
gap: 15px;
margin-bottom: 30px;
flex-wrap: wrap;
}
.btn {
padding: 12px 30px;
border: none;
border-radius: 8px;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
}
.btn-connect {
background: linear-gradient(90deg, #f7931a, #ffcc00);
color: #0f0c29;
}
.btn-disconnect {
background: #ff4757;
color: #fff;
}
.btn:hover {
transform: translateY(-2px);
box-shadow: 0 10px 30px rgba(247,147,26,0.3);
}
.btn:disabled {
opacity: 0.5;
cursor: not-allowed;
transform: none;
}
.api-key-input {
display: flex;
justify-content: center;
gap: 10px;
margin-bottom: 30px;
flex-wrap: wrap;
}
.api-key-input input {
padding: 12px 20px;
width: 300px;
border: 2px solid rgba(255,255,255,0.1);
border-radius: 8px;
background: rgba(255,255,255,0.05);
color: #fff;
font-size: 1rem;
}
.api-key-input input:focus {
outline: none;
border-color: #f7931a;
}
.cards-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 20px;
}
.card {
background: rgba(255,255,255,0.05);
border-radius: 16px;
padding: 25px;
border: 1px solid rgba(255,255,255,0.1);
transition: all 0.3s ease;
position: relative;
overflow: hidden;
}
.card::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
height: 3px;
background: linear-gradient(90deg, #f7931a, #ffcc00);
opacity: 0;
transition: opacity 0.3s;
}
.card:hover::before {
opacity: 1;
}
.card:hover {
transform: translateY(-5px);
border-color: rgba(247,147,26,0.3);
box-shadow: 0 20px 40px rgba(0,0,0,0.3);
}
.card-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
}
.symbol-info {
display: flex;
align-items: center;
gap: 12px;
}
.crypto-icon {
width: 40px;
height: 40px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-weight: bold;
font-size: 1.2rem;
}
.btc-icon { background: linear-gradient(135deg, #f7931a, #ffcc00); color: #fff; }
.eth-icon { background: linear-gradient(135deg, #627eea, #8c9eff); color: #fff; }
.bnb-icon { background: linear-gradient(135deg, #f3ba2f, #ffd54f); color: #333; }
.xrp-icon { background: linear-gradient(135deg, #23292f, #455a64); color: #fff; }
.sol-icon { background: linear-gradient(135deg, #9945ff, #14f195); color: #fff; }
.ada-icon { background: linear-gradient(135deg, #0033ad, #1976d2); color: #fff; }
.symbol {
font-size: 1.3rem;
font-weight: 700;
}
.symbol-name {
font-size: 0.85rem;
color: #a0a0c0;
}
.timeframe {
padding: 5px 12px;
background: rgba(247,147,26,0.2);
border-radius: 20px;
font-size: 0.85rem;
color: #f7931a;
}
.price-container {
margin-bottom: 15px;
}
.price {
font-size: 2.2rem;
font-weight: 700;
}
.price-usd {
color: #a0a0c0;
font-size: 0.9rem;
}
.change {
display: inline-block;
padding: 5px 15px;
border-radius: 20px;
font-weight: 600;
font-size: 1rem;
}
.change.positive {
background: rgba(0,255,136,0.2);
color: #00ff88;
}
.change.negative {
background: rgba(255,71,87,0.2);
color: #ff4757;
}
.ohlc {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 10px;
margin-top: 20px;
padding-top: 20px;
border-top: 1px solid rgba(255,255,255,0.1);
}
.ohlc-item {
display: flex;
justify-content: space-between;
}
.ohlc-label {
color: #a0a0c0;
font-size: 0.9rem;
}
.ohlc-value {
font-weight: 600;
}
.volume-bar {
margin-top: 15px;
height: 4px;
background: rgba(255,255,255,0.1);
border-radius: 2px;
overflow: hidden;
}
.volume-fill {
height: 100%;
background: linear-gradient(90deg, #f7931a, #ffcc00);
width: 0%;
transition: width 0.5s ease;
}
.log-container {
margin-top: 40px;
background: rgba(0,0,0,0.3);
border-radius: 12px;
padding: 20px;
max-height: 300px;
overflow-y: auto;
}
.log-container h3 {
margin-bottom: 15px;
color: #a0a0c0;
}
.log-entry {
font-family: 'Courier New', monospace;
font-size: 0.85rem;
padding: 5px 0;
border-bottom: 1px solid rgba(255,255,255,0.05);
color: #ccd6f6;
}
.log-entry.error {
color: #ff4757;
}
.log-entry.success {
color: #00ff88;
}
footer {
text-align: center;
margin-top: 40px;
padding: 20px;
color: #a0a0c0;
}
footer a {
color: #f7931a;
text-decoration: none;
}
</style>
</head>
<body>
<div class="container">
<header>
<h1>Cryptocurrency Real-time Prices</h1>
<p>Live crypto data from Binance via FCS WebSocket API</p>
</header>
<div class="api-key-input">
<input type="text" id="apiKey" placeholder="Enter your FCS API Key">
</div>
<div class="status-bar">
<div class="status">
<div class="status-dot" id="statusDot"></div>
<span id="statusText">Disconnected</span>
</div>
</div>
<div class="controls">
<button class="btn btn-connect" id="connectBtn" onclick="connectToFCS()">Connect</button>
<button class="btn btn-disconnect" id="disconnectBtn" onclick="disconnectFromFCS()" disabled>Disconnect</button>
</div>
<div class="cards-grid" id="cardsContainer">
<!-- Cards will be dynamically added here -->
</div>
<div class="log-container">
<h3>Connection Log</h3>
<div id="logContainer"></div>
</div>
<footer>
<p>Powered by <a href="https://fcsapi.com" target="_blank">FCS API</a> |
<a href="https://fcsapi.com/document/stock-api" target="_blank">Documentation</a></p>
</footer>
</div>
<script src="../fcs-client-lib.js"></script>
<script>
let client = null;
const cryptoPairs = [
{ symbol: 'BINANCE:BTCUSDT', name: 'Bitcoin', short: 'BTC', icon: 'btc' },
{ symbol: 'BINANCE:ETHUSDT', name: 'Ethereum', short: 'ETH', icon: 'eth' },
{ symbol: 'BINANCE:BNBUSDT', name: 'BNB', short: 'BNB', icon: 'bnb' },
{ symbol: 'BINANCE:XRPUSDT', name: 'Ripple', short: 'XRP', icon: 'xrp' },
{ symbol: 'BINANCE:SOLUSDT', name: 'Solana', short: 'SOL', icon: 'sol' },
{ symbol: 'BINANCE:ADAUSDT', name: 'Cardano', short: 'ADA', icon: 'ada' }
];
// Store previous prices for change calculation
const previousPrices = {};
// Initialize cards
function initCards() {
const container = document.getElementById('cardsContainer');
container.innerHTML = cryptoPairs.map(pair => `
<div class="card" id="card-${pair.symbol.replace(':', '-')}">
<div class="card-header">
<div class="symbol-info">
<div class="crypto-icon ${pair.icon}-icon">${pair.short.charAt(0)}</div>
<div>
<div class="symbol">${pair.short}/USDT</div>
<div class="symbol-name">${pair.name}</div>
</div>
</div>
<span class="timeframe">1D</span>
</div>
<div class="price-container">
<div class="price" id="price-${pair.symbol.replace(':', '-')}">$--</div>
</div>
<div class="change" id="change-${pair.symbol.replace(':', '-')}">--%</div>
<div class="ohlc">
<div class="ohlc-item">
<span class="ohlc-label">Open</span>
<span class="ohlc-value" id="open-${pair.symbol.replace(':', '-')}">--</span>
</div>
<div class="ohlc-item">
<span class="ohlc-label">High</span>
<span class="ohlc-value" id="high-${pair.symbol.replace(':', '-')}">--</span>
</div>
<div class="ohlc-item">
<span class="ohlc-label">Low</span>
<span class="ohlc-value" id="low-${pair.symbol.replace(':', '-')}">--</span>
</div>
<div class="ohlc-item">
<span class="ohlc-label">Volume</span>
<span class="ohlc-value" id="volume-${pair.symbol.replace(':', '-')}">--</span>
</div>
</div>
</div>
`).join('');
}
function log(message, type = 'info') {
const container = document.getElementById('logContainer');
const entry = document.createElement('div');
entry.className = `log-entry ${type}`;
entry.textContent = `[${new Date().toLocaleTimeString()}] ${message}`;
container.insertBefore(entry, container.firstChild);
while (container.children.length > 50) {
container.removeChild(container.lastChild);
}
}
function updateStatus(connected) {
const dot = document.getElementById('statusDot');
const text = document.getElementById('statusText');
const connectBtn = document.getElementById('connectBtn');
const disconnectBtn = document.getElementById('disconnectBtn');
if (connected) {
dot.classList.add('connected');
text.textContent = 'Connected';
connectBtn.disabled = true;
disconnectBtn.disabled = false;
} else {
dot.classList.remove('connected');
text.textContent = 'Disconnected';
connectBtn.disabled = false;
disconnectBtn.disabled = true;
}
}
function formatPrice(price, symbol) {
const p = parseFloat(price);
// BTC and ETH need 2 decimals, others need more precision
if (symbol.includes('BTC') || p > 1000) {
return '$' + p.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
} else if (p > 1) {
return '$' + p.toFixed(4);
} else {
return '$' + p.toFixed(6);
}
}
function formatVolume(vol) {
const v = parseFloat(vol);
if (v >= 1000000) return (v / 1000000).toFixed(2) + 'M';
if (v >= 1000) return (v / 1000).toFixed(2) + 'K';
return v.toFixed(2);
}
function updateCard(symbol, prices) {
const id = symbol.replace(':', '-');
const priceEl = document.getElementById(`price-${id}`);
const changeEl = document.getElementById(`change-${id}`);
const openEl = document.getElementById(`open-${id}`);
const highEl = document.getElementById(`high-${id}`);
const lowEl = document.getElementById(`low-${id}`);
const volumeEl = document.getElementById(`volume-${id}`);
if (!priceEl) return;
const p = prices;
if (p.mode === 'candle' || p.mode === 'initial') {
priceEl.textContent = formatPrice(p.c, symbol);
openEl.textContent = formatPrice(p.o, symbol);
highEl.textContent = formatPrice(p.h, symbol);
lowEl.textContent = formatPrice(p.l, symbol);
if (p.v) volumeEl.textContent = formatVolume(p.v);
// Calculate change from open
const open = parseFloat(p.o);
const close = parseFloat(p.c);
const change = ((close - open) / open) * 100;
changeEl.textContent = `${change >= 0 ? '+' : ''}${change.toFixed(2)}%`;
changeEl.className = `change ${change >= 0 ? 'positive' : 'negative'}`;
previousPrices[symbol] = { o: open, c: close };
} else if (p.mode === 'askbid') {
priceEl.textContent = formatPrice(p.c, symbol);
// Update change if we have open price
if (previousPrices[symbol]) {
const open = previousPrices[symbol].o;
const close = parseFloat(p.c);
const change = ((close - open) / open) * 100;
changeEl.textContent = `${change >= 0 ? '+' : ''}${change.toFixed(2)}%`;
changeEl.className = `change ${change >= 0 ? 'positive' : 'negative'}`;
}
}
}
function connectToFCS() {
const apiKey = document.getElementById('apiKey').value.trim();
if (!apiKey) {
log('Please enter your API key', 'error');
alert('Please enter your FCS API key');
return;
}
log('Connecting to FCS WebSocket...');
client = new FCSClient(apiKey);
client.focusTimeout = 5;
client.onconnected = () => {
log('Connected successfully!', 'success');
updateStatus(true);
};
client.onmessage = (data) => {
// Handle price updates
if (data.type === 'price' && data.prices) {
const p = data.prices;
updateCard(data.symbol, p);
if (p.mode === 'candle' || p.mode === 'initial') {
log(`${data.symbol}: ${formatPrice(p.c, data.symbol)} Vol: ${formatVolume(p.v || 0)}`);
} else if (p.mode === 'askbid') {
log(`${data.symbol}: Ask=${p.a} Bid=${p.b}`);
}
} else if (data.type === 'message') {
if (data.short === 'joined_room') {
log(`Joined: ${data.symbol} (${data.timeframe})`, 'success');
} else {
log(data.message || JSON.stringify(data));
}
}
};
client.onclose = (event) => {
log(`Connection closed: ${event.reason || 'Unknown reason'}`, 'error');
updateStatus(false);
};
client.onerror = (err) => {
log(`Error: ${err.message || 'Unknown error'}`, 'error');
};
client.onreconnect = () => {
log('Reconnected successfully!', 'success');
updateStatus(true);
};
client.connect().then(() => {
cryptoPairs.forEach(pair => {
client.join(pair.symbol, '1D');
log(`Subscribing to ${pair.name} (${pair.short})...`);
});
}).catch(err => {
log(`Connection failed: ${err.message}`, 'error');
updateStatus(false);
});
}
function disconnectFromFCS() {
if (client) {
client.disconnect();
client = null;
log('Disconnected manually');
updateStatus(false);
}
}
// Initialize on page load
initCards();
</script>
</body>
</html>