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
use crate::{common::*, constants::*, Inputs, OptionType};
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
impl Inputs {
pub fn calc_price(&self) -> Result<f32, String> {
let (nd1, nd2): (f32, f32) = calc_nd1nd2(&self)?;
let price: f32 = match self.option_type {
OptionType::Call => f32::max(
0.0,
nd1 * self.s * E.powf(-self.q * self.t) - nd2 * self.k * E.powf(-self.r * self.t),
),
OptionType::Put => f32::max(
0.0,
nd2 * self.k * E.powf(-self.r * self.t) - nd1 * self.s * E.powf(-self.q * self.t),
),
};
Ok(price)
}
}