Skip to content

Commit

Permalink
compute signal line separately (#104)
Browse files Browse the repository at this point in the history
don't force signal lines to be computed by a specific smoothing fn
  • Loading branch information
chungg authored Sep 19, 2024
1 parent 632f90e commit 9a8b1f4
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 36 deletions.
2 changes: 1 addition & 1 deletion benches/traquer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ fn criterion_benchmark(c: &mut Criterion) {
b.iter(|| black_box(momentum::disparity(&stats.close, 16).collect::<Vec<f64>>()))
});
c.bench_function("sig-momentum-tsi", |b| {
b.iter(|| black_box(momentum::tsi(&stats.close, 6, 10, 3).collect::<Vec<_>>()))
b.iter(|| black_box(momentum::tsi(&stats.close, 6, 10).collect::<Vec<_>>()))
});
c.bench_function("sig-momentum-special_k", |b| {
b.iter(|| black_box(momentum::special_k(&stats.close).collect::<Vec<_>>()))
Expand Down
15 changes: 3 additions & 12 deletions src/momentum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1301,15 +1301,14 @@ pub fn psych<T: ToPrimitive>(data: &[T], window: usize) -> impl Iterator<Item =
///
/// momentum::tsi(
/// &[1.0,2.0,3.0,4.0,5.0,6.0,4.0,5.0,2.0,3.0,4.0,5.0,6.0,4.0],
/// 3, 6, 3).collect::<Vec<(f64,f64)>>();
/// 3, 6).collect::<Vec<f64>>();
///
/// ```
pub fn tsi<T: ToPrimitive>(
data: &[T],
short: usize,
long: usize,
signal: usize,
) -> impl Iterator<Item = (f64, f64)> + '_ {
) -> impl Iterator<Item = f64> + '_ {
let diffs = data
.windows(2)
.map(|pair| pair[1].to_f64().unwrap() - pair[0].to_f64().unwrap())
Expand All @@ -1326,15 +1325,7 @@ pub fn tsi<T: ToPrimitive>(
.zip(abs_pcds)
.map(|(pcd, apcd)| 100.0 * pcd / apcd)
.collect::<Vec<f64>>();
let signal = iter::repeat(f64::NAN)
.take(short - 1)
.chain(smooth::ewma(&tsi[short - 1..], signal));
iter::repeat((f64::NAN, f64::NAN)).take(long).chain(
tsi.iter()
.zip(signal)
.map(|(&x, y)| (x, y))
.collect::<Vec<(f64, f64)>>(),
)
iter::repeat(f64::NAN).take(long).chain(tsi)
}

/// Pring's Special K
Expand Down
24 changes: 1 addition & 23 deletions tests/momentum_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1180,7 +1180,7 @@ fn test_psych() {
#[test]
fn test_tsi() {
let stats = common::test_data();
let (tsi, tsi_sig): (Vec<f64>, Vec<f64>) = momentum::tsi(&stats.close, 6, 10, 3).unzip();
let tsi: Vec<f64> = momentum::tsi(&stats.close, 6, 10).collect();
assert_eq!(stats.close.len(), tsi.len());
assert_eq!(
vec![
Expand All @@ -1206,28 +1206,6 @@ fn test_tsi() {
],
tsi[15..]
);
assert_eq!(
vec![
-11.901736158099775,
-12.240208722437886,
-12.289327166384927,
-10.991640654357855,
-8.530253464421122,
-5.601551181836266,
-3.7880858146019802,
1.4475747198659064,
7.983461323250848,
10.65262893880699,
13.50756522794859,
18.206767909525055,
20.489542773794945,
23.61778069502798,
27.76303682465747,
33.64024394535278,
33.98750951181481,
],
tsi_sig[15 + 2..]
);
}

#[test]
Expand Down

0 comments on commit 9a8b1f4

Please sign in to comment.