7 releases
new 0.1.6 | Mar 22, 2025 |
---|---|
0.1.5 | Mar 22, 2025 |
#14 in Finance
559 downloads per month
160KB
2.5K
SLoC
quantrs
Quantrs is a tiny quantitative finance library for Rust. It is designed to be as intuitive and easy to use as possible so that you can work with derivatives without the need to write complex code or have a PhD in reading QuantLib documentation. The library is still in the early stages of development, and many features are not yet implemented.
Please check out the documentation here.
Features
Options Pricing
Quantrs supports options pricing with various models for both vanilla and exotic options as well as options trading strategies for both basic options spreads and non-directional strategies.
Click to see supported models
Black-Scholes | Black-76 | Lattice | ³Monte-Carlo | Finite Diff | Heston | |
---|---|---|---|---|---|---|
European | ✅ | ⏳ | ✅ | ✅ | ⏳ | ⏳ |
American | ❌ | ❌ | ✅ | ❌ (L. Sq.) | ⏳ | ❌ |
Bermudan | ❌ | ❌ | ⏳ | ❌ (L. Sq.) | ❌ (complex) | ❌ |
¹Basket | ⏳ (∀component) | ❌ | ⏳ (approx.) | ⏳ | ❌ | ❌ |
¹Rainbow | ✅ (∀component) | ❌ | ✅ | ✅ | ❌ | ❌ |
²Barrier | ❌ (mod. BSM) | ❌ | ⏳ | ⏳ | ⏳ | ⏳ |
²Double Barrier | ❌ (mod. BSM) | ❌ | ⏳ | ⏳ | ❌ (complex) | ⏳ |
²Asian (fixed strike) | ❌ (mod. BSM) | ❌ | ❌ | ✅ | ⏳ | ⏳ |
²Asian (floating strike) | ❌ (mod. BSM) | ❌ | ❌ | ✅ | ⏳ | ⏳ |
²Lookback (fixed strike) | ⏳ | ❌ | ❌ | ⏳ | ⏳ | ⏳ |
²Lookback (floating strike) | ⏳ | ❌ | ❌ | ⏳ | ⏳ | ⏳ |
²Binary Cash-or-Nothing | ✅ | ⏳ | ✅ | ✅ | ❌ (mod. PDE) | ⏳ |
²Binary Asset-or-Nothing | ✅ | ⏳ | ✅ | ✅ | ❌ (mod. PDE) | ⏳ |
Greeks (Δ,ν,Θ,ρ,Γ) | ✅ | ⏳ | ⏳ | ❌ | ❌ | ❌ |
Implied Volatility | ✅ | ⏳ | ⏳ | ❌ | ❌ | ❌ |
¹ "Exotic" options with standard exercise style; only differ in their payoff value
² Non-vanilla path-dependent "exotic" options
³ MC simulates underlying price paths based on geometric Brownian motion for Black-Scholes models and geometric average price paths for Asian and Lookback options
✅ = Supported, ⏳ = Planned / In progress, ❌ = Not supported / Not applicable
Click to see supported strategies
Strategy Name | Type | Description |
---|---|---|
Covered Call | Income | Long stock + short call |
Protective Put | Hedging | Long stock + long put |
Guts | Volatility | Long ITM call + long ITM put |
Straddle | Volatility | Long ATM call + long ATM put |
Strangle | Volatility | Long OTM call + long OTM put |
Butterfly Spread | ¹Spread | Long ITM call, short two ATM calls, long OTM call (or all puts) |
Iron Butterfly | ¹Spread | Short straddle + long wings |
Christmas Tree Butterfly | ¹Spread | Long 1 ATM call, short 3 OTM calls, long 2 high-strike OTM calls (or all puts) |
Condor Spread | ¹Spread | Long low-strike ITM call, short ITM call, short OTM call, long high-strike OTM call (or all puts) |
Iron Condor | ¹Spread | Short strangle + long wings |
Calendar Spread | ²Time Spread | Long far-expiry ATM call + short near-expiry ATM call (or all puts) |
Diagonal Spread | ³Time Spread | Short near-expiry OTM call + long far-expiry further OTM call (or all puts) |
Back Spread | Directional | Long 2 OTM calls + short 1 ATM call (or all puts) |
¹ Also referred to as 'vertical'
² Also referred to as 'horizontal'
³ Also referred to as 'diagonal'
Usage
Add this to your Cargo.toml
:
[dependencies]
quantrs = "0.1.6"
Now if you want to e.g., model binary call options using the Black-Scholes model, you can:
use quantrs::options::*;
// Create a new instrument with a spot price of 100 and a dividend yield of 2%
let instrument = Instrument::new().with_spot(100.0).with_cont_yield(0.02);
// Create a new Cash-or-Nothing binary call option with:
// - Strike price (K) = 85
// - Time to maturity (T) = 0.78 years
let option = BinaryOption::cash_or_nothing(instrument, 85.0, 0.78, Call);
// Create a new Black-Scholes model with:
// - Risk-free interest rate (r) = 5%
// - Volatility (σ) = 20%
let model = BlackScholesModel::new(0.05, 0.2);
// Calculate the price of the binary call option using the Black-Scholes model
println!("Price: {}", model.price(&option));
// Calculate first order greeks for the option
println!("{:?}", Greeks::calculate(&model, &option));
This will output:
Price: 0.8006934914644723
Greeks { delta: 0.013645840354947947, gamma: -0.0008813766475726433, theta: 0.17537248302290848, vega: -1.3749475702133236, rho: 0.4398346243436515 }
Plotting
Quantrs also supports plotting option prices and strategies using the plotters
backend.
E.g., Plot the P/L of a slightly skewed Condor spread:
Click to see example code
use quantrs::options::*;
// Create a new instrument with a spot price of 100 and a dividend yield of 2%
let instrument = Instrument::new().with_spot(100.0).with_cont_yield(0.02);
// Create a vector of European call options with different strike prices
let options = vec![
EuropeanOption::new(instrument.clone(), 85.0, 1.0, Call),
EuropeanOption::new(instrument.clone(), 95.0, 1.0, Call),
EuropeanOption::new(instrument.clone(), 102.0, 1.0, Call),
EuropeanOption::new(instrument.clone(), 115.0, 1.0, Call),
];
// Create a new Black-Scholes model with:
// - Risk-free interest rate (r) = 5%
// - Volatility (σ) = 20%
let model = BlackScholesModel::new(0.05, 0.2);
// Plot a breakdown of the Condor spread with a spot price range of [80,120]
model.plot_strategy_breakdown(
"Condor Example",
model.condor(&options[0], &options[1], &options[2], &options[3]),
80.0..120.0,
"path/to/destination.png",
&options,
);
See the documentation for more information and examples.
Benchmarks
Compared to other popular and well-maintained (i.e., actively developed, well-documented, and feature-rich) options pricing libraries, quantrs is significantly faster:
- 29x faster than
QuantLib
(python bindings) - 113x faster than
py_vollib
- 15x faster than
RustQuant
- 2.7x faster than
Q-Fin
Library | Mean Execution Time (μs) | Median Execution Time (μs) | Standard Deviation (μs) | Operations / Second (OPS) |
---|---|---|---|---|
quantrs | 0.0971 | 0.0970 | 0.0007 | 10,142,000 |
QuantLib (cpp) | n.a. | n.a. | n.a. | n.a. |
QuantLib (py) | 2.8551 | 2.8630 | 0.9391 | 350,250 |
py_vollib | 10.9959 | 10.8950 | 1.1398 | 90,943 |
Q-Fin | 0.2622 | 0.2603 | 0.0356 | 3,813,700 |
RustQuant | 1.4777 | 1.4750 | 0.0237 | 676,727 |
You can find the benchmarks at quantrs.pages.dev/report
Published benchmarks have been measured on a selfhosted VM with 32 GB RAM, AMD Ryzen 7 PRO 6850U @ 2.70GHz, and Manjaro Linux x86_64
Minimum supported Rust version (MSRV)
This crate requires a Rust version of 1.77.0 or higher. Increases in MSRV will be considered a semver non-breaking API change and require a version increase (PATCH until 1.0.0, MINOR after 1.0.0).
Outlook
See OUTLOOK.md for a list of planned features and improvements.
Contributing
If you find any bugs or have suggestions for improvement, please open a new issue or submit a pull request.
License
This project is licensed under either of:
at your option.
© Carlo Bortolan
Carlo Bortolan · GitHub carlobortolan · contact via carlobortolan@gmail.com
Dependencies
~15MB
~270K SLoC