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
|
"use client";
import { useMemo } from "react";
import { bsPrice, bsGreeks, bsSynthIV } from "@/lib/blackScholes";
import type { ChainRow, OptionType } from "./types";
function hashRand(seed: number): number {
const x = Math.sin(seed) * 10000;
return x - Math.floor(x);
}
export function buildChain(S: number, T: number, r: number, q: number, atmSigma: number, expirySeed: number): ChainRow[] {
const rawMin = Math.round(S * 0.85 / 5) * 5;
const rawMax = Math.round(S * 1.20 / 5) * 5;
const rows: ChainRow[] = [];
for (let K = rawMin; K <= rawMax; K += 5) {
const iv = bsSynthIV(S, K, T, atmSigma);
const cMid = bsPrice(S, K, T, r, q, iv, 'C');
const pMid = bsPrice(S, K, T, r, q, iv, 'P');
const cG = bsGreeks(S, K, T, r, q, iv, 'C');
const pG = bsGreeks(S, K, T, r, q, iv, 'P');
const seed = expirySeed * 1000 + K;
const cOi = Math.round(hashRand(seed + 1) * 50000 + 1000);
const pOi = Math.round(hashRand(seed + 2) * 40000 + 800);
const cVol = Math.round(hashRand(seed + 3) * 20000 + 200);
const pVol = Math.round(hashRand(seed + 4) * 15000 + 150);
rows.push({
K,
cMid, pMid,
cIv: iv, pIv: iv,
cDelta: cG.delta, pDelta: pG.delta,
cOi, pOi,
cVol, pVol,
});
}
return rows;
}
export function findAtmStrike(strikes: number[], S: number): number {
return strikes.reduce((prev, curr) => Math.abs(curr - S) < Math.abs(prev - S) ? curr : prev, strikes[0]);
}
function fmtOi(n: number): string {
if (n >= 1000) return (n / 1000).toFixed(1) + 'k';
return n.toString();
}
interface ChainTableProps {
rows: ChainRow[];
atmStrike: number;
selectedK: number;
type: OptionType;
onPick: (K: number) => void;
compact?: boolean;
}
export function ChainTable({ rows, atmStrike, selectedK, type, onPick, compact = false }: ChainTableProps) {
return (
<div className={`opt-chain-wrap${compact ? ' compact' : ''}`}>
<div className="opt-chain-head">
<h3>Option Chain</h3>
<span className="sub">{compact ? 'Compact' : 'Full'} · {rows.length} strikes</span>
</div>
<div className="opt-chain-scroll">
<table className="opt-chain">
<thead>
<tr>
{!compact && <th className="group side-c" colSpan={2} style={{ textAlign: 'center' }}>— calls —</th>}
{compact && <th className="group side-c" colSpan={3} style={{ textAlign: 'center' }}>— calls —</th>}
<th className="group k" style={{ textAlign: 'center' }}>strike</th>
{!compact && <th className="group side-p" colSpan={2} style={{ textAlign: 'center' }}>— puts —</th>}
{compact && <th className="group side-p" colSpan={3} style={{ textAlign: 'center' }}>— puts —</th>}
</tr>
{!compact ? (
<tr>
<th className="side-c">OI</th>
<th className="side-c">IV</th>
<th className="side-c">last</th>
<th className="side-c">Δ</th>
<th className="k">K</th>
<th className="side-p">Δ</th>
<th className="side-p">last</th>
<th className="side-p">IV</th>
<th className="side-p">OI</th>
</tr>
) : (
<tr>
<th className="side-c">IV</th>
<th className="side-c">last</th>
<th className="side-c">Δ</th>
<th className="k">K</th>
<th className="side-p">Δ</th>
<th className="side-p">last</th>
<th className="side-p">IV</th>
</tr>
)}
</thead>
<tbody>
{rows.map((row) => {
const isAtm = row.K === atmStrike;
const isSel = row.K === selectedK;
const cItm = type === 'C' ? row.K < atmStrike : row.K > atmStrike;
const pItm = type === 'P' ? row.K < atmStrike : row.K > atmStrike;
return (
<tr
key={row.K}
className={`${isAtm ? 'atm' : ''} ${isSel ? 'selected' : ''}`}
onClick={() => onPick(row.K)}
>
{!compact && <td className="dim">{fmtOi(row.cOi)}</td>}
<td className="iv">{(row.cIv * 100).toFixed(1)}%</td>
<td className={cItm ? 'itm' : 'otm'}>{row.cMid.toFixed(2)}</td>
<td className={cItm ? 'itm' : 'otm'}>{row.cDelta.toFixed(2)}</td>
<td className="k">{row.K.toFixed(0)}</td>
<td className={pItm ? 'itm' : 'otm'}>{row.pDelta.toFixed(2)}</td>
<td className={pItm ? 'itm' : 'otm'}>{row.pMid.toFixed(2)}</td>
<td className="iv">{(row.pIv * 100).toFixed(1)}%</td>
{!compact && <td className="dim">{fmtOi(row.pOi)}</td>}
</tr>
);
})}
</tbody>
</table>
</div>
</div>
);
}
interface OptionsChainProps {
S: number;
T: number;
r: number;
q: number;
atmSigma: number;
expirySeed: number;
selectedK: number;
type: OptionType;
onPick: (K: number) => void;
compact?: boolean;
}
export function OptionsChain({ S, T, r, q, atmSigma, expirySeed, selectedK, type, onPick, compact }: OptionsChainProps) {
const rows = useMemo(() => buildChain(S, T, r, q, atmSigma, expirySeed), [S, T, r, q, atmSigma, expirySeed]);
const atmStrike = useMemo(() => findAtmStrike(rows.map(rw => rw.K), S), [rows, S]);
return (
<ChainTable
rows={rows}
atmStrike={atmStrike}
selectedK={selectedK}
type={type}
onPick={onPick}
compact={compact}
/>
);
}
|