1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// use std::fmt::Binary;
// use chrono::{Datelike, Local, NaiveDate};
// use crate::equity::{binomial,finite_difference,montecarlo};
// use super::super::core::termstructure::YieldTermStructure;
// use super::super::core::quotes::Quote;
// use super::super::core::traits::{Instrument,Greeks};
// use super::blackscholes;
// use crate::equity::utils::{Engine};
// use crate::core::trade::{OptionType,Transection};
// use crate::core::utils::{Contract,ContractStyle};
// use crate::core::trade;
// impl Instrument for BinaryOption {
// fn npv(&self) -> f64 {
// match self.engine{
// Engine::BlackScholes => {
// let value = blackscholes::npv(&self);
// value
// }
// Engine::MonteCarlo => {
//
// let value = montecarlo::npv(&self,false);
// value
// }
// Engine::Binomial => {
//
// let value = binomial::npv(&self);
// value
// }
// Engine::FiniteDifference => {
// let value = finite_difference::npv(&self);
// value
// }
//
// }
// }
// }
// /// This struct represents a real world equity option contract
// #[derive(Debug)]
// pub struct BinaryOption {
// pub option_type: OptionType,
// pub payoff_type: String,
// pub binary_type: String,
// pub transection: Transection,
// pub underlying_price: Quote,
// pub current_price: Quote,
// pub strike_price: f64,
// pub dividend_yield: f64,
// pub volatility: f64,
// pub maturity_date: NaiveDate,
// pub valuation_date: NaiveDate,
// pub term_structure: YieldTermStructure<f64>,
// pub risk_free_rate: f64,
// pub transection_price: f64,
// pub engine: Engine,
// pub simulation:Option<u64>,
// pub style: ContractStyle,
// }
// impl BinaryOption{
// pub fn time_to_maturity(&self) -> f64{
// let time_to_maturity = (self.maturity_date - self.valuation_date).num_days() as f64/365.0;
// time_to_maturity
// }
// }
// impl BinaryOption {
// pub fn from_json(data: &Contract) -> Box<BinaryOption> {
// let market_data = data.market_data.as_ref().unwrap();
// let underlying_quote = Quote::new(market_data.underlying_price);
// //TODO: Add term structure
// let date = vec![0.01, 0.02, 0.05, 0.1, 0.5, 1.0, 2.0, 3.0];
// let rates = vec![0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05];
// let ts = YieldTermStructure::new(date, rates);
// let option_type = &market_data.option_type;
// let side: OptionType;
// match option_type.trim() {
// "C" | "c" | "Call" | "call" => side = OptionType::Call,
// "P" | "p" | "Put" | "put" => side = OptionType::Put,
// _ => panic!("Invalide side argument! Side has to be either 'C' or 'P'."),
// }
// let maturity_date = &market_data.maturity;
// let today = Local::today();
// let future_date = NaiveDate::parse_from_str(&maturity_date, "%Y-%m-%d").expect("Invalid date format");
//
// let risk_free_rate = Some(market_data.risk_free_rate).unwrap();
// let dividend = Some(market_data.dividend).unwrap();
// //let mut op = 0.0;
//
// let option_price = Quote::new(match market_data.option_price {
// Some(x) => x,
// None => 0.0,
// });
// //let volatility = Some(market_data.volatility);
// let volatility = match market_data.volatility {
// Some(x) => {
// x
// }
// None => 0.2
// };
// let mut option = BinaryOption {
// option_type: side,
// transection: Transection::Buy,
// underlying_price: underlying_quote,
// current_price: option_price,
// strike_price: market_data.strike_price,
// volatility: volatility,
// maturity_date: future_date,
// risk_free_rate: risk_free_rate.unwrap_or(0.0),
// dividend_yield: dividend.unwrap_or(0.0),
// transection_price: 0.0,
// term_structure: ts,
// engine: Engine::BlackScholes,
// simulation: None,
// style: ContractStyle::European,
// valuation_date: today.naive_utc(),
// };
// match data.pricer.trim() {
// "Analytical" | "analytical"|"bs" => {
// option.engine = crate::equity::utils::PricingEngine::from_kind(Engine::BlackScholes);
// }
// "MonteCarlo" | "montecarlo" | "MC"|"mc" => {
// option.engine = crate::equity::utils::PricingEngine::from_kind(Engine::MonteCarlo);
// }
// "Binomial" | "binomial"|"bino" => {
// option.engine = crate::equity::utils::PricingEngine::from_kind(Engine::Binomial);
// }
// "FiniteDifference" | "finitdifference" |"FD" |"fd" => {
// option.engine = crate::equity::utils::PricingEngine::from_kind(Engine::FiniteDifference);
// }
// _ => {
// panic!("Invalid pricer");
// }
// }
// match data.style.as_ref().unwrap_or(&"European".to_string()).trim() {
// "European" | "european" => {
// option.style = ContractStyle::European;
// }
// "American" | "american" => {
// option.style = ContractStyle::American;
// }
// _ => {
// option.style = ContractStyle::European;
// }
// }
// option.set_risk_free_rate();
// return Box::new(option);
// }
// }
//
// #[cfg(test)]
// mod tests {
// //write a unit test for from_json
// use super::*;
// use crate::core::utils::{Contract,MarketData};
// use crate::core::trade::OptionType;
// use crate::core::trade::Transection;
// use crate::core::utils::ContractStyle;
// use crate::core::termstructure::YieldTermStructure;
// use crate::core::quotes::Quote;
// use chrono::{Datelike, Local, NaiveDate};
// #[test]
// fn test_from_json() {
// let data = Contract {
// action: "PV".to_string(),
// market_data: Some(MarketData {
// underlying_price: 100.0,
// strike_price: 100.0,
// volatility: None,
// option_price: Some(10.0),
// risk_free_rate: Some(0.05),
// dividend: Some(0.0),
// maturity: "2024-01-01".to_string(),
// option_type: "C".to_string(),
// simulation: None
// }),
// pricer: "Analytical".to_string(),
// asset: "".to_string(),
// style: Some("European".to_string()),
// rate_data: None
// };
// let option = BinaryOption::from_json(&data);
// assert_eq!(option.option_type, OptionType::Call);
// assert_eq!(option.transection, Transection::Buy);
// assert_eq!(option.underlying_price.value, 100.0);
// assert_eq!(option.strike_price, 100.0);
// assert_eq!(option.current_price.value, 10.0);
// assert_eq!(option.dividend_yield, 0.0);
// assert_eq!(option.volatility, 0.2);
// assert_eq!(option.maturity_date, NaiveDate::from_ymd(2024, 1, 1));
// assert_eq!(option.valuation_date, Local::today().naive_utc());
// assert_eq!(option.engine, Engine::BlackScholes);
// assert_eq!(option.style, ContractStyle::European);
// }
// }
//