Skip to content

hayden4r4/blackscholes-rust

Repository files navigation

BlackScholes

A library providing Black-Scholes option pricing, Greek calculations, and implied-volatility solver.

Crates.io Documentation Benchmarks

A Black-Scholes option pricing, Greek calculation, and implied volatility calculation library.

The library handles both European and American style options for the following option types:

  • Vanilla Put/Call
  • Binary Put/Call
  • Binary OT Range (In/Out)
  • Barrier

Performance Optimization

This library is optimized for both single-option pricing and high-throughput batch processing. We've implemented a comprehensive benchmarking infrastructure to measure and improve performance.

Key Performance Characteristics

  • Single Option Pricing: ~35-40 ns per option
  • Rational Pricing Method: ~55-65 ns per option
  • Delta Calculation: ~30-35 ns per option
  • Gamma Calculation: ~14-15 ns per option
  • Batch Processing: Scales linearly up to large batch sizes
  • All Greeks Calculation: ~2 ms per 1000 options

Benchmarking Infrastructure

The library includes a comprehensive benchmarking system for performance tracking:

  • Interactive Charts: Professional benchmark visualizations on GitHub Pages
  • Automated Regression Detection: CI-integrated tests that fail on performance regressions (>10% threshold)
  • Historical Tracking: Continuous monitoring of performance trends over time
  • Pull Request Comments: Automatic performance comparison comments on PRs

View live benchmark results at: https://przemyslawolszewski.github.io/bs-rs/

Usage Examples

use blackscholes::{Inputs, OptionType, Pricing, Greeks, ImpliedVolatility};

// Basic option pricing
let inputs = Inputs::new(
    OptionType::Call,   // Call option
    100.0,              // Spot price
    100.0,              // Strike price
    None,               // Option price (not needed for pricing)
    0.05,               // Risk-free rate
    0.01,               // Dividend yield
    1.0,                // Time to maturity (in years)
    Some(0.2),          // Volatility
);

// Calculate option price
let price = inputs.calc_price().unwrap();
println!("Option price: {}", price);

// Calculate option Greeks
let delta = inputs.calc_delta().unwrap();
let gamma = inputs.calc_gamma().unwrap();
let theta = inputs.calc_theta().unwrap();
let vega = inputs.calc_vega().unwrap();
let rho = inputs.calc_rho().unwrap();

println!("Delta: {}, Gamma: {}, Vega: {}", delta, gamma, vega);

// Calculate implied volatility from price
let mut iv_inputs = Inputs::new(
    OptionType::Call,
    100.0,
    100.0,
    Some(10.0),  // Option price
    0.05,
    0.01,
    1.0,
    None,        // Volatility is what we're solving for
);

let iv = iv_inputs.calc_iv(0.0001).unwrap();
println!("Implied volatility: {}", iv);

License

This project is licensed under the MIT License - see the LICENSE file for details.

blackscholes

Crates.io Docs.rs License

This library provides a simple, lightweight, and efficient (though not heavily optimized) implementation of the Black-Scholes-Merton model for pricing European options.

Includes all first, second, and third order Greeks.

Implements both:

Usage

View the docs for usage and examples.

Other packages available:
Python: Pypi
WASM: npm

About

A Black-Scholes pricing model built in Rust

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 6

Languages