From 458ba18bbbf58057095b7ef65c71d511c3172348 Mon Sep 17 00:00:00 2001 From: Siddharth Date: Sat, 28 Oct 2023 18:28:35 -0400 Subject: [PATCH] serialize vol term structure --- derivatives/Cargo.lock | 1 + derivatives/Cargo.toml | 2 +- derivatives/src/equity/vanila_option.rs | 2 -- derivatives/src/equity/vol_surface.rs | 3 ++- derivatives/src/rates/utils.rs | 2 +- derivatives/src/utils/parse_json.rs | 7 +++++-- 6 files changed, 10 insertions(+), 7 deletions(-) diff --git a/derivatives/Cargo.lock b/derivatives/Cargo.lock index 8b3a1be..b4e55de 100644 --- a/derivatives/Cargo.lock +++ b/derivatives/Cargo.lock @@ -98,6 +98,7 @@ dependencies = [ "js-sys", "num-integer", "num-traits", + "serde", "time", "wasm-bindgen", "winapi", diff --git a/derivatives/Cargo.toml b/derivatives/Cargo.toml index 9f50892..e0bcda7 100644 --- a/derivatives/Cargo.toml +++ b/derivatives/Cargo.toml @@ -10,7 +10,7 @@ rand = "0.8.4" rand_distr = "0.4.3" probability = "0.18.0" libm = "0.2.1" -chrono = "0.4.22" +chrono = {version = "0.4.22",features = ["serde"]} csv = "1.1" rand_chacha = "0.3.1" rand_pcg = " 0.3.1" diff --git a/derivatives/src/equity/vanila_option.rs b/derivatives/src/equity/vanila_option.rs index 56bef21..a9a44ea 100644 --- a/derivatives/src/equity/vanila_option.rs +++ b/derivatives/src/equity/vanila_option.rs @@ -126,8 +126,6 @@ impl EquityOption { } } option.set_risk_free_rate(); - println!("{:?}", option); - //option.volatility = option.imp_vol(option.current_price.value); return Box::new(option); } } diff --git a/derivatives/src/equity/vol_surface.rs b/derivatives/src/equity/vol_surface.rs index 582f09e..c6b8025 100644 --- a/derivatives/src/equity/vol_surface.rs +++ b/derivatives/src/equity/vol_surface.rs @@ -1,7 +1,8 @@ +use serde::{Deserialize, Serialize}; use std::collections::{BTreeMap, HashMap}; use crate::rates::utils::{DayCountConvention}; use chrono::{NaiveDate}; -#[derive(Clone,Debug)] +#[derive(Clone,Debug,Serialize,Deserialize)] pub struct VolSurface{ pub term_structure: BTreeMap>, pub spot: f64, diff --git a/derivatives/src/rates/utils.rs b/derivatives/src/rates/utils.rs index 6d6541b..e362269 100644 --- a/derivatives/src/rates/utils.rs +++ b/derivatives/src/rates/utils.rs @@ -42,7 +42,7 @@ fn adjust_for_weekend(mut date: NaiveDate) -> NaiveDate { } date } -#[derive(Clone,Debug)] +#[derive(Clone,Debug,Serialize,Deserialize)] pub enum DayCountConvention{ Act365, Act360, diff --git a/derivatives/src/utils/parse_json.rs b/derivatives/src/utils/parse_json.rs index 02c659f..b78fdf4 100644 --- a/derivatives/src/utils/parse_json.rs +++ b/derivatives/src/utils/parse_json.rs @@ -43,10 +43,13 @@ pub fn build_curve(mut file: &mut File,output_filename: &str)->() { if !vol_dir.exists() { let _ = fs::create_dir(vol_dir); } - dir.push("vol_surface.csv"); + dir.push("vol_surface.json"); //Todo write Vol Surface to file - println!("{:?}",vol_surface); + let mut file = File::create(dir).expect("Failed to create file"); + let mut output: String = String::new(); + let serialized_vol_surface = serde_json::to_string(&vol_surface).unwrap(); + file.write_all(serialized_vol_surface.as_bytes()).expect("Failed to write to file"); } else if list_contracts.asset=="CO"{