From d692476d05f9e542ebb2eede4658f01afa57e5c9 Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Tue, 26 Nov 2024 00:31:59 +0000 Subject: [PATCH] add annualization factor and implement frequency calculation for historic volatility --- src/panel/historic_vol.rs | 94 +++++++++++++++------------------------ 1 file changed, 37 insertions(+), 57 deletions(-) diff --git a/src/panel/historic_vol.rs b/src/panel/historic_vol.rs index 74cdde5..a7f6077 100644 --- a/src/panel/historic_vol.rs +++ b/src/panel/historic_vol.rs @@ -1,6 +1,14 @@ +use chrono::NaiveDate; use ndarray::{Array, Array1, Zip}; use polars::prelude::*; +/// Returns the annualization factor for 252 trading days. +/// (SQRT(252)) +#[allow(dead_code)] +fn annualization_factor() -> f64 { + 252f64.sqrt() +} + #[allow(dead_code)] fn expo_weights(lback_periods: usize, half_life: f64) -> Array1 { // Calculates exponential series weights for finite horizon, normalized to 1. @@ -48,25 +56,37 @@ fn flat_std(x: &Array1, remove_zeros: bool) -> f64 { filtered_x.mapv(f64::abs).mean().unwrap_or(0.0) } +fn freq_daily_calc( + dfw: &DataFrame, + lback_periods: usize, + lback_method: &str, + half_life: Option, + remove_zeros: bool, + nan_tolerance: f64, +) -> Result> { + if lback_method == "xma" { + assert!( + half_life.is_some(), + "If lback_method is 'xma', half_life must be provided." + ); + } + let mut new_df = DataFrame::new(vec![])?; + match lback_method { + "ma" => Ok(new_df), + "xma" => Ok(new_df), + _ => Err("Invalid lookback method.".into()), + } +} // #[allow(dead_code)] -// fn single - - - - - - - - - - - - - - - +// fn single_calc( +// // end_date: -- naive datetime +// end_date: NaiveDate, +// wide_df: &DataFrame, +// lback_periods: usize, +// lback_method: &str, +// nan_tolerance: f64, /// Calculate historic volatility. /// Arguments: @@ -101,46 +121,6 @@ pub fn historic_vol( ) -> Result> { println!("Calculating historic volatility with the following parameters:"); println!("xcat: {:?},\ncids: {:?},\nlback_periods: {:?},lback_method: {:?},\nhalf_life: {:?},\nstart: {:?},\nend: {:?},\nest_freq: {:?},\nremove_zeros: {:?},\npostfix: {:?},\nnan_tolerance: {:?}", xcat, cids, lback_periods,lback_method, half_life, start, end, est_freq, remove_zeros, postfix, nan_tolerance); - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + Ok(df.to_owned()) }