Skip to content

Commit

Permalink
added unit test for from json
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharthqs committed Oct 29, 2023
1 parent 25b4ef7 commit ef2fd21
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 10 deletions.
2 changes: 1 addition & 1 deletion derivatives/src/core/trade.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(Debug,Clone)]
#[derive(PartialEq,Debug,Clone)]
pub enum Transection {
Buy,
Sell,
Expand Down
2 changes: 1 addition & 1 deletion derivatives/src/core/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::f64::consts::{PI, SQRT_2};
use serde::Serialize;
use crate::Deserialize;

#[derive(Clone,Debug)]
#[derive(PartialEq,Clone,Debug)]
pub enum ContractStyle {
European,
American,
Expand Down
2 changes: 1 addition & 1 deletion derivatives/src/equity/build_contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::equity::utils::{Engine};
use std::collections::BTreeMap;

pub fn build_eq_contracts_from_json(data: Vec<Contract>) -> Vec<Box<EquityOption>> {
let derivatives:Vec<Box<EquityOption>> = data.iter().map(|x| EquityOption::equityoption_from_json(x.clone())).collect();
let derivatives:Vec<Box<EquityOption>> = data.iter().map(|x| EquityOption::from_json(x.clone())).collect();
return derivatives;
}
pub fn build_volatility_surface(mut contracts:Vec<Box<EquityOption>>) -> VolSurface {
Expand Down
2 changes: 1 addition & 1 deletion derivatives/src/equity/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(Clone,Debug)]
#[derive(PartialEq,Clone,Debug)]
pub enum Engine{
BlackScholes,
MonteCarlo,
Expand Down
64 changes: 59 additions & 5 deletions derivatives/src/equity/vanila_option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ impl Instrument for EquityOption {
value
}
Engine::MonteCarlo => {
println!("Using MonteCarlo ");
println!("Using MonteCarlo Engine ");
let value = montecarlo::npv(&self,false);
value
}
Engine::Binomial => {
println!("Using Binomial ");
println!("Using Binomial Engine ");
let value = binomial::npv(&self);
value
}
Expand Down Expand Up @@ -59,7 +59,7 @@ impl EquityOption{
}
}
impl EquityOption {
pub fn equityoption_from_json(data: Contract) -> Box<EquityOption> {
pub fn from_json(data: Contract) -> Box<EquityOption> {
let market_data = data.market_data.unwrap();
let underlying_quote = Quote::new(market_data.underlying_price);
//TODO: Add term structure
Expand All @@ -83,13 +83,20 @@ impl EquityOption {
Some(x) => x.unwrap(),
None => 0.0,
});
//let volatility = Some(market_data.volatility);
let volatility = match market_data.volatility {
Some(x) => {
x
}
None => 0.2
};
let mut option = EquityOption {
option_type: side,
transection: trade::Transection::Buy,
transection: Transection::Buy,
underlying_price: underlying_quote,
current_price: option_price,
strike_price: market_data.strike_price,
volatility: 0.2,
volatility: volatility,
maturity_date: future_date,
risk_free_rate: risk_free_rate.unwrap_or(0.0),
dividend_yield: dividend.unwrap_or(0.0),
Expand Down Expand Up @@ -129,3 +136,50 @@ impl EquityOption {
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 = EquityOption::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);
}
}

2 changes: 1 addition & 1 deletion derivatives/src/utils/parse_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub fn process_contract(data: utils::Contract) -> String {

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

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 };
println!("Theoretical Price ${}", contract_output.pv);
Expand Down

0 comments on commit ef2fd21

Please sign in to comment.