Skip to content

Commit

Permalink
add workspace && gen docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Liberxue committed Jul 23, 2024
1 parent 30f0c55 commit 0a8edaa
Show file tree
Hide file tree
Showing 50 changed files with 1,370 additions and 88 deletions.
Binary file modified .DS_Store
Binary file not shown.
33 changes: 21 additions & 12 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,30 @@ on:

jobs:
build:

runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Checkout repository
uses: actions/checkout@v3

- name: Cache Cargo registry
uses: actions/cache@v3
with:
path: |
~/.cargo/registry
~/.cargo/git
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-cargo-
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
- name: Install Rust
uses: actions-rs/toolchain@v2
with:
toolchain: stable
override: true

- name: Build
run: cargo build --verbose
- name: Build
run: cargo build --workspace --verbose

- name: Run tests
run: cargo test --verbose
- name: Run tests
run: cargo test --workspace --verbose
120 changes: 120 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 2 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,8 @@ name = "cqf"
version = "0.1.0"
edition = "2021"

authors = ["[email protected]"]
repository = "https://github.com/liberxue/cqf"
homepage = "https://github.com/liberxue"


[workspace]
members = ["bench", "cli", "core"]
members = ["core", "bench", "cli"]


[profile.release]
lto = true
codegen-units = 1
strip = "symbols"
[dependencies]
4 changes: 1 addition & 3 deletions bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@ name = "bench"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "bench"
path = "src/main.rs"
[dependencies]
6 changes: 0 additions & 6 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,5 @@
name = "cli"
version = "0.1.0"
edition = "2021"
authors = ["[email protected]"]
repository = "https://github.com/liberxue/cqf"
homepage = "https://github.com/liberxue"
[[bin]]
name = "cli"
path = "src/main.rs"

[dependencies]
Binary file modified core/.DS_Store
Binary file not shown.
7 changes: 7 additions & 0 deletions core/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,5 @@ version = "0.1.0"
edition = "2021"

[dependencies]

[lib]
path = "src/lib.rs"


rand = "0.8"
rand_distr = "0.4"


[[test]]
name = "core_tests"
path = "tests/black_scholes_tests.rs"
Binary file modified core/src/.DS_Store
Binary file not shown.
10 changes: 10 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
//pub mod models {
// pub mod black_scholes;
// pub mod monte_carlo;
//}
//
//pub mod strategies {
// pub mod butterfly;
// pub mod collar;
//}

pub mod models;
pub mod strategies;
54 changes: 53 additions & 1 deletion core/src/models/binomial.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,87 @@
extern crate rand;
use rand::Rng;
use crate::models::OptionPricingModel;
use rand::Rng;

/// A Monte Carlo simulation model for option pricing.
pub struct MonteCarloModel {
/// The number of simulations to run for estimating option prices.
pub simulations: usize,
}

impl OptionPricingModel for MonteCarloModel {
/// Calculates the price of a European call option using Monte Carlo simulation.
///
/// # Arguments
///
/// * `s` - The current stock price.
/// * `k` - The strike price of the option.
/// * `r` - The risk-free interest rate (annualized).
/// * `sigma` - The volatility of the stock (annualized).
/// * `t` - The time to maturity in years.
///
/// # Returns
///
/// Returns the estimated price of the European call option.
///
/// # Example
///
/// ```
/// let model = MonteCarloModel { simulations: 10000 };
/// let call_price = model.call_price(100.0, 100.0, 0.05, 0.2, 1.0);
/// println!("Call Price: {}", call_price);
/// ```
fn call_price(&self, s: f64, k: f64, r: f64, sigma: f64, t: f64) -> f64 {
let mut rng = rand::thread_rng();
let mut payoff_sum = 0.0;

for _ in 0..self.simulations {
// Generate a random sample from the standard normal distribution.
let z: f64 = rng.sample(rand::distributions::StandardNormal);
// Calculate the simulated stock price at maturity.
let st = s * ((r - 0.5 * sigma.powi(2)) * t + sigma * t.sqrt() * z).exp();
// Accumulate the payoff for the call option.
payoff_sum += (st - k).max(0.0);
}

// Discount the average payoff to present value.
(payoff_sum / self.simulations as f64) * (-r * t).exp()
}

/// Calculates the price of a European put option using Monte Carlo simulation.
///
/// # Arguments
///
/// * `s` - The current stock price.
/// * `k` - The strike price of the option.
/// * `r` - The risk-free interest rate (annualized).
/// * `sigma` - The volatility of the stock (annualized).
/// * `t` - The time to maturity in years.
///
/// # Returns
///
/// Returns the estimated price of the European put option.
///
/// # Example
///
/// ```
/// let model = MonteCarloModel { simulations: 10000 };
/// let put_price = model.put_price(100.0, 100.0, 0.05, 0.2, 1.0);
/// println!("Put Price: {}", put_price);
/// ```
fn put_price(&self, s: f64, k: f64, r: f64, sigma: f64, t: f64) -> f64 {
let mut rng = rand::thread_rng();
let mut payoff_sum = 0.0;

for _ in 0..self.simulations {
// Generate a random sample from the standard normal distribution.
let z: f64 = rng.sample(rand::distributions::StandardNormal);
// Calculate the simulated stock price at maturity.
let st = s * ((r - 0.5 * sigma.powi(2)) * t + sigma * t.sqrt() * z).exp();
// Accumulate the payoff for the put option.
payoff_sum += (k - st).max(0.0);
}

// Discount the average payoff to present value.
(payoff_sum / self.simulations as f64) * (-r * t).exp()
}
}
Expand Down
Loading

0 comments on commit 0a8edaa

Please sign in to comment.