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
|
"use client";
import { useMemo } from "react";
import { bsPrice, bsGreeks, bsSynthIV } from "@/lib/blackScholes";
import type { Expiry, OptionType } from "./types";
interface PolarSmileProps {
S: number;
K: number;
T: number;
r: number;
q: number;
atmSigma: number;
type: OptionType;
expiries: Expiry[];
selectedT: number;
onPickT: (T: number) => void;
}
export function PolarSmile({ S, K, T, r, q, atmSigma, type, expiries, selectedT, onPickT }: PolarSmileProps) {
const W = 680, H = 680;
const cx = W / 2, cy = H / 2;
const rOuter = 240;
const eyeR = 64;
// Strike range ±15% moneyness
const N = 48;
const kMin = S * 0.85;
const kMax = S * 1.15;
function strikeAt(i: number): number {
return kMin + (i / N) * (kMax - kMin);
}
// Piecewise angle: puts descend LEFT (counterclockwise), calls descend RIGHT (clockwise).
// ATM → top (−π/2); both wings meet at BOTTOM (−3π/2 ≡ +π/2). Full closed lens.
function angleFor(K_: number): number {
if (K_ <= S) {
const t = (S - K_) / (S - kMin || 1);
return -Math.PI / 2 - t * Math.PI;
}
const t = (K_ - S) / (kMax - S || 1);
return -Math.PI / 2 + t * Math.PI;
}
// Strike tick labels — round multiples of a step fitted to the price range
const rawStep = (kMax - kMin) / 8;
const tickStep = rawStep >= 40 ? 50 : rawStep >= 20 ? 25 : rawStep >= 8 ? 10 : 5;
const tickStrikes: number[] = [];
for (let kk = Math.ceil(kMin / tickStep) * tickStep; kk <= kMax; kk += tickStep) {
tickStrikes.push(kk);
}
// Dynamic IV range across all expiries → defines radial scale
const { ivLo, ivHi } = useMemo(() => {
let lo = Infinity, hi = -Infinity;
expiries.forEach(e => {
for (let i = 0; i <= N; i++) {
const iv = bsSynthIV(S, strikeAt(i), e.T, atmSigma);
if (iv < lo) lo = iv;
if (iv > hi) hi = iv;
}
});
return { ivLo: Math.max(0.05, lo - 0.02), ivHi: hi + 0.02 };
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [S, atmSigma, expiries]);
// Radial scale: inner floor 60px, outer wall rOuter
function ivToR(iv: number): number {
return 60 + ((iv - ivLo) / (ivHi - ivLo)) * (rOuter - 60);
}
// 5 evenly-spaced IV ring labels
const ivLabels = useMemo(
() => Array.from({ length: 5 }, (_, step) => ivLo + (step / 4) * (ivHi - ivLo)),
[ivLo, ivHi]
);
// Smooth closed curves (N+1 samples, joined with Z so wings meet at bottom)
const curves = useMemo(() => {
return expiries.map(e => {
const pts: [number, number][] = [];
for (let i = 0; i <= N; i++) {
const Kk = strikeAt(i);
const iv = bsSynthIV(S, Kk, e.T, atmSigma);
const a = angleFor(Kk);
const rr = ivToR(iv);
pts.push([cx + Math.cos(a) * rr, cy + Math.sin(a) * rr]);
}
return { expiry: e, pts, isSelected: Math.abs(e.T - selectedT) < 1e-6 };
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [S, atmSigma, expiries, selectedT, ivLo, ivHi]);
function pathD(pts: [number, number][]): string {
const inner = pts.map(([px, py]) => `${px.toFixed(2)} ${py.toFixed(2)}`).join(' L ');
return `M ${inner} Z`;
}
function styleFor(idx: number, isSelected: boolean) {
if (isSelected) return {
stroke: 'var(--brass-bright)', strokeWidth: 2.5, strokeDasharray: undefined,
fill: 'rgba(194,170,122,0.12)',
};
const strokes = ['var(--fg-1)', 'var(--fg-2)', 'var(--fg-3)', 'var(--fg-4)'];
const dashes = [undefined, '4 3', '5 4', '2 4'];
const widths = [1.4, 1.2, 1.2, 1.1];
const i = Math.min(3, idx);
return { stroke: strokes[i], strokeWidth: widths[i], strokeDasharray: dashes[i], fill: 'none' };
}
// Selected expiry object
const selectedExpiry = expiries.find(e => Math.abs(e.T - selectedT) < 1e-6) ?? expiries[0];
// Current K dot on selected-expiry curve
const curIv = bsSynthIV(S, K, selectedExpiry?.T ?? T, atmSigma);
const aCur = angleFor(K);
const rCur = ivToR(curIv);
const dotX = cx + Math.cos(aCur) * rCur;
const dotY = cy + Math.sin(aCur) * rCur;
const dotLabelRight = aCur > -Math.PI / 2 && aCur < Math.PI / 2;
// Fair value + greeks for selected K & expiry
const selT = selectedExpiry?.T ?? T;
const fair = useMemo(() => bsPrice(S, K, selT, r, q, atmSigma, type), [S, K, selT, r, q, atmSigma, type]);
const gr = useMemo(() => bsGreeks(S, K, selT, r, q, atmSigma, type), [S, K, selT, r, q, atmSigma, type]);
return (
<div className="opt-surface">
<div className="head">
<h3>Vol Surface <em>polar</em></h3>
</div>
<div className="opt-surface-legend">
{expiries.map((e, idx) => {
const isSel = Math.abs(e.T - selectedT) < 1e-6;
const s = styleFor(idx, isSel);
return (
<div key={e.label}
className={`item${isSel ? '' : ' muted'}`}
onClick={() => onPickT(e.T)}
style={{ cursor: 'pointer' }}
>
<div className="swatch" style={{ background: s.stroke }} />
{e.label}
</div>
);
})}
</div>
<div className="opt-polar-wrap">
<svg viewBox={`0 0 ${W} ${H}`} width="100%" className="opt-polar" style={{ display: 'block' }}>
{/* Concentric IV rings */}
{ivLabels.map((iv, i) => (
<circle key={i}
className={`ring${i === ivLabels.length - 1 ? ' outer' : ''}`}
cx={cx} cy={cy} r={ivToR(iv)} />
))}
{ivLabels.map((iv, i) => (
<text key={`l${i}`} className="iv" x={cx + 6} y={(cy - ivToR(iv) - 2).toFixed(1)}>
{(iv * 100).toFixed(0)}%
</text>
))}
{/* Spoke lines + round strike tick labels */}
{tickStrikes.map(k => {
const a = angleFor(k);
const isAtm = Math.abs(k - S) < 2.5;
const x2 = cx + Math.cos(a) * rOuter;
const y2 = cy + Math.sin(a) * rOuter;
const lx = cx + Math.cos(a) * (rOuter + 18);
const ly = cy + Math.sin(a) * (rOuter + 18);
return (
<g key={k}>
<line
className={`spoke${isAtm ? ' atm' : ''}`}
x1={cx} y1={cy}
x2={x2.toFixed(1)} y2={y2.toFixed(1)}
style={{ opacity: isAtm ? 1 : 0.35 }}
/>
<text x={lx.toFixed(1)} y={(ly + 3).toFixed(1)}
textAnchor="middle"
className={`tick${isAtm ? ' atm' : ''}`}>
{k.toFixed(0)}
</text>
</g>
);
})}
{/* Directional labels */}
<text x={(cx - rOuter - 6).toFixed(1)} y={cy} textAnchor="end"
style={{ fontFamily: 'var(--font-sans)', fontSize: 10, letterSpacing: 'var(--tr-wider)', textTransform: 'uppercase', fill: 'var(--negative)' }}>
OTM puts ↓
</text>
<text x={(cx + rOuter + 6).toFixed(1)} y={cy} textAnchor="start"
style={{ fontFamily: 'var(--font-sans)', fontSize: 10, letterSpacing: 'var(--tr-wider)', textTransform: 'uppercase', fill: 'var(--positive)' }}>
↓ OTM calls
</text>
<text x={cx} y={(cy - rOuter - 24).toFixed(1)} textAnchor="middle"
style={{ fontFamily: 'var(--font-sans)', fontSize: 10, letterSpacing: 'var(--tr-wider)', textTransform: 'uppercase', fill: 'var(--brass)' }}>
ATM ↑
</text>
{/* Expiry curves — closed lens shape */}
{curves.map((c, idx) => {
const s = styleFor(idx, c.isSelected);
const d = pathD(c.pts);
return (
<g key={idx}>
{c.isSelected && (
<path d={d} className="expiry-fill" fill={s.fill} stroke="none" />
)}
<path d={d} className="expiry"
stroke={s.stroke}
strokeWidth={s.strokeWidth}
strokeDasharray={s.strokeDasharray}
fill="none"
style={{ cursor: 'pointer' }}
onClick={() => onPickT(c.expiry.T)}
/>
</g>
);
})}
{/* Dashed spoke from center to selected-K marker */}
<line className="marker-line" x1={cx} y1={cy} x2={dotX.toFixed(1)} y2={dotY.toFixed(1)} />
{/* Selected-K dot */}
<circle cx={dotX.toFixed(1)} cy={dotY.toFixed(1)} r={6} className="dot" />
{/* K label */}
<text
x={(dotX + (dotLabelRight ? 10 : -10)).toFixed(1)}
y={(dotY + 4).toFixed(1)}
textAnchor={dotLabelRight ? 'start' : 'end'}
className="brass" style={{ fontSize: 12 }}
>
K {K.toFixed(0)} · {(curIv * 100).toFixed(1)}%
</text>
{/* Center eye */}
<circle cx={cx} cy={cy} r={eyeR} className="eye" />
<text x={cx} y={(cy - 30).toFixed(1)} textAnchor="middle" className="eye-lbl">
{type === 'C' ? 'call · fair' : 'put · fair'}
</text>
<text x={cx} y={(cy - 4).toFixed(1)} textAnchor="middle" className="eye-num" style={{ fontSize: 30 }}>
{fair.toFixed(2)}
</text>
<text x={cx} y={(cy + 16).toFixed(1)} textAnchor="middle"
style={{ fontFamily: 'var(--font-mono)', fontSize: 10, fill: 'var(--fg-2)' }}>
Δ {gr.delta.toFixed(2)} · ν {gr.vega.toFixed(2)}
</text>
<text x={cx} y={(cy + 32).toFixed(1)} textAnchor="middle" className="eye-lbl" style={{ fontSize: 9 }}>
{selectedExpiry?.label} · {selectedExpiry?.dte}d
</text>
</svg>
</div>
</div>
);
}
interface IvHeatmapProps {
S: number;
atmSigma: number;
expiries: Expiry[];
selectedExpiryIdx: number;
selectedK: number;
onSelect: (expiryIdx: number, K: number) => void;
}
export function IvHeatmap({ S, atmSigma, expiries, selectedExpiryIdx, selectedK, onSelect }: IvHeatmapProps) {
const nStrikes = 13;
const kMin = S * 0.85, kMax = S * 1.15;
const strikes = useMemo(() => {
const arr: number[] = [];
for (let i = 0; i < nStrikes; i++) {
arr.push(Math.round((kMin + (kMax - kMin) * i / (nStrikes - 1)) / 5) * 5);
}
return arr;
}, [kMin, kMax]);
const ivGrid = useMemo(() => {
return expiries.map(e =>
strikes.map(K => bsSynthIV(S, K, e.T, atmSigma))
);
}, [S, atmSigma, expiries, strikes]);
const allIvs = ivGrid.flat();
const ivGridMin = Math.min(...allIvs);
const ivGridMax = Math.max(...allIvs);
function cellColor(iv: number): string {
const pct = Math.round(((iv - ivGridMin) / (ivGridMax - ivGridMin + 0.001)) * 80);
return `color-mix(in oklch, var(--ink-2), var(--brass-deep) ${pct}%)`;
}
function textColor(iv: number): string {
const pct = (iv - ivGridMin) / (ivGridMax - ivGridMin + 0.001);
return pct > 0.5 ? 'var(--fg-1)' : 'var(--fg-3)';
}
const atmK = Math.round(S / 5) * 5;
return (
<div className="opt-surface">
<div className="head">
<h3>IV Heatmap <em>surface</em></h3>
</div>
<div className="opt-heat">
<div className="ylabs">
{expiries.map(e => (
<span key={e.label}>{e.label}</span>
))}
</div>
<div>
<div className="grid" style={{ gridTemplateColumns: `repeat(${nStrikes}, 1fr)` }}>
{ivGrid.map((row, eIdx) =>
row.map((iv, kIdx) => {
const K = strikes[kIdx];
const isCursor = eIdx === selectedExpiryIdx && K === selectedK;
return (
<div
key={`${eIdx}-${kIdx}`}
className={`cell${isCursor ? ' cursor' : ''}`}
style={{ background: cellColor(iv), color: textColor(iv) }}
onClick={() => onSelect(eIdx, K)}
>
{(iv * 100).toFixed(0)}%
</div>
);
})
)}
</div>
<div className="xlabs" style={{ gridTemplateColumns: `repeat(${nStrikes}, 1fr)` }}>
{strikes.map((K, i) => (
<span key={i} className={K === atmK ? 'atm' : ''}>{K}</span>
))}
</div>
</div>
</div>
</div>
);
}
|