use crate::util::dataframe_utils::check_window_size;
use polars::prelude::*;
pub fn calculate_aroon(df: &DataFrame, window: usize) -> PolarsResult<(Series, Series)> {
check_window_size(df, window, "Aroon")?;
let high = df.column("high")?.f64()?;
let low = df.column("low")?.f64()?;
let mut aroon_up = Vec::with_capacity(df.height());
let mut aroon_down = Vec::with_capacity(df.height());
for _ in 0..window - 1 {
aroon_up.push(f64::NAN);
aroon_down.push(f64::NAN);
}
for i in window - 1..df.height() {
let mut high_idx = 0;
let mut low_idx = 0;
let mut high_val = f64::MIN;
let mut low_val = f64::MAX;
for j in 0..window {
let h = high.get(i - j).unwrap_or(0.0);
let l = low.get(i - j).unwrap_or(0.0);
if h > high_val {
high_val = h;
high_idx = j;
}
if l < low_val {
low_val = l;
low_idx = j;
}
}
let up = 100.0 * ((window as f64 - high_idx as f64) / window as f64);
let down = 100.0 * ((window as f64 - low_idx as f64) / window as f64);
aroon_up.push(up);
aroon_down.push(down);
}
Ok((
Series::new("aroon_up".into(), aroon_up),
Series::new("aroon_down".into(), aroon_down),
))
}