-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
123 additions
and
20 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ members = [ | |
"patterns/candlestick", | ||
"price", | ||
"strategies/base", | ||
"strategies/trend_follow", | ||
"utils" | ||
] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "trend_follow" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
base = { path = "../base" } | ||
trend = { path = "../../indicators/trend" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use base::base::{BaseStrategy, OHLCVSeries, Strategy, TradeAction, OHLCV}; | ||
use std::cmp::max; | ||
use std::collections::HashMap; | ||
use trend::sma::sma; | ||
|
||
pub struct MACrossStrategy { | ||
base: BaseStrategy, | ||
short_period: usize, | ||
long_period: usize, | ||
} | ||
|
||
impl MACrossStrategy { | ||
pub fn new(short_period: usize, long_period: usize) -> Self { | ||
let lookback_period = max(short_period, long_period); | ||
|
||
MACrossStrategy { | ||
base: BaseStrategy::new(lookback_period), | ||
short_period, | ||
long_period, | ||
} | ||
} | ||
} | ||
|
||
impl Strategy for MACrossStrategy { | ||
fn next(&mut self, data: OHLCV) -> TradeAction { | ||
self.base.next(data) | ||
} | ||
|
||
fn can_process(&self) -> bool { | ||
self.base.can_process() | ||
} | ||
|
||
fn params(&self) -> HashMap<String, usize> { | ||
let mut map = self.base.params(); | ||
map.insert(String::from("short_period"), self.short_period); | ||
map.insert(String::from("long_period"), self.long_period); | ||
map | ||
} | ||
|
||
fn entry(&self, data: &OHLCVSeries) -> (bool, 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(); | ||
|
||
( | ||
long_signal.last().cloned().unwrap_or_default(), | ||
short_signal.last().cloned().unwrap_or_default(), | ||
) | ||
} | ||
|
||
fn exit(&self, data: &OHLCVSeries) -> (bool, bool) { | ||
self.base.exit(data) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_macrossstrategy_new() { | ||
let strat = MACrossStrategy::new(50, 100); | ||
assert_eq!(strat.short_period, 50); | ||
assert_eq!(strat.long_period, 100); | ||
} | ||
|
||
#[test] | ||
fn test_macrossstrategy_params() { | ||
let strat = MACrossStrategy::new(50, 100); | ||
let params = strat.params(); | ||
assert_eq!(params.get("lookback_period"), Some(&100)); | ||
assert_eq!(params.get("short_period"), Some(&50)); | ||
assert_eq!(params.get("long_period"), Some(&100)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod cross_ma; |