Skip to content

Commit

Permalink
add pretty good oscillator (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
chungg authored May 14, 2024
1 parent 275b592 commit 4d36540
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
19 changes: 19 additions & 0 deletions src/indicator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,22 @@ pub fn ultimate(h: &[f64], l: &[f64], c: &[f64], win1: u8, win2: u8, win3: u8) -
})
.collect::<Vec<f64>>()
}

/// pretty good oscillator
/// https://library.tradingtechnologies.com/trade/chrt-ti-pretty-good-oscillator.html
pub fn pgo(h: &[f64], l: &[f64], c: &[f64], window: u8) -> Vec<f64> {
let atr = smooth::ewma(
&izip!(&h[1..], &l[1..], &c[..c.len() - 1])
.map(|(h, l, prevc)| (h - l).max(f64::abs(h - prevc)).max(f64::abs(l - prevc)))
.collect::<Vec<f64>>(),
window,
);
let sma_close = smooth::sma(c, window);
izip!(
&c[c.len() - atr.len()..],
&sma_close[sma_close.len() - atr.len()..],
atr
)
.map(|(c, c_ma, tr_ma)| (c - c_ma) / tr_ma)
.collect::<Vec<f64>>()
}
9 changes: 1 addition & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,5 @@ fn main() {
let data = fs::read_to_string("./tests/rddt.input").expect("Unable to read file");
let stats: SecStats = serde_json::from_str(&data).expect("JSON does not have correct format.");

dbg!(indicator::ultimate(
&stats.high,
&stats.low,
&stats.close,
6,
12,
24
));
dbg!(indicator::pgo(&stats.high, &stats.low, &stats.close, 16,));
}
29 changes: 29 additions & 0 deletions tests/indicator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,3 +726,32 @@ fn test_ultimate() {
result
);
}

#[test]
fn test_pgo() {
let stats = common::test_data();
let result = indicator::pgo(&stats.high, &stats.low, &stats.close, 16);
assert_eq!(
vec![
-1.1728195008646218,
-1.3831996312942632,
-0.669537387649706,
-0.6555101010723227,
-0.37389208904655474,
-0.18480884776195328,
-0.01324917609586888,
-0.11871191726864071,
0.6407704612678665,
0.8895192850294852,
0.4619045740578769,
0.8450928215439151,
1.234792786595683,
0.9512531985339747,
1.3179726777354788,
1.5016968220594469,
1.7570721475194715,
1.0410621667725752,
],
result
);
}

0 comments on commit 4d36540

Please sign in to comment.