Skip to content

Commit c529b99

Browse files
4 implement binomial tree model (#19)
* Update README.md * Add basic options pricing structure * Update README.md * Add documentation * Add documentation * Complete basic options pricing structure * Update crates * Add implied volatility calculation and extend Greeks trait for options * Add OptionStyle enum * Refactor option pricing implementations by removing redundant comments and cleaning up Greeks trait * Add monte carlo greeks test * Remove print * Generate documentation for option pricing models and Greeks * Add payoff calculation and strike method to option pricing traits; implement Greeks calculations in Monte Carlo option * Implement binomial tree options pricing * Update tests * Update tests * Update tests * Update tests * Update tests * Update tests * Update codecov.yml * Update codecov.yml
1 parent d89a17e commit c529b99

File tree

9 files changed

+746
-203
lines changed

9 files changed

+746
-203
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ description = " A tiny Rust library for quantitative finance"
66
license = "MIT"
77
# keywords = ["quantitative", "statistics", "finance", "options", "timeseries"]
88
# categories = ["science"]
9-
homepage = "https://github.com/carlobortolan/quantrs"
109
repository = "https://github.com/carlobortolan/quantrs"
10+
documentation = "https://docs.rs/quantrs"
11+
homepage = "https://github.com/carlobortolan/quantrs"
1112
edition = "2021"
1213

1314
include = ["CHANGELOG.md", "LICENSE.md", "src/", "tests/"]

codecov.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ coverage:
22
status:
33
project:
44
default:
5+
target: 90%
6+
threshold: 0.5%
7+
base: auto
58
enabled: yes
9+
patch:
10+
default:
611
target: 90%
7-
threshold: 0.5%
12+
threshold: 10%
13+
base: auto
14+
enabled: yes

examples/options_pricing.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use quantrs::options::{
44
BinomialTreeOption, BlackScholesOption, Greeks, MonteCarloOption, OptionGreeks, OptionPricing,
5-
OptionType,
5+
OptionStyle, OptionType,
66
};
77

88
fn main() {
@@ -19,6 +19,7 @@ fn example_black_scholes() {
1919
time_to_maturity: 1.0,
2020
risk_free_rate: 0.05,
2121
volatility: 0.2,
22+
..Default::default()
2223
};
2324

2425
let call_price = bs_option.price(OptionType::Call);
@@ -40,6 +41,7 @@ fn example_binomial_tree() {
4041
risk_free_rate: 0.05,
4142
volatility: 0.2,
4243
steps: 100,
44+
style: OptionStyle::American,
4345
};
4446

4547
let bt_call_price = bt_option.price(OptionType::Call);
@@ -61,6 +63,7 @@ fn example_greeks() {
6163
risk_free_rate: 0.05,
6264
volatility: 0.2,
6365
steps: 100,
66+
..Default::default()
6467
};
6568

6669
let greeks = OptionGreeks::calculate(&bt_option, OptionType::Call);
@@ -86,12 +89,17 @@ fn example_monte_carlo() {
8689
time_to_maturity: 1.0,
8790
risk_free_rate: 0.05,
8891
volatility: 0.2,
89-
simulations: 10000,
92+
simulations: 10_000,
93+
..Default::default()
9094
};
9195

9296
let mc_call_price = mc_option.price(OptionType::Call);
9397
println!("Monte Carlo Call Price: {}", mc_call_price);
9498

9599
let mc_put_price = mc_option.price(OptionType::Put);
96100
println!("Monte Carlo Put Price: {}", mc_put_price);
101+
102+
let market_price = mc_call_price; // Example market price
103+
let implied_volatility = mc_option.implied_volatility(market_price, OptionType::Call);
104+
println!("Implied Volatility: {}", implied_volatility);
97105
}

src/options.rs

Lines changed: 136 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
//! Options pricing models.
1+
//! Module for various option pricing models.
2+
//!
3+
//! ## Supported models
4+
//!
5+
//! - [Black-Scholes Option Pricing Model](black_scholes/struct.BlackScholesOption.html)
6+
//! - [Binomial Option Pricing Model](binomial_tree/struct.BinomialTreeOption.html)
7+
//! - [Monte Carlo Option Pricing Model](monte_carlo/struct.MonteCarloOption.html)
8+
//!
9+
//! ## Greek calculations
10+
//!
11+
//! This module also provides implementations of the Greeks for each option pricing model.
12+
//! See the [Greeks](options/trait.Greeks.html) trait for more information.
213
314
pub mod binomial_tree;
415
pub mod black_scholes;
@@ -7,17 +18,16 @@ pub mod monte_carlo;
718

819
pub use binomial_tree::BinomialTreeOption;
920
pub use black_scholes::BlackScholesOption;
10-
pub use greeks::Greeks;
1121
pub use greeks::OptionGreeks;
1222
pub use monte_carlo::MonteCarloOption;
13-
14-
/// Enum representing the type of option.
15-
#[derive(Clone, Copy)]
16-
pub enum OptionType {
17-
/// Call option
18-
Call,
19-
/// Put option
20-
Put,
23+
/// Supertrait that combines OptionPricing and Greeks.
24+
pub trait Option: OptionPricing + Greeks {
25+
/// Get the style of the option.
26+
///
27+
/// # Returns
28+
///
29+
/// The style of the option.
30+
fn style(&self) -> &OptionStyle;
2131
}
2232

2333
/// Trait for option pricing models.
@@ -44,9 +54,122 @@ pub trait OptionPricing {
4454
///
4555
/// The implied volatility.
4656
fn implied_volatility(&self, market_price: f64, option_type: OptionType) -> f64;
57+
58+
/// Calculate the payoff of the option at maturity.
59+
///
60+
/// # Arguments
61+
///
62+
/// * `underlying_price` - The price of the underlying asset at maturity.
63+
/// * `option_type` - The type of option (Call or Put).
64+
///
65+
/// # Returns
66+
///
67+
/// The payoff of the option.
68+
fn payoff(&self, spot: f64, option_type: OptionType) -> f64 {
69+
match option_type {
70+
OptionType::Call => (spot - self.strike()).max(0.0),
71+
OptionType::Put => (self.strike() - spot).max(0.0),
72+
}
73+
}
74+
75+
/// Get the strike price of the option.
76+
///
77+
/// # Returns
78+
///
79+
/// The strike price of the option.
80+
fn strike(&self) -> f64;
81+
}
82+
83+
/// Trait for calculating the Greeks of an option.
84+
pub trait Greeks {
85+
// First order Greeks
86+
/// Delta measures the rate of change of the option price with respect to changes in the price of the underlying asset.
87+
fn delta(&self, option_type: OptionType) -> f64;
88+
/// Gamma measures the rate of change of the option delta with respect to changes in the price of the underlying asset.
89+
fn gamma(&self, option_type: OptionType) -> f64;
90+
/// Theta measures the rate of change of the option price with respect to changes in time to maturity.
91+
fn theta(&self, option_type: OptionType) -> f64;
92+
/// Vega measures the rate of change of the option price with respect to changes in the volatility of the underlying asset.
93+
fn vega(&self, option_type: OptionType) -> f64;
94+
/// Rho measures the rate of change of the option price with respect to changes in the risk-free interest rate.
95+
fn rho(&self, option_type: OptionType) -> f64;
96+
97+
// Higher order Greeks
98+
/// Lambda measures the rate of change of the option delta with respect to changes in the risk-free interest rate.
99+
fn lambda(&self, option_type: OptionType) -> f64 {
100+
0.0 // Placeholder value
101+
}
102+
/// Vanna measures the rate of change of the option delta with respect to changes in the volatility of the underlying asset.
103+
fn vanna(&self, option_type: OptionType) -> f64 {
104+
0.0 // Placeholder value
105+
}
106+
/// Charm measures the rate of change of the option delta with respect to changes in time to maturity.
107+
fn charm(&self, option_type: OptionType) -> f64 {
108+
0.0 // Placeholder value
109+
}
110+
/// Vomma measures the rate of change of the option vega with respect to changes in the volatility of the underlying asset.
111+
fn vomma(&self, option_type: OptionType) -> f64 {
112+
0.0 // Placeholder value
113+
}
114+
/// Veta measures the rate of change of the option vega with respect to changes in time to maturity.
115+
fn veta(&self, option_type: OptionType) -> f64 {
116+
0.0 // Placeholder value
117+
}
118+
/// Speed measures the rate of change of the option gamma with respect to changes in the price of the underlying asset.
119+
fn speed(&self, option_type: OptionType) -> f64 {
120+
0.0 // Placeholder value
121+
}
122+
/// Zomma measures the rate of change of the option gamma with respect to changes in the volatility of the underlying asset.
123+
fn zomma(&self, option_type: OptionType) -> f64 {
124+
0.0 // Placeholder value
125+
}
126+
/// Color measures the rate of change of the option gamma with respect to changes in time to maturity.
127+
fn color(&self, option_type: OptionType) -> f64 {
128+
0.0 // Placeholder value
129+
}
130+
/// Ultima measures the rate of change of the option vomma with respect to changes in the volatility of the underlying asset.
131+
fn ultima(&self, option_type: OptionType) -> f64 {
132+
0.0 // Placeholder value
133+
}
134+
}
135+
136+
/// Enum representing the type of option.
137+
#[derive(Clone, Copy, Debug)]
138+
pub enum OptionType {
139+
/// Call option (gives the holder the right to buy the underlying asset)
140+
Call,
141+
/// Put option (gives the holder the right to sell the underlying asset)
142+
Put,
47143
}
48144

49-
/// A supertrait that combines OptionPricing and Greeks.
50-
pub trait Option: OptionPricing + Greeks {}
145+
/// Enum representing the style of the option.
146+
#[derive(Clone, Copy, Debug, PartialEq)]
147+
pub enum OptionStyle {
148+
/// American option (can be exercised at any time)
149+
American,
150+
/// European option (default, can be exercised only at expiration)
151+
European,
152+
/// Bermudan option (can be exercised at specific dates)
153+
Bermudan,
154+
/// Asian option (payoff depends on average price of underlying asset)
155+
Asian,
156+
/// Barrier option (payoff depends on whether underlying asset crosses a barrier)
157+
Barrier,
158+
/// Binary option (payout is fixed amount or nothing)
159+
Binary,
160+
/// Digital option (payout is fixed amount or nothing; also known as cash-or-nothing or asset-or-nothing option)
161+
Digital,
162+
/// Lookback option (payoff depends on extrema of underlying asset)
163+
Lookback,
164+
}
51165

52-
impl<T: OptionPricing + Greeks> Option for T {}
166+
impl Default for OptionStyle {
167+
/// Default option style is European.
168+
///
169+
/// # Returns
170+
///
171+
/// The default option style.
172+
fn default() -> Self {
173+
OptionStyle::European
174+
}
175+
}

0 commit comments

Comments
 (0)