Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
m5l14i11 committed Apr 28, 2024
1 parent eec14b7 commit 4bd53ba
Show file tree
Hide file tree
Showing 65 changed files with 241 additions and 207 deletions.
40 changes: 35 additions & 5 deletions ta_lib/strategies/base/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ pub struct OHLCV {

#[derive(Debug, Clone)]
pub struct OHLCVSeries {
pub open: Series<f32>,
pub high: Series<f32>,
pub low: Series<f32>,
pub close: Series<f32>,
pub volume: Series<f32>,
open: Series<f32>,
high: Series<f32>,
low: Series<f32>,
close: Series<f32>,
volume: Series<f32>,
}

impl OHLCVSeries {
Expand Down Expand Up @@ -55,4 +55,34 @@ impl OHLCVSeries {
volume: Series::from(volume),
}
}

#[inline]
pub fn len(&self) -> usize {
self.close.len()
}

#[inline]
pub fn open(&self) -> &Series<f32> {
&self.open
}

#[inline]
pub fn high(&self) -> &Series<f32> {
&self.high
}

#[inline]
pub fn low(&self) -> &Series<f32> {
&self.low
}

#[inline]
pub fn close(&self) -> &Series<f32> {
&self.close
}

#[inline]
pub fn volume(&self) -> &Series<f32> {
&self.volume
}
}
14 changes: 4 additions & 10 deletions ta_lib/strategies/base/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,30 @@ use core::prelude::*;
use price::prelude::*;

pub trait Source {
fn close(&self) -> Series<f32>;
fn hl2(&self) -> Series<f32>;
fn hlc3(&self) -> Series<f32>;
fn hlcc4(&self) -> Series<f32>;
fn ohlc4(&self) -> Series<f32>;
}

impl Source for OHLCVSeries {
#[inline]
fn close(&self) -> Series<f32> {
self.close.clone()
}

#[inline]
fn hl2(&self) -> Series<f32> {
median_price(&self.high, &self.low)
median_price(self.high(), self.low())
}

#[inline]
fn hlc3(&self) -> Series<f32> {
typical_price(&self.high, &self.low, &self.close)
typical_price(self.high(), self.low(), self.close())
}

#[inline]
fn hlcc4(&self) -> Series<f32> {
wcl(&self.high, &self.low, &self.close)
wcl(&self.high(), self.low(), self.close())
}

#[inline]
fn ohlc4(&self) -> Series<f32> {
average_price(&self.open, &self.high, &self.low, &self.close)
average_price(self.open(), self.high(), self.low(), self.close())
}
}
26 changes: 13 additions & 13 deletions ta_lib/strategies/base/src/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ impl Strategy for BaseStrategy {
match self.trade_signals() {
(true, false, false, false) => TradeAction::GoLong(theo_price),
(false, true, false, false) => TradeAction::GoShort(theo_price),
(false, false, true, false) => TradeAction::ExitLong(data.close),
(false, false, false, true) => TradeAction::ExitShort(data.close),
(false, false, true, false) => TradeAction::ExitLong(theo_price),
(false, false, false, true) => TradeAction::ExitShort(theo_price),
_ => TradeAction::DoNothing,
}
}
Expand Down Expand Up @@ -187,7 +187,7 @@ mod tests {
}

fn generate(&self, data: &OHLCVSeries) -> (Series<bool>, Series<bool>) {
let len = data.close.len();
let len = data.len();
(Series::one(len).into(), Series::zero(len).into())
}
}
Expand All @@ -202,7 +202,7 @@ mod tests {
}

fn validate(&self, data: &OHLCVSeries) -> (Series<bool>, Series<bool>) {
let len = data.close.len();
let len = data.len();
(Series::one(len).into(), Series::zero(len).into())
}
}
Expand All @@ -217,7 +217,7 @@ mod tests {
}

fn assess(&self, data: &OHLCVSeries) -> (Series<bool>, Series<bool>) {
let len = data.close.len();
let len = data.len();
(Series::one(len).into(), Series::one(len).into())
}
}
Expand All @@ -232,12 +232,12 @@ mod tests {
}

fn generate(&self, data: &OHLCVSeries) -> (Series<bool>, Series<bool>) {
let len = data.close.len();
let len = data.len();
(Series::one(len).into(), Series::zero(len).into())
}

fn filter(&self, data: &OHLCVSeries) -> (Series<bool>, Series<bool>) {
let len = data.close.len();
let len = data.len();
(Series::one(len).into(), Series::zero(len).into())
}
}
Expand All @@ -253,7 +253,7 @@ mod tests {
}

fn find(&self, data: &OHLCVSeries) -> (Series<f32>, Series<f32>) {
let len = data.close.len();
let len = data.len();
(
Series::from(vec![5.0; len]) * self.multi,
Series::from(vec![6.0; len]) * self.multi,
Expand All @@ -269,7 +269,7 @@ mod tests {
}

fn evaluate(&self, data: &OHLCVSeries) -> (Series<bool>, Series<bool>) {
let len = data.close.len();
let len = data.len();
(Series::one(len).into(), Series::zero(len).into())
}
}
Expand Down Expand Up @@ -322,10 +322,10 @@ mod tests {

let series = OHLCVSeries::from_data(&strategy.data);

let hl2: Vec<f32> = series.hl2().into();
let hlc3: Vec<f32> = series.hlc3().into();
let hlcc4: Vec<f32> = series.hlcc4().into();
let ohlc4: Vec<f32> = series.ohlc4().into();
let hl2: Vec<f32> = series.hl2().clone().into();
let hlc3: Vec<f32> = series.hlc3().clone().into();
let hlcc4: Vec<f32> = series.hlcc4().clone().into();
let ohlc4: Vec<f32> = series.ohlc4().clone().into();

assert_eq!(hl2, vec![1.25]);
assert_eq!(hlc3, vec![1.333_333_4]);
Expand Down
4 changes: 2 additions & 2 deletions ta_lib/strategies/base/src/volatility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ pub trait Volatility {
impl Volatility for OHLCVSeries {
#[inline]
fn atr(&self, period: usize, smooth_type: Smooth) -> Series<f32> {
atr(&self.high, &self.low, &self.close, smooth_type, period)
atr(self.high(), self.low(), self.close(), smooth_type, period)
}

#[inline]
fn tr(&self) -> Series<f32> {
tr(&self.high, &self.low, &self.close)
tr(self.high(), self.low(), self.close())
}
}
4 changes: 2 additions & 2 deletions ta_lib/strategies/baseline/src/ma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl MaBaseLine {
period: period as usize,
signal: vec![
Box::new(MaSurpassSignal::new(ma, period)),
// Box::new(MaQuadrupleSignal::new(ma, period)),
Box::new(MaQuadrupleSignal::new(ma, period)),
],
}
}
Expand All @@ -40,7 +40,7 @@ impl BaseLine for MaBaseLine {
let ma = ma_indicator(&self.ma, data, self.period);
let prev_ma = ma.shift(1);

let dist = (&ma - &data.close).abs();
let dist = (&ma - data.close()).abs();
let atr = data.atr(DEFAULT_ATR_LOOKBACK, Smooth::SMMA) * DEFAULT_ATR_FACTOR;

(
Expand Down
2 changes: 1 addition & 1 deletion ta_lib/strategies/confirm/src/dpo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Confirm for DpoConfirm {
}

fn validate(&self, data: &OHLCVSeries) -> (Series<bool>, Series<bool>) {
let dpo = dpo(&data.close, self.smooth_type, self.period);
let dpo = dpo(data.close(), self.smooth_type, self.period);

(dpo.sgt(&ZERO_LINE), dpo.slt(&ZERO_LINE))
}
Expand Down
2 changes: 1 addition & 1 deletion ta_lib/strategies/confirm/src/dso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Confirm for DsoConfirm {

fn validate(&self, data: &OHLCVSeries) -> (Series<bool>, Series<bool>) {
let (k, d) = dso(
&data.close,
data.close(),
self.smooth_type,
self.smooth_period,
self.k_period,
Expand Down
2 changes: 1 addition & 1 deletion ta_lib/strategies/confirm/src/dumb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl Confirm for DumbConfirm {
}

fn validate(&self, data: &OHLCVSeries) -> (Series<bool>, Series<bool>) {
let len = data.close.len();
let len = data.len();

(Series::one(len).into(), Series::one(len).into())
}
Expand Down
6 changes: 3 additions & 3 deletions ta_lib/strategies/confirm/src/eom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ impl Confirm for EomConfirm {
fn validate(&self, data: &OHLCVSeries) -> (Series<bool>, Series<bool>) {
let eom = eom(
&data.hl2(),
&data.high,
&data.low,
&data.volume,
data.high(),
data.low(),
data.volume(),
self.smooth_type,
self.period,
self.divisor,
Expand Down
2 changes: 1 addition & 1 deletion ta_lib/strategies/confirm/src/roc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Confirm for RocConfirm {
}

fn validate(&self, data: &OHLCVSeries) -> (Series<bool>, Series<bool>) {
let roc = roc(&data.close, self.period);
let roc = roc(data.close(), self.period);

(roc.sgt(&ZERO_LINE), roc.slt(&ZERO_LINE))
}
Expand Down
2 changes: 1 addition & 1 deletion ta_lib/strategies/confirm/src/rsi_neutrality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Confirm for RsiNeutralityConfirm {
}

fn validate(&self, data: &OHLCVSeries) -> (Series<bool>, Series<bool>) {
let rsi = rsi(&data.close, self.smooth_type, self.period);
let rsi = rsi(data.close(), self.smooth_type, self.period);

let lower_barrier = RSI_LOWER_BARRIER + self.threshold;
let upper_barrier = RSI_UPPER_BARRIER - self.threshold;
Expand Down
3 changes: 2 additions & 1 deletion ta_lib/strategies/confirm/src/rsi_signalline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ impl Confirm for RsiSignalLineConfirm {
}

fn validate(&self, data: &OHLCVSeries) -> (Series<bool>, Series<bool>) {
let rsi = rsi(&data.close, self.smooth_type, self.rsi_period);
let rsi = rsi(&data.close(), self.smooth_type, self.rsi_period);
let signal = rsi.smooth(self.smooth_signal, self.smooth_period);

let upper_barrier = RSI_UPPER_BARRIER + self.threshold;
let lower_barrier = RSI_LOWER_BARRIER - self.threshold;

Expand Down
2 changes: 1 addition & 1 deletion ta_lib/strategies/confirm/src/stc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Confirm for StcConfirm {

fn validate(&self, data: &OHLCVSeries) -> (Series<bool>, Series<bool>) {
let stc = stc(
&data.close,
data.close(),
self.smooth_type,
self.fast_period,
self.slow_period,
Expand Down
4 changes: 2 additions & 2 deletions ta_lib/strategies/confirm/src/vi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ impl Confirm for ViConfirm {

fn validate(&self, data: &OHLCVSeries) -> (Series<bool>, Series<bool>) {
let (vip, vim) = vi(
&data.high,
&data.low,
data.high(),
data.low(),
&data.atr(self.atr_period, Smooth::SMMA),
self.period,
);
Expand Down
2 changes: 1 addition & 1 deletion ta_lib/strategies/exit/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl Exit for AstExit {

fn evaluate(&self, data: &OHLCVSeries) -> (Series<bool>, Series<bool>) {
let (direction, _) = ast(
&data.close,
data.close(),
&data.atr(self.atr_period, Smooth::SMMA),
self.factor,
);
Expand Down
2 changes: 1 addition & 1 deletion ta_lib/strategies/exit/src/dumb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ impl Exit for DumbExit {
}

fn evaluate(&self, data: &OHLCVSeries) -> (Series<bool>, Series<bool>) {
let len = data.close.len();
let len = data.len();

(Series::zero(len).into(), Series::zero(len).into())
}
Expand Down
4 changes: 2 additions & 2 deletions ta_lib/strategies/exit/src/highlow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ impl Exit for HighLowExit {

fn evaluate(&self, data: &OHLCVSeries) -> (Series<bool>, Series<bool>) {
(
data.close.sgt(&data.high.shift(self.period)),
data.close.slt(&data.low.shift(self.period)),
data.close().sgt(&data.high().shift(self.period)),
data.close().slt(&data.low().shift(self.period)),
)
}
}
2 changes: 1 addition & 1 deletion ta_lib/strategies/exit/src/ma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ impl Exit for MaExit {
fn evaluate(&self, data: &OHLCVSeries) -> (Series<bool>, Series<bool>) {
let ma = ma_indicator(&self.ma, data, self.period);

(data.close.cross_under(&ma), data.close.cross_over(&ma))
(data.close().cross_under(&ma), data.close().cross_over(&ma))
}
}
2 changes: 1 addition & 1 deletion ta_lib/strategies/exit/src/mfi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Exit for MfiExit {
}

fn evaluate(&self, data: &OHLCVSeries) -> (Series<bool>, Series<bool>) {
let mfi = mfi(&data.hlc3(), &data.volume, self.period);
let mfi = mfi(&data.hlc3(), data.volume(), self.period);
let upper_bound = MFI_OVERBOUGHT - self.threshold;
let lower_bound = MFI_OVERSOLD + self.threshold;

Expand Down
3 changes: 2 additions & 1 deletion ta_lib/strategies/exit/src/rsi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ impl Exit for RsiExit {
}

fn evaluate(&self, data: &OHLCVSeries) -> (Series<bool>, Series<bool>) {
let rsi = rsi(&data.close, self.smooth_type, self.period);
let rsi = rsi(data.close(), self.smooth_type, self.period);

let upper_bound = RSI_OVERBOUGHT - self.threshold;
let lower_bound = RSI_OVERSOLD + self.threshold;

Expand Down
2 changes: 1 addition & 1 deletion ta_lib/strategies/exit/src/trix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl Exit for TrixExit {
}

fn evaluate(&self, data: &OHLCVSeries) -> (Series<bool>, Series<bool>) {
let trix = trix(&data.close, self.smooth_type, self.period);
let trix = trix(data.close(), self.smooth_type, self.period);
let signal_line = trix.smooth(self.smooth_type, self.signal_period);

(
Expand Down
Loading

0 comments on commit 4bd53ba

Please sign in to comment.