@continuumdao/quantlib 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +242 -0
- package/baroneAdesiWhaley.js +136 -0
- package/binomial.js +158 -0
- package/black.js +174 -0
- package/decide.js +379 -0
- package/derive.js +243 -0
- package/heston.js +330 -0
- package/index.js +31 -0
- package/jsonlStore.js +42 -0
- package/normal.js +19 -0
- package/optionsDb.js +69 -0
- package/package.json +26 -4
- package/replay.js +91 -0
- package/rysk.js +61 -0
- package/smile.js +169 -0
- package/spreads.js +90 -0
- package/store.js +94 -0
- package/svi.js +344 -0
- package/README.md +0 -3
package/black.js
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Black-76 and Black-Scholes-Merton prices and Greeks.
|
|
3
|
+
* QuantLib: ql/pricingengines/blackformula.cpp and ql/pricingengines/blackcalculator.cpp
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { normalCdf, normalPdf } from './normal.js'
|
|
7
|
+
|
|
8
|
+
export const YEAR_SECONDS = 365.25 * 24 * 60 * 60
|
|
9
|
+
|
|
10
|
+
export function yearFraction(fromSec, toSec) {
|
|
11
|
+
return (Number(toSec) - Number(fromSec)) / YEAR_SECONDS
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function typeSign(type) {
|
|
15
|
+
const name = String(type ?? '').toLowerCase()
|
|
16
|
+
if (name === 'call' || name === 'c') return 1
|
|
17
|
+
if (name === 'put' || name === 'p') return -1
|
|
18
|
+
throw new Error('type must be call or put.')
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function finite(name, value) {
|
|
22
|
+
const n = Number(value)
|
|
23
|
+
if (!Number.isFinite(n)) throw new Error(`${name} must be a finite number.`)
|
|
24
|
+
return n
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Black-76 on a forward. Greeks are with respect to the forward.
|
|
29
|
+
* delta is dPrice/dForward. vega is dPrice/dVol. theta and rho are per year.
|
|
30
|
+
*/
|
|
31
|
+
export function black76(input) {
|
|
32
|
+
const sign = typeSign(input.type)
|
|
33
|
+
const strike = finite('strike', input.strike)
|
|
34
|
+
const forward = finite('forward', input.forward)
|
|
35
|
+
const vol = finite('vol', input.vol)
|
|
36
|
+
const time = finite('time', input.time)
|
|
37
|
+
const rate = input.rate == null ? 0 : finite('rate', input.rate)
|
|
38
|
+
if (strike < 0) throw new Error('strike must be non-negative.')
|
|
39
|
+
if (!(forward > 0)) throw new Error('forward must be positive.')
|
|
40
|
+
if (vol < 0) throw new Error('vol must be non-negative.')
|
|
41
|
+
if (time < 0) throw new Error('time must be non-negative.')
|
|
42
|
+
|
|
43
|
+
const discount = Math.exp(-rate * time)
|
|
44
|
+
if (!(time > 0) || vol === 0) {
|
|
45
|
+
const intrinsic = Math.max(sign * (forward - strike), 0) * discount
|
|
46
|
+
const itm = sign * (forward - strike) > 0
|
|
47
|
+
const atm = forward === strike
|
|
48
|
+
let delta = 0
|
|
49
|
+
if (atm) delta = sign * 0.5 * discount
|
|
50
|
+
else if (itm) delta = sign * discount
|
|
51
|
+
return { price: intrinsic, delta, gamma: 0, vega: 0, theta: 0, rho: rate === 0 ? 0 : -time * intrinsic }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const stdDev = vol * Math.sqrt(time)
|
|
55
|
+
const d1 = Math.log(forward / strike) / stdDev + 0.5 * stdDev
|
|
56
|
+
const d2 = d1 - stdDev
|
|
57
|
+
const nd1 = normalCdf(sign * d1)
|
|
58
|
+
const nd2 = normalCdf(sign * d2)
|
|
59
|
+
const price = discount * sign * (forward * nd1 - strike * nd2)
|
|
60
|
+
const pdf = normalPdf(d1)
|
|
61
|
+
const delta = discount * sign * normalCdf(sign * d1)
|
|
62
|
+
const gamma = discount * pdf / (forward * stdDev)
|
|
63
|
+
const vega = discount * forward * pdf * Math.sqrt(time)
|
|
64
|
+
const theta = -(Math.log(discount) * price + 0.5 * vol * vol * time * forward * forward * gamma) / time
|
|
65
|
+
const rho = -time * price
|
|
66
|
+
return { price, delta, gamma, vega, theta, rho }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Spot Black-Scholes-Merton. dividend is the continuous yield q.
|
|
71
|
+
* delta and gamma are with respect to spot.
|
|
72
|
+
*/
|
|
73
|
+
export function blackScholesMerton(input) {
|
|
74
|
+
const sign = typeSign(input.type)
|
|
75
|
+
const strike = finite('strike', input.strike)
|
|
76
|
+
const spot = finite('spot', input.spot)
|
|
77
|
+
const vol = finite('vol', input.vol)
|
|
78
|
+
const time = finite('time', input.time)
|
|
79
|
+
const rate = input.rate == null ? 0 : finite('rate', input.rate)
|
|
80
|
+
const dividend = input.dividend == null ? 0 : finite('dividend', input.dividend)
|
|
81
|
+
if (!(spot > 0)) throw new Error('spot must be positive.')
|
|
82
|
+
const forward = spot * Math.exp((rate - dividend) * time)
|
|
83
|
+
const black = black76({ type: input.type, strike, forward, vol, time, rate })
|
|
84
|
+
if (!(time > 0) || vol === 0) {
|
|
85
|
+
const delta = black.delta * (forward / spot)
|
|
86
|
+
return { ...black, delta, gamma: 0, vega: 0 }
|
|
87
|
+
}
|
|
88
|
+
const stdDev = vol * Math.sqrt(time)
|
|
89
|
+
const d1 = Math.log(forward / strike) / stdDev + 0.5 * stdDev
|
|
90
|
+
const d2 = d1 - stdDev
|
|
91
|
+
const dfq = Math.exp(-dividend * time)
|
|
92
|
+
const delta = dfq * sign * normalCdf(sign * d1)
|
|
93
|
+
const gamma = dfq * normalPdf(d1) / (spot * stdDev)
|
|
94
|
+
const vega = spot * dfq * normalPdf(d1) * Math.sqrt(time)
|
|
95
|
+
const discount = Math.exp(-rate * time)
|
|
96
|
+
const theta = -(
|
|
97
|
+
Math.log(discount) * black.price
|
|
98
|
+
+ Math.log(forward / spot) * spot * delta
|
|
99
|
+
+ 0.5 * vol * vol * time * spot * spot * gamma
|
|
100
|
+
) / time
|
|
101
|
+
const rho = sign * strike * time * discount * normalCdf(sign * d2)
|
|
102
|
+
return { price: black.price, delta, gamma, vega, theta, rho }
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function discountOf(rate, time) {
|
|
106
|
+
return Math.exp(-rate * time)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function solveVol(target, priceAtVol) {
|
|
110
|
+
let vol = 0.3
|
|
111
|
+
for (let i = 0; i < 60; i += 1) {
|
|
112
|
+
const priced = priceAtVol(vol)
|
|
113
|
+
const diff = priced.price - target
|
|
114
|
+
if (Math.abs(diff) <= Math.max(1e-8, 1e-8 * Math.abs(target))) return vol
|
|
115
|
+
if (!(priced.vega > 1e-10)) break
|
|
116
|
+
vol -= diff / priced.vega
|
|
117
|
+
if (vol < 1e-6) vol = 1e-6
|
|
118
|
+
if (vol > 5) vol = 5
|
|
119
|
+
}
|
|
120
|
+
return null
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Black-76 implied vol of a forward premium.
|
|
125
|
+
* iv is null when the premium is outside no-arbitrage bounds or the solve fails.
|
|
126
|
+
*/
|
|
127
|
+
export function impliedVolBlack76(input) {
|
|
128
|
+
const strike = finite('strike', input.strike)
|
|
129
|
+
const forward = finite('forward', input.forward)
|
|
130
|
+
const price = finite('price', input.price)
|
|
131
|
+
const time = finite('time', input.time)
|
|
132
|
+
const rate = input.rate == null ? 0 : finite('rate', input.rate)
|
|
133
|
+
typeSign(input.type)
|
|
134
|
+
if (strike < 0) throw new Error('strike must be non-negative.')
|
|
135
|
+
if (!(forward > 0)) throw new Error('forward must be positive.')
|
|
136
|
+
if (time < 0) throw new Error('time must be non-negative.')
|
|
137
|
+
if (price < 0) throw new Error('price must be non-negative.')
|
|
138
|
+
const sign = typeSign(input.type)
|
|
139
|
+
const discount = discountOf(rate, time)
|
|
140
|
+
const intrinsic = Math.max(sign * (forward - strike), 0) * discount
|
|
141
|
+
const upper = discount * (sign > 0 ? forward : Math.max(strike, 0))
|
|
142
|
+
const tol = 1e-8
|
|
143
|
+
if (price < intrinsic - tol) return { iv: null, reason: 'price below intrinsic' }
|
|
144
|
+
if (price > upper + tol) return { iv: null, reason: 'price above the maximum' }
|
|
145
|
+
if (!(time > 0) || Math.abs(price - intrinsic) <= tol) return { iv: 0, reason: null }
|
|
146
|
+
const iv = solveVol(price, (vol) => black76({ type: input.type, strike, forward, vol, time, rate }))
|
|
147
|
+
if (iv == null) return { iv: null, reason: 'implied vol did not converge' }
|
|
148
|
+
return { iv, reason: null }
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Black-Scholes-Merton implied vol of a spot premium.
|
|
153
|
+
* dividend is the continuous yield q. iv is null outside no-arbitrage bounds.
|
|
154
|
+
*/
|
|
155
|
+
export function impliedVolBlackScholesMerton(input) {
|
|
156
|
+
const strike = finite('strike', input.strike)
|
|
157
|
+
const spot = finite('spot', input.spot)
|
|
158
|
+
const price = finite('price', input.price)
|
|
159
|
+
const time = finite('time', input.time)
|
|
160
|
+
const rate = input.rate == null ? 0 : finite('rate', input.rate)
|
|
161
|
+
const dividend = input.dividend == null ? 0 : finite('dividend', input.dividend)
|
|
162
|
+
if (!(spot > 0)) throw new Error('spot must be positive.')
|
|
163
|
+
if (time < 0) throw new Error('time must be non-negative.')
|
|
164
|
+
if (price < 0) throw new Error('price must be non-negative.')
|
|
165
|
+
const forward = spot * Math.exp((rate - dividend) * time)
|
|
166
|
+
const solved = impliedVolBlack76({ type: input.type, strike, forward, price, time, rate })
|
|
167
|
+
return solved
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** q such that spot * exp((rate - q) * time) equals forward. */
|
|
171
|
+
export function dividendForForward(spot, forward, time, rate = 0) {
|
|
172
|
+
if (!(time > 0) || !(spot > 0) || !(forward > 0)) return 0
|
|
173
|
+
return rate - Math.log(forward / spot) / time
|
|
174
|
+
}
|
package/decide.js
ADDED
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Decisions a plugin can make from one stored snapshot.
|
|
3
|
+
* The snapshot is the document written by the options store. No venue is fetched.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { binomial } from './binomial.js'
|
|
7
|
+
import { baroneAdesiWhaley } from './baroneAdesiWhaley.js'
|
|
8
|
+
import {
|
|
9
|
+
black76,
|
|
10
|
+
blackScholesMerton,
|
|
11
|
+
dividendForForward,
|
|
12
|
+
impliedVolBlack76,
|
|
13
|
+
yearFraction,
|
|
14
|
+
} from './black.js'
|
|
15
|
+
import { fitHeston, hestonPrice } from './heston.js'
|
|
16
|
+
import { optionSmile } from './smile.js'
|
|
17
|
+
import { fitSviByExpiry, sviVolatility } from './svi.js'
|
|
18
|
+
|
|
19
|
+
function sviMap(tickers, now) {
|
|
20
|
+
const fits = new Map()
|
|
21
|
+
for (const slice of fitSviByExpiry(tickers, { now })) {
|
|
22
|
+
fits.set(slice.expiry == null ? 'none' : String(slice.expiry), slice)
|
|
23
|
+
}
|
|
24
|
+
return fits
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const OPTION_MODELS = Object.freeze([
|
|
28
|
+
'black76',
|
|
29
|
+
'black-scholes',
|
|
30
|
+
'binomial',
|
|
31
|
+
'barone-adesi-whaley',
|
|
32
|
+
'heston',
|
|
33
|
+
'svi',
|
|
34
|
+
])
|
|
35
|
+
|
|
36
|
+
const MODELS = new Set(OPTION_MODELS)
|
|
37
|
+
|
|
38
|
+
function num(value) {
|
|
39
|
+
const n = Number(value)
|
|
40
|
+
return Number.isFinite(n) ? n : null
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function requireModel(model) {
|
|
44
|
+
if (!MODELS.has(model)) {
|
|
45
|
+
throw new Error('model must be black76, black-scholes, binomial, barone-adesi-whaley, heston, or svi.')
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** cheap when the model is above the ask, rich when it is below the bid. */
|
|
50
|
+
export function quoteSide(model, bid, ask) {
|
|
51
|
+
if (model == null || bid == null || ask == null) return null
|
|
52
|
+
if (model > ask) return 'cheap'
|
|
53
|
+
if (model < bid) return 'rich'
|
|
54
|
+
return 'inside'
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function contractOf(ticker, snapshot, rate) {
|
|
58
|
+
const spot = num(ticker.index) ?? num(snapshot?.spot)
|
|
59
|
+
const forward = num(ticker.forward) ?? spot
|
|
60
|
+
const strike = num(ticker.strike)
|
|
61
|
+
const expiry = num(ticker.expiry)
|
|
62
|
+
const time = num(ticker.time) ?? (expiry != null && snapshot?.ts != null ? yearFraction(snapshot.ts, expiry) : null)
|
|
63
|
+
const type = ticker.type === 'call' || ticker.type === 'put' ? ticker.type : null
|
|
64
|
+
if (!(spot > 0) || !(forward > 0) || !(strike > 0) || !(time > 0) || !type) return null
|
|
65
|
+
return {
|
|
66
|
+
type,
|
|
67
|
+
spot,
|
|
68
|
+
strike,
|
|
69
|
+
forward,
|
|
70
|
+
time,
|
|
71
|
+
rate,
|
|
72
|
+
dividend: dividendForForward(spot, forward, time, rate),
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Venue IV when the ticker has one, otherwise IV inverted from the mark. */
|
|
77
|
+
export function attachImpliedVol(ticker, snapshot, rate = 0) {
|
|
78
|
+
const base = contractOf(ticker, snapshot, rate)
|
|
79
|
+
const ivVenue = num(ticker.iv)
|
|
80
|
+
let ivMark = null
|
|
81
|
+
let ivReason = null
|
|
82
|
+
if (!base) ivReason = 'missing contract'
|
|
83
|
+
else if (num(ticker.mark) == null) ivReason = 'no mark'
|
|
84
|
+
else {
|
|
85
|
+
const solved = impliedVolBlack76({
|
|
86
|
+
type: base.type,
|
|
87
|
+
strike: base.strike,
|
|
88
|
+
forward: base.forward,
|
|
89
|
+
price: Number(ticker.mark),
|
|
90
|
+
time: base.time,
|
|
91
|
+
rate,
|
|
92
|
+
})
|
|
93
|
+
ivMark = solved.iv
|
|
94
|
+
ivReason = solved.reason
|
|
95
|
+
}
|
|
96
|
+
const iv = ivVenue != null && ivVenue > 0 ? ivVenue : ivMark
|
|
97
|
+
return { ...ticker, iv, ivVenue, ivMark, ivReason }
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function greeksOf(priced) {
|
|
101
|
+
if (!priced || typeof priced !== 'object') return null
|
|
102
|
+
return {
|
|
103
|
+
delta: priced.delta ?? null,
|
|
104
|
+
gamma: priced.gamma ?? null,
|
|
105
|
+
vega: priced.vega ?? null,
|
|
106
|
+
theta: priced.theta ?? null,
|
|
107
|
+
rho: priced.rho ?? null,
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function priceModel(model, base, vol, fit) {
|
|
112
|
+
if (model === 'heston') {
|
|
113
|
+
if (!fit?.heston) return { price: null, greeks: null, reason: fit?.reason ?? 'heston fit refused' }
|
|
114
|
+
const priced = hestonPrice({ ...base, ...fit.heston })
|
|
115
|
+
return { price: priced.price, greeks: greeksOf(priced), reason: null }
|
|
116
|
+
}
|
|
117
|
+
if (model === 'svi') {
|
|
118
|
+
if (!fit?.svi) return { price: null, greeks: null, reason: fit?.reason ?? 'thin smile', modelVol: null }
|
|
119
|
+
const modelVol = sviVolatility({
|
|
120
|
+
strike: base.strike,
|
|
121
|
+
forward: fit.svi.forward,
|
|
122
|
+
time: fit.svi.time,
|
|
123
|
+
...fit.svi,
|
|
124
|
+
})
|
|
125
|
+
const priced = black76({
|
|
126
|
+
type: base.type,
|
|
127
|
+
strike: base.strike,
|
|
128
|
+
forward: base.forward,
|
|
129
|
+
vol: modelVol,
|
|
130
|
+
time: base.time,
|
|
131
|
+
rate: base.rate,
|
|
132
|
+
})
|
|
133
|
+
return { price: priced.price, greeks: greeksOf(priced), reason: null, modelVol }
|
|
134
|
+
}
|
|
135
|
+
if (!(vol > 0)) return { price: null, greeks: null, reason: 'no vol' }
|
|
136
|
+
if (model === 'black76') {
|
|
137
|
+
const priced = black76({ type: base.type, strike: base.strike, forward: base.forward, vol, time: base.time, rate: base.rate })
|
|
138
|
+
return { price: priced.price, greeks: greeksOf(priced), reason: null }
|
|
139
|
+
}
|
|
140
|
+
if (model === 'black-scholes') {
|
|
141
|
+
const priced = blackScholesMerton({ ...base, vol })
|
|
142
|
+
return { price: priced.price, greeks: greeksOf(priced), reason: null }
|
|
143
|
+
}
|
|
144
|
+
if (model === 'binomial') {
|
|
145
|
+
const priced = binomial({ ...base, vol, exercise: 'european', tree: 'crr', steps: 128 })
|
|
146
|
+
return { price: priced.price, greeks: greeksOf(priced), reason: null }
|
|
147
|
+
}
|
|
148
|
+
const priced = baroneAdesiWhaley({ ...base, vol })
|
|
149
|
+
return { price: priced.price, greeks: greeksOf(priced), reason: null }
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Price one contract. heston is a fitHeston parameter object.
|
|
154
|
+
* svi is the parameter object from fitSvi.
|
|
155
|
+
*/
|
|
156
|
+
export function priceOption(input) {
|
|
157
|
+
requireModel(input.model)
|
|
158
|
+
const rate = input.rate ?? 0
|
|
159
|
+
const spot = num(input.spot ?? input.index)
|
|
160
|
+
const forward = num(input.forward) ?? spot
|
|
161
|
+
const strike = num(input.strike)
|
|
162
|
+
const time = num(input.time)
|
|
163
|
+
const type = input.type === 'call' || input.type === 'put' ? input.type : null
|
|
164
|
+
if (!(spot > 0) || !(forward > 0) || !(strike > 0) || !(time > 0) || !type) {
|
|
165
|
+
return { price: null, greeks: null, reason: 'missing contract' }
|
|
166
|
+
}
|
|
167
|
+
const base = {
|
|
168
|
+
type,
|
|
169
|
+
spot,
|
|
170
|
+
strike,
|
|
171
|
+
forward,
|
|
172
|
+
time,
|
|
173
|
+
rate,
|
|
174
|
+
dividend: input.dividend == null ? dividendForForward(spot, forward, time, rate) : Number(input.dividend),
|
|
175
|
+
}
|
|
176
|
+
const fit = input.model === 'svi'
|
|
177
|
+
? { svi: input.svi ?? null, reason: input.svi ? null : 'thin smile' }
|
|
178
|
+
: { heston: input.heston ?? null, reason: input.reason ?? (input.heston ? null : 'heston fit refused') }
|
|
179
|
+
return priceModel(input.model, base, num(input.vol), fit)
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function expiryKey(ticker) {
|
|
183
|
+
const expiry = num(ticker.expiry)
|
|
184
|
+
return expiry == null ? 'none' : String(expiry)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function selectTickers(tickers, query) {
|
|
188
|
+
return tickers.filter((ticker) => {
|
|
189
|
+
if (query.strike != null && num(ticker.strike) !== Number(query.strike)) return false
|
|
190
|
+
if (query.expiry != null && num(ticker.expiry) !== Number(query.expiry)) return false
|
|
191
|
+
if (query.type && ticker.type !== query.type) return false
|
|
192
|
+
if (query.strikeMin != null && !(num(ticker.strike) >= query.strikeMin)) return false
|
|
193
|
+
if (query.strikeMax != null && !(num(ticker.strike) <= query.strikeMax)) return false
|
|
194
|
+
if (query.expiryMin != null && !(num(ticker.expiry) >= query.expiryMin)) return false
|
|
195
|
+
if (query.expiryMax != null && !(num(ticker.expiry) <= query.expiryMax)) return false
|
|
196
|
+
const abs = Math.abs(num(ticker.delta) ?? NaN)
|
|
197
|
+
if (query.minDelta != null && !(abs >= query.minDelta)) return false
|
|
198
|
+
if (query.maxDelta != null && !(abs <= query.maxDelta)) return false
|
|
199
|
+
return true
|
|
200
|
+
})
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function withVols(snapshot, rate) {
|
|
204
|
+
return (snapshot?.tickers ?? []).map((ticker) => attachImpliedVol(ticker, snapshot, rate))
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function hestonQuotes(tickers) {
|
|
208
|
+
return tickers.filter((ticker) => num(ticker.iv) > 0 && num(ticker.strike) > 0 && num(ticker.time ?? ticker.expiry) > 0)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Attach implied vols and fit Heston and SVI once.
|
|
213
|
+
* priceSnapshot, impliedVols, snapshotSmile, assessContract, and assessRange accept this value
|
|
214
|
+
* and do not fit again. A liquidity floor on the smile still refits that filtered slice.
|
|
215
|
+
*/
|
|
216
|
+
export function prepareSnapshot(snapshot, options = {}) {
|
|
217
|
+
if (snapshot?.prepared === true) {
|
|
218
|
+
if (options.rate != null && options.rate !== snapshot.rate) throw new Error('prepared snapshot rate does not match.')
|
|
219
|
+
return snapshot
|
|
220
|
+
}
|
|
221
|
+
const rate = options.rate ?? 0
|
|
222
|
+
const tickers = withVols(snapshot, rate)
|
|
223
|
+
const now = snapshot?.ts ?? null
|
|
224
|
+
return {
|
|
225
|
+
prepared: true,
|
|
226
|
+
snapshot,
|
|
227
|
+
rate,
|
|
228
|
+
tickers,
|
|
229
|
+
fit: fitHeston(hestonQuotes(tickers), { now, rate }),
|
|
230
|
+
sviByExpiry: sviMap(tickers, now),
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function preparedOf(snapshot, options) {
|
|
235
|
+
if (snapshot?.prepared !== true) return null
|
|
236
|
+
if (options.rate != null && options.rate !== snapshot.rate) throw new Error('prepared snapshot rate does not match.')
|
|
237
|
+
return snapshot
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function header(snapshot, fit, model) {
|
|
241
|
+
return {
|
|
242
|
+
venue: snapshot?.venue ?? null,
|
|
243
|
+
underlying: snapshot?.underlying ?? null,
|
|
244
|
+
ts: snapshot?.ts ?? null,
|
|
245
|
+
engine: fit?.engine ?? null,
|
|
246
|
+
singleExpiry: fit?.singleExpiry ?? null,
|
|
247
|
+
hestonReason: fit?.reason ?? null,
|
|
248
|
+
model,
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function rowResult(ticker, model, fit, sviByExpiry, rate, snapshot) {
|
|
253
|
+
const base = contractOf(ticker, snapshot, rate)
|
|
254
|
+
const slice = model === 'svi' ? sviByExpiry.get(expiryKey(ticker)) : fit
|
|
255
|
+
if (!base) {
|
|
256
|
+
return {
|
|
257
|
+
instrumentName: ticker.instrumentName ?? null,
|
|
258
|
+
type: ticker.type ?? null,
|
|
259
|
+
strike: num(ticker.strike),
|
|
260
|
+
expiry: num(ticker.expiry),
|
|
261
|
+
modelPrice: null,
|
|
262
|
+
side: null,
|
|
263
|
+
reason: 'missing contract',
|
|
264
|
+
ivVenue: ticker.ivVenue ?? null,
|
|
265
|
+
ivMark: ticker.ivMark ?? null,
|
|
266
|
+
venue: { bid: num(ticker.bid), ask: num(ticker.ask), mark: num(ticker.mark) },
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
const priced = priceModel(model, base, num(ticker.iv), slice)
|
|
270
|
+
return {
|
|
271
|
+
instrumentName: ticker.instrumentName ?? null,
|
|
272
|
+
type: base.type,
|
|
273
|
+
strike: base.strike,
|
|
274
|
+
expiry: num(ticker.expiry),
|
|
275
|
+
modelPrice: priced.price,
|
|
276
|
+
greeks: priced.greeks,
|
|
277
|
+
side: quoteSide(priced.price, num(ticker.bid), num(ticker.ask)),
|
|
278
|
+
reason: priced.reason,
|
|
279
|
+
ivVenue: ticker.ivVenue ?? null,
|
|
280
|
+
ivMark: ticker.ivMark ?? null,
|
|
281
|
+
iv: num(ticker.iv),
|
|
282
|
+
...(model === 'svi' ? { modelVol: priced.modelVol ?? null } : {}),
|
|
283
|
+
venue: {
|
|
284
|
+
bid: num(ticker.bid),
|
|
285
|
+
ask: num(ticker.ask),
|
|
286
|
+
mark: num(ticker.mark),
|
|
287
|
+
delta: num(ticker.delta),
|
|
288
|
+
},
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Price every selected ticker on a snapshot.
|
|
294
|
+
* Filters are strike, expiry, type, strikeMin, strikeMax, expiryMin, expiryMax, minDelta, and maxDelta.
|
|
295
|
+
*/
|
|
296
|
+
export function priceSnapshot(snapshot, options = {}) {
|
|
297
|
+
requireModel(options.model)
|
|
298
|
+
const prepared = preparedOf(snapshot, options)
|
|
299
|
+
const rate = prepared ? prepared.rate : (options.rate ?? 0)
|
|
300
|
+
const source = prepared ? prepared.snapshot : snapshot
|
|
301
|
+
const tickers = prepared ? prepared.tickers : withVols(snapshot, rate)
|
|
302
|
+
const fit = prepared ? prepared.fit : fitHeston(hestonQuotes(tickers), { now: source?.ts ?? null, rate })
|
|
303
|
+
const sviByExpiry = prepared
|
|
304
|
+
? prepared.sviByExpiry
|
|
305
|
+
: (options.model === 'svi' ? sviMap(tickers, source?.ts ?? null) : new Map())
|
|
306
|
+
const rows = selectTickers(tickers, options).map((ticker) => rowResult(ticker, options.model, fit, sviByExpiry, rate, source))
|
|
307
|
+
return { ...header(source, fit, options.model), rows }
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** Venue IV and mark IV for the selected tickers. */
|
|
311
|
+
export function impliedVols(snapshot, options = {}) {
|
|
312
|
+
const prepared = preparedOf(snapshot, options)
|
|
313
|
+
const rate = prepared ? prepared.rate : (options.rate ?? 0)
|
|
314
|
+
const source = prepared ? prepared.snapshot : snapshot
|
|
315
|
+
const tickers = prepared ? prepared.tickers : withVols(snapshot, rate)
|
|
316
|
+
const rows = selectTickers(tickers, options).map((ticker) => ({
|
|
317
|
+
instrumentName: ticker.instrumentName ?? null,
|
|
318
|
+
type: ticker.type ?? null,
|
|
319
|
+
strike: num(ticker.strike),
|
|
320
|
+
expiry: num(ticker.expiry),
|
|
321
|
+
ivVenue: ticker.ivVenue ?? null,
|
|
322
|
+
ivMark: ticker.ivMark ?? null,
|
|
323
|
+
reason: ticker.ivVenue > 0 ? null : ticker.ivReason,
|
|
324
|
+
}))
|
|
325
|
+
return {
|
|
326
|
+
venue: source?.venue ?? null,
|
|
327
|
+
underlying: source?.underlying ?? null,
|
|
328
|
+
ts: source?.ts ?? null,
|
|
329
|
+
rows,
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** Smile, smirk, and a per-expiry SVI fit. quoteStrikes reads the curve off the listed strikes. */
|
|
334
|
+
export function snapshotSmile(snapshot, options = {}) {
|
|
335
|
+
const prepared = preparedOf(snapshot, options)
|
|
336
|
+
const source = prepared ? prepared.snapshot : snapshot
|
|
337
|
+
const rate = prepared ? prepared.rate : (options.rate ?? 0)
|
|
338
|
+
const tickers = prepared ? prepared.tickers : withVols(snapshot, rate)
|
|
339
|
+
const expiry = options.expiry == null ? null : Number(options.expiry)
|
|
340
|
+
const rows = tickers.filter((ticker) => expiry == null || num(ticker.expiry) === expiry)
|
|
341
|
+
const floor = options.minOpenInterest != null || options.minVolume != null
|
|
342
|
+
const smile = optionSmile(rows, {
|
|
343
|
+
weight: options.weight ?? 'none',
|
|
344
|
+
minOpenInterest: options.minOpenInterest,
|
|
345
|
+
minVolume: options.minVolume,
|
|
346
|
+
now: source?.ts ?? null,
|
|
347
|
+
quoteStrikes: options.quoteStrikes,
|
|
348
|
+
sviByExpiry: prepared && !floor ? prepared.sviByExpiry : null,
|
|
349
|
+
})
|
|
350
|
+
return {
|
|
351
|
+
venue: source?.venue ?? null,
|
|
352
|
+
underlying: source?.underlying ?? null,
|
|
353
|
+
ts: source?.ts ?? null,
|
|
354
|
+
...smile,
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/** One contract. strike, expiry, and type are required. */
|
|
359
|
+
export function assessContract(snapshot, options = {}) {
|
|
360
|
+
if (options.strike == null || options.expiry == null || !options.type) {
|
|
361
|
+
throw new Error('strike, expiry, and type are required.')
|
|
362
|
+
}
|
|
363
|
+
const priced = priceSnapshot(snapshot, options)
|
|
364
|
+
const row = priced.rows[0] ?? null
|
|
365
|
+
if (!row) throw new Error('no contract')
|
|
366
|
+
const { rows, ...rest } = priced
|
|
367
|
+
return { ...rest, row }
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/** A strike span, an expiry span, or a delta band, with counts of cheap, rich, and inside. */
|
|
371
|
+
export function assessRange(snapshot, options = {}) {
|
|
372
|
+
const priced = priceSnapshot(snapshot, options)
|
|
373
|
+
const counts = { cheap: 0, rich: 0, inside: 0, skipped: 0 }
|
|
374
|
+
for (const row of priced.rows) {
|
|
375
|
+
if (row.side === 'cheap' || row.side === 'rich' || row.side === 'inside') counts[row.side] += 1
|
|
376
|
+
else counts.skipped += 1
|
|
377
|
+
}
|
|
378
|
+
return { ...priced, counts }
|
|
379
|
+
}
|