### Prepare Put Option Inputs Source: https://docs.rs/black_scholes/0.11.1/src/black_scholes/lib.rs.html Example setup for calculating put option values using pre-computed discount and volatility factors. ```rust let stock = 5.0; let strike = 4.5; let discount = 0.99; let sigma = 0.3; let maturity: f64 = 2.0; let sqrt_maturity_sigma = sigma * maturity.sqrt(); ``` -------------------------------- ### Example usage of put_rho Source: https://docs.rs/black_scholes/0.11.1/black_scholes/fn.put_rho.html Demonstrates how to call the put_rho function with standard financial parameters. ```rust let stock = 5.0; let strike = 4.5; let rate = 0.05; let sigma = 0.3; let maturity = 1.0; let rho = black_scholes::put_rho( stock, strike, rate, sigma, maturity ); ``` -------------------------------- ### Compute All Prices and Greeks Source: https://docs.rs/black_scholes/0.11.1/black_scholes/fn.compute_all.html Use this function to get all call and put prices and Greeks. It is optimized for scenarios where all information is needed for a given stock and strike price due to internal caching. ```rust let sigma = 0.3; let stock = 5.0; let strike = 4.5; let rate = 0.05; let maturity = 1.0; let all_prices_and_greeks = black_scholes::compute_all( stock, strike, rate, sigma, maturity, ); ``` -------------------------------- ### Get Option Prices and Greeks Source: https://docs.rs/black_scholes/0.11.1/src/black_scholes/lib.rs.html Retrieves all calculated prices and Greeks (Delta, Gamma, Theta, Vega, Rho, Vanna, Vomma, Charm) for both call and put options. This endpoint is optimized for scenarios where all option metrics are needed simultaneously, potentially leveraging cached computations. ```APIDOC ## POST /api/black_scholes/prices_and_greeks ### Description Calculates and returns a comprehensive set of option pricing and Greek values for both call and put options. This endpoint is designed for efficiency when multiple metrics are required, potentially utilizing internal caching mechanisms. ### Method POST ### Endpoint /api/black_scholes/prices_and_greeks ### Parameters #### Request Body - **s** (f64) - Required - Current stock price - **k** (f64) - Required - Strike price - **rate** (f64) - Required - Risk-free interest rate - **maturity** (f64) - Required - Time to maturity in years - **volatility** (f64) - Required - The volatility of the underlying asset ### Request Example { "s": 5.0, "k": 4.5, "rate": 0.05, "maturity": 1.0, "volatility": 0.25 } ### Response #### Success Response (200) - **call_price** (f64) - Price of the call option - **call_delta** (f64) - Delta of the call option - **call_gamma** (f64) - Gamma of the call option - **call_theta** (f64) - Theta of the call option - **call_vega** (f64) - Vega of the call option - **call_rho** (f64) - Rho of the call option - **call_vanna** (f64) - Vanna of the call option - **call_vomma** (f64) - Vomma of the call option - **call_charm** (f64) - Charm of the call option - **put_price** (f64) - Price of the put option - **put_delta** (f64) - Delta of the put option - **put_gamma** (f64) - Gamma of the put option - **put_theta** (f64) - Theta of the put option - **put_vega** (f64) - Vega of the put option - **put_rho** (f64) - Rho of the put option - **put_vanna** (f64) - Vanna of the put option - **put_vomma** (f64) - Vomma of the put option - **put_charm** (f64) - Charm of the put option #### Response Example { "call_price": 0.5, "call_delta": 0.6, "call_gamma": 0.1, "call_theta": -0.02, "call_vega": 0.05, "call_rho": 0.01, "call_vanna": -0.005, "call_vomma": 0.002, "call_charm": -0.0001, "put_price": 0.2, "put_delta": -0.4, "put_gamma": 0.09, "put_theta": -0.015, "put_vega": 0.045, "put_rho": -0.008, "put_vanna": 0.004, "put_vomma": 0.0018, "put_charm": 0.00008 } ``` -------------------------------- ### Black-76 Model Calculation Source: https://docs.rs/black_scholes/0.11.1/src/black_scholes/lib.rs.html Calculates option price and Greeks using the Black-76 model, which is suitable for futures options. This example demonstrates its usage and verifies results against expected values. ```rust let s = 55.; let k = 50.0; let maturity = 1.0; let sigma = 0.15; let rate = 0.0025; let PricesAndGreeks { call_price, call_delta, call_gamma, call_theta, call_vega, call_rho, call_vanna, call_vomma, call_charm, put_price, put_delta, put_gamma, put_theta, put_vega, put_rho, put_vanna, put_vomma, put_charm, } = black76(s, k, rate, sigma, maturity); assert_approx_eq!(call_price, 6.234516); assert_approx_eq!(call_delta, 0.759371); assert_approx_eq!(call_gamma, 0.037478); assert_approx_eq!(call_theta, -1.259855); assert_approx_eq!(call_vega, 17.005889); assert_approx_eq!(call_rho, -6.234516); assert_approx_eq!(call_vanna, -1.155166); assert_approx_eq!(call_vomma, 45.134728); assert_approx_eq!(call_charm, -0.088535); // value not verified externally :( assert_approx_eq!(put_price, 1.247001); assert_approx_eq!(put_delta, -0.238131); assert_approx_eq!(put_gamma, 0.037478); assert_approx_eq!(put_theta, -1.272324); assert_approx_eq!(put_vega, 17.005889); assert_approx_eq!(put_rho, -1.247001); assert_approx_eq!(put_vanna, -1.155166); assert_approx_eq!(put_vomma, 45.134728); ``` -------------------------------- ### GET /put_vanna Source: https://docs.rs/black_scholes/0.11.1/black_scholes/fn.put_vanna.html Calculates the Vanna of a Black-Scholes put option, which measures the sensitivity of delta to changes in volatility. ```APIDOC ## GET /put_vanna ### Description Returns the vanna of a BS put option. Vanna measures the sensitivity of delta to changes in volatility. For put options, vanna is identical to call options. ### Parameters #### Query Parameters - **s** (f64) - Required - Current stock price - **k** (f64) - Required - Strike price - **rate** (f64) - Required - Risk-free interest rate - **sigma** (f64) - Required - Volatility of the underlying asset - **maturity** (f64) - Required - Time to maturity in years ### Request Example GET /put_vanna?s=5.0&k=4.5&rate=0.05&sigma=0.3&maturity=1.0 ### Response #### Success Response (200) - **vanna** (f64) - The calculated vanna value ``` -------------------------------- ### Calculate Implied Volatility for Put Option (with initial guess) Source: https://docs.rs/black_scholes/0.11.1/src/black_scholes/lib.rs.html Calculates the implied volatility for a put option given its market price, parameters, and an initial guess for volatility. This is useful when a specific starting point for the numerical solver is desired. ```APIDOC ## POST /api/black_scholes/put_iv_guess ### Description Calculates the implied volatility for a put option using the Black-Scholes model with a provided initial guess. This function employs the Newton-Raphson method for finding the root of the Black-Scholes equation. ### Method POST ### Endpoint /api/black_scholes/put_iv_guess ### Parameters #### Request Body - **price** (f64) - Required - Market price of the put option - **s** (f64) - Required - Current stock price - **k** (f64) - Required - Strike price - **rate** (f64) - Required - Risk-free interest rate - **maturity** (f64) - Required - Time to maturity in years - **initial_guess** (f64) - Required - An initial guess for the implied volatility ### Request Example { "price": 0.3, "s": 5.0, "k": 4.5, "rate": 0.05, "maturity": 1.0, "initial_guess": 0.3 } ### Response #### Success Response (200) - **volatility** (f64) - The calculated implied volatility #### Response Example { "volatility": 0.35 } ``` -------------------------------- ### Compare Black-Scholes and BSM Results Source: https://docs.rs/black_scholes/0.11.1/src/black_scholes/lib.rs.html Asserts that the results from the Black-Scholes model and the Black-Scholes-Merton model are approximately equal for various option Greeks. This is useful for verifying consistency when dividend yields are zero. ```rust assert_abs_diff_eq!(bs_result.put_delta, bsm_result.put_delta, epsilon = 1e-10); assert_abs_diff_eq!(bs_result.call_gamma, bsm_result.call_gamma, epsilon = 1e-10); assert_abs_diff_eq!(bs_result.put_gamma, bsm_result.put_gamma, epsilon = 1e-10); assert_abs_diff_eq!(bs_result.call_theta, bsm_result.call_theta, epsilon = 1e-10); assert_abs_diff_eq!(bs_result.put_theta, bsm_result.put_theta, epsilon = 1e-10); assert_abs_diff_eq!(bs_result.call_vega, bsm_result.call_vega, epsilon = 1e-10); assert_abs_diff_eq!(bs_result.put_vega, bsm_result.put_vega, epsilon = 1e-10); assert_abs_diff_eq!(bs_result.call_rho, bsm_result.call_rho, epsilon = 1e-10); assert_abs_diff_eq!(bs_result.put_rho, bsm_result.put_rho, epsilon = 1e-10); assert_abs_diff_eq!(bs_result.call_vanna, bsm_result.call_vanna, epsilon = 1e-10); assert_abs_diff_eq!(bs_result.put_vanna, bsm_result.put_vanna, epsilon = 1e-10); assert_abs_diff_eq!(bs_result.call_vomma, bsm_result.call_vomma, epsilon = 1e-10); assert_abs_diff_eq!(bs_result.put_vomma, bsm_result.put_vomma, epsilon = 1e-10); assert_abs_diff_eq!(bs_result.call_charm, bsm_result.call_charm, epsilon = 1e-10); assert_abs_diff_eq!(bs_result.put_charm, bsm_result.put_charm, epsilon = 1e-10); ``` -------------------------------- ### Use black76 Function for Options Pricing Source: https://docs.rs/black_scholes/0.11.1/black_scholes/fn.black76.html Demonstrates how to use the black76 function to calculate call and put prices and Greeks for options on futures. Ensure all parameters are correctly set for accurate results. ```rust let forward_price = 55.0; let strike = 50.0; let rate = 0.05; let sigma = 0.2; let maturity = 1.0; let all_prices_and_greeks = black_scholes::black76( forward_price, strike, rate, sigma, maturity, ); ``` -------------------------------- ### Test compute_all Functionality (Zero Interest Rate) Source: https://docs.rs/black_scholes/0.11.1/src/black_scholes/lib.rs.html Verifies the `compute_all` function by comparing its output for call and put prices and Greeks against individual Greek calculation functions when the interest rate is zero. Uses `assert_approx_eq!` for floating-point comparisons. ```rust let s = 550.88; let sigma = 0.37; let k = 510.0; let rate = 0.0; let maturity = 0.09; let PricesAndGreeks { call_price, call_delta, call_gamma, call_theta, call_vega, call_rho, call_vanna: call_vanna_result, call_vomma: call_vomma_result, call_charm: call_charm_result, put_price, put_delta, put_gamma, put_theta, put_vega, put_rho, .. } = compute_all(s, k, rate, sigma, maturity); assert_approx_eq!(call_price, call(s, k, rate, sigma, maturity)); assert_approx_eq!(call_delta, 0.773418151717179); assert_approx_eq!(call_gamma, 0.00492419827941365); assert_approx_eq!(call_theta, -102.28760152696525); assert_approx_eq!(call_vega, 49.761535877983086); assert_approx_eq!(call_rho, 33.90346975078879); assert_approx_eq!(put_price, put(s, k, rate, sigma, maturity)); assert_approx_eq!(put_delta, -0.22658184828282102); assert_approx_eq!(put_gamma, 0.00492419827941365); assert_approx_eq!(put_theta, -102.28760152696525); assert_approx_eq!(put_vega, 49.761535877983086); assert_approx_eq!(put_rho, -11.996530249211213); assert_approx_eq!(call_vanna_result, call_vanna(s, k, rate, sigma, maturity)); assert_approx_eq!(call_vomma_result, call_vomma(s, k, rate, sigma, maturity)); assert_approx_eq!(call_charm_result, call_charm(s, k, rate, sigma, maturity)); ``` -------------------------------- ### compute_all Source: https://docs.rs/black_scholes/0.11.1/src/black_scholes/lib.rs.html Calculates call and put prices and Greeks using the standard Black-Scholes formula. ```APIDOC ## compute_all ### Description Computes call and put prices and Greeks for an option using the Black-Scholes formula without dividend yield. ### Parameters - **stock** (f64) - Required - Current stock price - **strike** (f64) - Required - Strike price - **rate** (f64) - Required - Risk-free interest rate - **sigma** (f64) - Required - Volatility of the underlying asset - **maturity** (f64) - Required - Time to maturity in years ### Response - **PricesAndGreeks** (struct) - Contains call and put prices, delta, gamma, theta, vega, rho, vanna, vomma, and charm. ``` -------------------------------- ### Compute All Black-Scholes Metrics Source: https://docs.rs/black_scholes/0.11.1/src/black_scholes/lib.rs.html Calculates all prices and Greeks for European options using the Black-Scholes formula. Handles edge cases where volatility is zero. ```rust let sigma = 0.3; let stock = 5.0; let strike = 4.5; let rate = 0.05; let maturity = 1.0; let all_prices_and_greeks = black_scholes::compute_all( stock, strike, rate, sigma, maturity, ); ``` ```rust pub fn compute_all( stock: f64, strike: f64, rate: f64, sigma: f64, maturity: f64, ) -> PricesAndGreeks { let discount = (-rate * maturity).exp(); let sqrt_maturity = maturity.sqrt(); let sqrt_maturity_sigma = sqrt_maturity * sigma; let k_discount = strike * discount; if sqrt_maturity_sigma > 0.0 { let d1 = d1(stock, strike, discount, sqrt_maturity_sigma); let d2 = d1 - sqrt_maturity_sigma; let cdf_d1 = cum_norm(d1); let cdf_d2 = cum_norm(d2); let pdf_d1 = inc_norm(d1); let call_price = stock * cdf_d1 - k_discount * cdf_d2; let call_delta = cdf_d1; let call_gamma = pdf_d1 / (stock * sqrt_maturity_sigma); let call_theta = -stock * pdf_d1 * sigma / (2.0 * sqrt_maturity) - rate * k_discount * cdf_d2; let call_vega = stock * pdf_d1 * sqrt_maturity; let call_rho = k_discount * maturity * cdf_d2; let call_vanna = call_vega / stock * (1.0 - d1 / sqrt_maturity_sigma); let call_vomma = call_vega * d1 * d2 / sigma; let call_charm = -pdf_d1 * (2.0 * rate * maturity - d2 * sqrt_maturity_sigma) / (2.0 * maturity * sqrt_maturity_sigma); let put_price = call_price + k_discount - stock; let put_delta = cdf_d1 - 1.0; let put_gamma = call_gamma; let put_theta = -stock * pdf_d1 * sigma / (2.0 * sqrt_maturity) + rate * k_discount * (1.0 - cdf_d2); let put_vega = call_vega; let put_rho = -1.0 * k_discount * maturity * (1.0 - cdf_d2); let put_vanna = call_vanna; let put_vomma = call_vomma; let put_charm = call_charm; PricesAndGreeks { call_price, call_delta, call_gamma, call_theta, call_vega, call_rho, call_vanna, call_vomma, call_charm, put_price, put_delta, put_gamma, put_theta, put_vega, put_rho, put_vanna, put_vomma, put_charm, } } else { PricesAndGreeks { call_price: max_or_zero(stock - strike), call_delta: if stock > strike { 1.0 } else { 0.0 }, call_gamma: 0.0, call_theta: 0.0, call_vega: 0.0, call_rho: 0.0, call_vanna: 0.0, call_vomma: 0.0, call_charm: 0.0, put_price: max_or_zero(strike - stock), put_delta: if strike > stock { -1.0 } else { 0.0 }, put_gamma: 0.0, put_theta: 0.0, put_vega: 0.0, put_rho: 0.0, put_vanna: 0.0, put_vomma: 0.0, put_charm: 0.0, } } } ``` -------------------------------- ### Test compute_all Functionality (Non-Zero Interest Rate) Source: https://docs.rs/black_scholes/0.11.1/src/black_scholes/lib.rs.html Tests the `compute_all` function with a non-zero interest rate. It compares the computed call and put prices and Greeks against their respective individual calculation functions. ```rust let s = 550.88; let sigma = 0.37; let k = 510.0; let rate = 0.05; let maturity = 0.09; let result = compute_all(s, k, rate, sigma, maturity); assert_approx_eq!(result.call_price, call(s, k, rate, sigma, maturity)); assert_approx_eq!(result.call_delta, call_delta(s, k, rate, sigma, maturity)); assert_approx_eq!(result.call_gamma, call_gamma(s, k, rate, sigma, maturity)); assert_approx_eq!(result.call_theta, call_theta(s, k, rate, sigma, maturity)); assert_approx_eq!(result.call_vega, call_vega(s, k, rate, sigma, maturity)); assert_approx_eq!(result.call_rho, call_rho(s, k, rate, sigma, maturity)); assert_approx_eq!(result.put_price, put(s, k, rate, sigma, maturity)); assert_approx_eq!(result.put_delta, put_delta(s, k, rate, sigma, maturity)); assert_approx_eq!(result.put_gamma, put_gamma(s, k, rate, sigma, maturity)); assert_approx_eq!(result.put_theta, put_theta(s, k, rate, sigma, maturity)); assert_approx_eq!(result.put_vega, put_vega(s, k, rate, sigma, maturity)); assert_approx_eq!(result.put_rho, put_rho(s, k, rate, sigma, maturity)); ``` -------------------------------- ### Comprehensive Calculation Source: https://docs.rs/black_scholes/0.11.1/black_scholes Calculates all option prices and Greeks efficiently using caching for complex computations. ```APIDOC ## GET /black_scholes/compute_all ### Description Returns call and put prices and Greeks. Due to caching the complex computations (such as N(d1)), this implementation is faster if you need to obtain all the information for a given stock price and strike price. ### Method GET ### Endpoint /black_scholes/compute_all ### Parameters #### Query Parameters - **s** (f64) - Required - Current stock price. - **k** (f64) - Required - Strike price of the option. - **t** (f64) - Required - Time to expiration in years. - **r** (f64) - Required - Risk-free interest rate. - **sigma** (f64) - Required - Volatility of the underlying asset. ### Response #### Success Response (200) - **PricesAndGreeks** (object) - Container for option prices and Greeks. - **call_price** (f64) - The calculated call option price. - **put_price** (f64) - The calculated put option price. - **call_delta** (f64) - The Delta of the call option. - **put_delta** (f64) - The Delta of the put option. - **call_gamma** (f64) - The Gamma of the call option. - **put_gamma** (f64) - The Gamma of the put option. - **call_theta** (f64) - The Theta of the call option. - **put_theta** (f64) - The Theta of the put option. - **call_vega** (f64) - The Vega of the call option. - **put_vega** (f64) - The Vega of the put option. - **call_rho** (f64) - The Rho of the call option. - **put_rho** (f64) - The Rho of the put option. - **call_vanna** (f64) - The Vanna of the call option. - **put_vanna** (f64) - The Vanna of the put option. - **call_vomma** (f64) - The Vomma of the call option. - **put_vomma** (f64) - The Vomma of the put option. - **call_charm** (f64) - The Charm of the call option. - **put_charm** (f64) - The Charm of the put option. #### Response Example ```json { "call_price": 10.50, "put_price": 5.25, "call_delta": 0.65, "put_delta": -0.35, "call_gamma": 0.05, "put_gamma": 0.05, "call_theta": -0.01, "put_theta": -0.008, "call_vega": 0.15, "put_vega": 0.15, "call_rho": 0.05, "put_rho": -0.04, "call_vanna": 0.20, "put_vanna": -0.20, "call_vomma": 0.50, "put_vomma": 0.50, "call_charm": -0.005, "put_charm": 0.004 } ``` ``` -------------------------------- ### Calculate put option price with put_discount Source: https://docs.rs/black_scholes/0.11.1/black_scholes/fn.put_discount.html Computes the put option price using pre-calculated discount and volatility factors. ```rust let stock = 5.0; let strike = 4.5; let discount = 0.99; let sigma = 0.3; let maturity: f64 = 2.0; let sqrt_maturity_sigma = sigma * maturity.sqrt(); let price = black_scholes::put_discount( stock, strike, discount, sqrt_maturity_sigma ); ``` -------------------------------- ### Compute Black-Scholes-Merton Metrics Source: https://docs.rs/black_scholes/0.11.1/src/black_scholes/lib.rs.html Calculates option prices and Greeks using the Black-Scholes-Merton model, which incorporates continuous dividend yields. This function is equivalent to `compute_all` when the dividend yield is zero. ```rust let stock = 5.0; let strike = 4.5; let sigma = 0.3; let risk_free_rate = 0.05; ``` -------------------------------- ### Test bsm_compute_all Equivalence to compute_all (Zero Dividend Yield) Source: https://docs.rs/black_scholes/0.11.1/src/black_scholes/lib.rs.html Compares the output of `bsm_compute_all` with `compute_all` when the dividend yield `q` is zero. This test verifies that `bsm_compute_all` behaves identically to `compute_all` under these conditions. ```rust let s = 550.88; let sigma = 0.37; let k = 510.0; let rate = 0.05; let q = 0.0; let maturity = 0.09; let r0 = compute_all(s, k, rate, sigma, maturity); let r1 = bsm_compute_all(s, k, sigma, rate, q, maturity); let PricesAndGreeks { call_price, call_delta, call_gamma, call_theta, call_vega, ``` -------------------------------- ### Helper Utilities for Testing Source: https://docs.rs/black_scholes/0.11.1/src/black_scholes/lib.rs.html Utility functions and macros used to facilitate testing, including random number generation and approximate equality assertions. ```rust use rand::SeedableRng; use rand::distr::{Distribution, Uniform}; use rand::rngs::StdRng; use std::f64::consts::PI; fn get_rng_seed(seed: [u8; 32]) -> StdRng { SeedableRng::from_seed(seed) } fn get_over_region(lower: f64, upper: f64, rand: f64) -> f64 { lower + (upper - lower) * rand } macro_rules! assert_approx_eq { ($a:expr, $b:expr) => {{ let (a, b) = (&$a, &$b); assert!( (*a - *b).abs() < 1.0e-6, "{} is not approximately equal to {}", *a, *b ); }}; } ``` -------------------------------- ### Function: compute_all Source: https://docs.rs/black_scholes/0.11.1/black_scholes/all.html Computes all option prices and greeks for a given set of parameters. ```APIDOC ## Function: compute_all ### Description Computes all option prices and greeks for a given set of parameters. ### Parameters * **spot_price** (f64) - The current price of the underlying asset. * **strike_price** (f64) - The strike price of the option. * **time_to_expiry** (f64) - The time to expiration in years. * **volatility** (f64) - The volatility of the underlying asset. * **risk_free_rate** (f64) - The risk-free interest rate. * **dividend_yield** (f64) - The dividend yield of the underlying asset. * **option_type** (enum) - The type of option ('call' or 'put'). ### Returns * PricesAndGreeks - A struct containing the calculated prices and greeks. ``` -------------------------------- ### Compute All Black-Scholes Prices and Greeks Source: https://docs.rs/black_scholes/0.11.1/src/black_scholes/lib.rs.html Calculates call and put prices and their associated Greeks using the Black-Scholes model. Handles the edge case where volatility is zero by returning simplified values. Requires `cum_norm` and `inc_norm` functions to be defined elsewhere. ```rust pub fn bsm_compute_all( stock: f64, strike: f64, sigma: f64, risk_free_rate: f64, dividend_yield: f64, maturity: f64, ) -> PricesAndGreeks { let dividend = (-dividend_yield * maturity).exp(); let discount = (-risk_free_rate * maturity).exp(); let sqrt_maturity = maturity.sqrt(); let sqrt_maturity_sigma = sqrt_maturity * sigma; let k_discount = strike * discount; if sqrt_maturity_sigma > 0.0 { let d1 = ((stock / strike).ln() + (risk_free_rate - dividend_yield + 0.5 * sigma.powi(2)) * maturity) / sqrt_maturity_sigma; let d2 = d1 - sqrt_maturity_sigma; let cdf_d1 = cum_norm(d1); let cdf_d2 = cum_norm(d2); let pdf_d1 = inc_norm(d1); let call_price = stock * dividend * cdf_d1 - k_discount * cdf_d2; let call_delta = dividend * cdf_d1; let call_gamma = dividend * pdf_d1 / (stock * sqrt_maturity_sigma); let call_theta = -dividend * stock * pdf_d1 * sigma / (2.0 * sqrt_maturity) - risk_free_rate * k_discount * cdf_d2 + dividend_yield * stock * dividend * cdf_d1; let call_vega = stock * pdf_d1 * sqrt_maturity; let call_rho = k_discount * maturity * cdf_d2; let call_vanna = call_vega / stock * (1.0 - d1 / sqrt_maturity_sigma); let call_vomma = call_vega * d1 * d2 / sigma; let charm_part = dividend * pdf_d1 * (2.0 * (risk_free_rate - dividend_yield) * maturity - d2 * sqrt_maturity_sigma) / (2.0 * maturity * sqrt_maturity_sigma); let call_charm = dividend_yield * dividend * cdf_d1 - charm_part; let put_price = call_price + k_discount - stock * dividend; let put_delta = dividend * (cdf_d1 - 1.0); let put_gamma = call_gamma; let put_theta = -dividend * stock * pdf_d1 * sigma / (2.0 * sqrt_maturity) + risk_free_rate * k_discount * (1.0 - cdf_d2) - dividend_yield * stock * dividend * (1.0 - cdf_d1); let put_vega = call_vega; let put_rho = -k_discount * maturity * (1.0 - cdf_d2); let put_vanna = call_vanna; let put_vomma = call_vomma; let put_charm = -dividend_yield * dividend * (1.0 - cdf_d1) - charm_part; PricesAndGreeks { call_price, call_delta, call_gamma, call_theta, call_vega, call_rho, call_vanna, call_vomma, call_charm, put_price, put_delta, put_gamma, put_theta, put_vega, put_rho, put_vanna, put_vomma, put_charm, } } else { PricesAndGreeks { call_price: max_or_zero(stock - strike), call_delta: if stock > strike { 1.0 } else { 0.0 }, call_gamma: 0.0, call_theta: 0.0, call_vega: 0.0, call_rho: 0.0, call_vanna: 0.0, call_vomma: 0.0, call_charm: 0.0, put_price: max_or_zero(strike - stock), put_delta: if strike > stock { -1.0 } else { 0.0 }, put_gamma: 0.0, put_theta: 0.0, put_vega: 0.0, put_rho: 0.0, put_vanna: 0.0, put_vomma: 0.0, put_charm: 0.0, } } } ``` -------------------------------- ### Struct: PricesAndGreeks Source: https://docs.rs/black_scholes/0.11.1/black_scholes/all.html Represents the calculated prices and greeks for an option. ```APIDOC ## Struct: PricesAndGreeks ### Description Represents the calculated prices and greeks for an option. ### Fields * **call_price** (f64) - The price of the call option. * **put_price** (f64) - The price of the put option. * **call_delta** (f64) - The delta of the call option. * **put_delta** (f64) - The delta of the put option. * **call_gamma** (f64) - The gamma of the call option. * **put_gamma** (f64) - The gamma of the put option. * **call_theta** (f64) - The theta of the call option. * **put_theta** (f64) - The theta of the put option. * **call_vega** (f64) - The vega of the call option. * **put_vega** (f64) - The vega of the put option. * **call_rho** (f64) - The rho of the call option. * **put_rho** (f64) - The rho of the put option. * **call_charm** (f64) - The charm of the call option. * **put_charm** (f64) - The charm of the put option. * **call_vanna** (f64) - The vanna of the call option. * **put_vanna** (f64) - The vanna of the put option. * **call_vomma** (f64) - The vomma of the call option. * **put_vomma** (f64) - The vomma of the put option. * **call_iv** (f64) - The implied volatility of the call option. * **put_iv** (f64) - The implied volatility of the put option. ``` -------------------------------- ### bsm_compute_all Function Source: https://docs.rs/black_scholes/0.11.1/black_scholes/fn.bsm_compute_all.html Computes call and put prices and Greeks using the Black-Scholes-Merton formula, which accounts for continuous dividends. ```APIDOC ## bsm_compute_all ### Description Returns call and put prices and Greeks using Black-Scholes-Merton formula. The Black-Scholes-Merton model extends the Black-Scholes model to account for continuous dividends. If `dividend_yield` is 0, this gives same results as `compute_all` using Black-Scholes formula but `compute_all` will be slightly less compute intensive. ### Method N/A (This is a function, not an API endpoint) ### Endpoint N/A ### Parameters #### Arguments - **stock** (f64) - Required - Current stock price - **strike** (f64) - Required - Strike price - **sigma** (f64) - Required - Volatility of the underlying asset - **risk_free_rate** (f64) - Required - Annualized continuously compounded risk-free interest rate - **dividend_yield** (f64) - Required - Annualized continuously compounded dividend yield - **maturity** (f64) - Required - Time to maturity in years ### Returns A `PricesAndGreeks` struct containing all prices and Greeks. ### Examples ```rust let stock = 5.0; let strike = 4.5; let sigma = 0.3; let risk_free_rate = 0.05; let dividend_yield = 0.02; let maturity = 1.0; let all_prices_and_greeks = black_scholes::bsm_compute_all( stock, strike, sigma, risk_free_rate, dividend_yield, maturity, ); ``` ``` -------------------------------- ### bsm_compute_all Function Source: https://docs.rs/black_scholes/0.11.1/src/black_scholes/lib.rs.html Computes all call and put prices and Greeks using the Black-Scholes model. ```APIDOC ## POST /api/black_scholes/compute_all ### Description Computes all call and put prices and Greeks using the Black-Scholes model. ### Method POST ### Endpoint /api/black_scholes/compute_all ### Parameters #### Request Body - **stock** (f64) - Required - The current price of the underlying stock. - **strike** (f64) - Required - The strike price of the option. - **sigma** (f64) - Required - The volatility of the underlying stock. - **risk_free_rate** (f64) - Required - The risk-free interest rate. - **dividend_yield** (f64) - Required - The dividend yield of the underlying stock. - **maturity** (f64) - Required - The time to maturity of the option in years. ### Request Example ```json { "stock": 100.0, "strike": 100.0, "sigma": 0.2, "risk_free_rate": 0.05, "dividend_yield": 0.02, "maturity": 1.0 } ``` ### Response #### Success Response (200) - **call_price** (f64) - The calculated price of the call option. - **call_delta** (f64) - The delta of the call option. - **call_gamma** (f64) - The gamma of the call option. - **call_theta** (f64) - The theta of the call option. - **call_vega** (f64) - The vega of the call option. - **call_rho** (f64) - The rho of the call option. - **call_vanna** (f64) - The vanna of the call option. - **call_vomma** (f64) - The vomma of the call option. - **call_charm** (f64) - The charm of the call option. - **put_price** (f64) - The calculated price of the put option. - **put_delta** (f64) - The delta of the put option. - **put_gamma** (f64) - The gamma of the put option. - **put_theta** (f64) - The theta of the put option. - **put_vega** (f64) - The vega of the put option. - **put_rho** (f64) - The rho of the put option. - **put_vanna** (f64) - The vanna of the put option. - **put_vomma** (f64) - The vomma of the put option. - **put_charm** (f64) - The charm of the put option. #### Response Example ```json { "call_price": 10.45, "call_delta": 0.57, "call_gamma": 0.03, "call_theta": -0.05, "call_vega": 0.25, "call_rho": 0.40, "call_vanna": -0.10, "call_vomma": 0.50, "call_charm": -0.02, "put_price": 7.80, "put_delta": -0.43, "put_gamma": 0.03, "put_theta": -0.04, "put_vega": 0.25, "put_rho": -0.30, "put_vanna": -0.10, "put_vomma": 0.50, "put_charm": 0.01 } ``` ``` -------------------------------- ### Calculate Black-Scholes-Merton prices and Greeks Source: https://docs.rs/black_scholes/0.11.1/black_scholes/fn.bsm_compute_all.html Computes call and put prices and Greeks based on stock price, strike, volatility, risk-free rate, dividend yield, and maturity. ```rust let stock = 5.0; let strike = 4.5; let sigma = 0.3; let risk_free_rate = 0.05; let dividend_yield = 0.02; let maturity = 1.0; let all_prices_and_greeks = black_scholes::bsm_compute_all( stock, strike, sigma, risk_free_rate, dividend_yield, maturity, ); ``` -------------------------------- ### Test Full Computation Suite Source: https://docs.rs/black_scholes/0.11.1/src/black_scholes/lib.rs.html Validates that all Greeks and prices are correctly computed as zero or intrinsic values under zero-volatility conditions. ```rust #[test] fn compute_all_with_zero_volatility() { let s = 5.0; let k = 4.5; let rate = 0.05; let sigma = 0.0; let maturity = 1.0; let result = compute_all(s, k, rate, sigma, maturity); assert_eq!(result.call_price, (s - k).max(0.0)); assert_eq!(result.put_price, (k - s).max(0.0)); assert_eq!(result.call_delta, if s > k { 1.0 } else { 0.0 }); assert_eq!(result.put_delta, if k > s { -1.0 } else { 0.0 }); assert_eq!(result.call_gamma, 0.0); assert_eq!(result.put_gamma, 0.0); assert_eq!(result.call_theta, 0.0); assert_eq!(result.put_theta, 0.0); assert_eq!(result.call_vega, 0.0); assert_eq!(result.put_vega, 0.0); assert_eq!(result.call_rho, 0.0); assert_eq!(result.put_rho, 0.0); assert_eq!(result.call_vanna, 0.0); assert_eq!(result.put_vanna, 0.0); assert_eq!(result.call_vomma, 0.0); assert_eq!(result.put_vomma, 0.0); assert_eq!(result.call_charm, 0.0); assert_eq!(result.put_charm, 0.0); } ``` -------------------------------- ### Option Pricing Formula Tests Source: https://docs.rs/black_scholes/0.11.1/src/black_scholes/lib.rs.html Tests for call and put option pricing functions, including edge cases like zero volatility. ```rust #[test] fn call_formula_works() { assert_approx_eq!(call(5.0, 4.5, 0.05, 0.3, 1.0), 0.9848721043419868); } #[test] fn call_formula_works_close_maturity() { assert_approx_eq!( call(14341.0, 14000.0, 0.1, 0.2125, 0.25 / 365.0), 341.95898982726794 ); } #[test] fn call_formula_works_with_zero_vol() { assert_eq!(call(5.0, 4.5, 0.05, 0.3, 0.0), 0.5); } #[test] fn put_formula_works() { assert_approx_eq!(put(5.0, 4.5, 0.05, 0.3, 1.0), 0.2654045145951993); } #[test] fn put_formula_works_with_zero_vol() { assert_eq!(put(5.0, 4.5, 0.05, 0.3, 0.0), 0.0); } ``` -------------------------------- ### BSM Compute All with Positive Dividend Source: https://docs.rs/black_scholes/0.11.1/src/black_scholes/lib.rs.html Tests the `bsm_compute_all` function with a positive dividend yield. It asserts that call prices are lower and put prices are higher compared to the Black-Scholes model without dividends. ```rust let s = 100.0; let k = 100.0; let sigma = 0.2; let rate = 0.05; let q = 0.02; // Positive dividend yield let maturity = 1.0; let bs_result = compute_all(s, k, rate, sigma, maturity); let bsm_result = bsm_compute_all(s, k, sigma, rate, q, maturity); // With positive dividend yield, call prices should be lower and put prices higher assert!(bsm_result.call_price < bs_result.call_price); assert!(bsm_result.put_price > bs_result.put_price); ``` -------------------------------- ### Validate Black-Scholes Parameters Source: https://docs.rs/black_scholes/0.11.1/src/black_scholes/lib.rs.html Checks if the input parameters for option pricing are within valid ranges. Ensures stock price, strike price, volatility, and maturity are finite and meet basic positivity/non-negativity constraints. ```rust pub fn validate_params(s: f64, k: f64, _rate: f64, sigma: f64, maturity: f64) -> bool { s > 0.0 && k > 0.0 && sigma >= 0.0 && maturity >= 0.0 && s.is_finite() && k.is_finite() && sigma.is_finite() && maturity.is_finite() } ```