Skip to content

Commit 12ebe38

Browse files
author
uyplayer
committed
fixed rma_indicator
1 parent ee14463 commit 12ebe38

File tree

3 files changed

+49
-31
lines changed

3 files changed

+49
-31
lines changed

Cargo.lock

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ name = "tech_analysis"
1919
path = "src/main.rs"
2020

2121
[dependencies]
22-
polars = { version = "^0.32.1", features = ["lazy","describe"] }
22+
polars = { version = "^0.32.1", features = ["lazy","describe","rolling_window"] }
2323
rand = "0.8.5"

src/lorentzian_classification/helper.rs

+44-26
Original file line numberDiff line numberDiff line change
@@ -94,33 +94,39 @@ pub fn rescale<'a>(src: &'a Series, old_min: f64, old_max: f64, new_min: f64, n
9494
/// # Returns
9595
///
9696
/// The series containing the EWMA values.
97-
pub fn rma_indicator(src: &Series, length: i32)->Result<Series, Box<dyn std::error::Error>> {
98-
99-
let rolling = src.rolling_mean( length as usize)?;
100-
let mut ewma: Vec<Option<f64>> = vec![];
101-
let alpha = 2.0 / (length + 1) as f64;
102-
103-
let mut prev_ema = None;
104-
for opt in rolling.into_iter() {
105-
if let Some(val) = opt {
106-
match prev_ema {
107-
Some(prev) => {
108-
let ema = alpha * val + (1.0 - alpha) * prev;
109-
ewma.push(Some(ema));
110-
prev_ema = Some(ema);
111-
}
112-
None => {
113-
ewma.push(Some(val));
114-
prev_ema = Some(val);
115-
}
116-
}
117-
} else {
118-
ewma.push(None);
119-
prev_ema = None;
120-
}
97+
pub fn rma_indicator<'a>(src: &'a Series, length: i32)->Result<Series, Box<dyn std::error::Error>> {
98+
let duration = Duration::new(length.into());
99+
let options = RollingOptionsImpl {
100+
window_size: duration,
101+
min_periods: 1,
102+
weights: None,
103+
center: false,
104+
by: None,
105+
tu: None,
106+
tz: None,
107+
closed_window: None,
108+
fn_params: None,
109+
};
110+
let rolling_mean = src.rolling_mean(options)?;
111+
112+
let alpha = 2.0 / (length as f64 + 1.0);
113+
let mut prev_ema: Option<f64> = None;
114+
let mut ewma = Vec::new();
115+
for opt in rolling_mean.f64()? {
116+
let val = match opt {
117+
Some(v) => v,
118+
None => continue,
119+
};
120+
121+
let ema = match prev_ema {
122+
Some(prev) => alpha * val + (1.0 - alpha) * prev,
123+
None => val,
124+
};
125+
ewma.push(ema);
126+
prev_ema = Some(ema);
121127
}
122-
Ok(Series::new("data", &ewma))
123-
128+
let ewm_series = Series::new("date", ewma);
129+
Ok(ewm_series)
124130
}
125131

126132
#[cfg(test)]
@@ -151,4 +157,16 @@ mod tests {
151157
Ok(())
152158
}
153159

160+
#[test]
161+
fn test_rma_indicator()->Result<(), Box<dyn std::error::Error>>{
162+
163+
let mut rng = rand::thread_rng();
164+
let random_data: Vec<f64> = (0..1000).map(|_| rng.gen_range(1.0..2000.0)).collect();
165+
let src = Series::new("data", random_data);
166+
let res = rma_indicator(&src,10)?;
167+
eprintln!("{:?}", res);
168+
Ok(())
169+
170+
}
171+
154172
}

0 commit comments

Comments
 (0)