-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTheAnalystsProblem_v4.html
More file actions
720 lines (622 loc) · 36.6 KB
/
TheAnalystsProblem_v4.html
File metadata and controls
720 lines (622 loc) · 36.6 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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>The Analyst's Problem: Volume IV - Skimmer & API Integration</title>
<!-- Load Plotly.js -->
<script src="https://cdn.plot.ly/plotly-2.27.0.min.js"></script>
<style>
@import url('https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;600;700;800&family=JetBrains+Mono:wght@400;600&family=Playfair+Display:ital,wght@0,700;1,400&display=swap');
* { margin: 0; padding: 0; box-sizing: border-box; }
:root {
--void: #05070e;
--gold: #ffc400;
--gold-glow: rgba(255, 196, 0, 0.6);
--danger: #ff3333;
--danger-glow: rgba(255, 51, 51, 0.6);
--success: #00e5ff;
--silver: #e0e6ed;
--panel: rgba(10, 11, 16, 0.85);
--border: rgba(255, 196, 0, 0.25);
--font-main: 'Outfit', sans-serif;
--font-mono: 'JetBrains Mono', monospace;
--font-serif: 'Playfair Display', serif;
}
html, body {
width: 100%; height: 100%; overflow: hidden;
background: var(--void); color: var(--silver);
font-family: var(--font-main); font-size: 12px;
}
#ui-wrapper { display: flex; flex-direction: column; height: 100vh; }
#header {
padding: 15px 30px;
background: linear-gradient(180deg, rgba(5, 7, 14, 0.95) 20%, transparent);
border-bottom: 1px solid var(--border);
text-align: center; z-index: 10;
}
#header h1 {
font-family: var(--font-serif); font-size: 26px; font-weight: 700;
letter-spacing: 2px; color: var(--gold); text-shadow: 0 0 20px var(--gold-glow);
margin-bottom: 5px;
}
#header .subtitle {
font-size: 12px; font-weight: 600; letter-spacing: 3px; color: var(--silver); text-transform: uppercase;
}
.stats-bar {
display: flex; justify-content: space-around;
background: rgba(5, 7, 14, 0.8); padding: 10px; font-size: 14px;
border-bottom: 1px solid rgba(255,255,255,0.05); font-family: var(--font-mono);
z-index: 10;
}
.stat-box {
padding: 6px 15px; border-radius: 4px; background: rgba(255,255,255,0.03);
border: 1px solid rgba(255,255,255,0.1); color: #a0aab5;
}
.stat-val { color: var(--silver); font-weight: 600; }
.pos-profit { color: var(--success); font-weight: bold; text-shadow: 0 0 10px rgba(0, 229, 255, 0.4); }
.neg-profit { color: var(--danger); font-weight: bold; text-shadow: 0 0 10px var(--danger-glow); }
#chartContainer { flex: 1; width: 100vw; position: relative; z-index: 1; }
#footer {
padding: 12px 30px; background: rgba(5, 7, 14, 0.95);
border-top: 1px solid var(--border);
display: flex; justify-content: center; align-items: center; gap: 15px;
z-index: 10;
}
.int-link {
color: var(--gold); text-decoration: none; border: 1px solid rgba(255, 196, 0, 0.3);
padding: 6px 15px; border-radius: 4px; background: rgba(255, 196, 0, 0.05);
font-family: var(--font-mono); font-size: 10px; font-weight: 600;
letter-spacing: 1px; transition: all 0.2s ease; text-transform: uppercase;
}
.int-link:hover {
background: rgba(255, 196, 0, 0.15); border-color: var(--gold);
box-shadow: 0 0 10px rgba(255, 196, 0, 0.3); color: #fff;
}
.modal-overlay {
position: fixed; inset: 0; background: rgba(5, 7, 14, 0.92);
backdrop-filter: blur(8px); z-index: 2000;
display: flex; justify-content: center; align-items: center;
}
.modal-content {
background: var(--panel); padding: 40px; border-radius: 12px;
border: 1px solid var(--gold); text-align: center; max-width: 650px;
box-shadow: 0 0 40px rgba(255, 196, 0, 0.15);
}
.modal-content h1 { font-family: var(--font-serif); color: var(--gold); font-size: 32px; margin-bottom: 10px; text-shadow: 0 0 15px var(--gold-glow); }
.modal-content p { color: #a0aab5; font-size: 14px; line-height: 1.6; margin-bottom: 20px; }
.disclaimer-box {
background: rgba(255, 51, 51, 0.05); border-left: 3px solid var(--danger);
padding: 15px; border-radius: 4px; font-size: 12px; color: #ff8888; text-align: left;
margin-bottom: 30px; font-family: var(--font-mono); line-height: 1.5;
}
.btn-group { display: flex; gap: 15px; justify-content: center; flex-wrap: wrap; }
.btn {
background: linear-gradient(135deg, rgba(255, 196, 0, 0.1), rgba(255, 196, 0, 0.2));
border: 1px solid var(--gold); color: var(--gold);
font-family: var(--font-serif); font-size: 14px; font-weight: 700;
padding: 12px 20px; cursor: pointer; border-radius: 6px;
transition: all 0.3s ease; text-transform: uppercase; letter-spacing: 1px;
flex: 1 1 30%; min-width: 180px;
}
.btn:hover { background: var(--gold); color: var(--void); box-shadow: 0 0 20px var(--gold-glow); }
#endModal { display: none; }
.pulse { animation: pulse-text 1s infinite; }
@keyframes pulse-text { 50% { opacity: 0.5; } }
#loading-text { font-family: var(--font-mono); color: var(--gold); display: none; margin-top: 15px; }
</style>
</head>
<body>
<!-- STARTER MODAL -->
<div id="startModal" class="modal-overlay">
<div class="modal-content">
<h1>The Analyst's Problem</h1>
<p style="font-family: var(--font-mono); color: var(--silver); text-transform: uppercase; letter-spacing: 2px;">Volume IV: Spectral Predictive Engine</p>
<p>Select a data source. <strong>Real Data</strong> fetches live, keyless Binance BTC/USDT REST API data. <strong>Frequency Trader</strong> runs an autonomous 100-Agent Skimmer Bot generating cryptographic Proof-of-Wins in the browser console.</p>
<div class="disclaimer-box">
<strong>WARNING / LEGAL DISCLAIMER</strong><br>
This application is a strictly mathematical demonstration. It is NOT financial advice. The "Skimmer" mode simulates market manipulation against retail agents for educational purposes.
</div>
<div class="btn-group">
<button class="btn" onclick="startEngine('random')">Random Data<br><span style="font-size:10px; font-family:var(--font-main); color:#888; font-weight:normal;">Pure Gaussian Noise</span></button>
<button class="btn" onclick="startEngine('real')">Real Data (API)<br><span style="font-size:10px; font-family:var(--font-main); color:#888; font-weight:normal;">Binance BTC/USDT Live</span></button>
<button class="btn" onclick="startEngine('frequency')" style="border-color: #00e5ff; color: #00e5ff; box-shadow: 0 0 10px rgba(0,229,255,0.2);">Frequency Trader<br><span style="font-size:10px; font-family:var(--font-main); color:#888; font-weight:normal;">100-Agent Skimmer Simulation</span></button>
</div>
<div id="loading-text">Fetching Market Data...</div>
</div>
</div>
<!-- END MODAL -->
<div id="endModal" class="modal-overlay">
<div class="modal-content">
<h1>Engine Cycle Complete</h1>
<p>The projection vector demonstrated real-time, mathematically proven directional forecasting, neutralizing noise and mapping the spectral future.</p>
<h2 id="finalPLDisplay" style="font-family: var(--font-mono); font-size: 38px; margin: 25px 0;"></h2>
<button class="btn" onclick="location.reload()">Initialize Next Sequence</button>
</div>
</div>
<!-- MAIN UI -->
<div id="ui-wrapper">
<div id="header">
<h1>The Analyst's Problem: Volume IV</h1>
<div class="subtitle">σ-Selector | Bochner Kernel | Toeplitz Energy | Parseval Projection</div>
</div>
<div class="stats-bar">
<div class="stat-box">Capital: <span id="capitalUI" class="stat-val">$100,000.00</span></div>
<div class="stat-box">Position: <span id="posUI" style="color:#8b949e; font-weight:600;">NONE</span></div>
<div class="stat-box">Realized P/L: <span id="plUI" class="stat-val">$0.00</span></div>
<div class="stat-box">Trades: <span id="tradesUI" class="stat-val" style="color:var(--gold);">0</span></div>
<div class="stat-box">Ticks Remaining: <span id="countdown" class="stat-val" style="color:var(--danger);">300</span></div>
</div>
<div id="chartContainer"></div>
<div id="footer">
<a href="https://github.com/jmullings/TheAnalystsProblem" target="_blank" class="int-link">GitHub</a>
<a href="https://www.youtube.com/@TheAnalystsProblem" target="_blank" class="int-link">YouTube</a>
<a href="https://www.amazon.com/s?k=%22The+analyst%E2%80%99s+problem%22" target="_blank" class="int-link">E-Book</a>
<a href="https://www.patreon.com/posts/jason-mullings-155411204" target="_blank" class="int-link">Patreon</a>
</div>
</div>
<script>
/**
* =========================================================
* CRYPTO HELPER FOR PROOFS (Browser Web Crypto API)
* =========================================================
*/
async function generateProofHash(tradeDataStr) {
const msgBuffer = new TextEncoder().encode(tradeDataStr);
const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
const hashArray = Array.from(new Uint8Array(hashBuffer));
return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
}
/**
* =========================================================
* DATA SOURCE LAYER
* =========================================================
*/
class DataSource { async init(){} next() { throw "Not implemented"; } }
// Mode 1: Pure Random Walk
class RandomMarket extends DataSource {
constructor(startPrice, vol) { super(); this.p = startPrice; this.v = vol; }
next() {
const open = this.p;
this.p = open + (Math.random() - 0.5) * this.v;
return { open: open, high: Math.max(open, this.p) + Math.random()*(this.v*0.5), low: Math.min(open, this.p) - Math.random()*(this.v*0.5), close: this.p };
}
}
// Mode 2: REAL DATA via Binance Public API (Keyless with Fallback)
class RealDataMarket extends DataSource {
constructor() {
super();
this.history = [];
this.currentIndex = 0;
this.fallbackPrice = 60000;
}
async init() {
try {
// Try official Binance API
const res = await fetch('https://api.binance.com/api/v3/klines?symbol=BTCUSDT&interval=1s&limit=500');
if (!res.ok) throw new Error("API Blocked");
const data = await res.json();
this.history = data.map(d => ({
open: parseFloat(d[1]), high: parseFloat(d[2]), low: parseFloat(d[3]), close: parseFloat(d[4])
}));
} catch (e) {
console.warn("Binance API Blocked (CORS/Geo). Falling back to simulated real-market data...", e);
// Generate realistic looking data if API fails to prevent crash
let p = 60000;
for (let i = 0; i < 500; i++) {
let noise = (Math.random() - 0.5) * 20;
let open = p;
p += noise;
this.history.push({ open: open, high: Math.max(open, p) + 5, low: Math.min(open, p) - 5, close: p });
}
}
}
next() {
if (this.currentIndex >= this.history.length) return this.history[this.history.length - 1];
return this.history[this.currentIndex++];
}
}
// Mode 3: SKIMMER ENGINE (100-Agent Simulation)
class FrequencyMarket extends DataSource {
constructor(startPrice) {
super();
this.skimmer = new ChartSkimmerEngine({ initialPrice: startPrice });
this.agents = Array.from({ length: 100 }, (_, i) => `Agent_${i.toString().padStart(3, '0')}`);
}
next() {
// Simulate crowd submitting orders
const tradesThisTick = Math.floor(Math.random() * 11) + 5;
const crowdBias = Math.random();
for(let i = 0; i < tradesThisTick; i++) {
const agent = this.agents[Math.floor(Math.random() * 100)];
let side = 'LONG';
if (crowdBias > 0.7) side = 'LONG';
else if (crowdBias < 0.3) side = 'SHORT';
else side = Math.random() > 0.5 ? 'LONG' : 'SHORT';
const margin = Math.floor(Math.random() * 4000) + 500;
const lev = Math.floor(Math.random() * 25) + 5;
this.skimmer.submitOrder(agent, side, margin, lev);
}
// Tick the skimmer to process imbalances and manipulate price
const oldPrice = this.skimmer.currentPrice;
this.skimmer.tick();
const newPrice = this.skimmer.currentPrice;
return {
open: oldPrice,
high: Math.max(oldPrice, newPrice) + (Math.random() * 5),
low: Math.min(oldPrice, newPrice) - (Math.random() * 5),
close: newPrice
};
}
}
/**
* =========================================================
* AUTONOMOUS SKIMMER TRADER (Adapted for Browser)
* =========================================================
*/
class ChartSkimmerEngine {
constructor(config = {}) {
this.currentPrice = config.initialPrice || 60000.0;
this.houseProfit = 0.0;
this.manipulationImpact = config.manipulationImpact || 0.015;
this.randomVolatility = config.randomVolatility || 0.001;
this.imbalanceThreshold = config.imbalanceThreshold || 250000;
this.activePositions = new Map();
this.totalLongExposure = 0;
this.totalShortExposure = 0;
this.tradeCounter = 0;
this.skimmer = {
positions: [],
maxPositions: 5,
maxNotional: 2000000,
tpPct: 0.015,
slPct: 0.01,
baseSizeUsd: 50000,
pnl: 0.0
};
}
submitOrder(agentId, side, marginUsdt, leverage = 10) {
this.tradeCounter++;
const positionId = `POS_${this.tradeCounter}`;
const positionSize = marginUsdt * leverage;
const liqDistance = this.currentPrice * (1 / leverage);
const liquidationPrice = side === 'LONG' ? this.currentPrice - liqDistance : this.currentPrice + liqDistance;
this.activePositions.set(positionId, { id: positionId, agentId, side, margin: marginUsdt, size: positionSize, liquidationPrice });
if (side === 'LONG') this.totalLongExposure += positionSize; else this.totalShortExposure += positionSize;
}
_skimmerOpen(side) {
if (this.skimmer.positions.length >= this.skimmer.maxPositions) return;
const tpPrice = side === 'LONG' ? this.currentPrice * (1 + this.skimmer.tpPct) : this.currentPrice * (1 - this.skimmer.tpPct);
const slPrice = side === 'LONG' ? this.currentPrice * (1 - this.skimmer.slPct) : this.currentPrice * (1 + this.skimmer.slPct);
this.skimmer.positions.push({ id: `SKIM_${Date.now()}`, side, entryPrice: this.currentPrice, sizeUsd: this.skimmer.baseSizeUsd, tpPrice, slPrice });
}
_skimmerManagePositions() {
const remaining = [];
for (const pos of this.skimmer.positions) {
let close = false, reason = '';
if (pos.side === 'LONG') {
if (this.currentPrice >= pos.tpPrice) { close = true; reason = 'TP'; }
else if (this.currentPrice <= pos.slPrice) { close = true; reason = 'SL'; }
} else {
if (this.currentPrice <= pos.tpPrice) { close = true; reason = 'TP'; }
else if (this.currentPrice >= pos.slPrice) { close = true; reason = 'SL'; }
}
if (close) {
const diff = pos.side === 'LONG' ? (this.currentPrice - pos.entryPrice) : (pos.entryPrice - this.currentPrice);
const pnl = (diff / pos.entryPrice) * pos.sizeUsd;
this.skimmer.pnl += pnl;
if (pnl > 0) {
const tradeData = { tradeId: pos.id, side: pos.side, entry: pos.entryPrice.toFixed(2), exit: this.currentPrice.toFixed(2), pnl: pnl.toFixed(2) };
const receiptStr = JSON.stringify(tradeData);
// Generate Hash Asynchronously and log
generateProofHash(receiptStr).then(hash => {
console.log(`%c🏆 [PROOF OF WIN GENERATED]`, 'color: #00e5ff; font-weight: bold;');
console.log(`Data: ${receiptStr}`);
console.log(`Hash: ${hash}\n`);
});
}
} else {
remaining.push(pos);
}
}
this.skimmer.positions = remaining;
}
tick() {
const netExposure = this.totalLongExposure - this.totalShortExposure;
let priceDelta = 0, skimmerSideToOpen = null;
if (netExposure > this.imbalanceThreshold) {
priceDelta = -(this.currentPrice * this.manipulationImpact);
skimmerSideToOpen = 'SHORT';
console.log(`%c⚠️ HOUSE ALERT: Heavy LONGS. Dropping price!`, 'color: #ff3333;');
} else if (netExposure < -this.imbalanceThreshold) {
priceDelta = (this.currentPrice * this.manipulationImpact);
skimmerSideToOpen = 'LONG';
console.log(`%c⚠️ HOUSE ALERT: Heavy SHORTS. Spiking price!`, 'color: #00e5ff;');
} else {
priceDelta = this.currentPrice * this.randomVolatility * ((Math.random() - 0.5) * 2);
}
this.currentPrice += priceDelta;
// Process liquidations
for (const [id, pos] of this.activePositions.entries()) {
let liq = false;
if (pos.side === 'LONG' && this.currentPrice <= pos.liquidationPrice) liq = true;
if (pos.side === 'SHORT' && this.currentPrice >= pos.liquidationPrice) liq = true;
if (liq) {
this.houseProfit += pos.margin;
if (pos.side === 'LONG') this.totalLongExposure -= pos.size; else this.totalShortExposure -= pos.size;
this.activePositions.delete(id);
}
}
this._skimmerManagePositions();
if (skimmerSideToOpen) this._skimmerOpen(skimmerSideToOpen);
}
}
/**
* =========================================================
* TAP CORE ENGINE (Volumes I - IV Authenticated)
* =========================================================
*/
class SigmaSelector {
constructor(N) { this.N = N; }
dynamicSigmaEstimate(marketVariance) {
let perturbation = Math.min(Math.max((marketVariance * 0.001) - 0.02, -0.05), 0.05);
return 0.5 + perturbation;
}
}
class Kernel {
constructor(H) { this.H = H; }
k(t) {
if (Math.abs(t / this.H) > 40) return 0.0;
const s = 1.0 / Math.cosh(t / this.H);
return (6.0 / (this.H * this.H)) * Math.pow(s, 4);
}
k_hat(xi) {
const pi = Math.PI, lam = 4.0 / (this.H * this.H);
const factor = Math.pow(2 * pi * xi, 2) + lam;
if (xi === 0) return factor * (this.H / pi);
const denom = Math.sinh(pi * pi * this.H * xi);
if (denom === 0) return 0.0;
return factor * (pi * this.H * this.H * xi / denom);
}
}
class TAPEngine {
constructor(config = {}) {
this.H = config.H || 3.0;
this.N = config.lookback || 30;
this.zetaZeros = [ 14.134725, 21.022040, 25.010857, 30.424876, 32.935061, 37.586178 ];
this.sigmaSelector = new SigmaSelector(this.N);
this.kernel = new Kernel(this.H);
this.priceHistory = [];
}
update(price) {
this.priceHistory.push(price);
if (this.priceHistory.length > this.N + 1) this.priceHistory.shift();
}
_getLogReturns() {
let lr = [];
for (let i = 1; i < this.priceHistory.length; i++) lr.push(Math.log(this.priceHistory[i] / this.priceHistory[i - 1]));
return lr;
}
_getLocalVariance(logReturns) {
if (logReturns.length === 0) return 0;
let m = logReturns.reduce((a, b) => a + b, 0) / logReturns.length;
return logReturns.reduce((a, b) => a + Math.pow(b - m, 2), 0) / logReturns.length;
}
computeToeplitzEnergy(logReturns, sigma) {
const N = logReturns.length;
if (N === 0) return 0;
let energy = 0;
for (let m = 1; m <= N; m++) {
for (let n = 1; n <= N; n++) {
const weight = Math.pow(m, -sigma) * Math.pow(n, -sigma) * this.kernel.k(Math.log(m) - Math.log(n));
energy += weight * logReturns[N - m] * logReturns[N - n];
}
}
return energy;
}
predict(steps = 100) {
const logReturns = this._getLogReturns();
const N = logReturns.length;
if (N === 0) return [];
const variance = this._getLocalVariance(logReturns);
const sigma = this.sigmaSelector.dynamicSigmaEstimate(variance);
const spectrum = [];
for (let i = 0; i < this.zetaZeros.length; i++) {
const gamma = this.zetaZeros[i];
let real = 0.0, imag = 0.0;
for (let n = 1; n <= N; n++) {
const r = logReturns[N - n];
const sigmaFactor = Math.pow(n, -sigma);
const phase = -gamma * Math.log(n);
const weight = this.kernel.k(Math.log(n) - Math.log(N));
const contrib = r * sigmaFactor * weight;
real += contrib * Math.cos(phase);
imag += contrib * Math.sin(phase);
}
const amplitude = Math.hypot(real, imag);
const phaseOffset = Math.atan2(imag, real);
const specWeight = this.kernel.k_hat(gamma);
spectrum.push({ gamma, amplitude, phaseOffset, weight: specWeight });
}
const predictions = [];
let simulatedPrice = this.priceHistory[this.priceHistory.length - 1];
for (let step = 1; step <= steps; step++) {
let projectedReturn = 0.0, totalW = 0.0;
for (let k = 0; k < spectrum.length; k++) {
const wave = spectrum[k];
const forwardPhase = wave.gamma * Math.log(N + step) - wave.phaseOffset;
projectedReturn += (wave.weight || 1.0) * wave.amplitude * Math.cos(forwardPhase);
totalW += (wave.weight || 1.0);
}
if (totalW > 0) projectedReturn /= totalW;
const dampFactor = Math.exp(-(12.0 / this.H) * Math.log(N + step) / N);
projectedReturn *= dampFactor * -35.0;
simulatedPrice = simulatedPrice * Math.exp(projectedReturn);
predictions.push(simulatedPrice);
}
return predictions;
}
getSignal() {
const logReturns = this._getLogReturns();
if (logReturns.length === 0) return { q_signal: 0, futurePrices: [] };
const variance = this._getLocalVariance(logReturns);
const sigma = this.sigmaSelector.dynamicSigmaEstimate(variance);
const F2 = this.computeToeplitzEnergy(logReturns, sigma);
const positivityMargin = F2 / (1 + variance);
const futurePrices = this.predict(20);
const currentPrice = this.priceHistory[this.priceHistory.length - 1];
const spectralDelta = futurePrices.length ? (futurePrices[19] - currentPrice) : 0;
const q_signal = (0.08 * spectralDelta) + (50000 * positivityMargin);
return { q_signal, futurePrices };
}
}
/**
* =========================================================
* UI CONTROLLER & PLOTLY ENGINE
* =========================================================
*/
let market, engineInterval;
const tapEngine = new TAPEngine({ H: 3.0, lookback: 30 });
const THRESHOLD = 1.0;
let capital = 100000, position = 0, entryPrice = 0, shares = 0, realizedPL = 0, totalTrades = 0;
const uiCap = document.getElementById('capitalUI');
const uiPos = document.getElementById('posUI');
const uiPL = document.getElementById('plUI');
const uiTrades = document.getElementById('tradesUI');
function formatMoney(n) { return (n >= 0 ? "+$" : "-$") + Math.abs(n).toLocaleString('en-US', {minimumFractionDigits: 2, maximumFractionDigits: 2}); }
function executeTrade(signal, price) {
if (signal === 'BUY' && position <= 0) {
if (position === -1) { let profit = (entryPrice - price) * shares; realizedPL += profit; capital += profit; }
position = 1; entryPrice = price; shares = capital / price; totalTrades++;
uiPos.innerHTML = "<span class='pos-profit pulse'>LONG</span>";
return price - 25;
} else if (signal === 'SELL' && position >= 0) {
if (position === 1) { let profit = (price - entryPrice) * shares; realizedPL += profit; capital += profit; }
position = -1; entryPrice = price; shares = capital / price; totalTrades++;
uiPos.innerHTML = "<span class='neg-profit pulse'>SHORT</span>";
return price + 25;
}
return null;
}
function updateTradeUI() {
uiCap.innerText = capital.toLocaleString('en-US', {minimumFractionDigits: 2, maximumFractionDigits: 2});
uiPL.innerText = formatMoney(realizedPL);
uiPL.className = realizedPL >= 0 ? "pos-profit" : "neg-profit";
uiTrades.innerText = totalTrades;
}
// START BUTTON HANDLER
async function startEngine(mode) {
document.getElementById('loading-text').style.display = 'block';
if(mode === 'random') {
market = new RandomMarket(60000, 20);
}
else if(mode === 'real') {
market = new RealDataMarket();
await market.init();
}
else if(mode === 'frequency') {
market = new FrequencyMarket(60000);
console.log("=== FREQUENCY TRADER INITIALIZED ===");
console.log("Check console for Proof-of-Win Hashes generated from Skimmer trades.");
}
document.getElementById('startModal').style.display = 'none';
// Prefill 30 periods
let currentTime = new Date();
currentTime.setSeconds(currentTime.getSeconds() - 30);
let dates = [], opens = [], highs = [], lows = [], closes = [];
let tapSignals = [], threshUp = [], threshDown = [];
let buy_y = [], sell_y = [];
for (let i = 0; i < 30; i++) {
currentTime.setSeconds(currentTime.getSeconds() + 1);
let tick = market.next();
dates.push(currentTime.toISOString().replace('T', ' ').split('.')[0]);
opens.push(tick.open); highs.push(tick.high); lows.push(tick.low); closes.push(tick.close);
tapEngine.update(tick.close);
let sigData = tapEngine.getSignal();
tapSignals.push(sigData.q_signal);
threshUp.push(THRESHOLD); threshDown.push(-THRESHOLD);
buy_y.push(null); sell_y.push(null);
}
// PLOTLY SETUP
const traceCandles = { x: dates.slice(), open: opens.slice(), high: highs.slice(), low: lows.slice(), close: closes.slice(), type: 'candlestick', xaxis: 'x', yaxis: 'y', name: 'Price', increasing: { line: { color: '#00e5ff' } }, decreasing: { line: { color: '#ff3333' } } };
const traceBuys = { x: dates.slice(), y: buy_y.slice(), mode: 'markers', type: 'scatter', name: 'Engine Buy', marker: { symbol: 'triangle-up', color: '#00e5ff', size: 14, line: {color: '#ffffff', width: 1} }, xaxis: 'x', yaxis: 'y' };
const traceSells = { x: dates.slice(), y: sell_y.slice(), mode: 'markers', type: 'scatter', name: 'Engine Sell', marker: { symbol: 'triangle-down', color: '#ff3333', size: 14, line: {color: '#ffffff', width: 1} }, xaxis: 'x', yaxis: 'y' };
const traceProjection = { x: [], y: [], type: 'scatter', mode: 'lines', name: 'Spectral Projection', line: { color: '#ffc400', width: 3, shape: 'spline', smoothing: 1.3 }, xaxis: 'x', yaxis: 'y' };
const traceTAPSignal = { x: dates.slice(), y: tapSignals.slice(), type: 'scatter', mode: 'lines', name: 'Unified Signal', line: { color: '#ffc400', width: 2 }, xaxis: 'x', yaxis: 'y2', fill: 'tozeroy', fillcolor: 'rgba(255, 196, 0, 0.1)' };
const traceUpper = { x: dates.slice(), y: threshUp.slice(), type: 'scatter', mode: 'lines', name: 'Short Threshold', line: { color: '#a0aab5', dash: 'dash', width: 1 }, xaxis: 'x', yaxis: 'y2', hoverinfo: 'none' };
const traceLower = { x: dates.slice(), y: threshDown.slice(), type: 'scatter', mode: 'lines', name: 'Long Threshold', line: { color: '#a0aab5', dash: 'dash', width: 1 }, xaxis: 'x', yaxis: 'y2', hoverinfo: 'none' };
const layout = {
plot_bgcolor: '#05070e', paper_bgcolor: '#05070e', font: { color: '#e0e6ed', family: 'JetBrains Mono' },
margin: { t: 30, b: 30, l: 60, r: 40 }, showlegend: false, hovermode: 'x unified',
xaxis: { type: 'date', anchor: 'y2', rangeslider: { visible: false }, showgrid: true, gridcolor: 'rgba(255,255,255,0.05)', zeroline: false, showspikes: true, spikemode: 'across', spikecolor: '#a0aab5', spikethickness: 1 },
yaxis: { domain: [0.42, 1], showgrid: true, gridcolor: 'rgba(255,255,255,0.05)', tickformat: ',.0f', title: 'Asset Price' },
yaxis2: { domain: [0, 0.38], showgrid: true, gridcolor: 'rgba(255,255,255,0.05)', title: 'Unified Signal (Q)' }
};
Plotly.newPlot('chartContainer', [traceCandles, traceBuys, traceSells, traceProjection, traceTAPSignal, traceUpper, traceLower], layout, { responsive: true, displayModeBar: false });
// MAIN EXECUTION LOOP (125ms ticks)
const MAX_TICKS = 300;
let loopCount = 0;
let countdownEl = document.getElementById('countdown');
engineInterval = setInterval(() => {
loopCount++;
countdownEl.innerText = (MAX_TICKS - loopCount);
currentTime.setSeconds(currentTime.getSeconds() + 1);
let newTimeStr = currentTime.toISOString().replace('T', ' ').split('.')[0];
let newTick = market.next();
closes.push(newTick.close);
tapEngine.update(newTick.close);
let sigData = tapEngine.getSignal();
let q_signal = sigData.q_signal;
let futurePrices = tapEngine.predict(100);
let targetDates = [];
let targetPrices = [];
let lookback = Math.min(10, closes.length - 1);
for(let i = lookback; i > 0; i--) {
let pt = new Date(currentTime.getTime() - i * 1000);
targetDates.push(pt.toISOString().replace('T', ' ').split('.')[0]);
targetPrices.push(closes[closes.length - 1 - i]);
}
targetDates.push(newTimeStr);
targetPrices.push(newTick.close);
let targetTime = new Date(currentTime);
for(let i=0; i < futurePrices.length; i++){
targetTime.setSeconds(targetTime.getSeconds() + 1);
targetDates.push(targetTime.toISOString().replace('T', ' ').split('.')[0]);
targetPrices.push(futurePrices[i]);
}
let tradeAction = null, buyMarker = null, sellMarker = null;
// SKIMMER / MEAN-REVERSION FIX:
// High Q-Signal (overbought peak) -> SHORT (SELL)
// Low Q-Signal (oversold trough) -> LONG (BUY)
if (q_signal > THRESHOLD) tradeAction = 'SELL';
else if (q_signal < -THRESHOLD) tradeAction = 'BUY';
if (tradeAction) {
let markerPrice = executeTrade(tradeAction, newTick.close);
if (tradeAction === 'BUY') buyMarker = markerPrice;
if (tradeAction === 'SELL') sellMarker = markerPrice;
}
updateTradeUI();
Plotly.extendTraces('chartContainer', { x: [[newTimeStr]], open: [[newTick.open]], high: [[newTick.high]], low: [[newTick.low]], close: [[newTick.close]] }, [0]);
Plotly.extendTraces('chartContainer', { x: [[newTimeStr], [newTimeStr], [newTimeStr], [newTimeStr], [newTimeStr]], y: [[buyMarker], [sellMarker], [q_signal], [THRESHOLD], [-THRESHOLD]] }, [1, 2, 4, 5, 6]);
Plotly.update('chartContainer', { x: [targetDates], y: [targetPrices] }, {}, [3]);
let viewStart = new Date(currentTime.getTime() - 40 * 1000).toISOString().replace('T', ' ').split('.')[0];
let viewEnd = new Date(currentTime.getTime() + 105 * 1000).toISOString().replace('T', ' ').split('.')[0];
Plotly.relayout('chartContainer', { 'xaxis.range': [viewStart, viewEnd] });
if (loopCount >= MAX_TICKS) {
clearInterval(engineInterval);
if(position !== 0) {
let profit = (position === 1) ? (newTick.close - entryPrice)*shares : (entryPrice - newTick.close)*shares;
realizedPL += profit; capital += profit; position = 0;
updateTradeUI();
uiPos.innerHTML = "<span style='color:#8b949e'>CLOSED</span>";
}
let finalPLText = document.getElementById('finalPLDisplay');
finalPLText.innerText = `Mathematical Profit: ${formatMoney(realizedPL)}`;
finalPLText.className = realizedPL >= 0 ? "pos-profit" : "neg-profit";
document.getElementById('endModal').style.display = 'flex';
}
}, 125);
}
</script>
</body>
</html>