Skip to content

Commit 8da29ed

Browse files
committed
refactor data model
1 parent d96821c commit 8da29ed

18 files changed

+542
-406
lines changed

src/cmdty/black76.rs

Lines changed: 126 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,126 @@
1-
use libm::{exp, log};
2-
use std::f64::consts::{PI, SQRT_2};
3-
use crate::core::utils::{dN, N};
4-
use crate::core::trade;
5-
6-
use super::cmdty_option::{CmdtyOption,Engine};
7-
use super::super::core::termstructure::YieldTermStructure;
8-
use super::super::core::traits::{Instrument,Greeks};
9-
use super::super::core::interpolation;
10-
11-
pub fn npv(bsd_option: &&CmdtyOption) -> f64 {
12-
assert!(bsd_option.volatility >= 0.0);
13-
assert!(bsd_option.time_to_maturity >= 0.0);
14-
assert!(bsd_option.current_price.value >= 0.0);
15-
if bsd_option.option_type == trade::OptionType::Call {
16-
let option_price = bsd_option.current_price.value() * N(bsd_option.d1())
17-
- bsd_option.strike_price * N(bsd_option.d2());
18-
return option_price;
19-
} else {
20-
let option_price = -bsd_option.current_price.value()
21-
* N(-bsd_option.d1())
22-
+ bsd_option.strike_price * N(-bsd_option.d2());
23-
return option_price;
24-
}
25-
}
26-
27-
28-
29-
impl CmdtyOption {
30-
pub fn set_risk_free_rate(&mut self){
31-
let model = interpolation::CubicSpline::new(&self.term_structure.date, &self.term_structure.rates);
32-
let r = model.interpolation(self.time_to_maturity);
33-
self.risk_free_rate = Some(r);
34-
}
35-
pub fn get_premium_at_risk(&self) -> f64 {
36-
let value = self.npv();
37-
let mut pay_off = 0.0;
38-
if self.option_type == trade::OptionType::Call {
39-
pay_off = self.current_price.value() - self.strike_price;
40-
} else if self.option_type == trade::OptionType::Put {
41-
pay_off = self.strike_price - self.current_price.value();
42-
}
43-
if pay_off > 0.0 {
44-
return value - pay_off;
45-
} else {
46-
return value;
47-
}
48-
}
49-
pub fn d1(&self) -> f64 {
50-
//Black76 d1 function Parameters
51-
let tmp1 = (self.current_price.value() / self.strike_price).ln()
52-
+ (0.5 * self.volatility.powi(2))
53-
* self.time_to_maturity;
54-
55-
let tmp2 = self.volatility * (self.time_to_maturity.sqrt());
56-
return tmp1 / tmp2;
57-
}
58-
pub fn d2(&self) -> f64 {
59-
let d2 = self.d1() - self.volatility * self.time_to_maturity.powf(0.5);
60-
return d2;
61-
}
62-
// pub fn imp_vol(&mut self,option_price:f64) -> f64 {
63-
// for i in 0..100{
64-
// let d_sigma = (self.npv()-option_price)/self.vega();
65-
// self.volatility -= d_sigma
66-
// }
67-
// self.volatility
68-
// }
69-
}
70-
impl Greeks for CmdtyOption{
71-
fn delta(&self) -> f64 {
72-
let mut delta = N(self.d1());
73-
if self.option_type == trade::OptionType::Call {
74-
delta = delta;
75-
} else if self.option_type == trade::OptionType::Put {
76-
delta = delta - 1.0;
77-
}
78-
return delta;
79-
}
80-
fn gamma(&self) -> f64 {
81-
let gamma = dN(self.d1());
82-
83-
let var_sqrt = self.volatility * (self.time_to_maturity.sqrt());
84-
return gamma / (self.current_price.value() * var_sqrt);
85-
}
86-
fn vega(&self) -> f64 {
87-
//St * dN(d1) * math.sqrt(T - t)
88-
let vega = self.current_price.value() * dN(self.d1()) * self.time_to_maturity.sqrt();
89-
return vega;
90-
}
91-
fn theta(&self) -> f64 {
92-
let mut theta = 0.0;
93-
if self.option_type == trade::OptionType::Call {
94-
//-(St * dN(d1) * sigma / (2 * math.sqrt(T - t)) + r * K * math.exp(-r * (T - t)) * N(d2))
95-
let t1 = -self.current_price.value() * dN(self.d1()) * self.volatility
96-
/ (2.0 * self.time_to_maturity.sqrt());
97-
98-
theta = t1;
99-
} else if self.option_type == trade::OptionType::Put {
100-
//-(St * dN(d1) * sigma / (2 * math.sqrt(T - t)) - r * K * math.exp(-r * (T - t)) * N(d2))
101-
let t1 = -self.current_price.value() * dN(self.d1()) * self.volatility
102-
/ (2.0 * self.time_to_maturity.sqrt());
103-
104-
theta = t1;
105-
}
106-
107-
return theta;
108-
}
109-
fn rho(&self) -> f64 {
110-
//rho K * (T - t) * math.exp(-r * (T - t)) * N(d2)
111-
let mut rho = 0.0;
112-
if self.option_type == trade::OptionType::Call {
113-
rho = self.strike_price
114-
* self.time_to_maturity
115-
116-
* N(self.d2());
117-
} else if self.option_type == trade::OptionType::Put {
118-
//put_rho = -K * (T - t) * math.exp(-r * (T - t)) * N(-d2)
119-
rho = -self.strike_price
120-
* self.time_to_maturity
121-
* N(-self.d2());
122-
}
123-
124-
return rho;
125-
}
126-
}
1+
// use libm::{exp, log};
2+
// use std::f64::consts::{PI, SQRT_2};
3+
// use crate::core::utils::{dN, N};
4+
// use crate::core::trade;
5+
//
6+
// use super::cmdty_option::{CmdtyOption,Engine};
7+
// use super::super::core::termstructure::YieldTermStructure;
8+
// use super::super::core::traits::{Instrument,Greeks};
9+
// use super::super::core::interpolation;
10+
//
11+
// pub fn npv(bsd_option: &&CmdtyOption) -> f64 {
12+
// assert!(bsd_option.volatility >= 0.0);
13+
// assert!(bsd_option.time_to_maturity >= 0.0);
14+
// assert!(bsd_option.current_price.value >= 0.0);
15+
// if bsd_option.option_type == trade::PutOrCall::Call {
16+
// let option_price = bsd_option.current_price.value() * N(bsd_option.d1())
17+
// - bsd_option.strike_price * N(bsd_option.d2());
18+
// return option_price;
19+
// } else {
20+
// let option_price = -bsd_option.current_price.value()
21+
// * N(-bsd_option.d1())
22+
// + bsd_option.strike_price * N(-bsd_option.d2());
23+
// return option_price;
24+
// }
25+
// }
26+
//
27+
//
28+
//
29+
// impl CmdtyOption {
30+
// pub fn set_risk_free_rate(&mut self){
31+
// let model = interpolation::CubicSpline::new(&self.term_structure.date, &self.term_structure.rates);
32+
// let r = model.interpolation(self.time_to_maturity);
33+
// self.risk_free_rate = Some(r);
34+
// }
35+
// pub fn get_premium_at_risk(&self) -> f64 {
36+
// let value = self.npv();
37+
// let mut pay_off = 0.0;
38+
// if self.option_type == trade::OptionType::Call {
39+
// pay_off = self.current_price.value() - self.strike_price;
40+
// } else if self.option_type == trade::OptionType::Put {
41+
// pay_off = self.strike_price - self.current_price.value();
42+
// }
43+
// if pay_off > 0.0 {
44+
// return value - pay_off;
45+
// } else {
46+
// return value;
47+
// }
48+
// }
49+
// pub fn d1(&self) -> f64 {
50+
// //Black76 d1 function Parameters
51+
// let tmp1 = (self.current_price.value() / self.strike_price).ln()
52+
// + (0.5 * self.volatility.powi(2))
53+
// * self.time_to_maturity;
54+
//
55+
// let tmp2 = self.volatility * (self.time_to_maturity.sqrt());
56+
// return tmp1 / tmp2;
57+
// }
58+
// pub fn d2(&self) -> f64 {
59+
// let d2 = self.d1() - self.volatility * self.time_to_maturity.powf(0.5);
60+
// return d2;
61+
// }
62+
// // pub fn imp_vol(&mut self,option_price:f64) -> f64 {
63+
// // for i in 0..100{
64+
// // let d_sigma = (self.npv()-option_price)/self.vega();
65+
// // self.volatility -= d_sigma
66+
// // }
67+
// // self.volatility
68+
// // }
69+
// }
70+
// impl Greeks for CmdtyOption{
71+
// fn delta(&self) -> f64 {
72+
// let mut delta = N(self.d1());
73+
// if self.option_type == trade::OptionType::Call {
74+
// delta = delta;
75+
// } else if self.option_type == trade::OptionType::Put {
76+
// delta = delta - 1.0;
77+
// }
78+
// return delta;
79+
// }
80+
// fn gamma(&self) -> f64 {
81+
// let gamma = dN(self.d1());
82+
//
83+
// let var_sqrt = self.volatility * (self.time_to_maturity.sqrt());
84+
// return gamma / (self.current_price.value() * var_sqrt);
85+
// }
86+
// fn vega(&self) -> f64 {
87+
// //St * dN(d1) * math.sqrt(T - t)
88+
// let vega = self.current_price.value() * dN(self.d1()) * self.time_to_maturity.sqrt();
89+
// return vega;
90+
// }
91+
// fn theta(&self) -> f64 {
92+
// let mut theta = 0.0;
93+
// if self.option_type == trade::OptionType::Call {
94+
// //-(St * dN(d1) * sigma / (2 * math.sqrt(T - t)) + r * K * math.exp(-r * (T - t)) * N(d2))
95+
// let t1 = -self.current_price.value() * dN(self.d1()) * self.volatility
96+
// / (2.0 * self.time_to_maturity.sqrt());
97+
//
98+
// theta = t1;
99+
// } else if self.option_type == trade::OptionType::Put {
100+
// //-(St * dN(d1) * sigma / (2 * math.sqrt(T - t)) - r * K * math.exp(-r * (T - t)) * N(d2))
101+
// let t1 = -self.current_price.value() * dN(self.d1()) * self.volatility
102+
// / (2.0 * self.time_to_maturity.sqrt());
103+
//
104+
// theta = t1;
105+
// }
106+
//
107+
// return theta;
108+
// }
109+
// fn rho(&self) -> f64 {
110+
// //rho K * (T - t) * math.exp(-r * (T - t)) * N(d2)
111+
// let mut rho = 0.0;
112+
// if self.option_type == trade::OptionType::Call {
113+
// rho = self.strike_price
114+
// * self.time_to_maturity
115+
//
116+
// * N(self.d2());
117+
// } else if self.option_type == trade::OptionType::Put {
118+
// //put_rho = -K * (T - t) * math.exp(-r * (T - t)) * N(-d2)
119+
// rho = -self.strike_price
120+
// * self.time_to_maturity
121+
// * N(-self.d2());
122+
// }
123+
//
124+
// return rho;
125+
// }
126+
// }

src/cmdty/cmdty_option.rs

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
1-
use super::super::core::termstructure::YieldTermStructure;
2-
use super::super::core::quotes::Quote;
3-
use super::super::core::traits::{Instrument,Greeks};
4-
use crate::core::trade;
5-
use crate::cmdty::black76;
6-
7-
pub enum Engine{
8-
Black76,
9-
MonteCarlo
10-
}
11-
12-
13-
pub struct CmdtyOption {
14-
pub option_type: trade::OptionType,
15-
pub transection: trade::Transection,
16-
pub current_price: Quote,
17-
pub strike_price: f64,
18-
pub volatility: f64,
19-
pub time_to_maturity: f64,
20-
pub time_to_future_maturity: Option<f64>,
21-
pub term_structure: YieldTermStructure<f64>,
22-
pub risk_free_rate: Option<f64>,
23-
pub transection_price: f64,
24-
pub engine: Engine,
25-
pub simulation:Option<u64>
26-
}
27-
28-
impl Instrument for CmdtyOption {
29-
fn npv(&self) -> f64 {
30-
match self.engine{
31-
Engine::Black76 => {
32-
let value = black76::npv(&self);
33-
value
34-
}
35-
_ => {
36-
0.0
37-
}
38-
}
39-
}
40-
}
1+
// use super::super::core::termstructure::YieldTermStructure;
2+
// use super::super::core::quotes::Quote;
3+
// use super::super::core::traits::{Instrument,Greeks};
4+
// use crate::core::trade;
5+
// use crate::cmdty::black76;
6+
//
7+
// pub enum Engine{
8+
// Black76,
9+
// MonteCarlo
10+
// }
11+
//
12+
//
13+
// pub struct CmdtyOption {
14+
// pub option_type: trade::PutOrCall,
15+
// pub transection: trade::Transection,
16+
// pub current_price: Quote,
17+
// pub strike_price: f64,
18+
// pub volatility: f64,
19+
// pub time_to_maturity: f64,
20+
// pub time_to_future_maturity: Option<f64>,
21+
// pub term_structure: YieldTermStructure<f64>,
22+
// pub risk_free_rate: Option<f64>,
23+
// pub transection_price: f64,
24+
// pub engine: Engine,
25+
// pub simulation:Option<u64>
26+
// }
27+
//
28+
// impl Instrument for CmdtyOption {
29+
// fn npv(&self) -> f64 {
30+
// match self.engine{
31+
// Engine::Black76 => {
32+
// let value = black76::npv(&self);
33+
// value
34+
// }
35+
// _ => {
36+
// 0.0
37+
// }
38+
// }
39+
// }
40+
// }

0 commit comments

Comments
 (0)