-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor equity option and added volterm structure
- Loading branch information
1 parent
9664b01
commit 8656f07
Showing
14 changed files
with
272 additions
and
77 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
//use crate::rates; | ||
//use crate::rates::deposits::Deposit; | ||
|
||
use chrono::{NaiveDate,Local,Weekday}; | ||
use chrono::Datelike; | ||
use crate::core::trade; | ||
use super::vanila_option::{EquityOption}; | ||
use super::super::core::termstructure::YieldTermStructure; | ||
use crate::rates::utils::TermStructure; | ||
use crate::equity::vol_surface::VolSurface; | ||
use crate::rates::utils::{DayCountConvention}; | ||
use crate::core::quotes::Quote; | ||
use crate::core::utils::{Contract,ContractStyle}; | ||
use crate::equity::utils::{Engine}; | ||
use std::collections::HashMap; | ||
pub fn build_eq_contracts(data: Contract)-> Box<EquityOption>{ | ||
let market_data = data.market_data.clone().unwrap(); | ||
let underlying_quote = Quote::new( market_data.underlying_price); | ||
let date = vec![0.01,0.02,0.05,0.1,0.5,1.0,2.0,3.0]; | ||
let rates = vec![0.01,0.02,0.05,0.07,0.08,0.1,0.11,0.12]; | ||
let ts = YieldTermStructure::new(date,rates); | ||
let option_type = &market_data.option_type; | ||
let side: trade::OptionType; | ||
match option_type.trim() { | ||
"C" | "c" | "Call" | "call" => side = trade::OptionType::Call, | ||
"P" | "p" | "Put" | "put" => side = trade::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 duration = future_date.signed_duration_since(today.naive_utc()); | ||
let year_fraction = duration.num_days() as f64 / 365.0; | ||
let rf = Some(market_data.risk_free_rate).unwrap(); | ||
let div = Some(market_data.dividend).unwrap(); | ||
let price = Some(market_data.option_price).unwrap(); | ||
let option_price = Quote::new(price.unwrap()); | ||
let mut option = EquityOption { | ||
option_type: side, | ||
transection: trade::Transection::Buy, | ||
underlying_price: underlying_quote, | ||
current_price: option_price, | ||
strike_price: market_data.strike_price, | ||
volatility: 0.2, | ||
time_to_maturity: year_fraction, | ||
risk_free_rate: rf.unwrap_or(0.0), | ||
dividend_yield: div.unwrap_or(0.0), | ||
transection_price: 0.0, | ||
term_structure: ts, | ||
engine: Engine::BlackScholes, | ||
simulation: None, | ||
style: ContractStyle::European, | ||
//style: Option::from(data.style.as_ref().unwrap_or(&default_style)).map(|x| &**x), | ||
}; | ||
match data.pricer.trim() { | ||
"Analytical" |"analytical" => { | ||
option.engine = Engine::BlackScholes; | ||
} | ||
"MonteCarlo" |"montecarlo"|"MC" => { | ||
option.engine = Engine::MonteCarlo; | ||
} | ||
"Binomial"|"binomial" => { | ||
option.engine = Engine::Binomial; | ||
} | ||
_ => { | ||
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(); | ||
option.volatility = option.imp_vol(option.current_price.value); | ||
return Box::new(option); | ||
} | ||
|
||
pub fn build_eq_contracts_from_json(data: Vec<Contract>) -> Vec<Box<EquityOption>> { | ||
let mut derivatives:Vec<Box<EquityOption>> = Vec::new(); | ||
for contract in data { | ||
let eq = build_eq_contracts(contract); | ||
derivatives.push(eq); | ||
} | ||
return derivatives; | ||
} | ||
pub fn build_vol_surface(mut contracts:Vec<Box<EquityOption>>) -> VolSurface { | ||
//let mut ts:rates::utils::TermStructure = rates::utils::TermStructure::new(vec![],vec![],vec![], | ||
// rates::utils::DayCountConvention::Act360); | ||
// let mut vol_surface:VolSurface = VolSurface::new(Default::default(), 0.0, spot_date: NaiveDate::from_ymd(, 2020), | ||
// DayCountConvention::Act365); | ||
let mut hash_map:HashMap<NaiveDate,Vec<(f64,f64)>> = HashMap::new(); | ||
let spot_date = Local::today(); | ||
let spot_price = contracts[0].underlying_price.value; | ||
for i in 0..contracts.len(){ | ||
let mut contract = contracts[i].as_mut(); | ||
let stike = contract.underlying_price.value / contract.strike_price as f64; | ||
let vol = contract.volatility; | ||
let maturity = contract.time_to_maturity; | ||
hash_map.entry(maturity).or_insert(Vec::new()).push((stike,vol)); | ||
} | ||
let vol_surface:VolSurface = VolSurface::new(hash_map, spot_price, spot_date, | ||
DayCountConvention::Act365); | ||
return vol_surface; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use std::collections::HashMap; | ||
use crate::rates::utils::{DayCountConvention}; | ||
use chrono::{NaiveDate}; | ||
#[derive(Clone,Debug)] | ||
pub struct VolSurface{ | ||
pub term_structure: HashMap<NaiveDate, Vec<(f64,f64)>>, | ||
pub spot: f64, | ||
pub spot_date: NaiveDate, | ||
pub day_count: DayCountConvention, | ||
} | ||
|
||
impl VolSurface { | ||
pub fn new(term_structure: HashMap<NaiveDate, Vec<(f64,f64)>>,spot:f64,spot_date:NaiveDate,day_count:DayCountConvention) -> VolSurface { | ||
VolSurface { | ||
term_structure, | ||
spot, | ||
spot_date, | ||
day_count | ||
} | ||
} | ||
pub fn get_vol(&self,val_date:NaiveDate,maturity_date:NaiveDate,strike:f64)-> f64{ | ||
0.0 | ||
} | ||
pub fn get_year_fraction(&self,val_date:NaiveDate,maturity_date:NaiveDate) -> f64 { | ||
self.day_count.year_fraction(val_date,maturity_date) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod deposits; | ||
pub(crate) mod utils; | ||
pub mod fra; | ||
pub mod fra; | ||
pub mod build_contracts; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,3 @@ pub mod read_csv; | |
pub mod RNG; | ||
pub mod stochastic_processes; | ||
pub mod parse_json; | ||
pub mod build_contracts; |
Oops, something went wrong.