Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
m5l14i11 committed May 2, 2024
1 parent 72d4848 commit da134a5
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 77 deletions.
13 changes: 12 additions & 1 deletion ta_lib/core/src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ mod tests {
}

#[test]
fn test_sum() {
fn test_sum_first_nan() {
let source = Series::from([f32::NAN, 2.0, 3.0, 4.0, 5.0]);
let expected = Series::from([f32::NAN, 2.0, 5.0, 9.0, 12.0]);
let n = 3;
Expand All @@ -215,6 +215,17 @@ mod tests {
assert_eq!(result, expected);
}

#[test]
fn test_sum_last_nan() {
let source = Series::from([1.0, 2.0, f32::NAN, f32::NAN, f32::NAN]);
let expected = Series::from([1.0, 3.0, 3.0, 2.0, f32::NAN]);
let n = 3;

let result = source.sum(n);

assert_eq!(result, expected);
}

#[test]
fn test_std() {
let source = Series::from([2.0, 4.0, 6.0, 8.0, 10.0, 9.0, 8.0, 7.0, 6.0, 5.0]);
Expand Down
6 changes: 4 additions & 2 deletions ta_lib/core/src/ops.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::constants::ZERO;
use crate::series::Series;
use crate::traits::Operation;
use std::ops::{Add, Div, Mul, Sub};
Expand All @@ -21,7 +22,7 @@ impl Operation<f32, f32, f32> for Series<f32> {
}

fn sdiv(&self, scalar: &f32) -> Series<f32> {
self.ops(scalar, |v, s| if s != &0.0 { v / s } else { 0.0 })
self.ops(scalar, |v, s| if *s != ZERO { v / s } else { ZERO })
}

fn ssub(&self, scalar: &f32) -> Series<f32> {
Expand Down Expand Up @@ -51,7 +52,8 @@ impl Operation<Series<f32>, f32, f32> for Series<f32> {

fn sdiv(&self, rhs: &Series<f32>) -> Series<f32> {
self.zip_with(rhs, |a, b| match (a, b) {
(Some(a_val), Some(b_val)) if b_val != &0.0 => Some(a_val / b_val),
(Some(a_val), Some(b_val)) if *b_val == ZERO => Some(ZERO),
(Some(a_val), Some(b_val)) if *b_val != ZERO => Some(a_val / b_val),
_ => None,
})
}
Expand Down
52 changes: 7 additions & 45 deletions ta_lib/core/src/smoothing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,55 +219,17 @@ mod tests {
]);

let expected = Series::from([
f32::NAN,
7.088,
7.10375,
7.1230836,
7.121082,
7.133084,
7.1425056,
7.117501,
7.1051655,
7.1194954,
7.13158,
7.152491,
7.1531696,
7.143088,
7.140269,
7.142896,
7.1491756,
7.1520867,
7.1434836,
7.1423316,
7.15166,
7.146748,
7.136764,
7.1304145,
7.1352515,
7.1282535,
7.117897,
7.113399,
7.124653,
7.125424,
7.121816,
7.122751,
7.118672,
7.1230693,
7.124495,
7.114359,
7.1180387,
7.1298475,
7.1348333,
7.133007,
7.1183147,
7.1196218,
7.15893,
7.164693,
7.1547427,
7.1135, 7.088, 7.10375, 7.1230836, 7.121082, 7.133084, 7.1425056, 7.117501, 7.1051655,
7.1194954, 7.13158, 7.152491, 7.1531696, 7.143088, 7.140269, 7.142896, 7.1491756,
7.1520867, 7.1434836, 7.1423316, 7.15166, 7.146748, 7.136764, 7.1304145, 7.1352515,
7.1282535, 7.117897, 7.113399, 7.124653, 7.125424, 7.121816, 7.122751, 7.118672,
7.1230693, 7.124495, 7.114359, 7.1180387, 7.1298475, 7.1348333, 7.133007, 7.1183147,
7.1196218, 7.15893, 7.164693, 7.1547427,
]);

let result = source.linreg(3);

assert_eq!(result.len(), expected.len());
assert_eq!(result, expected);
}
}
2 changes: 1 addition & 1 deletion ta_lib/indicators/momentum/src/cmo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub fn cmo(source: &Series<f32>, period: usize) -> Series<f32> {
let zero = Series::zero(source.len());

let hcls = iff!(mom.sgte(&ZERO), mom, zero);
let lcls = iff!(mom.slte(&ZERO), mom.negate(), zero);
let lcls = iff!(mom.slt(&ZERO), mom.negate(), zero);

let hsum = hcls.sum(period);
let lsum = lcls.sum(period);
Expand Down
8 changes: 1 addition & 7 deletions ta_lib/indicators/momentum/src/pr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ pub fn pr(
let hh = high.highest(period);
let ll = low.lowest(period);

let div = &hh - &ll;

iff!(
div.seq(&ZERO),
Series::zero(hh.len()),
SCALE * (source - &hh) / &div
)
SCALE * (source - &hh) / (&hh - &ll)
}

#[cfg(test)]
Expand Down
8 changes: 1 addition & 7 deletions ta_lib/indicators/momentum/src/stoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ pub fn stoch(
let hh = high.highest(period);
let ll = low.lowest(period);

let div = &hh - &ll;

iff!(
div.seq(&ZERO),
Series::zero(hh.len()),
SCALE * (source - &ll) / div
)
SCALE * (source - &ll) / (&hh - &ll)
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion ta_lib/indicators/trend/src/lsma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod tests {
#[test]
fn test_lsma() {
let source = Series::from([1.0, 2.0, 3.0, 4.0, 5.0]);
let expected = vec![0.0, 2.0, 3.0000002, 4.0, 5.0];
let expected = vec![1.0, 2.0, 3.0000002, 4.0, 5.0];

let result: Vec<f32> = lsma(&source, 3).into();

Expand Down
12 changes: 6 additions & 6 deletions ta_lib/indicators/trend/src/zlsma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ mod tests {
]);
let period = 3;
let expected = vec![
0.0, 7.088, 5.925042, 7.1236806, 7.1175256, 7.135417, 7.1420703, 7.1117597, 7.1072783,
7.1239357, 7.1312175, 7.1539693, 7.1497893, 7.1412854, 7.141484, 7.143811, 7.149786,
7.15152, 7.1415567, 7.143571, 7.1534204, 7.1443624, 7.135909, 7.1310244, 7.137115,
7.1262765, 7.117335, 7.114386, 7.127291, 7.1236925, 7.12111, 7.1235223, 7.117843,
7.1245036, 7.1240044, 7.1124487, 7.1202955, 7.1312103, 7.1337156, 7.131895, 7.116152,
7.1222796, 7.1652455, 7.1591597, 7.152091,
7.1135, 7.088, 7.1106257, 7.1236806, 7.1175256, 7.135417, 7.1420703, 7.1117597,
7.1072783, 7.1239357, 7.1312175, 7.1539693, 7.1497893, 7.1412854, 7.141484, 7.143811,
7.149786, 7.15152, 7.1415567, 7.143571, 7.1534204, 7.1443624, 7.135909, 7.1310244,
7.137115, 7.1262765, 7.117335, 7.114386, 7.127291, 7.1236925, 7.12111, 7.1235223,
7.117843, 7.1245036, 7.1240044, 7.1124487, 7.1202955, 7.1312103, 7.1337156, 7.131895,
7.116152, 7.1222796, 7.1652455, 7.1591597, 7.152091,
];

let result: Vec<f32> = zlsma(&source, period).into();
Expand Down
2 changes: 1 addition & 1 deletion ta_lib/indicators/volatility/src/snatr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn test_snatr() {
let period = 3;
let epsilon = 0.001;
let expected = [
0.0, 0.0, 0.0, 0.8257546, 0.99494743, 0.9974737, 1.0, 0.9014031, 0.5520136,
0.0, 0.0, 0.5, 0.8257546, 0.99494743, 0.9974737, 1.0, 0.9014031, 0.5520136,
];

let result: Vec<f32> = snatr(&atr, atr_period, Smooth::WMA, period).into();
Expand Down
6 changes: 2 additions & 4 deletions ta_lib/indicators/volume/src/mfi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ pub fn mfi(hlc3: &Series<f32>, volume: &Series<f32>, period: usize) -> Series<f3

let money_ratio = upper / lower;

let mfi = SCALE - SCALE / (1. + money_ratio);

mfi.nz(Some(0.5 * SCALE))
SCALE - SCALE / (1. + money_ratio)
}

#[cfg(test)]
Expand All @@ -29,7 +27,7 @@ mod tests {
let period = 3;
let epsilon = 0.001;

let expected = [50.0, 50.0, 51.9992, 36.1106, 34.2859];
let expected = [0.0, 0.0, 51.9992, 36.1106, 34.2859];

let result: Vec<f32> = mfi(&hlc3, &volume, period).into();

Expand Down
2 changes: 1 addition & 1 deletion ta_lib/strategies/base/src/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::collections::VecDeque;

const DEFAULT_LOOKBACK: usize = 55;
const DEFAULT_STOP_LEVEL: f32 = -1.0;
const DEFAULT_BUFF_SIZE: f32 = 1.3;
const DEFAULT_BUFF_SIZE: f32 = 1.236;

#[derive(Debug, PartialEq)]
pub enum TradeAction {
Expand Down
8 changes: 7 additions & 1 deletion ta_lib/strategies/indicator/src/ma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ pub fn ma_indicator(
) -> Series<f32> {
match ma {
MovingAverageType::ALMA => alma(&data.source(source_type), period, 0.85, 6.0),
MovingAverageType::CAMA => cama(data.close(), data.high(), data.low(), &data.tr(), period),
MovingAverageType::CAMA => cama(
&data.source(source_type),
data.high(),
data.low(),
&data.tr(),
period,
),
MovingAverageType::DEMA => dema(&data.source(source_type), period),
MovingAverageType::EMA => ema(&data.source(source_type), period),
MovingAverageType::FRAMA => {
Expand Down
109 changes: 109 additions & 0 deletions ta_lib/strategies/pulse/src/tdfi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,112 @@ impl Pulse for TdfiPulse {
(tdfi.sgt(&TDFI_UPPER_LINE), tdfi.slt(&TDFI_LOWER_LINE))
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::collections::VecDeque;

#[test]
fn test_pulse_tdfi() {
let pulse = TdfiPulse::new(SourceType::CLOSE, Smooth::TEMA, 6.0, 3.0);
let data = VecDeque::from([
OHLCV {
ts: 1679825700,
open: 5.993,
high: 6.000,
low: 5.983,
close: 5.997,
volume: 100.0,
},
OHLCV {
ts: 1679826000,
open: 5.997,
high: 6.001,
low: 5.989,
close: 6.001,
volume: 100.0,
},
OHLCV {
ts: 1679826300,
open: 6.001,
high: 6.0013,
low: 5.993,
close: 6.007,
volume: 100.0,
},
OHLCV {
ts: 1679826600,
open: 6.007,
high: 6.008,
low: 5.980,
close: 5.992,
volume: 100.0,
},
OHLCV {
ts: 1679826900,
open: 5.992,
high: 5.993,
low: 5.976,
close: 5.980,
volume: 100.0,
},
OHLCV {
ts: 1679827200,
open: 5.980,
high: 5.986,
low: 5.966,
close: 5.969,
volume: 100.0,
},
OHLCV {
ts: 1679827500,
open: 5.969,
high: 5.969,
low: 5.943,
close: 5.946,
volume: 100.0,
},
OHLCV {
ts: 1679827800,
open: 5.946,
high: 5.960,
low: 5.939,
close: 5.953,
volume: 100.0,
},
OHLCV {
ts: 1679828100,
open: 5.953,
high: 5.961,
low: 5.937,
close: 5.939,
volume: 100.0,
},
OHLCV {
ts: 1679828400,
open: 5.939,
high: 5.945,
low: 5.919,
close: 5.943,
volume: 100.0,
},
]);
let series = OHLCVSeries::from_data(&data);

let (long_signal, short_signal) = pulse.assess(&series);

let expected_long_signal = vec![
false, true, true, false, false, false, false, false, false, false,
];
let expected_short_signal = vec![
false, false, false, true, true, true, true, false, false, false,
];

let result_long_signal: Vec<bool> = long_signal.into();
let result_short_signal: Vec<bool> = short_signal.into();

assert_eq!(result_long_signal, expected_long_signal);
assert_eq!(result_short_signal, expected_short_signal);
}
}

0 comments on commit da134a5

Please sign in to comment.