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
//! # blackscholes_python
//! This library provides an simple, lightweight, and efficient (though not heavily optimized) implementation of the Black-Scholes-Merton model for pricing European options.
//!
//! ## Usage
//! Simply create an instance of the `Inputs` struct and call the desired method.
//!
//! Example:
//! ```
//! let inputs: blackscholes::Inputs = blackscholes::Inputs.new(blackscholes::OptionType::Call, 100.0, 100.0, None, 0.05, 0.02, 20.0 / 365.25, Some(0.2));
//! let price: f64 = inputs.calc_price();
//! ```
//!
//! See the [Github Repo](https://github.com/hayden4r4/blackscholes-rust/tree/python_package) for full source code. Other implementations such as a [npm WASM package](https://www.npmjs.com/package/@haydenr4/blackscholes_wasm) and a [pure Rust Crate](https://crates.io/crates/blackscholes) are also available.
use statrs::distribution::{Continuous, ContinuousCDF, Normal};
use std::f64::consts::{E, PI};
use std::fmt::{Display, Formatter, Result};
use pyo3::prelude::*;
#[pymodule]
fn blackscholes_python(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<OptionType>()?;
m.add_class::<Inputs>()?;
Ok(())
}
/// The type of option to be priced. Call or Put.
#[derive(Debug, Clone, Eq, PartialEq)]
#[pyclass(text_signature = "(Call, Put, /)")]
pub enum OptionType {
Call,
Put,
}
impl Display for OptionType {
fn fmt(&self, f: &mut Formatter) -> Result {
match self {
OptionType::Call => write!(f, "Call"),
OptionType::Put => write!(f, "Put"),
}
}
}
#[pymethods]
impl OptionType {
/// Creates an instance of the `OptionType` enum.
/// # Arguments
/// * `option_type` - The type of option to be priced. Call or Put.
/// # Example
/// ```
/// use blackscholes::OptionType;
/// let option_type: OptionType = OptionType::Call;
/// ```
/// # Returns
/// An instance of the `OptionType` enum.
#[new]
pub fn new(option_type: &str) -> Self {
match option_type {
"Call" => OptionType::Call,
"Put" => OptionType::Put,
_ => panic!("Option type must be either Call or Put"),
}
}
/// # Returns
/// A string representation of the `OptionType` enum.
pub fn __str__(&self) -> String {
format!("{}", self)
}
}
/// The inputs to the Black-Scholes-Merton model.
#[derive(Debug, Clone)]
#[pyclass(text_signature = "(option_type, s, k, p, r, q, t, sigma, /)")]
pub struct Inputs {
/// The type of the option (call or put)
pub option_type: OptionType,
/// Stock price
pub s: f64,
/// Strike price
pub k: f64,
/// Option price
pub p: Option<f64>,
/// Risk-free rate
pub r: f64,
/// Dividend yield
pub q: f64,
/// Time to maturity in years
pub t: f64,
/// Volatility
pub sigma: Option<f64>,
}
impl Display for Inputs {
fn fmt(&self, f: &mut Formatter) -> Result {
writeln!(f, "Option type: {}", self.option_type)?;
writeln!(f, "Stock price: {:.2}", self.s)?;
writeln!(f, "Strike price: {:.2}", self.k)?;
match self.p {
Some(p) => writeln!(f, "Option price: {:.2}", p)?,
None => writeln!(f, "Option price: None")?,
}
writeln!(f, "Risk-free rate: {:.4}", self.r)?;
writeln!(f, "Dividend yield: {:.4}", self.q)?;
writeln!(f, "Time to maturity: {:.4}", self.t)?;
match self.sigma {
Some(sigma) => writeln!(f, "Volatility: {:.4}", sigma)?,
None => writeln!(f, "Volatility: None")?,
}
Ok(())
}
}
/// Calculates the d1, d2, nd1, and nd2 values for the option.
/// # Returns
/// Tuple (f64, f64) of the nd1 and nd2 values for the given inputs.
fn nd1nd2(inputs: &Inputs, normal: bool) -> (f64, f64) {
let sigma: f64 = match inputs.sigma {
Some(sigma) => sigma,
None => panic!("Expected an Option(f64) for inputs.sigma, received None"),
};
let nd1nd2 = {
// Calculating numerator of d1
let numd1: f64 =
(inputs.s / inputs.k).ln() + (inputs.r - inputs.q + (sigma.powi(2)) / 2.0) * inputs.t;
// Calculating denominator of d1 and d2
let den: f64 = sigma * (inputs.t.sqrt());
let d1: f64 = numd1 / den;
let d2: f64 = d1 - den;
let d1d2: (f64, f64) = (d1, d2);
// Returns d1 and d2 values if deriving from normal distribution is not necessary
// (i.e. gamma, vega, and theta calculations)
if !normal {
return d1d2;
}
// Creating normal distribution
let n: Normal = Normal::new(0.0, 1.0).unwrap();
// Calculates the nd1 and nd2 values
// Checks if OptionType is Call or Put
let nd1nd2: (f64, f64) = match inputs.option_type {
OptionType::Call => (n.cdf(d1d2.0), n.cdf(d1d2.1)),
OptionType::Put => (n.cdf(-d1d2.0), n.cdf(-d1d2.1)),
};
nd1nd2
};
nd1nd2
}
/// # Returns
/// f64 of the derivative of the nd1.
fn calc_nprimed1(inputs: &Inputs) -> f64 {
let (d1, _): (f64, f64) = nd1nd2(&inputs, false);
// Generate normal probability distribution
let n: Normal = Normal::new(0.0, 1.0).unwrap();
// Get the standard normal probability density function value of d1
let nprimed1: f64 = n.pdf(d1);
nprimed1
}
/// Methods for calculating the price, greeks, and implied volatility of an option.
#[pymethods]
impl Inputs {
/// Creates instance ot the `Inputs` struct.
/// # Arguments
/// * `option_type` - The type of option to be priced.
/// * `s` - The current price of the underlying asset.
/// * `k` - The strike price of the option.
/// * `p` - The dividend yield of the underlying asset.
/// * `r` - The risk-free interest rate.
/// * `q` - The dividend yield of the underlying asset.
/// * `t` - The time to maturity of the option in years.
/// * `sigma` - The volatility of the underlying asset.
/// # Example
/// ```
/// use blackscholes::Inputs;
/// let inputs = Inputs::new(OptionType::Call, 100.0, 100.0, None, 0.05, 0.2, 20/365.25, Some(0.2));
/// ```
/// # Returns
/// An instance of the `Inputs` struct.
#[new]
pub fn new(
option_type: OptionType,
s: f64,
k: f64,
p: Option<f64>,
r: f64,
q: f64,
t: f64,
sigma: Option<f64>,
) -> Self {
Self {
option_type,
s,
k,
p,
r,
q,
t,
sigma,
}
}
/// # Returns
/// string representation of the `Inputs` struct.
pub fn __str__(&self) -> String {
format!(
"OptionType: {}, S: {}, K: {}, P: {}, R: {}, Q: {}, T: {}, Sigma: {}",
self.option_type,
self.s,
self.k,
match self.p {
Some(p) => format!("{}", p),
None => "None".to_string(),
},
self.r,
self.q,
self.t,
match self.sigma {
Some(sigma) => format!("{}", sigma),
None => "None".to_string(),
},
)
}
/// Calculates the price of the option.
/// # Requires
/// s, k, r, q, t, sigma.
/// # Returns
/// f64 of the price of the option.
/// # Example
/// ```
/// use blackscholes::Inputs;
/// let inputs = Inputs::new(OptionType::Call, 100.0, 100.0, None, 0.05, 0.2, 20/365.25, Some(0.2));
/// let price = inputs.calc_price();
/// ```
pub fn calc_price(&self) -> PyResult<f64> {
let (nd1, nd2): (f64, f64) = nd1nd2(self, true);
let price: f64 = match self.option_type {
OptionType::Call => f64::max(
0.0,
nd1 * self.s * E.powf(-self.q * self.t) - nd2 * self.k * E.powf(-self.r * self.t),
),
OptionType::Put => f64::max(
0.0,
nd2 * self.k * E.powf(-self.r * self.t) - nd1 * self.s * E.powf(-self.q * self.t),
),
};
Ok(price)
}
/// Calculates the delta of the option.
/// # Requires
/// s, k, r, q, t, sigma
/// # Returns
/// f64 of the delta of the option.
/// # Example
/// ```
/// use blackscholes::Inputs;
/// let inputs = Inputs::new(OptionType::Call, 100.0, 100.0, None, 0.05, 0.2, 20/365.25, Some(0.2));
/// let delta = inputs.calc_delta();
/// ```
pub fn calc_delta(&self) -> PyResult<f64> {
let (nd1, _): (f64, f64) = nd1nd2(self, true);
let delta: f64 = match self.option_type {
OptionType::Call => nd1 * E.powf(-self.q * self.t),
OptionType::Put => -nd1 * E.powf(-self.q * self.t),
};
Ok(delta)
}
/// Calculates the gamma of the option.
/// # Requires
/// s, k, r, q, t, sigma
/// # Returns
/// f64 of the gamma of the option.
/// # Example
/// ```
/// use blackscholes::Inputs;
/// let inputs = Inputs::new(OptionType::Call, 100.0, 100.0, None, 0.05, 0.2, 20/365.25, Some(0.2));
/// let gamma = inputs.calc_gamma();
/// ```
pub fn calc_gamma(&self) -> PyResult<f64> {
let sigma: f64 = match self.sigma {
Some(sigma) => sigma,
None => panic!("Expected an Option(f64) for self.sigma, received None"),
};
let nprimed1: f64 = calc_nprimed1(self);
let gamma: f64 = E.powf(-self.q * self.t) * nprimed1 / (self.s * sigma * self.t.sqrt());
Ok(gamma)
}
/// Calculates the theta of the option.
/// Uses 365.25 days in a year for calculations.
/// # Requires
/// s, k, r, q, t, sigma
/// # Returns
/// f64 of theta per day (not per year).
/// # Example
/// ```
/// use blackscholes::Inputs;
/// let inputs = Inputs::new(OptionType::Call, 100.0, 100.0, None, 0.05, 0.2, 20/365.25, Some(0.2));
/// let theta = inputs.calc_theta();
/// ```
pub fn calc_theta(&self) -> PyResult<f64> {
let sigma: f64 = match self.sigma {
Some(sigma) => sigma,
None => panic!("Expected an Option(f64) for self.sigma, received None"),
};
let nprimed1: f64 = calc_nprimed1(self);
let (nd1, nd2): (f64, f64) = nd1nd2(self, true);
// Calculation uses 360 for T: Time of days per year.
let theta: f64 = match self.option_type {
OptionType::Call => {
(-(self.s * sigma * E.powf(-self.q * self.t) * nprimed1 / (2.0 * self.t.sqrt()))
- self.r * self.k * E.powf(-self.r * self.t) * nd2
+ self.q * self.s * E.powf(-self.q * self.t) * nd1)
/ 365.25
}
OptionType::Put => {
(-(self.s * sigma * E.powf(-self.q * self.t) * nprimed1 / (2.0 * self.t.sqrt()))
+ self.r * self.k * E.powf(-self.r * self.t) * nd2
- self.q * self.s * E.powf(-self.q * self.t) * nd1)
/ 365.25
}
};
Ok(theta)
}
/// Calculates the vega of the option.
/// # Requires
/// s, k, r, q, t, sigma
/// # Returns
/// f64 of the vega of the option.
/// # Example
/// ```
/// use blackscholes::Inputs;
/// let inputs = Inputs::new(OptionType::Call, 100.0, 100.0, None, 0.05, 0.2, 20/365.25, Some(0.2));
/// let vega = inputs.calc_vega();
/// ```
pub fn calc_vega(&self) -> PyResult<f64> {
let nprimed1: f64 = calc_nprimed1(self);
let vega: f64 = 1.0 / 100.0 * self.s * E.powf(-self.q * self.t) * self.t.sqrt() * nprimed1;
Ok(vega)
}
/// Calculates the rho of the option.
/// # Requires
/// s, k, r, q, t, sigma
/// # Returns
/// f64 of the rho of the option.
/// # Example
/// ```
/// use blackscholes::Inputs;
/// let inputs = Inputs::new(OptionType::Call, 100.0, 100.0, None, 0.05, 0.2, 20/365.25, Some(0.2));
/// let rho = inputs.calc_rho();
/// ```
pub fn calc_rho(&self) -> PyResult<f64> {
let (_, nd2): (f64, f64) = nd1nd2(self, true);
let rho: f64 = match self.option_type {
OptionType::Call => 1.0 / 100.0 * self.k * self.t * E.powf(-self.r * self.t) * nd2,
OptionType::Put => -1.0 / 100.0 * self.k * self.t * E.powf(-self.r * self.t) * nd2,
};
Ok(rho)
}
/// Calculates the implied volatility of the option.
/// Tolerance is the max error allowed for the implied volatility,
/// the lower the tolerance the more iterations will be required.
/// Recommended to be a value between 0.001 - 0.0001 for highest efficiency/accuracy.
/// Initializes estimation of sigma using Brenn and Subrahmanyam (1998) method of calculating initial iv estimation.
/// Uses Newton Raphson algorithm to calculate implied volatility.
/// # Requires
/// s, k, r, q, t, p
/// # Returns
/// f64 of the implied volatility of the option.
/// # Example:
/// ```
/// use blackscholes::Inputs;
/// let inputs = Inputs::new(OptionType::Call, 100.0, 100.0, Some(10), 0.05, 0.02, 20.0 / 365.25, None);
/// let iv = inputs.calc_iv(0.0001);
/// ```
pub fn calc_iv(&self, tolerance: f64) -> PyResult<f64> {
let mut inputs: Inputs = self.clone();
let p: f64 = match inputs.p {
Some(p) => p,
None => panic!("inputs.p must contain Some(f64), found None"),
};
// Initialize estimation of sigma using Brenn and Subrahmanyam (1998) method of calculating initial iv estimation
let mut sigma: f64 = (2.0 * PI / inputs.t).sqrt() * (p / inputs.s);
// Initialize diff to 100 for use in while loop
let mut diff: f64 = 100.0;
// Uses Newton Raphson algorithm to calculate implied volatility
// Test if the difference between calculated option price and actual option price is > tolerance
// If so then iterate until the difference is less than tolerance
while diff.abs() > tolerance {
inputs.sigma = Some(sigma);
diff = inputs.calc_price().unwrap() - p;
sigma -= diff / (inputs.calc_vega().unwrap() * 100.0);
}
Ok(sigma)
}
}