danielhstahl / black_scholes_rust

Rust for black scholes
https://danielhstahl.github.io/black_scholes_rust/dev/bench/
22 stars 8 forks source link

call_iv NaN? #14

Closed brandonros closed 1 year ago

brandonros commented 1 year ago

image

image

fn main() {
    let option_price = 10.74;
    let underlying_price = 380.13; // SPY
    let strike = 380.0;
    let rate = 0.03938; // 10 year
    let maturity = 23.0; // 23 days to expiration (2022-10-27 -> 2022-11-18)
    let iv = black_scholes::call_iv(
        option_price, 
        underlying_price, 
        strike, 
        rate,
        maturity
    ).unwrap();
    println!("{}", iv);
}
$ cargo run
   Compiling options-rs v0.1.0 (/Users/brandonros/Desktop/options-rs)
    Finished dev [unoptimized + debuginfo] target(s) in 0.32s
     Running `target/debug/options-rs`
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: NaN', src/main.rs:13:7
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
brandonros commented 1 year ago

my first guess is the maturity

For example, for an option that expires in 30 days, the Black-Scholes time to expiration input is 30/365 = 0.0822 or 8.22%.

should be more like this

fn days_to_expire_to_maturity(days_to_expire: f64) -> f64 {
    return (days_to_expire / 365.5);
}

fn main() {
    let option_price = 10.74;
    let underlying_price = 380.13; // SPY
    let strike = 380.0;
    let rate = 0.03938; // 10 year
    let maturity = days_to_expire_to_maturity(23.0); // 23 days to expiration (2022-10-27 -> 2022-11-18)
    let iv = black_scholes::call_iv(
        option_price, 
        underlying_price, 
        strike, 
        rate,
        maturity
    ).unwrap();
    println!("{}", iv);
}
$ cargo run
   Compiling options-rs v0.1.0 (/Users/brandonros/Desktop/options-rs)
    Finished dev [unoptimized + debuginfo] target(s) in 0.31s
     Running `target/debug/options-rs`
0.26844376511258566

much better, thanks, sorry for the noise