use super::pricing::{black_price, Greeks};
use super::symbol::Right;
use crate::mathx;
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct OptionDealerSpec {
pub tick: f64,
pub tick_large: f64,
pub tick_switch: f64,
pub displayed_size: f64,
pub levels: usize,
pub level_step: f64,
pub moneyness_slope: f64,
pub inventory_skew: f64,
pub inventory_cap: f64,
pub inventory_half_life: f64,
}
pub const INDEX_OPTION_DEALER: OptionDealerSpec = OptionDealerSpec {
tick: 0.05,
tick_large: 0.10,
tick_switch: 3.0,
displayed_size: 50.0,
levels: 10,
level_step: 0.5,
moneyness_slope: 0.25,
inventory_skew: 0.5,
inventory_cap: 4.0,
inventory_half_life: crate::rates::INVENTORY_HALF_LIFE_TICKS,
};
pub const NAME_OPTION_DEALER: OptionDealerSpec = OptionDealerSpec {
tick: 0.01,
tick_large: 0.05,
tick_switch: 3.0,
displayed_size: 20.0,
levels: 10,
level_step: 0.5,
moneyness_slope: 0.25,
inventory_skew: 0.5,
inventory_cap: 4.0,
inventory_half_life: crate::rates::INVENTORY_HALF_LIFE_TICKS,
};
impl OptionDealerSpec {
pub fn tick_at(&self, price: f64) -> f64 {
if price >= self.tick_switch {
self.tick_large
} else {
self.tick
}
}
pub fn half_spread(&self, base: f64, vix: f64, z: f64) -> f64 {
base * crate::microstructure::vix_spread_multiplier(vix) * (1.0 + self.moneyness_slope * z.abs())
}
pub fn inventory_shift(&self, pressure: f64, atm_half_spread: f64) -> f64 {
if pressure == 0.0 || !(atm_half_spread > 0.0) {
return 0.0;
}
let cap = self.inventory_cap * atm_half_spread;
mathx::clamp(self.inventory_skew * atm_half_spread * pressure / self.displayed_size, -cap, cap)
}
pub fn decay(&self) -> f64 {
mathx::pow(0.5, 1.0 / self.inventory_half_life)
}
}
fn on_grid(spec: &OptionDealerSpec, price: f64, up: bool) -> f64 {
let tick = spec.tick_at(price);
if up {
(price / tick - 1e-9).ceil() * tick
} else {
(price / tick + 1e-9).floor() * tick
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct Level {
pub price: f64,
pub size: f64,
pub vol: f64,
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct Ladder {
pub mid: f64,
pub bids: Vec<Level>,
pub asks: Vec<Level>,
}
#[allow(clippy::too_many_arguments)]
pub fn ladder(
spec: &OptionDealerSpec,
right: Right,
forward: f64,
strike: f64,
years: f64,
discount: f64,
vol: f64,
half_spread: f64,
) -> Ladder {
ladder_with(spec, |v| black_price(right, forward, strike, years, v, discount), vol, half_spread)
}
#[allow(clippy::too_many_arguments)]
pub fn american_ladder(
spec: &OptionDealerSpec,
right: Right,
forward: f64,
strike: f64,
years: f64,
discount: f64,
vol: f64,
half_spread: f64,
mid: f64,
) -> Ladder {
let at_mid = black_price(right, forward, strike, years, vol, discount);
ladder_with(
spec,
|v| if v == vol { mid } else { mid + black_price(right, forward, strike, years, v, discount) - at_mid },
vol,
half_spread,
)
}
fn ladder_with(spec: &OptionDealerSpec, price_at: impl Fn(f64) -> f64, vol: f64, half_spread: f64) -> Ladder {
let mid = price_at(vol);
let mut asks: Vec<Level> = Vec::with_capacity(spec.levels);
let mut bids: Vec<Level> = Vec::with_capacity(spec.levels);
for j in 0..spec.levels {
let h = half_spread * (1.0 + j as f64 * spec.level_step);
let ask_vol = vol + h;
let mut ask = on_grid(spec, price_at(ask_vol), true);
let floor = match asks.last() {
Some(prev) => prev.price + spec.tick_at(prev.price),
None => on_grid(spec, mid, false) + spec.tick_at(mid),
};
if ask < floor - 1e-12 {
ask = floor;
}
asks.push(Level { price: ask, size: spec.displayed_size, vol: ask_vol });
}
for j in 0..spec.levels {
let h = half_spread * (1.0 + j as f64 * spec.level_step);
let bid_vol = mathx::max(1e-4, vol - h);
let mut bid = on_grid(spec, price_at(bid_vol), false);
let cap = match bids.last() {
Some(prev) => prev.price - spec.tick_at(prev.price - 1e-9),
None => on_grid(spec, mid, true) - spec.tick_at(mid),
};
if bid > cap + 1e-12 {
bid = cap;
}
if bid < spec.tick - 1e-12 {
break;
}
bids.push(Level { price: bid, size: spec.displayed_size, vol: bid_vol });
}
Ladder { mid, bids, asks }
}
pub fn price_ladder(spec: &OptionDealerSpec, mid: f64, half: f64, vol: f64) -> Ladder {
let mut asks: Vec<Level> = Vec::with_capacity(spec.levels);
let mut bids: Vec<Level> = Vec::with_capacity(spec.levels);
for j in 0..spec.levels {
let h = half * (1.0 + j as f64 * spec.level_step);
let mut ask = on_grid(spec, mid + h, true);
let floor = match asks.last() {
Some(prev) => prev.price + spec.tick_at(prev.price),
None => on_grid(spec, mid, false) + spec.tick_at(mid),
};
if ask < floor - 1e-12 {
ask = floor;
}
asks.push(Level { price: ask, size: spec.displayed_size, vol });
}
for j in 0..spec.levels {
let h = half * (1.0 + j as f64 * spec.level_step);
let mut bid = on_grid(spec, mathx::max(0.0, mid - h), false);
let cap = match bids.last() {
Some(prev) => prev.price - spec.tick_at(prev.price - 1e-9),
None => on_grid(spec, mid, true) - spec.tick_at(mid),
};
if bid > cap + 1e-12 {
bid = cap;
}
if bid < spec.tick - 1e-12 {
break;
}
bids.push(Level { price: bid, size: spec.displayed_size, vol });
}
Ladder { mid, bids, asks }
}
pub fn moneyness_factor(abs_delta: f64, otm: f64, itm: f64) -> f64 {
let a = mathx::clamp(abs_delta, 0.25, 0.75);
if a < 0.5 {
otm + (1.0 - otm) * (a - 0.25) / 0.25
} else {
1.0 + (itm - 1.0) * (a - 0.5) / 0.25
}
}
pub fn walk(levels: &[Level], quantity: f64, limit: Option<f64>, buying: bool) -> Vec<(f64, f64)> {
let mut out = Vec::new();
let mut left = quantity;
for level in levels {
if !(left > 1e-9) {
break;
}
if let Some(l) = limit {
let ok = if buying { level.price <= l + 1e-12 } else { level.price >= l - 1e-12 };
if !ok {
break;
}
}
let take = mathx::min(left, level.size);
out.push((level.price, take));
left -= take;
}
out
}
pub fn pressure_after(pressure: f64, sold: f64, vega: f64, atm_vega: f64) -> f64 {
if !(atm_vega > 0.0) {
return pressure;
}
pressure + sold * vega / atm_vega
}
pub fn dealer_thetas(expiries: &[(f64, f64)], shifts: &[f64]) -> Vec<f64> {
let mut out = Vec::with_capacity(expiries.len());
let mut top: f64 = 0.0;
for (i, &(sessions, theta)) in expiries.iter().enumerate() {
let shift = shifts.get(i).copied().unwrap_or(0.0);
let shifted = if shift == 0.0 || !(theta > 0.0 && sessions > 0.0) {
theta
} else {
let vol = mathx::sqrt(theta * 252.0 / sessions);
let moved = mathx::max(0.5 * vol, vol + shift);
theta * (moved / vol) * (moved / vol)
};
top = mathx::max(top, shifted);
out.push(top);
}
out
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct OptionQuote {
pub symbol: String,
pub root: String,
pub expiry: i64,
pub right: Right,
pub strike: f64,
pub bid: Option<f64>,
pub ask: Option<f64>,
pub bid_size: f64,
pub ask_size: f64,
pub mid: f64,
pub fair: f64,
pub iv: f64,
pub surface_iv: f64,
pub greeks: Greeks,
pub forward: f64,
pub index: f64,
pub rate: f64,
pub dividends: f64,
pub sessions_to_expiry: f64,
pub multiplier: f64,
pub tick: f64,
pub open_interest: f64,
pub initial_margin: Option<f64>,
pub maintenance_margin: Option<f64>,
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct ExpirySummary {
pub root: String,
pub expiry: i64,
pub sessions_to_expiry: f64,
pub forward: f64,
pub strike_low: f64,
pub strike_high: f64,
pub strike_step: f64,
pub atm_iv: Option<f64>,
pub put_25d_iv: Option<f64>,
pub call_25d_iv: Option<f64>,
pub skew_25d: Option<f64>,
}
pub fn read_smile(chain: &[OptionQuote]) -> (Option<f64>, Option<f64>, Option<f64>) {
let side = |right: Right| {
let mut v: Vec<&OptionQuote> =
chain.iter().filter(|q| q.right == right && q.sessions_to_expiry > 0.0 && q.iv > 0.0).collect();
v.sort_by(|a, b| a.strike.total_cmp(&b.strike));
v
};
let (calls, puts) = (side(Right::Call), side(Right::Put));
let atm = calls.windows(2).find_map(|w| {
let (ka, kb) = (mathx::log(w[0].strike / w[0].forward), mathx::log(w[1].strike / w[1].forward));
(ka <= 0.0 && 0.0 <= kb && kb > ka).then(|| w[0].iv + (w[1].iv - w[0].iv) * (0.0 - ka) / (kb - ka))
});
let at_delta = |quotes: &[&OptionQuote], target: f64| {
quotes.windows(2).find_map(|w| {
let (da, db) = (w[0].greeks.delta, w[1].greeks.delta);
(da >= target && target >= db && da > db).then(|| w[0].iv + (w[1].iv - w[0].iv) * (da - target) / (da - db))
})
};
(atm, at_delta(&puts, -0.25), at_delta(&calls, 0.25))
}
pub fn chain_violations(quotes: &[OptionQuote], half_tick: f64) -> Vec<String> {
let mut out = Vec::new();
let mut expiries: Vec<i64> = quotes.iter().map(|q| q.expiry).collect();
expiries.sort_unstable();
expiries.dedup();
let mut smiles: Vec<(f64, Vec<(f64, f64)>)> = Vec::new();
for &e in &expiries {
for right in [Right::Call, Right::Put] {
let mut side: Vec<&OptionQuote> = quotes.iter().filter(|q| q.expiry == e && q.right == right).collect();
side.sort_by(|a, b| a.strike.total_cmp(&b.strike));
for w in side.windows(2) {
let tol = 1e-9 * mathx::max(1.0, w[0].mid.abs());
let bad = match right {
Right::Call => w[1].mid > w[0].mid + tol,
Right::Put => w[1].mid < w[0].mid - tol,
};
if bad {
out.push(format!(
"expiry {e}: the {} mid moves the wrong way from strike {} ({}) to {} ({})",
if right == Right::Call { "call" } else { "put" },
w[0].strike, w[0].mid, w[1].strike, w[1].mid
));
}
}
for w in side.windows(3) {
let left = (w[1].mid - w[0].mid) / (w[1].strike - w[0].strike);
let rightslope = (w[2].mid - w[1].mid) / (w[2].strike - w[1].strike);
if rightslope < left - 1e-9 {
out.push(format!(
"expiry {e}: the butterfly at strike {} is negative ({} then {})",
w[1].strike, left, rightslope
));
}
}
}
let calls: Vec<&OptionQuote> = quotes.iter().filter(|q| q.expiry == e && q.right == Right::Call).collect();
for c in &calls {
let Some(p) = quotes.iter().find(|q| q.expiry == e && q.right == Right::Put && q.strike == c.strike) else {
continue;
};
let discount = mathx::exp(-c.rate * c.sessions_to_expiry / 252.0);
let gap = c.mid - p.mid - discount * (c.forward - c.strike);
if gap.abs() > half_tick {
out.push(format!("expiry {e}: strike {} is {gap} off put-call parity", c.strike));
}
}
let mut smile: Vec<(f64, f64)> = calls
.iter()
.map(|c| (mathx::log(c.strike / c.forward), c.iv * c.iv * c.sessions_to_expiry / 252.0))
.collect();
smile.sort_by(|a, b| a.0.total_cmp(&b.0));
if let Some(c) = calls.first() {
smiles.push((c.sessions_to_expiry, smile));
}
}
for w in smiles.windows(2) {
let (early, late) = (&w[0].1, &w[1].1);
for &(k, v) in early {
let Some(j) = late.windows(2).position(|p| p[0].0 <= k && k <= p[1].0) else {
continue;
};
let (a, b) = (late[j], late[j + 1]);
let later = if b.0 > a.0 { a.1 + (b.1 - a.1) * (k - a.0) / (b.0 - a.0) } else { a.1 };
if later < v - 1e-12 {
out.push(format!(
"calendar: total variance falls from {v} at {} sessions to {later} at {} at log-moneyness {k}",
w[0].0, w[1].0
));
}
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
use crate::derivatives::pricing::black_greeks;
use crate::derivatives::surface::Ssvi;
const SPEC: OptionDealerSpec = INDEX_OPTION_DEALER;
#[test]
fn the_ladder_straddles_the_mid_on_the_grid_and_widens_outward() {
for (k, t, v) in [(1000.0, 21.0, 0.2), (900.0, 63.0, 0.3), (1100.0, 5.0, 0.15), (600.0, 21.0, 0.6)] {
for right in [Right::Call, Right::Put] {
let years = t / 252.0;
let l = ladder(&SPEC, right, 1000.0, k, years, 0.999, v, 0.004);
assert_eq!(l.asks.len(), SPEC.levels);
assert!(l.asks[0].price > l.mid, "{right:?} {k}");
for w in l.asks.windows(2) {
assert!(w[1].price > w[0].price);
}
if let Some(b) = l.bids.first() {
assert!(b.price < l.mid && b.price >= SPEC.tick - 1e-12);
}
for w in l.bids.windows(2) {
assert!(w[1].price < w[0].price);
}
for level in l.asks.iter().chain(l.bids.iter()) {
let steps = level.price / SPEC.tick;
assert!((steps - steps.round()).abs() < 1e-6, "{}", level.price);
}
}
}
let (p, g) = black_greeks(Right::Call, 1000.0, 1000.0, 21.0 / 252.0, 0.2, 0.0);
let l = ladder(&SPEC, Right::Call, 1000.0, 1000.0, 21.0 / 252.0, 1.0, 0.2, 0.01);
assert!((l.mid - p).abs() < 1e-9);
let half = 0.5 * (l.asks[0].price - l.bids[0].price);
assert!((half - 0.01 * g.vega).abs() <= SPEC.tick_large, "{half} {}", 0.01 * g.vega);
}
#[test]
fn the_spread_grows_with_the_vix_and_the_distance_from_the_money() {
let h = |vix: f64, z: f64| SPEC.half_spread(0.005, vix, z);
assert_eq!(h(15.0, 0.0), 0.005);
assert!(h(30.0, 0.0) > h(15.0, 0.0));
assert!(h(15.0, 2.0) > h(15.0, 1.0));
assert_eq!(h(15.0, -2.0), h(15.0, 2.0));
}
#[test]
fn a_walk_takes_levels_in_order_up_to_its_limit() {
let l = ladder(&SPEC, Right::Put, 1000.0, 980.0, 21.0 / 252.0, 1.0, 0.22, 0.005);
let all = walk(&l.asks, 120.0, None, true);
assert_eq!(all.len(), 3);
assert_eq!(all[2].1, 20.0);
assert_eq!(all.iter().map(|f| f.1).sum::<f64>(), 120.0);
let limited = walk(&l.asks, 120.0, Some(l.asks[0].price), true);
assert_eq!(limited, vec![(l.asks[0].price, 50.0)]);
assert!(walk(&l.bids, 10.0, Some(l.bids[0].price + 1.0), false).is_empty());
let big = walk(&l.asks, 1e6, None, true);
assert_eq!(big.iter().map(|f| f.1).sum::<f64>(), SPEC.levels as f64 * SPEC.displayed_size);
}
#[test]
fn inventory_moves_the_volatility_up_when_short_capped_and_decays() {
assert_eq!(SPEC.inventory_shift(0.0, 0.01), 0.0);
assert!((SPEC.inventory_shift(50.0, 0.01) - 0.005).abs() < 1e-15);
assert!((SPEC.inventory_shift(-50.0, 0.01) + 0.005).abs() < 1e-15);
assert_eq!(SPEC.inventory_shift(1e9, 0.01), 0.04);
let d = SPEC.decay();
assert!((mathx::pow(d, 15.0) - 0.5).abs() < 1e-12);
let p = pressure_after(0.0, 10.0, 50.0, 100.0);
assert_eq!(p, 5.0);
assert_eq!(pressure_after(p, -10.0, 50.0, 100.0), 0.0);
}
#[test]
fn shifted_thetas_stay_non_decreasing() {
let e = [(10.0, 0.001), (31.0, 0.003), (52.0, 0.005)];
let t = dealer_thetas(&e, &[0.05, 0.0, -0.05]);
assert!(t[0] > 0.001);
assert!(t[1] >= t[0] && t[2] >= t[1]);
assert_eq!(dealer_thetas(&e, &[]), vec![0.001, 0.003, 0.005]);
let one = dealer_thetas(&[(21.0, 0.2 * 0.2 * 21.0 / 252.0)], &[0.01]);
assert!(((one[0] * 252.0 / 21.0).sqrt() - 0.21).abs() < 1e-12);
}
fn quotes_from(ssvi: &Ssvi, thetas: &[(i64, f64, f64)], forward: f64, rate: f64) -> Vec<OptionQuote> {
let mut out = Vec::new();
for &(expiry, sessions, theta) in thetas {
let years = sessions / 252.0;
let fwd = forward * mathx::exp(rate * years);
for i in 0..41 {
let strike = 800.0 + 10.0 * i as f64;
let k = mathx::log(strike / fwd);
let iv = mathx::sqrt(ssvi.total_variance(k, theta) / years);
for right in [Right::Call, Right::Put] {
let (mid, greeks) = black_greeks(right, forward, strike, years, iv, rate);
out.push(OptionQuote {
symbol: String::new(),
root: "IDX".into(),
expiry,
right,
strike,
bid: None,
ask: None,
bid_size: 0.0,
ask_size: 0.0,
mid,
fair: mid,
iv,
surface_iv: iv,
greeks,
forward: fwd,
index: forward,
rate,
dividends: 0.0,
sessions_to_expiry: sessions,
multiplier: 100.0,
tick: 0.05,
open_interest: 0.0,
initial_margin: None,
maintenance_margin: None,
});
}
}
}
out
}
fn exact_at_delta(ssvi: &Ssvi, theta: f64, sessions: f64, right: Right, target: f64) -> f64 {
let (spot, rate, years) = (1000.0, 0.03, sessions / 252.0);
let fwd = spot * mathx::exp(rate * years);
let iv = |strike: f64| mathx::sqrt(ssvi.total_variance(mathx::log(strike / fwd), theta) / years);
let (mut lo, mut hi) = (700.0_f64, 1300.0_f64);
for _ in 0..100 {
let mid = 0.5 * (lo + hi);
if black_greeks(right, spot, mid, years, iv(mid), rate).1.delta > target {
lo = mid;
} else {
hi = mid;
}
}
iv(0.5 * (lo + hi))
}
#[test]
fn the_smile_reads_the_forward_and_the_25_delta_points() {
let (sessions, theta) = (34.6, 0.0056);
let flat = quotes_from(&Ssvi::new(0.0, 0.0, 0.5), &[(35, sessions, theta)], 1000.0, 0.03);
let sigma = mathx::sqrt(theta * 252.0 / sessions);
let (atm, put, call) = read_smile(&flat);
for v in [atm, put, call] {
assert!((v.unwrap() - sigma).abs() < 1e-12, "{v:?} {sigma}");
}
let ssvi = Ssvi::new(-0.7, 1.0, 0.5);
let skewed = quotes_from(&ssvi, &[(35, sessions, theta)], 1000.0, 0.03);
let (atm, put, call) = read_smile(&skewed);
assert!((atm.unwrap() - sigma).abs() < 2e-4, "{atm:?} {sigma}");
let (put, call) = (put.unwrap(), call.unwrap());
assert!((put - exact_at_delta(&ssvi, theta, sessions, Right::Put, -0.25)).abs() < 5e-4, "{put}");
assert!((call - exact_at_delta(&ssvi, theta, sessions, Right::Call, 0.25)).abs() < 5e-4, "{call}");
assert!(put > atm.unwrap() && atm.unwrap() > call, "{put} {atm:?} {call}");
let narrow: Vec<OptionQuote> =
skewed.iter().filter(|q| (q.strike - 1000.0).abs() <= 20.0).cloned().collect();
let (atm, put, call) = read_smile(&narrow);
assert!(atm.is_some() && put.is_none() && call.is_none());
let expired: Vec<OptionQuote> = skewed
.iter()
.cloned()
.map(|mut q| {
q.sessions_to_expiry = 0.0;
q.iv = 0.0;
q
})
.collect();
assert_eq!(read_smile(&expired), (None, None, None));
}
#[test]
fn a_chain_from_one_surface_is_clean_and_a_planted_violation_is_found() {
let ssvi = Ssvi::new(-0.7, 1.0, 0.5);
let thetas = [(14, 13.6, 0.0022), (35, 34.6, 0.0056), (56, 55.6, 0.0091)];
let quotes = quotes_from(&ssvi, &thetas, 1000.0, 0.03);
assert_eq!(chain_violations("es, 0.025), Vec::<String>::new());
let mut bad = quotes.clone();
let i = bad.iter().position(|q| q.expiry == 35 && q.right == Right::Call && q.strike == 1000.0).unwrap();
bad[i].mid += 2.0;
let v = chain_violations(&bad, 0.025);
assert!(v.iter().any(|s| s.contains("butterfly")), "{v:?}");
assert!(v.iter().any(|s| s.contains("parity")), "{v:?}");
let inverted = quotes_from(&ssvi, &[(14, 13.6, 0.0060), (35, 34.6, 0.0056)], 1000.0, 0.03);
let v = chain_violations(&inverted, 0.025);
assert!(v.iter().any(|s| s.contains("calendar")), "{v:?}");
}
}