-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiv_surface_institutional_v2.html
More file actions
741 lines (661 loc) · 28.2 KB
/
Copy pathiv_surface_institutional_v2.html
File metadata and controls
741 lines (661 loc) · 28.2 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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>IV Surface | PistonIV Institutional Analytics</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: #06060a;
color: #d4d4d8;
overflow: hidden;
}
#container { width: 100vw; height: 100vh; }
.panel {
position: absolute;
background: rgba(8, 8, 12, 0.94);
border: 1px solid #27272a;
font-size: 12px;
backdrop-filter: blur(12px);
}
#header {
top: 0; left: 0; right: 0;
padding: 14px 24px;
display: flex;
justify-content: space-between;
align-items: center;
border-bottom: 1px solid #27272a;
background: rgba(8, 8, 12, 0.98);
}
#header .title {
font-size: 14px;
font-weight: 600;
color: #fafafa;
letter-spacing: 1.5px;
}
#header .model-badge {
background: linear-gradient(135deg, #3b82f6, #8b5cf6);
padding: 4px 12px;
border-radius: 3px;
font-size: 10px;
font-weight: 600;
margin-left: 12px;
color: white;
}
#header .timestamp {
font-size: 10px;
color: #52525b;
font-family: 'SF Mono', Monaco, 'Courier New', monospace;
}
#metrics {
top: 60px; left: 20px;
padding: 20px; width: 260px;
border-radius: 4px;
}
#metrics h3, #model-stats h3 {
font-size: 9px;
font-weight: 700;
color: #71717a;
text-transform: uppercase;
letter-spacing: 1.2px;
margin-bottom: 16px;
padding-bottom: 12px;
border-bottom: 1px solid #27272a;
}
.metric-row {
display: flex;
justify-content: space-between;
padding: 10px 0;
border-bottom: 1px solid rgba(39, 39, 42, 0.4);
}
.metric-row:last-child { border-bottom: none; }
.metric-label { color: #a1a1aa; font-size: 12px; }
.metric-value {
color: #fafafa;
font-family: 'SF Mono', Monaco, 'Courier New', monospace;
font-weight: 600; font-size: 12px;
}
.metric-value.positive { color: #22c55e; }
.metric-value.warn { color: #eab308; }
#model-stats {
top: 380px; left: 20px;
padding: 20px; width: 260px;
border-radius: 4px;
}
.stat-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px; margin-top: 10px;
}
.stat-item {
background: rgba(24, 24, 27, 0.8);
padding: 12px; border-radius: 4px;
border: 1px solid #27272a;
}
.stat-item .label {
font-size: 8px; color: #71717a;
text-transform: uppercase;
letter-spacing: 0.8px; font-weight: 600;
}
.stat-item .value {
font-size: 15px; font-weight: 700;
color: #fafafa;
font-family: 'SF Mono', Monaco, 'Courier New', monospace;
margin-top: 4px;
}
.stat-item .value.best { color: #22c55e; }
#legend-panel {
bottom: 20px; left: 20px;
padding: 14px 18px; border-radius: 4px;
}
.legend-title {
color: #71717a; font-size: 9px;
text-transform: uppercase;
letter-spacing: 1px; margin-bottom: 10px; font-weight: 600;
}
.legend-bar {
width: 220px; height: 14px;
background: linear-gradient(90deg,
#1e3a8a 0%, #0891b2 18%, #059669 36%,
#84cc16 50%, #eab308 64%, #f97316 82%, #dc2626 100%
);
border-radius: 3px; border: 1px solid #27272a;
}
.legend-labels {
display: flex; justify-content: space-between;
width: 220px; font-size: 9px;
color: #71717a; margin-top: 6px;
font-family: 'SF Mono', Monaco, 'Courier New', monospace;
}
.legend-items { margin-top: 14px; border-top: 1px solid #27272a; padding-top: 12px; }
.legend-item {
display: flex; align-items: center;
gap: 10px; padding: 4px 0; font-size: 10px; color: #a1a1aa;
}
.legend-sphere {
width: 12px; height: 12px; border-radius: 50%;
border: 2px solid;
}
.legend-line { width: 20px; height: 3px; border-radius: 1px; }
#controls-panel {
bottom: 20px; right: 20px;
padding: 10px; display: flex;
gap: 8px; border-radius: 4px;
}
.ctrl-btn {
background: rgba(24, 24, 27, 0.8);
border: 1px solid #27272a;
color: #a1a1aa; padding: 10px 14px;
font-size: 11px; cursor: pointer;
transition: all 0.15s; font-family: inherit;
border-radius: 3px; font-weight: 500;
}
.ctrl-btn:hover { background: #27272a; color: #fafafa; border-color: #3f3f46; }
.ctrl-btn.active { background: #3b82f6; border-color: #3b82f6; color: #fff; }
#hover-display {
position: absolute;
bottom: 80px; left: 50%;
transform: translateX(-50%);
background: rgba(8, 8, 12, 0.96);
border: 1px solid #3b82f6;
padding: 14px 24px;
font-family: 'SF Mono', Monaco, 'Courier New', monospace;
font-size: 12px; display: none;
border-radius: 4px;
box-shadow: 0 4px 24px rgba(0,0,0,0.5);
}
#hover-display span { margin: 0 12px; color: #71717a; }
#hover-display .val { color: #fafafa; font-weight: 600; }
#hover-display .spread { color: #eab308; font-size: 10px; }
.axis-label {
position: absolute;
color: #a1a1aa;
font-family: 'SF Mono', Monaco, 'Courier New', monospace;
font-size: 11px;
font-weight: 600;
pointer-events: none;
text-shadow: 0 0 8px rgba(0,0,0,0.8);
}
</style>
</head>
<body>
<div id="container"></div>
<div id="header" class="panel">
<div style="display: flex; align-items: center;">
<span class="title">IMPLIED VOLATILITY SURFACE</span>
<span class="model-badge">PistonIV RBF</span>
</div>
<span class="timestamp">SPY 2024-10-15 15:55 EST | ThetaData Feed</span>
</div>
<div id="metrics" class="panel">
<h3>Market Parameters</h3>
<div class="metric-row">
<span class="metric-label">Underlying</span>
<span class="metric-value">SPY</span>
</div>
<div class="metric-row">
<span class="metric-label">Spot Reference</span>
<span class="metric-value">$580.64</span>
</div>
<div class="metric-row">
<span class="metric-label">ATM IV (30d)</span>
<span class="metric-value">16.18%</span>
</div>
<div class="metric-row">
<span class="metric-label">Realized Vol (30d)</span>
<span class="metric-value">13.14%</span>
</div>
<div class="metric-row">
<span class="metric-label">Variance Premium</span>
<span class="metric-value positive">+3.04 pts</span>
</div>
<div class="metric-row">
<span class="metric-label">Contracts Fitted</span>
<span class="metric-value">146</span>
</div>
<div class="metric-row">
<span class="metric-label">Strike Range</span>
<span class="metric-value">$260 - $620</span>
</div>
<div class="metric-row">
<span class="metric-label">Avg Bid-Ask Spread</span>
<span class="metric-value warn">$0.42</span>
</div>
</div>
<div id="model-stats" class="panel">
<h3>Model Fit Quality</h3>
<div class="stat-grid">
<div class="stat-item">
<div class="label">RMSE</div>
<div class="value best">0.09%</div>
</div>
<div class="stat-item">
<div class="label">R-Squared</div>
<div class="value best">0.9998</div>
</div>
<div class="stat-item">
<div class="label">Max Error</div>
<div class="value">1.27%</div>
</div>
<div class="stat-item">
<div class="label">Fit Latency</div>
<div class="value">1.81ms</div>
</div>
</div>
<div style="margin-top: 14px; padding-top: 14px; border-top: 1px solid #27272a;">
<div class="metric-row">
<span class="metric-label">vs SVI5 RMSE</span>
<span class="metric-value positive">70x better</span>
</div>
<div class="metric-row">
<span class="metric-label">vs SVI5 Chi-sq</span>
<span class="metric-value positive">6734x better</span>
</div>
</div>
</div>
<div id="legend-panel" class="panel">
<div class="legend-title">Implied Volatility Scale</div>
<div class="legend-bar"></div>
<div class="legend-labels">
<span>14%</span>
<span>45%</span>
<span>90%+</span>
</div>
<div class="legend-items">
<div class="legend-item">
<div class="legend-sphere" style="background: rgba(59,130,246,0.3); border-color: #3b82f6;"></div>
<span>Market Quote (with bid-ask)</span>
</div>
<div class="legend-item">
<div class="legend-line" style="background: linear-gradient(90deg, #3b82f6, #8b5cf6);"></div>
<span>RBF Fitted Surface</span>
</div>
</div>
</div>
<div id="controls-panel" class="panel">
<button class="ctrl-btn active" onclick="toggleMarketPoints()">Market Pts</button>
<button class="ctrl-btn active" onclick="toggleBidAsk()">Bid-Ask</button>
<button class="ctrl-btn active" onclick="toggleGrid()">Grid</button>
<button class="ctrl-btn" onclick="toggleRotate()">Rotate</button>
<button class="ctrl-btn" onclick="resetView()">Reset</button>
</div>
<div id="hover-display">
<span>Moneyness: <span class="val" id="hv-moneyness">-</span></span>
<span>DTE: <span class="val" id="hv-dte">-</span></span>
<span>IV: <span class="val" id="hv-iv">-</span></span>
</div>
<script type="importmap">
{
"imports": {
"three": "https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.module.js",
"three/addons/": "https://cdn.jsdelivr.net/npm/three@0.160.0/examples/jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { FontLoader } from 'three/addons/loaders/FontLoader.js';
import { TextGeometry } from 'three/addons/geometries/TextGeometry.js';
const config = {
spot: 580.64,
maturities: [7, 14, 30, 60, 90, 120, 180], // DTE (Days to Expiration)
moneynessRange: [0.75, 1.25] // K/S range
};
// Base IV smile from actual RBF data (30 DTE)
const baseSmile = [
{ m: 0.70, iv: 0.55 }, { m: 0.75, iv: 0.47 }, { m: 0.80, iv: 0.40 },
{ m: 0.85, iv: 0.33 }, { m: 0.90, iv: 0.27 }, { m: 0.95, iv: 0.20 },
{ m: 0.98, iv: 0.17 }, { m: 1.00, iv: 0.162 }, { m: 1.02, iv: 0.155 },
{ m: 1.05, iv: 0.150 }, { m: 1.10, iv: 0.158 }, { m: 1.15, iv: 0.175 },
{ m: 1.20, iv: 0.198 }, { m: 1.25, iv: 0.23 }, { m: 1.30, iv: 0.27 }
];
// Market data at T=0.082 (30 DTE)
const marketData = [
{ m: 0.448, iv: 0.901, bid: 0.02, ask: 0.03 },
{ m: 0.517, iv: 0.814, bid: 0.06, ask: 0.07 },
{ m: 0.620, iv: 0.665, bid: 0.16, ask: 0.17 },
{ m: 0.689, iv: 0.568, bid: 0.28, ask: 0.29 },
{ m: 0.758, iv: 0.473, bid: 0.49, ask: 0.50 },
{ m: 0.827, iv: 0.370, bid: 0.77, ask: 0.78 },
{ m: 0.861, iv: 0.321, bid: 1.03, ask: 1.04 },
{ m: 0.896, iv: 0.276, bid: 1.53, ask: 1.54 },
{ m: 0.930, iv: 0.235, bid: 2.57, ask: 2.59 },
{ m: 0.956, iv: 0.207, bid: 4.10, ask: 4.13 },
{ m: 0.982, iv: 0.179, bid: 6.87, ask: 6.90 },
{ m: 0.999, iv: 0.162, bid: 9.95, ask: 9.99 },
{ m: 1.008, iv: 0.154, bid: 12.04, ask: 12.10 },
{ m: 1.016, iv: 0.147, bid: 14.52, ask: 14.71 },
{ m: 1.033, iv: 0.142, bid: 21.23, ask: 21.71 },
{ m: 1.051, iv: 0.148, bid: 25.52, ask: 26.02 },
{ m: 1.068, iv: 0.198, bid: 40.34, ask: 40.77 },
];
// Interpolate base smile
function getBaseIV(moneyness) {
for (let i = 0; i < baseSmile.length - 1; i++) {
if (moneyness >= baseSmile[i].m && moneyness <= baseSmile[i+1].m) {
const t = (moneyness - baseSmile[i].m) / (baseSmile[i+1].m - baseSmile[i].m);
return baseSmile[i].iv + t * (baseSmile[i+1].iv - baseSmile[i].iv);
}
}
if (moneyness < baseSmile[0].m) return baseSmile[0].iv;
return baseSmile[baseSmile.length-1].iv;
}
// Generate IV with term structure (shorter = higher vol, steeper smile)
function getIV(moneyness, dte) {
const baseIV = getBaseIV(moneyness);
const atmIV = 0.162;
// Term structure: sqrt scaling for ATM, steeper smile for short-dated
const termFactor = Math.sqrt(30 / Math.max(dte, 1));
const smileStretching = 1 + 0.4 * (30 / Math.max(dte, 1) - 1);
// ATM level adjusts with term structure
const atmAdjusted = atmIV * Math.pow(termFactor, 0.25);
// Smile shape - wings stretch more for short-dated
const deviation = baseIV - atmIV;
const adjustedDev = deviation * Math.min(smileStretching, 2.5);
return Math.max(0.10, Math.min(0.85, atmAdjusted + adjustedDev));
}
// Three.js setup
const container = document.getElementById('container');
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x06060a);
const camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(7, 5, 7);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
container.appendChild(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.05;
controls.minDistance = 4;
controls.maxDistance = 20;
controls.target.set(0, 0.5, 0);
// Lighting
scene.add(new THREE.AmbientLight(0x404050, 0.5));
const dirLight1 = new THREE.DirectionalLight(0xffffff, 1.0);
dirLight1.position.set(5, 10, 5);
scene.add(dirLight1);
const dirLight2 = new THREE.DirectionalLight(0x6688ff, 0.4);
dirLight2.position.set(-5, 5, -5);
scene.add(dirLight2);
// Rainbow color function
function getColor(iv) {
const t = Math.max(0, Math.min(1, (iv - 0.12) / (0.55 - 0.12)));
const colors = [
[30, 58, 138], [8, 145, 178], [5, 150, 105],
[132, 204, 22], [234, 179, 8], [249, 115, 22], [220, 38, 38]
];
const idx = t * (colors.length - 1);
const i = Math.floor(idx);
const f = idx - i;
const c1 = colors[Math.min(i, colors.length - 1)];
const c2 = colors[Math.min(i + 1, colors.length - 1)];
return new THREE.Color(
(c1[0] + (c2[0] - c1[0]) * f) / 255,
(c1[1] + (c2[1] - c1[1]) * f) / 255,
(c1[2] + (c2[2] - c1[2]) * f) / 255
);
}
// Scale factors
const scaleM = 5; // Moneyness axis
const scaleT = 4; // Time axis
const scaleIV = 5; // IV axis
function mToX(m) { return (m - 1.0) * scaleM; }
function dteToZ(dte) { return (dte - 90) / 90 * scaleT; }
function ivToY(iv) { return (iv - 0.30) * scaleIV; }
// === BUILD 3D SURFACE ===
const numM = 50; // Moneyness resolution
const numT = config.maturities.length;
const geometry = new THREE.BufferGeometry();
const vertices = [], colors = [], indices = [];
for (let ti = 0; ti < numT; ti++) {
const T = config.maturities[ti];
for (let mi = 0; mi < numM; mi++) {
const m = config.moneynessRange[0] + (config.moneynessRange[1] - config.moneynessRange[0]) * mi / (numM - 1);
const iv = getIV(m, T);
vertices.push(mToX(m), ivToY(iv), dteToZ(T));
const color = getColor(iv);
colors.push(color.r, color.g, color.b);
}
}
// Create indices for triangles
for (let ti = 0; ti < numT - 1; ti++) {
for (let mi = 0; mi < numM - 1; mi++) {
const a = ti * numM + mi;
const b = a + 1;
const c = a + numM;
const d = c + 1;
indices.push(a, b, c, b, d, c);
}
}
geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3));
geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3));
geometry.setIndex(indices);
geometry.computeVertexNormals();
const surfaceMat = new THREE.MeshPhongMaterial({
vertexColors: true,
side: THREE.DoubleSide,
shininess: 50,
transparent: true,
opacity: 0.9
});
const surfaceMesh = new THREE.Mesh(geometry, surfaceMat);
scene.add(surfaceMesh);
// === GRID LINES ===
const gridGroup = new THREE.Group();
// Moneyness lines (constant T)
for (const T of config.maturities) {
const points = [];
for (let mi = 0; mi < numM; mi++) {
const m = config.moneynessRange[0] + (config.moneynessRange[1] - config.moneynessRange[0]) * mi / (numM - 1);
const iv = getIV(m, T);
points.push(new THREE.Vector3(mToX(m), ivToY(iv) + 0.01, dteToZ(T)));
}
const lineGeom = new THREE.BufferGeometry().setFromPoints(points);
gridGroup.add(new THREE.Line(lineGeom, new THREE.LineBasicMaterial({
color: 0xffffff, transparent: true, opacity: 0.2
})));
}
// Time lines (constant moneyness)
for (let mi = 0; mi <= 10; mi++) {
const m = config.moneynessRange[0] + (config.moneynessRange[1] - config.moneynessRange[0]) * mi / 10;
const points = [];
for (const T of config.maturities) {
const iv = getIV(m, T);
points.push(new THREE.Vector3(mToX(m), ivToY(iv) + 0.01, dteToZ(T)));
}
const lineGeom = new THREE.BufferGeometry().setFromPoints(points);
gridGroup.add(new THREE.Line(lineGeom, new THREE.LineBasicMaterial({
color: 0xffffff, transparent: true, opacity: 0.2
})));
}
scene.add(gridGroup);
// === MARKET DATA POINTS (at 30 DTE) ===
const marketGroup = new THREE.Group();
const bidAskGroup = new THREE.Group();
const marketDTE = 30;
marketData.forEach(point => {
const x = mToX(point.m);
const y = ivToY(point.iv);
const z = dteToZ(marketDTE);
const sphereGeom = new THREE.SphereGeometry(0.06, 16, 16);
const sphereMat = new THREE.MeshPhongMaterial({
color: 0x3b82f6,
transparent: true,
opacity: 0.85,
emissive: 0x3b82f6,
emissiveIntensity: 0.3
});
const sphere = new THREE.Mesh(sphereGeom, sphereMat);
sphere.position.set(x, y, z);
sphere.userData = { ...point, dte: marketDTE };
marketGroup.add(sphere);
// Bid-ask spread
const spread = point.ask - point.bid;
const spreadPct = spread / ((point.bid + point.ask) / 2);
const spreadHeight = Math.min(spreadPct * 2, 0.15);
const bidAskGeom = new THREE.CylinderGeometry(0.012, 0.012, spreadHeight, 8);
const bidAskMat = new THREE.MeshBasicMaterial({ color: 0xeab308, transparent: true, opacity: 0.7 });
const bidAskBar = new THREE.Mesh(bidAskGeom, bidAskMat);
bidAskBar.position.set(x, y, z);
bidAskGroup.add(bidAskBar);
});
scene.add(marketGroup);
scene.add(bidAskGroup);
// === AXES ===
const axisGroup = new THREE.Group();
const axisMat = new THREE.LineBasicMaterial({ color: 0x707070 });
// X axis (Moneyness)
const xAxisGeom = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(mToX(0.75), -0.7, dteToZ(7)),
new THREE.Vector3(mToX(1.25), -0.7, dteToZ(7))
]);
axisGroup.add(new THREE.Line(xAxisGeom, axisMat));
// Y axis (IV)
const yAxisGeom = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(mToX(0.75), -0.7, dteToZ(7)),
new THREE.Vector3(mToX(0.75), ivToY(0.60), dteToZ(7))
]);
axisGroup.add(new THREE.Line(yAxisGeom, axisMat));
// Z axis (DTE)
const zAxisGeom = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(mToX(0.75), -0.7, dteToZ(7)),
new THREE.Vector3(mToX(0.75), -0.7, dteToZ(180))
]);
axisGroup.add(new THREE.Line(zAxisGeom, axisMat));
scene.add(axisGroup);
// === TEXT LABELS (using sprites) ===
function createTextSprite(text, fontSize = 56, color = '#c0c0c0') {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = 512;
canvas.height = 128;
ctx.fillStyle = 'rgba(0,0,0,0)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.font = `bold ${fontSize}px Arial, sans-serif`;
ctx.fillStyle = color;
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(text, canvas.width/2, canvas.height/2);
const texture = new THREE.CanvasTexture(canvas);
const material = new THREE.SpriteMaterial({ map: texture, transparent: true });
const sprite = new THREE.Sprite(material);
sprite.scale.set(2.0, 0.5, 1);
return sprite;
}
// Moneyness axis label (X)
const mLabel = createTextSprite('Moneyness (K/S)', 52, '#ffffff');
mLabel.position.set(0, -1.1, dteToZ(7) - 0.6);
mLabel.scale.set(2.5, 0.6, 1);
scene.add(mLabel);
// Moneyness tick labels
[0.80, 0.90, 1.00, 1.10, 1.20].forEach(m => {
const tick = createTextSprite(m.toFixed(2), 44);
tick.position.set(mToX(m), -0.85, dteToZ(7) - 0.3);
tick.scale.set(1.0, 0.25, 1);
scene.add(tick);
});
// IV axis label (Y)
const ivLabel = createTextSprite('IV (%)', 52, '#ffffff');
ivLabel.position.set(mToX(0.75) - 0.8, ivToY(0.45), dteToZ(7));
ivLabel.scale.set(1.5, 0.4, 1);
scene.add(ivLabel);
// IV tick labels
[15, 25, 35, 45, 55].forEach(ivPct => {
const tick = createTextSprite(ivPct + '%', 40);
tick.position.set(mToX(0.75) - 0.5, ivToY(ivPct/100), dteToZ(7));
tick.scale.set(0.9, 0.22, 1);
scene.add(tick);
});
// DTE axis label (Z)
const dteLabel = createTextSprite('DTE (days)', 52, '#ffffff');
dteLabel.position.set(mToX(0.75) - 0.6, -1.1, dteToZ(90));
dteLabel.scale.set(2.0, 0.5, 1);
scene.add(dteLabel);
// DTE tick labels
[7, 30, 60, 90, 120, 180].forEach(dte => {
const tick = createTextSprite(dte + 'd', 40);
tick.position.set(mToX(0.75) - 0.35, -0.85, dteToZ(dte));
tick.scale.set(0.8, 0.2, 1);
scene.add(tick);
});
// ATM line
const atmLineGeom = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(mToX(1.0), -0.7, dteToZ(7)),
new THREE.Vector3(mToX(1.0), -0.7, dteToZ(180))
]);
scene.add(new THREE.Line(atmLineGeom, new THREE.LineBasicMaterial({
color: 0x3b82f6, transparent: true, opacity: 0.5
})));
// Floor grid
const floorGrid = new THREE.GridHelper(10, 20, 0x222228, 0x151518);
floorGrid.position.y = -0.7;
scene.add(floorGrid);
// Animation
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
animate();
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
// Controls
let marketVisible = true, bidAskVisible = true, gridVisible = true;
window.toggleMarketPoints = () => {
marketVisible = !marketVisible;
marketGroup.visible = marketVisible;
document.querySelectorAll('.ctrl-btn')[0].classList.toggle('active', marketVisible);
};
window.toggleBidAsk = () => {
bidAskVisible = !bidAskVisible;
bidAskGroup.visible = bidAskVisible;
document.querySelectorAll('.ctrl-btn')[1].classList.toggle('active', bidAskVisible);
};
window.toggleGrid = () => {
gridVisible = !gridVisible;
gridGroup.visible = gridVisible;
document.querySelectorAll('.ctrl-btn')[2].classList.toggle('active', gridVisible);
};
window.toggleRotate = () => {
controls.autoRotate = !controls.autoRotate;
controls.autoRotateSpeed = 0.4;
document.querySelectorAll('.ctrl-btn')[3].classList.toggle('active', controls.autoRotate);
};
window.resetView = () => {
camera.position.set(7, 5, 7);
controls.target.set(0, 0.5, 0);
controls.autoRotate = false;
document.querySelectorAll('.ctrl-btn')[3].classList.remove('active');
};
// Hover detection
const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();
const hoverDisplay = document.getElementById('hover-display');
renderer.domElement.addEventListener('mousemove', (e) => {
mouse.x = (e.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(e.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
const hits = raycaster.intersectObjects([surfaceMesh, ...marketGroup.children]);
if (hits.length > 0) {
const p = hits[0].point;
const m = (p.x / scaleM + 1.0).toFixed(2);
const dte = Math.round(p.z / scaleT * 90 + 90);
const iv = ((p.y / scaleIV + 0.30) * 100).toFixed(1);
document.getElementById('hv-moneyness').textContent = m;
document.getElementById('hv-dte').textContent = dte + ' days';
document.getElementById('hv-iv').textContent = iv + '%';
hoverDisplay.style.display = 'block';
} else {
hoverDisplay.style.display = 'none';
}
});
</script>
</body>
</html>