Skip to content

Commit ef2fd21

Browse files
committed
added unit test for from json
1 parent 25b4ef7 commit ef2fd21

File tree

6 files changed

+64
-10
lines changed

6 files changed

+64
-10
lines changed

derivatives/src/core/trade.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#[derive(Debug,Clone)]
1+
#[derive(PartialEq,Debug,Clone)]
22
pub enum Transection {
33
Buy,
44
Sell,

derivatives/src/core/utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::f64::consts::{PI, SQRT_2};
66
use serde::Serialize;
77
use crate::Deserialize;
88

9-
#[derive(Clone,Debug)]
9+
#[derive(PartialEq,Clone,Debug)]
1010
pub enum ContractStyle {
1111
European,
1212
American,

derivatives/src/equity/build_contracts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::equity::utils::{Engine};
1515
use std::collections::BTreeMap;
1616

1717
pub fn build_eq_contracts_from_json(data: Vec<Contract>) -> Vec<Box<EquityOption>> {
18-
let derivatives:Vec<Box<EquityOption>> = data.iter().map(|x| EquityOption::equityoption_from_json(x.clone())).collect();
18+
let derivatives:Vec<Box<EquityOption>> = data.iter().map(|x| EquityOption::from_json(x.clone())).collect();
1919
return derivatives;
2020
}
2121
pub fn build_volatility_surface(mut contracts:Vec<Box<EquityOption>>) -> VolSurface {

derivatives/src/equity/utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#[derive(Clone,Debug)]
1+
#[derive(PartialEq,Clone,Debug)]
22
pub enum Engine{
33
BlackScholes,
44
MonteCarlo,

derivatives/src/equity/vanila_option.rs

+59-5
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ impl Instrument for EquityOption {
1818
value
1919
}
2020
Engine::MonteCarlo => {
21-
println!("Using MonteCarlo ");
21+
println!("Using MonteCarlo Engine ");
2222
let value = montecarlo::npv(&self,false);
2323
value
2424
}
2525
Engine::Binomial => {
26-
println!("Using Binomial ");
26+
println!("Using Binomial Engine ");
2727
let value = binomial::npv(&self);
2828
value
2929
}
@@ -59,7 +59,7 @@ impl EquityOption{
5959
}
6060
}
6161
impl EquityOption {
62-
pub fn equityoption_from_json(data: Contract) -> Box<EquityOption> {
62+
pub fn from_json(data: Contract) -> Box<EquityOption> {
6363
let market_data = data.market_data.unwrap();
6464
let underlying_quote = Quote::new(market_data.underlying_price);
6565
//TODO: Add term structure
@@ -83,13 +83,20 @@ impl EquityOption {
8383
Some(x) => x.unwrap(),
8484
None => 0.0,
8585
});
86+
//let volatility = Some(market_data.volatility);
87+
let volatility = match market_data.volatility {
88+
Some(x) => {
89+
x
90+
}
91+
None => 0.2
92+
};
8693
let mut option = EquityOption {
8794
option_type: side,
88-
transection: trade::Transection::Buy,
95+
transection: Transection::Buy,
8996
underlying_price: underlying_quote,
9097
current_price: option_price,
9198
strike_price: market_data.strike_price,
92-
volatility: 0.2,
99+
volatility: volatility,
93100
maturity_date: future_date,
94101
risk_free_rate: risk_free_rate.unwrap_or(0.0),
95102
dividend_yield: dividend.unwrap_or(0.0),
@@ -129,3 +136,50 @@ impl EquityOption {
129136
return Box::new(option);
130137
}
131138
}
139+
140+
#[cfg(test)]
141+
mod tests {
142+
//write a unit test for from_json
143+
use super::*;
144+
use crate::core::utils::{Contract,MarketData};
145+
use crate::core::trade::OptionType;
146+
use crate::core::trade::Transection;
147+
use crate::core::utils::ContractStyle;
148+
use crate::core::termstructure::YieldTermStructure;
149+
use crate::core::quotes::Quote;
150+
use chrono::{Datelike, Local, NaiveDate};
151+
#[test]
152+
fn test_from_json() {
153+
let data = Contract {
154+
action: "PV".to_string(),
155+
market_data: Some(MarketData {
156+
underlying_price: 100.0,
157+
strike_price: 100.0,
158+
volatility: None,
159+
option_price: Some(10.0),
160+
risk_free_rate: Some(0.05),
161+
dividend: Some(0.0),
162+
maturity: "2024-01-01".to_string(),
163+
option_type: "C".to_string(),
164+
simulation: None
165+
}),
166+
pricer: "Analytical".to_string(),
167+
asset: "".to_string(),
168+
style: Some("European".to_string()),
169+
rate_data: None
170+
};
171+
let option = EquityOption::from_json(data);
172+
assert_eq!(option.option_type, OptionType::Call);
173+
assert_eq!(option.transection, Transection::Buy);
174+
assert_eq!(option.underlying_price.value, 100.0);
175+
assert_eq!(option.strike_price, 100.0);
176+
assert_eq!(option.current_price.value, 10.0);
177+
assert_eq!(option.dividend_yield, 0.0);
178+
assert_eq!(option.volatility, 0.2);
179+
assert_eq!(option.maturity_date, NaiveDate::from_ymd(2024, 1, 1));
180+
assert_eq!(option.valuation_date, Local::today().naive_utc());
181+
assert_eq!(option.engine, Engine::BlackScholes);
182+
assert_eq!(option.style, ContractStyle::European);
183+
}
184+
}
185+

derivatives/src/utils/parse_json.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub fn process_contract(data: utils::Contract) -> String {
108108

109109
if data.action=="PV" && data.asset=="EQ"{
110110
//let market_data = data.market_data.clone().unwrap();
111-
let option = EquityOption::equityoption_from_json(data.clone());
111+
let option = EquityOption::from_json(data.clone());
112112

113113
let contract_output = utils::ContractOutput{pv:option.npv(),delta:option.delta(),gamma:option.gamma(),vega:option.vega(),theta:option.theta(),rho:option.rho(), error: None };
114114
println!("Theoretical Price ${}", contract_output.pv);

0 commit comments

Comments
 (0)