Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
m5l14i11 committed Aug 8, 2023
1 parent 243a2d1 commit dffd9f8
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 16 deletions.
2 changes: 2 additions & 0 deletions ta_lib/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ta_lib/strategies/base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ version = "0.1.0"
edition = "2021"

[dependencies]
core = { path = "../../core" }
price = { path = "../../price" }
38 changes: 30 additions & 8 deletions ta_lib/strategies/base/src/base.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::series::Series;
use price::{average::average_price, median::median_price, typical::typical_price, wcl::wcl};
use std::{
cmp::max,
Expand Down Expand Up @@ -79,8 +80,8 @@ pub trait Strategy {
fn next(&mut self, data: OHLCV) -> TradeAction;
fn can_process(&self) -> bool;
fn params(&self) -> HashMap<String, usize>;
fn entry(&self, data: &OHLCVSeries) -> (bool, bool);
fn exit(&self, data: &OHLCVSeries) -> (bool, bool);
fn entry(&self, data: &OHLCVSeries) -> (Series<bool>, Series<bool>);
fn exit(&self, data: &OHLCVSeries) -> (Series<bool>, Series<bool>);
}

pub struct BaseStrategy {
Expand Down Expand Up @@ -117,8 +118,29 @@ impl Strategy for BaseStrategy {

let series = OHLCVSeries::new(&self.data);

let (go_long, go_short) = self.entry(&series);
let (exit_long, exit_short) = self.exit(&series);
let (go_long_series, go_short_series) = self.entry(&series);
let (exit_long_series, exit_short_series) = self.exit(&series);

let go_long = go_long_series
.into_iter()
.flatten()
.last()
.unwrap_or_default();
let go_short = go_short_series
.into_iter()
.flatten()
.last()
.unwrap_or_default();
let exit_long = exit_long_series
.into_iter()
.flatten()
.last()
.unwrap_or_default();
let exit_short = exit_short_series
.into_iter()
.flatten()
.last()
.unwrap_or_default();

let suggested_entry = series.hlc3().last().unwrap_or(&std::f64::NAN).clone();

Expand All @@ -142,12 +164,12 @@ impl Strategy for BaseStrategy {
map
}

fn entry(&self, _series: &OHLCVSeries) -> (bool, bool) {
(false, false)
fn entry(&self, _series: &OHLCVSeries) -> (Series<bool>, Series<bool>) {
(Series::empty(1), Series::empty(1))
}

fn exit(&self, _series: &OHLCVSeries) -> (bool, bool) {
(false, false)
fn exit(&self, _series: &OHLCVSeries) -> (Series<bool>, Series<bool>) {
(Series::empty(1), Series::empty(1))
}
}

Expand Down
1 change: 1 addition & 0 deletions ta_lib/strategies/trend_follow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ version = "0.1.0"
edition = "2021"

[dependencies]
core = { path = "../../core" }
base = { path = "../base" }
trend = { path = "../../indicators/trend" }
14 changes: 6 additions & 8 deletions ta_lib/strategies/trend_follow/src/cross_ma.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use base::base::{BaseStrategy, OHLCVSeries, Strategy, TradeAction, OHLCV};
use core::series::Series;
use std::cmp::max;
use std::collections::HashMap;
use trend::sma::sma;
Expand Down Expand Up @@ -37,20 +38,17 @@ impl Strategy for MACrossStrategy {
map
}

fn entry(&self, data: &OHLCVSeries) -> (bool, bool) {
fn entry(&self, data: &OHLCVSeries) -> (Series<bool>, Series<bool>) {
let short_ma = sma(&data.close, self.short_period);
let long_ma = sma(&data.close, self.long_period);

let long_signal: Vec<bool> = short_ma.cross_over(&long_ma).into();
let short_signal: Vec<bool> = short_ma.cross_under(&long_ma).into();
let long_signal = short_ma.cross_over(&long_ma);
let short_signal = short_ma.cross_under(&long_ma);

(
long_signal.last().cloned().unwrap_or_default(),
short_signal.last().cloned().unwrap_or_default(),
)
(long_signal, short_signal)
}

fn exit(&self, data: &OHLCVSeries) -> (bool, bool) {
fn exit(&self, data: &OHLCVSeries) -> (Series<bool>, Series<bool>) {
self.base.exit(data)
}
}
Expand Down

0 comments on commit dffd9f8

Please sign in to comment.