|
| 1 | +use volsurf::OptionType; |
| 2 | +use wasm_bindgen::prelude::*; |
| 3 | + |
| 4 | +use crate::error::to_js_err; |
| 5 | + |
| 6 | +/// Option type: call or put. Mirrors [`volsurf::OptionType`]. |
| 7 | +#[wasm_bindgen] |
| 8 | +#[derive(Clone, Copy)] |
| 9 | +pub enum WasmOptionType { |
| 10 | + Call, |
| 11 | + Put, |
| 12 | +} |
| 13 | + |
| 14 | +impl From<WasmOptionType> for OptionType { |
| 15 | + fn from(v: WasmOptionType) -> Self { |
| 16 | + match v { |
| 17 | + WasmOptionType::Call => OptionType::Call, |
| 18 | + WasmOptionType::Put => OptionType::Put, |
| 19 | + } |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +impl From<OptionType> for WasmOptionType { |
| 24 | + fn from(v: OptionType) -> Self { |
| 25 | + match v { |
| 26 | + OptionType::Call => WasmOptionType::Call, |
| 27 | + OptionType::Put => WasmOptionType::Put, |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +/// Undiscounted Black (lognormal) option price. |
| 33 | +#[wasm_bindgen] |
| 34 | +pub fn black_price( |
| 35 | + forward: f64, |
| 36 | + strike: f64, |
| 37 | + vol: f64, |
| 38 | + expiry: f64, |
| 39 | + option_type: WasmOptionType, |
| 40 | +) -> Result<f64, JsValue> { |
| 41 | + volsurf::implied::black_price(forward, strike, vol, expiry, option_type.into()) |
| 42 | + .map_err(to_js_err) |
| 43 | +} |
| 44 | + |
| 45 | +/// Undiscounted Bachelier (normal) option price. |
| 46 | +#[wasm_bindgen] |
| 47 | +pub fn normal_price( |
| 48 | + forward: f64, |
| 49 | + strike: f64, |
| 50 | + vol: f64, |
| 51 | + expiry: f64, |
| 52 | + option_type: WasmOptionType, |
| 53 | +) -> Result<f64, JsValue> { |
| 54 | + volsurf::implied::normal_price(forward, strike, vol, expiry, option_type.into()) |
| 55 | + .map_err(to_js_err) |
| 56 | +} |
| 57 | + |
| 58 | +/// Undiscounted displaced-diffusion option price. |
| 59 | +#[wasm_bindgen] |
| 60 | +pub fn displaced_price( |
| 61 | + forward: f64, |
| 62 | + strike: f64, |
| 63 | + vol: f64, |
| 64 | + expiry: f64, |
| 65 | + beta: f64, |
| 66 | + option_type: WasmOptionType, |
| 67 | +) -> Result<f64, JsValue> { |
| 68 | + volsurf::implied::displaced_price(forward, strike, vol, expiry, beta, option_type.into()) |
| 69 | + .map_err(to_js_err) |
| 70 | +} |
| 71 | + |
| 72 | +/// Black (lognormal) implied volatility extraction via Jäckel's algorithm. |
| 73 | +#[wasm_bindgen] |
| 74 | +pub struct WasmBlackImpliedVol; |
| 75 | + |
| 76 | +#[wasm_bindgen] |
| 77 | +impl WasmBlackImpliedVol { |
| 78 | + /// Extract Black implied volatility from an undiscounted option price. |
| 79 | + pub fn compute( |
| 80 | + option_price: f64, |
| 81 | + forward: f64, |
| 82 | + strike: f64, |
| 83 | + expiry: f64, |
| 84 | + option_type: WasmOptionType, |
| 85 | + ) -> Result<f64, JsValue> { |
| 86 | + volsurf::implied::BlackImpliedVol::compute( |
| 87 | + option_price, |
| 88 | + forward, |
| 89 | + strike, |
| 90 | + expiry, |
| 91 | + option_type.into(), |
| 92 | + ) |
| 93 | + .map(|v| v.0) |
| 94 | + .map_err(to_js_err) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +/// Bachelier (normal) implied volatility extraction. |
| 99 | +#[wasm_bindgen] |
| 100 | +pub struct WasmNormalImpliedVol; |
| 101 | + |
| 102 | +#[wasm_bindgen] |
| 103 | +impl WasmNormalImpliedVol { |
| 104 | + /// Extract normal (Bachelier) implied volatility from an undiscounted option price. |
| 105 | + pub fn compute( |
| 106 | + option_price: f64, |
| 107 | + forward: f64, |
| 108 | + strike: f64, |
| 109 | + expiry: f64, |
| 110 | + option_type: WasmOptionType, |
| 111 | + ) -> Result<f64, JsValue> { |
| 112 | + volsurf::implied::NormalImpliedVol::compute( |
| 113 | + option_price, |
| 114 | + forward, |
| 115 | + strike, |
| 116 | + expiry, |
| 117 | + option_type.into(), |
| 118 | + ) |
| 119 | + .map(|v| v.0) |
| 120 | + .map_err(to_js_err) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +/// Displaced-diffusion implied volatility extraction (interpolates normal ↔ Black). |
| 125 | +#[wasm_bindgen] |
| 126 | +pub struct WasmDisplacedImpliedVol { |
| 127 | + inner: volsurf::implied::DisplacedImpliedVol, |
| 128 | +} |
| 129 | + |
| 130 | +#[wasm_bindgen] |
| 131 | +impl WasmDisplacedImpliedVol { |
| 132 | + /// Construct with displacement parameter `beta` ∈ [0, 1]. |
| 133 | + #[wasm_bindgen(constructor)] |
| 134 | + pub fn new(beta: f64) -> Result<WasmDisplacedImpliedVol, JsValue> { |
| 135 | + let inner = volsurf::implied::DisplacedImpliedVol::new(beta).map_err(to_js_err)?; |
| 136 | + Ok(Self { inner }) |
| 137 | + } |
| 138 | + |
| 139 | + #[wasm_bindgen(getter)] |
| 140 | + pub fn beta(&self) -> f64 { |
| 141 | + self.inner.beta() |
| 142 | + } |
| 143 | + |
| 144 | + /// Extract displaced-diffusion implied volatility from an undiscounted option price. |
| 145 | + pub fn compute( |
| 146 | + &self, |
| 147 | + option_price: f64, |
| 148 | + forward: f64, |
| 149 | + strike: f64, |
| 150 | + expiry: f64, |
| 151 | + option_type: WasmOptionType, |
| 152 | + ) -> Result<f64, JsValue> { |
| 153 | + self.inner |
| 154 | + .compute(option_price, forward, strike, expiry, option_type.into()) |
| 155 | + .map(|v| v.0) |
| 156 | + .map_err(to_js_err) |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +/// Log-moneyness: k = ln(K / F). |
| 161 | +#[wasm_bindgen] |
| 162 | +pub fn log_moneyness(strike: f64, forward: f64) -> Result<f64, JsValue> { |
| 163 | + volsurf::conventions::log_moneyness(strike, forward).map_err(to_js_err) |
| 164 | +} |
| 165 | + |
| 166 | +/// Simple moneyness: m = K / F. |
| 167 | +#[wasm_bindgen] |
| 168 | +pub fn moneyness(strike: f64, forward: f64) -> Result<f64, JsValue> { |
| 169 | + volsurf::conventions::moneyness(strike, forward).map_err(to_js_err) |
| 170 | +} |
| 171 | + |
| 172 | +/// Forward price from spot: F = S · exp((r − q) · T). |
| 173 | +#[wasm_bindgen] |
| 174 | +pub fn forward_price( |
| 175 | + spot: f64, |
| 176 | + rate: f64, |
| 177 | + dividend_yield: f64, |
| 178 | + expiry: f64, |
| 179 | +) -> Result<f64, JsValue> { |
| 180 | + volsurf::conventions::forward_price(spot, rate, dividend_yield, expiry).map_err(to_js_err) |
| 181 | +} |
0 commit comments