optionstratlib 0.17.0

OptionStratLib is a comprehensive Rust library for options trading and strategy development across multiple asset classes.
Documentation
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/******************************************************************************
   Author: Joaquín Béjar García
   Email: jb@taunais.com
   Date: 12/01/26
******************************************************************************/

//! Binary option pricing module.
//!
//! Binary options (also called digital options) have a fixed payout if the
//! option expires in-the-money, regardless of how far in-the-money it is.
//!
//! # Variants
//!
//! - **Cash-or-Nothing**: Pays a fixed cash amount Q if the option is ITM
//! - **Asset-or-Nothing**: Pays the asset value S if the option is ITM
//! - **Gap**: Pays the difference between asset price and a "gap" strike
//!
//! # Formulas
//!
//! **Cash-or-Nothing Call**: `C = Q * e^(-rT) * N(d2)`
//! **Cash-or-Nothing Put**: `P = Q * e^(-rT) * N(-d2)`
//!
//! **Asset-or-Nothing Call**: `C = S * e^(-qT) * N(d1)`
//! **Asset-or-Nothing Put**: `P = S * e^(-qT) * N(-d1)`
//!
//! # Greeks Note
//!
//! Binary options have discontinuous Delta at the strike price.
//! Gamma can be extremely large near expiration when near the strike.

use crate::Options;
use crate::error::PricingError;
use crate::greeks::{big_n, d1, d2};
use crate::model::decimal::{d_mul, d_sub};
use crate::model::types::{BinaryType, OptionStyle, OptionType};
use positive::Positive;
use rust_decimal::Decimal;
use rust_decimal::prelude::*;
use rust_decimal_macros::dec;

/// Default cash payout for cash-or-nothing options (when not specified).
const DEFAULT_CASH_PAYOUT: Decimal = dec!(1.0);

/// Prices a Binary option using appropriate closed-form formula.
///
/// # Arguments
///
/// * `option` - The option to price. Must have `OptionType::Binary`.
///
/// # Returns
///
/// The option price as a `Decimal`, or a `PricingError` if pricing fails.
///
/// # Errors
///
/// Returns [`PricingError::UnsupportedOptionType`] when `option` is
/// not an [`OptionType::Binary`] variant, and propagates any
/// `PricingError` raised by intermediate Black–Scholes kernels
/// (typically `PricingError::ExpirationDate` or
/// [`PricingError::Positive`]).
pub fn binary_black_scholes(option: &Options) -> Result<Decimal, PricingError> {
    match &option.option_type {
        OptionType::Binary { binary_type } => match binary_type {
            BinaryType::CashOrNothing => cash_or_nothing_price(option, DEFAULT_CASH_PAYOUT),
            BinaryType::AssetOrNothing => asset_or_nothing_price(option),
            BinaryType::Gap => gap_binary_price(option),
        },
        _ => Err(PricingError::other(
            "binary_black_scholes requires OptionType::Binary",
        )),
    }
}

/// Prices a cash-or-nothing binary option.
///
/// **Call**: Pays Q if S_T > K at expiration
/// **Put**: Pays Q if S_T < K at expiration
///
/// # Formula
/// - Call: `C = Q * e^(-rT) * N(d2)`
/// - Put: `P = Q * e^(-rT) * N(-d2)`
fn cash_or_nothing_price(option: &Options, payout: Decimal) -> Result<Decimal, PricingError> {
    let s = option.underlying_price;
    let k = option.strike_price;
    let r = option.risk_free_rate;
    let q = option.dividend_yield.to_dec();
    let sigma = option.implied_volatility;
    let t = option
        .expiration_date
        .get_years()
        .map_err(|e| PricingError::other(&e.to_string()))?;

    if t == Positive::ZERO {
        return Ok(intrinsic_cash_or_nothing(option, payout));
    }

    if sigma == Positive::ZERO {
        // At zero vol, price is deterministic
        let forward = s * ((r - q) * t).exp();
        let itm = match option.option_style {
            OptionStyle::Call => forward > k,
            OptionStyle::Put => k > forward,
        };
        let discount = (-r * t).exp();
        let value = if itm {
            d_mul(payout, discount, "pricing::binary::cash::zero_vol")?
        } else {
            Decimal::ZERO
        };
        return Ok(apply_side(value, option));
    }

    // Calculate d2 for cash-or-nothing
    let b = r - q;
    let d2_val = d2(s, k, b, t, sigma)
        .map_err(|e: crate::error::GreeksError| PricingError::other(&e.to_string()))?;

    let discount = (-r * t).exp();

    let price = match option.option_style {
        OptionStyle::Call => {
            let n_d2 = big_n(d2_val).unwrap_or(Decimal::ZERO);
            let payout_disc = d_mul(payout, discount, "pricing::binary::cash::call::payout")?;
            d_mul(payout_disc, n_d2, "pricing::binary::cash::call::price")?
        }
        OptionStyle::Put => {
            let n_neg_d2 = big_n(-d2_val).unwrap_or(Decimal::ZERO);
            let payout_disc = d_mul(payout, discount, "pricing::binary::cash::put::payout")?;
            d_mul(payout_disc, n_neg_d2, "pricing::binary::cash::put::price")?
        }
    };

    Ok(apply_side(price, option))
}

/// Prices an asset-or-nothing binary option.
///
/// **Call**: Pays S_T if S_T > K at expiration
/// **Put**: Pays S_T if S_T < K at expiration
///
/// # Formula
/// - Call: `C = S * e^(-qT) * N(d1)`
/// - Put: `P = S * e^(-qT) * N(-d1)`
fn asset_or_nothing_price(option: &Options) -> Result<Decimal, PricingError> {
    let s = option.underlying_price;
    let k = option.strike_price;
    let r = option.risk_free_rate;
    let q = option.dividend_yield.to_dec();
    let sigma = option.implied_volatility;
    let t = option
        .expiration_date
        .get_years()
        .map_err(|e| PricingError::other(&e.to_string()))?;

    if t == Positive::ZERO {
        return Ok(intrinsic_asset_or_nothing(option));
    }

    if sigma == Positive::ZERO {
        let forward = s * ((r - q) * t).exp();
        let itm = match option.option_style {
            OptionStyle::Call => forward > k,
            OptionStyle::Put => k > forward,
        };
        let discount = (-q * t).exp();
        let value = if itm {
            d_mul(s.to_dec(), discount, "pricing::binary::asset::zero_vol")?
        } else {
            Decimal::ZERO
        };
        return Ok(apply_side(value, option));
    }

    let b = r - q;
    let d1_val = d1(s, k, b, t, sigma)
        .map_err(|e: crate::error::GreeksError| PricingError::other(&e.to_string()))?;

    let dividend_discount = (-q * t).exp();

    let price = match option.option_style {
        OptionStyle::Call => {
            let n_d1 = big_n(d1_val).unwrap_or(Decimal::ZERO);
            let s_disc = d_mul(
                s.to_dec(),
                dividend_discount,
                "pricing::binary::asset::call::s_disc",
            )?;
            d_mul(s_disc, n_d1, "pricing::binary::asset::call::price")?
        }
        OptionStyle::Put => {
            let n_neg_d1 = big_n(-d1_val).unwrap_or(Decimal::ZERO);
            let s_disc = d_mul(
                s.to_dec(),
                dividend_discount,
                "pricing::binary::asset::put::s_disc",
            )?;
            d_mul(s_disc, n_neg_d1, "pricing::binary::asset::put::price")?
        }
    };

    Ok(apply_side(price, option))
}

/// Prices a gap binary option.
///
/// Gap options pay the difference between the asset price and a "gap" strike
/// if the option expires in-the-money. For simplicity, we use the standard
/// strike as the gap strike in this implementation.
fn gap_binary_price(option: &Options) -> Result<Decimal, PricingError> {
    // Gap binary is similar to asset-or-nothing minus cash-or-nothing
    // C_gap = C_asset - K * C_cash_unit
    let asset_price = asset_or_nothing_price(option)?;
    let cash_price = cash_or_nothing_price(option, option.strike_price.to_dec())?;

    // For a gap call: pays (S - K) if S > K, otherwise 0
    // This is: asset_or_nothing - K * cash_or_nothing(payout=1)
    let unit_cash = cash_or_nothing_price(option, dec!(1.0))?;

    // Apply side correction (they already have side applied, so we need to be careful)
    let side_multiplier = match option.side {
        crate::model::types::Side::Long => dec!(1),
        crate::model::types::Side::Short => dec!(-1),
    };

    // Remove side from components, compute gap, then reapply.
    // `side_multiplier` is ±1 so the signed renormalisation cannot overflow,
    // but the strike·unit_cash product and the following subtraction must be
    // checked because they fuse two monetary flows into one.
    let asset_unsigned = asset_price * side_multiplier;
    let unit_cash_unsigned = unit_cash * side_multiplier;
    let strike_cash = d_mul(
        option.strike_price.to_dec(),
        unit_cash_unsigned,
        "pricing::binary::gap::strike_cash",
    )?;
    let gap_unsigned = d_sub(asset_unsigned, strike_cash, "pricing::binary::gap::price")?;

    // Suppress unused variable warning
    let _ = cash_price;

    Ok(gap_unsigned * side_multiplier)
}

/// Calculates intrinsic value for cash-or-nothing at expiration.
fn intrinsic_cash_or_nothing(option: &Options, payout: Decimal) -> Decimal {
    let s = option.underlying_price;
    let k = option.strike_price;
    let itm = match option.option_style {
        OptionStyle::Call => s > k,
        OptionStyle::Put => k > s,
    };
    let value = if itm { payout } else { Decimal::ZERO };
    apply_side(value, option)
}

/// Calculates intrinsic value for asset-or-nothing at expiration.
fn intrinsic_asset_or_nothing(option: &Options) -> Decimal {
    let s = option.underlying_price;
    let k = option.strike_price;
    let itm = match option.option_style {
        OptionStyle::Call => s > k,
        OptionStyle::Put => k > s,
    };
    let value = if itm { s.to_dec() } else { Decimal::ZERO };
    apply_side(value, option)
}

/// Applies the side (long/short) multiplier to the price.
fn apply_side(price: Decimal, option: &Options) -> Decimal {
    match option.side {
        crate::model::types::Side::Long => price,
        crate::model::types::Side::Short => -price,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ExpirationDate;
    use crate::assert_decimal_eq;
    use crate::model::types::{OptionStyle, OptionType, Side};
    use positive::pos_or_panic;
    use rust_decimal_macros::dec;

    fn create_binary_option(style: OptionStyle, binary_type: BinaryType) -> Options {
        Options::new(
            OptionType::Binary { binary_type },
            Side::Long,
            "TEST".to_string(),
            Positive::HUNDRED,                          // strike
            ExpirationDate::Days(pos_or_panic!(182.5)), // ~0.5 years
            pos_or_panic!(0.25),                        // volatility
            Positive::ONE,                              // quantity
            Positive::HUNDRED,                          // underlying (ATM)
            dec!(0.05),                                 // risk-free rate
            style,
            Positive::ZERO, // dividend yield
            None,
        )
    }

    #[test]
    fn test_cash_or_nothing_call() {
        let option = create_binary_option(OptionStyle::Call, BinaryType::CashOrNothing);
        let price = binary_black_scholes(&option).unwrap();
        // ATM call should have ~50% chance of payout, discounted
        assert!(
            price > Decimal::ZERO,
            "Cash-or-nothing call should be positive: {}",
            price
        );
        assert!(
            price < dec!(1.0),
            "Cash-or-nothing call payout=1 should be < 1: {}",
            price
        );
    }

    #[test]
    fn test_cash_or_nothing_put() {
        let option = create_binary_option(OptionStyle::Put, BinaryType::CashOrNothing);
        let price = binary_black_scholes(&option).unwrap();
        assert!(
            price > Decimal::ZERO,
            "Cash-or-nothing put should be positive: {}",
            price
        );
        assert!(
            price < dec!(1.0),
            "Cash-or-nothing put payout=1 should be < 1: {}",
            price
        );
    }

    #[test]
    fn test_asset_or_nothing_call() {
        let option = create_binary_option(OptionStyle::Call, BinaryType::AssetOrNothing);
        let price = binary_black_scholes(&option).unwrap();
        // ATM asset-or-nothing should have significant value
        assert!(
            price > dec!(30.0),
            "Asset-or-nothing call should have substantial value: {}",
            price
        );
        assert!(
            price < dec!(100.0),
            "Asset-or-nothing call should be < S: {}",
            price
        );
    }

    #[test]
    fn test_asset_or_nothing_put() {
        let option = create_binary_option(OptionStyle::Put, BinaryType::AssetOrNothing);
        let price = binary_black_scholes(&option).unwrap();
        assert!(
            price > dec!(30.0),
            "Asset-or-nothing put should have substantial value: {}",
            price
        );
    }

    #[test]
    fn test_gap_binary_call() {
        let option = create_binary_option(OptionStyle::Call, BinaryType::Gap);
        let price = binary_black_scholes(&option).unwrap();
        // Gap binary at ATM should have small value (pays S-K when ITM)
        assert!(
            price > dec!(-10.0),
            "Gap binary call should be reasonable: {}",
            price
        );
    }

    #[test]
    fn test_call_put_parity_cash_or_nothing() {
        // Cash-or-nothing call + put should equal discounted payout
        let call = create_binary_option(OptionStyle::Call, BinaryType::CashOrNothing);
        let put = create_binary_option(OptionStyle::Put, BinaryType::CashOrNothing);

        let call_price = binary_black_scholes(&call).unwrap();
        let put_price = binary_black_scholes(&put).unwrap();

        // Call + Put = Q * e^(-rT)
        let t = call.expiration_date.get_years().unwrap();
        let r = call.risk_free_rate;
        let discounted_payout = DEFAULT_CASH_PAYOUT * (-r * t).exp();

        assert_decimal_eq!(call_price + put_price, discounted_payout, dec!(0.01));
    }

    #[test]
    fn test_short_binary_option() {
        let mut option = create_binary_option(OptionStyle::Call, BinaryType::CashOrNothing);
        let long_price = binary_black_scholes(&option).unwrap();

        option.side = Side::Short;
        let short_price = binary_black_scholes(&option).unwrap();

        assert_decimal_eq!(long_price, -short_price, dec!(1e-10));
    }

    #[test]
    fn test_zero_time_to_expiry_itm() {
        let mut option = create_binary_option(OptionStyle::Call, BinaryType::CashOrNothing);
        option.underlying_price = pos_or_panic!(110.0); // ITM
        option.expiration_date = ExpirationDate::Days(Positive::ZERO);
        let price = binary_black_scholes(&option).unwrap();
        assert_decimal_eq!(price, DEFAULT_CASH_PAYOUT, dec!(1e-10));
    }

    #[test]
    fn test_zero_time_to_expiry_otm() {
        let mut option = create_binary_option(OptionStyle::Call, BinaryType::CashOrNothing);
        option.underlying_price = pos_or_panic!(90.0); // OTM
        option.expiration_date = ExpirationDate::Days(Positive::ZERO);
        let price = binary_black_scholes(&option).unwrap();
        assert_decimal_eq!(price, Decimal::ZERO, dec!(1e-10));
    }

    #[test]
    fn test_deep_itm_cash_or_nothing() {
        let mut option = create_binary_option(OptionStyle::Call, BinaryType::CashOrNothing);
        option.underlying_price = pos_or_panic!(150.0); // Deep ITM
        let price = binary_black_scholes(&option).unwrap();
        // Deep ITM should be close to discounted payout
        let t = option.expiration_date.get_years().unwrap();
        let r = option.risk_free_rate;
        let discounted = DEFAULT_CASH_PAYOUT * (-r * t).exp();
        assert!(
            price > discounted * dec!(0.9),
            "Deep ITM should be close to discounted payout: {}",
            price
        );
    }

    #[test]
    fn test_deep_otm_cash_or_nothing() {
        let mut option = create_binary_option(OptionStyle::Call, BinaryType::CashOrNothing);
        option.underlying_price = pos_or_panic!(50.0); // Deep OTM
        let price = binary_black_scholes(&option).unwrap();
        assert!(
            price < dec!(0.1),
            "Deep OTM should be nearly zero: {}",
            price
        );
    }
}