Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
m5l14i11 committed Aug 9, 2023
1 parent 334d219 commit 7ed92c6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
19 changes: 13 additions & 6 deletions ta_lib/strategies/base/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,19 @@ pub trait StrategySignals {
fn exit(&self, data: &OHLCVSeries) -> (Series<bool>, Series<bool>);
}

pub trait Strategy {
fn parameters(&self) -> Vec<usize>;
fn next(&mut self, data: OHLCV) -> TradeAction;
}

pub struct BaseStrategy<S: StrategySignals> {
data: VecDeque<OHLCV>,
strategy: S,
lookback_period: usize,
}

impl<S: StrategySignals> BaseStrategy<S> {
pub fn new(lookback_period: usize, strategy: S) -> BaseStrategy<S> {
pub fn new(strategy: S, lookback_period: usize) -> BaseStrategy<S> {
let lookback_period = max(lookback_period, DEFAULT_LOOKBACK);

BaseStrategy {
Expand All @@ -107,8 +112,10 @@ impl<S: StrategySignals> BaseStrategy<S> {
fn can_process(&self) -> bool {
self.data.len() >= self.lookback_period
}
}

pub fn next(&mut self, data: OHLCV) -> TradeAction {
impl<S: StrategySignals> Strategy for BaseStrategy<S> {
fn next(&mut self, data: OHLCV) -> TradeAction {
self.store(data);

if !self.can_process() {
Expand Down Expand Up @@ -152,7 +159,7 @@ impl<S: StrategySignals> BaseStrategy<S> {
}
}

pub fn parameters(&self) -> Vec<usize> {
fn parameters(&self) -> Vec<usize> {
self.strategy.parameters()
}
}
Expand Down Expand Up @@ -181,19 +188,19 @@ mod tests {

#[test]
fn test_base_strategy_lookback() {
let strategy = BaseStrategy::<DumbStrategy>::new(2, DumbStrategy { short_period: 10 });
let strategy = BaseStrategy::<DumbStrategy>::new(DumbStrategy { short_period: 10 }, 2);
assert_eq!(strategy.lookback_period, 55);
}

#[test]
fn test_base_strategy_parameters() {
let strategy = BaseStrategy::<DumbStrategy>::new(2, DumbStrategy { short_period: 10 });
let strategy = BaseStrategy::<DumbStrategy>::new(DumbStrategy { short_period: 10 }, 2);
assert_eq!(strategy.parameters(), vec![10]);
}

#[test]
fn test_strategy_data() {
let mut strategy = BaseStrategy::new(3, DumbStrategy { short_period: 10 });
let mut strategy = BaseStrategy::new(DumbStrategy { short_period: 10 }, 3);
let ohlcvs = vec![
OHLCV {
open: 1.0,
Expand Down
4 changes: 2 additions & 2 deletions ta_lib/strategies/trend_follow/src/cross_ma.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use base::base::{BaseStrategy, OHLCVSeries, StrategySignals};
use base::base::{BaseStrategy, OHLCVSeries, Strategy, StrategySignals};
use core::series::Series;
use std::cmp::max;
use trend::sma::sma;
Expand All @@ -16,7 +16,7 @@ impl MACrossStrategy {
long_period,
};

BaseStrategy::new(lookback_period, strategy)
BaseStrategy::new(strategy, lookback_period)
}
}

Expand Down

0 comments on commit 7ed92c6

Please sign in to comment.