Skip to content

Commit

Permalink
entry;exit
Browse files Browse the repository at this point in the history
  • Loading branch information
m5l14i11 committed Aug 3, 2023
1 parent e3d15ef commit c0fec5c
Showing 1 changed file with 72 additions and 58 deletions.
130 changes: 72 additions & 58 deletions ta_lib/strategy/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,23 @@ pub struct OHLCV {
pub volume: f64,
}

#[repr(u32)]
pub enum Action {
GoLong = 1,
GoShort = 2,
ExitLong = 3,
ExitShort = 4,
DoNothing = 0,
}

pub trait Strategy {
const DEFAULT_LOOKBACK: usize = 55;

fn next(&mut self, data: OHLCV);
fn next(&mut self, data: OHLCV) -> Action;
fn can_process(&self) -> bool;
fn params(&self) -> HashMap<String, usize>;
fn entry(&self) -> (bool, bool);
fn exit(&self) -> (bool, bool);
}

pub struct BaseStrategy {
Expand All @@ -37,12 +48,35 @@ impl BaseStrategy {
}

impl Strategy for BaseStrategy {
fn next(&mut self, data: OHLCV) {
fn next(&mut self, data: OHLCV) -> Action {
self.data.push_back(data);

if self.data.len() > self.lookback_period {
self.data.pop_front();
}

if self.can_process() {
let (go_long, go_short) = self.entry();
let (exit_long, exit_short) = self.exit();

if go_long {
return Action::GoLong;
}

if go_short {
return Action::GoShort;
}

if exit_long {
return Action::ExitLong;
}

if exit_short {
return Action::ExitShort;
}
}

Action::DoNothing
}

fn can_process(&self) -> bool {
Expand All @@ -55,71 +89,49 @@ impl Strategy for BaseStrategy {

map
}
}

pub trait StrategySeries {
fn open(&self) -> Vec<f64>;
fn high(&self) -> Vec<f64>;
fn low(&self) -> Vec<f64>;
fn close(&self) -> Vec<f64>;
fn volume(&self) -> Vec<f64>;
fn hl2(&self) -> Vec<f64>;
fn hlc3(&self) -> Vec<f64>;
fn hlcc4(&self) -> Vec<f64>;
fn ohlc4(&self) -> Vec<f64>;
}

impl StrategySeries for BaseStrategy {
fn open(&self) -> Vec<f64> {
self.data.iter().map(|ohlcv| ohlcv.open).collect()
fn entry(&self) -> (bool, bool) {
(false, false)
}

fn high(&self) -> Vec<f64> {
self.data.iter().map(|ohlcv| ohlcv.high).collect()
}

fn low(&self) -> Vec<f64> {
self.data.iter().map(|ohlcv| ohlcv.low).collect()
fn exit(&self) -> (bool, bool) {
(false, false)
}
}

fn close(&self) -> Vec<f64> {
self.data.iter().map(|ohlcv| ohlcv.close).collect()
}
pub struct OHLCVSeries {
pub open: Vec<f64>,
pub high: Vec<f64>,
pub low: Vec<f64>,
pub close: Vec<f64>,
pub volume: Vec<f64>,
}

fn volume(&self) -> Vec<f64> {
self.data.iter().map(|ohlcv| ohlcv.volume).collect()
impl OHLCVSeries {
fn new(data: &VecDeque<OHLCV>) -> 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<f64> {
let high = self.high();
let low = self.low();

median_price(&high, &low)
median_price(&self.high, &self.low)
}

fn hlc3(&self) -> Vec<f64> {
let high = self.high();
let low = self.low();
let close = self.close();

typical_price(&high, &low, &close)
typical_price(&self.high, &self.low, &self.close)
}

fn hlcc4(&self) -> Vec<f64> {
let high = self.high();
let low = self.low();
let close = self.close();

wcl(&high, &low, &close)
wcl(&self.high, &self.low, &self.close)
}

fn ohlc4(&self) -> Vec<f64> {
let open = self.open();
let high = self.high();
let low = self.low();
let close = self.close();

average_price(&open, &high, &low, &close)
average_price(&self.open, &self.high, &self.low, &self.close)
}
}

Expand Down Expand Up @@ -202,18 +214,20 @@ mod tests {
strategy.next(ohlcv);
}

assert_eq!(strategy.open(), vec![2.0, 3.0, 4.0]);
assert_eq!(strategy.high(), vec![3.0, 4.0, 5.0]);
assert_eq!(strategy.low(), vec![1.5, 2.5, 3.5]);
assert_eq!(strategy.close(), vec![2.5, 3.5, 4.5]);
assert_eq!(strategy.volume(), vec![200.0, 300.0, 400.0]);
let series = OHLCVSeries::new(&strategy.data);

assert_eq!(series.open, vec![2.0, 3.0, 4.0]);
assert_eq!(series.high, vec![3.0, 4.0, 5.0]);
assert_eq!(series.low, vec![1.5, 2.5, 3.5]);
assert_eq!(series.close, vec![2.5, 3.5, 4.5]);
assert_eq!(series.volume, vec![200.0, 300.0, 400.0]);

assert_eq!(strategy.hl2(), vec![2.25, 3.25, 4.25]);
assert_eq!(series.hl2(), vec![2.25, 3.25, 4.25]);
assert_eq!(
strategy.hlc3(),
series.hlc3(),
vec![2.3333333333333335, 3.3333333333333335, 4.333333333333333]
);
assert_eq!(strategy.hlcc4(), vec![2.375, 3.375, 4.375]);
assert_eq!(strategy.ohlc4(), vec![2.25, 3.25, 4.25]);
assert_eq!(series.hlcc4(), vec![2.375, 3.375, 4.375]);
assert_eq!(series.ohlc4(), vec![2.25, 3.25, 4.25]);
}
}

0 comments on commit c0fec5c

Please sign in to comment.