rustyqlib/cmdty/black76.rs
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// }