From 8430240b33fcb37e785aac977432eeff56b4a3c0 Mon Sep 17 00:00:00 2001 From: m5l14i11 Date: Thu, 3 Aug 2023 19:33:07 +0300 Subject: [PATCH] refactor --- ta_lib/strategy/src/base.rs | 95 +++++++++++++++++++++---------------- 1 file changed, 53 insertions(+), 42 deletions(-) diff --git a/ta_lib/strategy/src/base.rs b/ta_lib/strategy/src/base.rs index 36b3cd5a..371b6da5 100644 --- a/ta_lib/strategy/src/base.rs +++ b/ta_lib/strategy/src/base.rs @@ -12,6 +12,51 @@ pub struct OHLCV { pub volume: f64, } +pub struct OHLCVSeries { + pub open: Vec, + pub high: Vec, + pub low: Vec, + pub close: Vec, + pub volume: Vec, +} + +impl OHLCVSeries { + fn new(data: &VecDeque) -> OHLCVSeries { + OHLCVSeries { + open: data.iter().map(|ohlcv| ohlcv.open).collect(), + high: data.iter().map(|ohlcv| ohlcv.high).collect(), + low: data.iter().map(|ohlcv| ohlcv.low).collect(), + close: data.iter().map(|ohlcv| ohlcv.close).collect(), + volume: data.iter().map(|ohlcv| ohlcv.volume).collect(), + } + } +} + +trait Price { + fn hl2(&self) -> Vec; + fn hlc3(&self) -> Vec; + fn hlcc4(&self) -> Vec; + fn ohlc4(&self) -> Vec; +} + +impl Price for OHLCVSeries { + fn hl2(&self) -> Vec { + median_price(&self.high, &self.low) + } + + fn hlc3(&self) -> Vec { + typical_price(&self.high, &self.low, &self.close) + } + + fn hlcc4(&self) -> Vec { + wcl(&self.high, &self.low, &self.close) + } + + fn ohlc4(&self) -> Vec { + average_price(&self.open, &self.high, &self.low, &self.close) + } +} + #[repr(u32)] pub enum Action { GoLong = 1, @@ -27,8 +72,8 @@ pub trait Strategy { fn next(&mut self, data: OHLCV) -> Action; fn can_process(&self) -> bool; fn params(&self) -> HashMap; - fn entry(&self) -> (bool, bool); - fn exit(&self) -> (bool, bool); + fn entry(&self, data: &OHLCVSeries) -> (bool, bool); + fn exit(&self, data: &OHLCVSeries) -> (bool, bool); } pub struct BaseStrategy { @@ -56,8 +101,10 @@ impl Strategy for BaseStrategy { } if self.can_process() { - let (go_long, go_short) = self.entry(); - let (exit_long, exit_short) = self.exit(); + let series = OHLCVSeries::new(&self.data); + + let (go_long, go_short) = self.entry(&series); + let (exit_long, exit_short) = self.exit(&series); if go_long { return Action::GoLong; @@ -90,51 +137,15 @@ impl Strategy for BaseStrategy { map } - fn entry(&self) -> (bool, bool) { + fn entry(&self, _series: &OHLCVSeries) -> (bool, bool) { (false, false) } - fn exit(&self) -> (bool, bool) { + fn exit(&self, _series: &OHLCVSeries) -> (bool, bool) { (false, false) } } -pub struct OHLCVSeries { - pub open: Vec, - pub high: Vec, - pub low: Vec, - pub close: Vec, - pub volume: Vec, -} - -impl OHLCVSeries { - fn new(data: &VecDeque) -> OHLCVSeries { - OHLCVSeries { - open: data.iter().map(|ohlcv| ohlcv.open).collect(), - high: data.iter().map(|ohlcv| ohlcv.high).collect(), - low: data.iter().map(|ohlcv| ohlcv.low).collect(), - close: data.iter().map(|ohlcv| ohlcv.close).collect(), - volume: data.iter().map(|ohlcv| ohlcv.volume).collect(), - } - } - - fn hl2(&self) -> Vec { - median_price(&self.high, &self.low) - } - - fn hlc3(&self) -> Vec { - typical_price(&self.high, &self.low, &self.close) - } - - fn hlcc4(&self) -> Vec { - wcl(&self.high, &self.low, &self.close) - } - - fn ohlc4(&self) -> Vec { - average_price(&self.open, &self.high, &self.low, &self.close) - } -} - #[cfg(test)] mod tests { use super::*;