diff --git a/ta_lib/core/src/smoothing.rs b/ta_lib/core/src/smoothing.rs index 974e77f1..e0b4937f 100644 --- a/ta_lib/core/src/smoothing.rs +++ b/ta_lib/core/src/smoothing.rs @@ -1,5 +1,5 @@ +use crate::iff; use crate::series::Series; -use crate::{iff, nz}; #[derive(Copy, Clone)] pub enum Smooth { @@ -27,7 +27,7 @@ impl Series { sum } - pub fn wg(&self, weights: &[f32], period: usize) -> Self { + pub fn wg(&self, weights: &[f32]) -> Self { let mut sum = Series::zero(self.len()); let norm = weights.iter().sum::(); @@ -60,7 +60,7 @@ impl Series { fn wma(&self, period: usize) -> Self { let weights = (0..period).map(|i| (period - i) as f32).collect::>(); - self.wg(&weights, period) + self.wg(&weights) } fn swma(&self) -> Self { diff --git a/ta_lib/indicators/trend/src/alma.rs b/ta_lib/indicators/trend/src/alma.rs index daff80a5..ec6d5fd6 100644 --- a/ta_lib/indicators/trend/src/alma.rs +++ b/ta_lib/indicators/trend/src/alma.rs @@ -8,7 +8,7 @@ pub fn alma(source: &Series, period: usize, offset: f32, sigma: f32) -> Ser .map(|i| ((-1. * (i as f32 - m).powi(2)) / (2. * s.powi(2))).exp()) .collect::>(); - source.wg(&weights, period) + source.wg(&weights) } #[cfg(test)] diff --git a/ta_lib/indicators/trend/src/sinwma.rs b/ta_lib/indicators/trend/src/sinwma.rs index 59169dae..6ad39bb3 100644 --- a/ta_lib/indicators/trend/src/sinwma.rs +++ b/ta_lib/indicators/trend/src/sinwma.rs @@ -5,7 +5,7 @@ pub fn sinwma(source: &Series, period: usize) -> Series { .map(|i| ((i as f32 + 1.) * std::f32::consts::PI / (period as f32 + 1.)).sin()) .collect::>(); - source.wg(&weights, period) + source.wg(&weights) } #[cfg(test)] diff --git a/ta_lib/strategies/base/src/strategy.rs b/ta_lib/strategies/base/src/strategy.rs index 5ef2ee38..cdce51fa 100644 --- a/ta_lib/strategies/base/src/strategy.rs +++ b/ta_lib/strategies/base/src/strategy.rs @@ -1,10 +1,10 @@ use crate::source::Source; use crate::{BaseLine, Confirm, Exit, OHLCVSeries, Pulse, Signal, StopLoss, Strategy, OHLCV}; -use core::prelude::*; use std::collections::VecDeque; const DEFAULT_LOOKBACK: usize = 55; const DEFAULT_STOP_LEVEL: f32 = -1.0; +const DEFAULT_BUFF_SIZE: f32 = 1.3; #[derive(Debug, PartialEq)] pub enum TradeAction { @@ -65,7 +65,7 @@ impl BaseStrategy { } fn store(&mut self, data: OHLCV) { - let buf_size = (self.lookback_period as f32 * 1.3) as usize; + let buf_size = (self.lookback_period as f32 * DEFAULT_BUFF_SIZE) as usize; if self.data.len() > buf_size { self.data.pop_front(); @@ -174,6 +174,9 @@ mod tests { }; use core::Series; + const DEFAULT_BUFF_SIZE: f32 = 1.3; + const DEFAULT_LOOKBACK: usize = 55; + struct MockSignal { fast_period: usize, } @@ -300,19 +303,16 @@ mod tests { }), Box::new(MockExit {}), ); - let lookback = 55; - - let ohlcvs = vec![ - OHLCV { - ts: 1710297600000, - open: 1.0, - high: 2.0, - low: 0.5, - close: 1.5, - volume: 100.0, - }; - lookback - ]; + let lookback = (DEFAULT_BUFF_SIZE * DEFAULT_LOOKBACK as f32) as usize; + let data = OHLCV { + ts: 1710297600000, + open: 1.0, + high: 2.0, + low: 0.5, + close: 1.5, + volume: 100.0, + }; + let ohlcvs = vec![data; lookback]; let mut action = TradeAction::DoNothing; @@ -327,10 +327,10 @@ mod tests { let hlcc4: Vec = series.hlcc4().into(); let ohlc4: Vec = series.ohlc4().into(); - assert_eq!(hl2, vec![1.25; lookback]); - assert_eq!(hlc3, vec![1.333_333_4; lookback]); - assert_eq!(hlcc4, vec![1.375; lookback]); - assert_eq!(ohlc4, vec![1.25; lookback]); + assert_eq!(hl2, vec![1.25]); + assert_eq!(hlc3, vec![1.333_333_4]); + assert_eq!(hlcc4, vec![1.375]); + assert_eq!(ohlc4, vec![1.25]); assert_eq!(action, TradeAction::DoNothing); } } diff --git a/ta_lib/strategies/pulse/src/braid.rs b/ta_lib/strategies/pulse/src/braid.rs index 39bec469..89b4dff0 100644 --- a/ta_lib/strategies/pulse/src/braid.rs +++ b/ta_lib/strategies/pulse/src/braid.rs @@ -101,7 +101,7 @@ mod tests { volume: 100.0, }, OHLCV { - ts: 1679828100, + ts: 1679828400, open: 4.9053, high: 4.9093, low: 4.9046, @@ -109,7 +109,7 @@ mod tests { volume: 100.0, }, OHLCV { - ts: 1679828400, + ts: 1679828700, open: 4.9087, high: 4.9154, low: 4.9087, @@ -117,7 +117,7 @@ mod tests { volume: 100.0, }, OHLCV { - ts: 1679828700, + ts: 1679829000, open: 4.9131, high: 4.9131, low: 4.9040, @@ -125,7 +125,7 @@ mod tests { volume: 100.0, }, OHLCV { - ts: 1679829000, + ts: 1679829300, open: 4.9041, high: 4.9068, low: 4.8988, @@ -133,7 +133,7 @@ mod tests { volume: 100.0, }, OHLCV { - ts: 1679829300, + ts: 1679829600, open: 4.9023, high: 4.9051, low: 4.8949, @@ -141,7 +141,7 @@ mod tests { volume: 100.0, }, OHLCV { - ts: 1679829600, + ts: 1679829900, open: 4.9010, high: 4.9052, low: 4.8969, @@ -149,7 +149,7 @@ mod tests { volume: 100.0, }, OHLCV { - ts: 1679829900, + ts: 1679830200, open: 4.8969, high: 4.8969, low: 4.8819, @@ -157,7 +157,7 @@ mod tests { volume: 100.0, }, OHLCV { - ts: 1679830200, + ts: 1679830500, open: 4.8895, high: 4.8928, low: 4.8851, @@ -165,7 +165,7 @@ mod tests { volume: 100.0, }, OHLCV { - ts: 1679830500, + ts: 1679830800, open: 4.8901, high: 4.8910, low: 4.8813, @@ -173,7 +173,7 @@ mod tests { volume: 100.0, }, OHLCV { - ts: 1679830800, + ts: 1679831100, open: 4.8855, high: 4.8864, low: 4.8816, @@ -181,7 +181,7 @@ mod tests { volume: 100.0, }, OHLCV { - ts: 1679831100, + ts: 1679831400, open: 4.8824, high: 4.8934, low: 4.8814,