wip: hist vol

This commit is contained in:
Palash Tyagi 2024-11-29 00:49:11 +00:00
parent 6fb9b0f076
commit 421178a5bb

View File

@ -249,7 +249,7 @@ fn get_period_indices(
/// - `remove_zeros`: Whether to remove zero values from the calculation. Defaults to False. /// - `remove_zeros`: Whether to remove zero values from the calculation. Defaults to False.
/// - `postfix`: A string to append to XCAT of the result series. /// - `postfix`: A string to append to XCAT of the result series.
/// - `nan_tolerance`: The maximum proportion of NaN values allowed in the calculation. /// - `nan_tolerance`: The maximum proportion of NaN values allowed in the calculation.
/// ///
#[allow(unused_variables)] #[allow(unused_variables)]
pub fn historic_vol( pub fn historic_vol(
df: polars::prelude::DataFrame, df: polars::prelude::DataFrame,
@ -269,19 +269,40 @@ pub fn historic_vol(
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); 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);
let mut dfw = pivot_dataframe_by_ticker(df.clone(), Some("value".to_string()))?; let mut dfw = pivot_dataframe_by_ticker(df.clone(), Some("value".to_string()))?;
println!("Successfully pivoted the DataFrame.");
let lback_periods = lback_periods.unwrap_or(20); let lback_periods = lback_periods.unwrap_or(20);
let lback_method = lback_method.unwrap_or("ma".to_string()); let lback_method = lback_method.unwrap_or("ma".to_string());
let half_life = half_life; let half_life = half_life;
println!("Successfully got lback_periods, lback_method, and half_life.");
let start = start.unwrap_or(dfw.column("real_date")?.date()?.min().unwrap().to_string()); let start = start.unwrap_or(dfw.column("real_date")?.date()?.min().unwrap().to_string());
let end = end.unwrap_or(dfw.column("real_date")?.date()?.max().unwrap().to_string()); let end = end.unwrap_or(dfw.column("real_date")?.date()?.max().unwrap().to_string());
println!("Successfully got start and end dates.");
let est_freq = est_freq.unwrap_or("D".to_string()); let est_freq = est_freq.unwrap_or("D".to_string());
println!("Successfully got est_freq.");
let remove_zeros = remove_zeros.unwrap_or(false); let remove_zeros = remove_zeros.unwrap_or(false);
println!("Successfully got remove_zeros.");
let postfix = postfix.unwrap_or("_HISTVOL".to_string()); let postfix = postfix.unwrap_or("_HISTVOL".to_string());
println!("Successfully got postfix.");
let nan_tolerance = nan_tolerance.unwrap_or(0.25); let nan_tolerance = nan_tolerance.unwrap_or(0.25);
println!("Successfully got nan_tolerance.");
let start_date = NaiveDate::parse_from_str(&start, "%Y-%m-%d")?; let start_date = NaiveDate::parse_from_str(&start, "%Y-%m-%d")?;
let end_date = NaiveDate::parse_from_str(&end, "%Y-%m-%d")?; let end_date = NaiveDate::parse_from_str(&end, "%Y-%m-%d")?;
println!("Successfully parsed start and end dates.");
dfw = dfw dfw = dfw
.lazy() .lazy()
.filter( .filter(
@ -298,6 +319,8 @@ pub fn historic_vol(
) )
.collect()?; .collect()?;
println!("Successfully filtered the DataFrame.");
let period = match est_freq.as_str() { let period = match est_freq.as_str() {
"W" => "weekly", "W" => "weekly",
"M" => "monthly", "M" => "monthly",