Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Liberxue committed Jul 23, 2024
1 parent 6f3a4bf commit 5015f5d
Show file tree
Hide file tree
Showing 48 changed files with 618 additions and 455 deletions.
Binary file added .DS_Store
Binary file not shown.
156 changes: 0 additions & 156 deletions Cargo.lock

This file was deleted.

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

[dependencies]
ndarray = "0.15"
rand = "0.8"
rand_distr = "0.4"
authors = ["[email protected]"]
repository = "https://github.com/liberxue/cqf"
homepage = "https://github.com/liberxue"

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

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


[profile.release]
lto = true
codegen-units = 1
strip = "symbols"
49 changes: 49 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
.PHONY: init
# init env
init:
@echo "init"

.PHONY:fmt
# cargo fmt
fmt:
@cargo fmt -v

.PHONY:unitTest
# unit Test
test:
@export RUST_LOG=info
@cargo test -v

.PHONY: build
# cargo build
build:
@ if [ "$(OS)" == "Windows_NT" ]; then \
cargo clean && cargo check && cargo build --release --target=x86_64-pc-windows-gnu; \
elif [ "$(shell uname)" == "Darwin" ]; then \
cargo clean && cargo check && cargo build --release --target=x86_64-apple-darwin; \
else \
cargo clean && cargo check && cargo build --release --target=x86_64-unknown-linux-musl; \
fi

.PHONY: all
# generate all
all:
@echo "make all done"

# show help
help:
@echo ''
@echo 'Usage:'
@echo ' make [target]'
@echo ''
@echo 'Targets:'
@awk '/^[a-zA-Z\-\_0-9]+:/ { \
helpMessage = match(lastLine, /^# (.*)/); \
if (helpMessage) { \
helpCommand = substr($$1, 0, index($$1, ":")-1); \
helpMessage = substr(lastLine, RSTART + 2, RLENGTH); \
printf "\033[36m%-22s\033[0m %s\n", helpCommand,helpMessage; \
} \
} \
{ lastLine = $$0 }' $(MAKEFILE_LIST)
.DEFAULT_GOAL := help
8 changes: 8 additions & 0 deletions bench/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "bench"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "bench"
path = "src/main.rs"
File renamed without changes.
12 changes: 12 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
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]
3 changes: 3 additions & 0 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}
Binary file added core/.DS_Store
Binary file not shown.
18 changes: 18 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "core"
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 added core/src/.DS_Store
Binary file not shown.
File renamed without changes.
3 changes: 3 additions & 0 deletions core/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ fn standard_normal_cdf(x: f64) -> f64 {
}

fn erf(x: f64) -> f64 {
let a1 = 0.254829592;
let a1 = 0.254829592;
let a2 = -0.284496736;
let a3 = 1.421413741;
let a3 = 1.421413741;
let a4 = -1.453152027;
let a5 = 1.061405429;
let p = 0.3275911;
let a5 = 1.061405429;
let p = 0.3275911;

let sign = if x < 0.0 { -1.0 } else { 1.0 };
let x = x.abs();
Expand All @@ -36,4 +36,3 @@ fn erf(x: f64) -> f64 {

sign * y
}

1 change: 0 additions & 1 deletion src/models/mod.rs → core/src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pub mod black_scholes;
pub mod monte_carlo;


pub trait OptionPricingModel {
fn call_price(&self, s: f64, k: f64, r: f64, sigma: f64, t: f64) -> f64;
fn put_price(&self, s: f64, k: f64, r: f64, sigma: f64, t: f64) -> f64;
Expand Down
3 changes: 1 addition & 2 deletions src/models/monte_carlo.rs → core/src/models/monte_carlo.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate rand;
use crate::models::OptionPricingModel;
use rand::Rng;
use rand_distr::StandardNormal;
use crate::models::OptionPricingModel;

pub struct MonteCarloModel {
pub simulations: usize,
Expand Down Expand Up @@ -34,4 +34,3 @@ impl OptionPricingModel for MonteCarloModel {
(payoff_sum / self.simulations as f64) * (-r * t).exp()
}
}

File renamed without changes.
52 changes: 52 additions & 0 deletions core/src/strategies/butterfly.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use crate::models::OptionPricingModel;
use crate::strategies::OptionStrategy;

pub struct ButterflySpread<'a, T: OptionPricingModel> {
pub model: &'a T,
pub s: f64,
pub k1: f64,
pub k2: f64,
pub k3: f64,
pub r: f64,
pub sigma: f64,
pub t: f64,
}

impl<'a, T: OptionPricingModel> ButterflySpread<'a, T> {
pub fn new(
model: &'a T,
s: f64,
k1: f64,
k2: f64,
k3: f64,
r: f64,
sigma: f64,
t: f64,
) -> Self {
Self {
model,
s,
k1,
k2,
k3,
r,
sigma,
t,
}
}
}

impl<'a, T: OptionPricingModel> OptionStrategy for ButterflySpread<'a, T> {
fn price(&self) -> f64 {
let c1 = self
.model
.call_price(self.s, self.k1, self.r, self.sigma, self.t);
let c2 = self
.model
.call_price(self.s, self.k2, self.r, self.sigma, self.t);
let c3 = self
.model
.call_price(self.s, self.k3, self.r, self.sigma, self.t);
c1 - 2.0 * c2 + c3
}
}
Loading

0 comments on commit 5015f5d

Please sign in to comment.