Skip to content

Commit

Permalink
add random walk indicator (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
chungg authored May 21, 2024
1 parent a2aa3ae commit 13bef7a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/indicator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,3 +799,30 @@ pub fn relative_vigor(
.map(|(n, d)| n / d)
.collect::<Vec<f64>>()
}

/// Random Walk Index
/// https://www.technicalindicators.net/indicators-technical-analysis/168-rwi-random-walk-index
/// https://www.investopedia.com/terms/r/random-walk-index.asp
pub fn rwi(high: &[f64], low: &[f64], close: &[f64], window: usize) -> Vec<(f64, f64)> {
// looks back n number of periods *including* current. other libs may not include current.
izip!(
high[1..].windows(window),
low[1..].windows(window),
_true_range(high, low, close)
.collect::<Vec<f64>>()
.windows(window),
)
.map(|(h, l, tr)| {
let mut rwi_high: f64 = 0.0;
let mut rwi_low: f64 = 0.0;
let mut tr_sum = 0.0;
for i in 2..=window {
tr_sum += tr[window - i];
let denom = (tr_sum / (i - 1) as f64) * ((i - 1) as f64).sqrt();
rwi_high = rwi_high.max((h[window - 1] - l[window - i]) / denom);
rwi_low = rwi_low.max((h[window - i] - l[window - 1]) / denom);
}
(rwi_high, rwi_low)
})
.collect::<Vec<(f64, f64)>>()
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +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!(smooth::dema(&stats.close, 16).count());
dbg!(indicator::rwi(&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 @@ -1260,3 +1260,32 @@ fn test_relative_vigor() {
result
);
}

#[test]
fn test_rwi() {
let stats = common::test_data();
let result = indicator::rwi(&stats.high, &stats.low, &stats.close, 16);
assert_eq!(
vec![
(0.9498065403643551, 1.7412617466727387,),
(0.4556737540683345, 1.7685523890567207,),
(1.4057386211737706, 1.535472296106406,),
(1.3556485021734983, 0.9465833084480927,),
(0.8746671860087694, 0.9071038763955941,),
(1.7233013034774831, 0.754581037874933,),
(1.1384779666654379, 0.7193916946064202,),
(0.8296249568590155, 2.0526327677377094,),
(2.325650322092707, 0.5940724651955457,),
(1.8773568877196494, 0.28738638191882215,),
(1.1197267076507562, 1.3710140568798879,),
(1.3627638621436993, 0.9996428423372017,),
(1.5457049579330462, 0.6073614247767936,),
(1.1223183991371906, 0.8673129298714549,),
(1.4756091887717602, 0.7926835319767893,),
(1.1929354504792102, 0.7647051876347102,),
(2.6484633151147925, 0.21501740699560723,),
(1.111277033009784, 1.1996531008663207,),
],
result
);
}

0 comments on commit 13bef7a

Please sign in to comment.