Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
m5l14i11 committed Jul 13, 2023
1 parent 5b50797 commit 9171bd9
Show file tree
Hide file tree
Showing 15 changed files with 197 additions and 57 deletions.
143 changes: 132 additions & 11 deletions ta_lib/core/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ use crate::series::Series;
use std::ops::{Add, Div, Mul, Neg, Sub};

impl Series<f64> {
pub fn add(&self, rhs: &Series<f64>) -> Series<f64> {
pub fn add_series(&self, rhs: &Series<f64>) -> Series<f64> {
self.clone().zip_with(rhs, |a, b| match (a, b) {
(Some(a_val), Some(b_val)) => Some(a_val + b_val),
_ => None,
})
}

pub fn mul(&self, rhs: &Series<f64>) -> Series<f64> {
pub fn mul_series(&self, rhs: &Series<f64>) -> Series<f64> {
self.clone().zip_with(rhs, |a, b| match (a, b) {
(Some(a_val), Some(b_val)) => Some(a_val * b_val),
_ => None,
})
}

pub fn div(&self, rhs: &Series<f64>) -> Series<f64> {
pub fn div_series(&self, rhs: &Series<f64>) -> Series<f64> {
self.clone().zip_with(rhs, |a, b| match (a, b) {
(Some(a_val), Some(b_val)) => {
if b_val == 0.0 {
Expand All @@ -35,7 +35,7 @@ impl Series<f64> {
})
}

pub fn sub(&self, rhs: &Series<f64>) -> Series<f64> {
pub fn sub_series(&self, rhs: &Series<f64>) -> Series<f64> {
self.clone().zip_with(rhs, |a, b| match (a, b) {
(Some(a_val), Some(b_val)) => Some(a_val - b_val),
_ => None,
Expand Down Expand Up @@ -78,7 +78,7 @@ impl Series<f64> {
}

impl Series<bool> {
pub fn mul(&self, rhs: &Series<f64>) -> Series<f64> {
pub fn mul_series(&self, rhs: &Series<f64>) -> Series<f64> {
self.clone().zip_with(rhs, |b, val| match (b, val) {
(Some(b_val), Some(val_val)) => {
if b_val {
Expand All @@ -92,35 +92,99 @@ impl Series<bool> {
}
}

impl Add<Series<f64>> for &Series<f64> {
type Output = Series<f64>;

fn add(self, rhs: Series<f64>) -> Series<f64> {
self.add_series(&rhs)
}
}

impl Add<&Series<f64>> for Series<f64> {
type Output = Series<f64>;

fn add(self, rhs: &Series<f64>) -> Series<f64> {
self.add_series(rhs)
}
}

impl Add<&Series<f64>> for &Series<f64> {
type Output = Series<f64>;

fn add(self, rhs: &Series<f64>) -> Series<f64> {
self.add(rhs)
self.add_series(rhs)
}
}

impl Mul<Series<f64>> for &Series<f64> {
type Output = Series<f64>;

fn mul(self, rhs: Series<f64>) -> Series<f64> {
self.mul_series(&rhs)
}
}

impl Mul<&Series<f64>> for Series<f64> {
type Output = Series<f64>;

fn mul(self, rhs: &Series<f64>) -> Series<f64> {
self.mul_series(rhs)
}
}

impl Mul<&Series<f64>> for &Series<f64> {
type Output = Series<f64>;

fn mul(self, rhs: &Series<f64>) -> Series<f64> {
self.mul(rhs)
self.mul_series(rhs)
}
}

impl Div<&Series<f64>> for &Series<f64> {
type Output = Series<f64>;

fn div(self, rhs: &Series<f64>) -> Series<f64> {
self.div(rhs)
self.div_series(rhs)
}
}

impl Div<&Series<f64>> for Series<f64> {
type Output = Series<f64>;

fn div(self, rhs: &Series<f64>) -> Series<f64> {
self.div_series(rhs)
}
}

impl Div<Series<f64>> for &Series<f64> {
type Output = Series<f64>;

fn div(self, rhs: Series<f64>) -> Series<f64> {
self.div_series(&rhs)
}
}

impl Sub<&Series<f64>> for Series<f64> {
type Output = Series<f64>;

fn sub(self, rhs: &Series<f64>) -> Series<f64> {
self.sub_series(rhs)
}
}

impl Sub<Series<f64>> for &Series<f64> {
type Output = Series<f64>;

fn sub(self, rhs: Series<f64>) -> Series<f64> {
self.sub_series(&rhs)
}
}

impl Sub<&Series<f64>> for &Series<f64> {
type Output = Series<f64>;

fn sub(self, rhs: &Series<f64>) -> Series<f64> {
self.sub(rhs)
self.sub_series(rhs)
}
}

Expand All @@ -140,6 +204,14 @@ impl Mul<f64> for &Series<f64> {
}
}

impl Mul<f64> for Series<f64> {
type Output = Series<f64>;

fn mul(self, scalar: f64) -> Series<f64> {
self.mul_scalar(scalar)
}
}

impl Div<f64> for &Series<f64> {
type Output = Series<f64>;

Expand All @@ -148,6 +220,14 @@ impl Div<f64> for &Series<f64> {
}
}

impl Div<f64> for Series<f64> {
type Output = Series<f64>;

fn div(self, scalar: f64) -> Series<f64> {
self.div_scalar(scalar)
}
}

impl Sub<f64> for &Series<f64> {
type Output = Series<f64>;

Expand All @@ -164,6 +244,14 @@ impl Add<&Series<f64>> for f64 {
}
}

impl Add<Series<f64>> for f64 {
type Output = Series<f64>;

fn add(self, rhs: Series<f64>) -> Series<f64> {
rhs.add_scalar(self)
}
}

impl Mul<&Series<f64>> for f64 {
type Output = Series<f64>;

Expand All @@ -172,12 +260,29 @@ impl Mul<&Series<f64>> for f64 {
}
}

impl Mul<Series<f64>> for f64 {
type Output = Series<f64>;

fn mul(self, rhs: Series<f64>) -> Series<f64> {
rhs.mul_scalar(self)
}
}

impl Div<&Series<f64>> for f64 {
type Output = Series<f64>;

fn div(self, rhs: &Series<f64>) -> Series<f64> {
let scalars = vec![self; rhs.len()];
Series::from(&scalars).div(&rhs)
Series::from(&scalars).div_series(&rhs)
}
}

impl Div<Series<f64>> for f64 {
type Output = Series<f64>;

fn div(self, rhs: Series<f64>) -> Series<f64> {
let scalars = vec![self; rhs.len()];
Series::from(&scalars).div_series(&rhs)
}
}

Expand All @@ -189,11 +294,27 @@ impl Sub<&Series<f64>> for f64 {
}
}

impl Sub<Series<f64>> for f64 {
type Output = Series<f64>;

fn sub(self, rhs: Series<f64>) -> Series<f64> {
rhs.neg().sub_scalar(-self)
}
}

impl Mul<&Series<f64>> for &Series<bool> {
type Output = Series<f64>;

fn mul(self, rhs: &Series<f64>) -> Series<f64> {
self.mul(rhs)
self.mul_series(rhs)
}
}

impl Mul<&Series<f64>> for Series<bool> {
type Output = Series<f64>;

fn mul(self, rhs: &Series<f64>) -> Series<f64> {
self.mul_series(rhs)
}
}

Expand Down
6 changes: 3 additions & 3 deletions ta_lib/momentum/src/aosc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use core::series::Series;
pub fn aosc(hl2: &[f64], short_period: usize, long_period: usize) -> Series<f64> {
let hl2 = Series::from(hl2);

let ao_short = &hl2.mean(short_period);
let ao_long = &hl2.mean(long_period);
let ao_short = hl2.mean(short_period);
let ao_long = hl2.mean(long_period);

let aosc = ao_short - ao_long;
let aosc = ao_short - &ao_long;

aosc
}
Expand Down
6 changes: 3 additions & 3 deletions ta_lib/momentum/src/macd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ pub fn macd(
slow_period: usize,
signal_period: usize,
) -> (Series<f64>, Series<f64>, Series<f64>) {
let ema_fast = &ema(source, fast_period);
let ema_slow = &ema(source, slow_period);
let ema_fast = ema(source, fast_period);
let ema_slow = ema(source, slow_period);

let macd_line = ema_fast - ema_slow;
let macd_line = ema_fast - &ema_slow;
let macd_line_vec: Vec<f64> = macd_line.clone().into();

let signal_line = ema(&macd_line_vec, signal_period);
Expand Down
4 changes: 2 additions & 2 deletions ta_lib/momentum/src/rsi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ pub fn rsi(source: &[f64], period: usize) -> Series<f64> {
let up = smma(&gains, period);
let down = smma(&losses, period);

let rs = &up / &down;
let rs = up / &down;

let rsi = 100.0 - &(100.0 / &(1.0 + &rs));
let rsi = 100.0 - 100.0 / (1.0 + rs);

rsi.nz(Some(100.0))
}
Expand Down
16 changes: 10 additions & 6 deletions ta_lib/price/src/average.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use core::series::Series;

pub fn average_price(open: &[f64], high: &[f64], low: &[f64], close: &[f64]) -> Vec<f64> {
high.iter()
.zip(low)
.zip(open)
.zip(close)
.map(|(((&h, &l), &o), &c)| (h + l + o + c) / 4.0)
.collect()
let open = Series::from(open);
let high = Series::from(high);
let low = Series::from(low);
let close = Series::from(close);

let average_price = (open + &high + &low + &close) / 4.0;

average_price.into()
}

#[cfg(test)]
Expand Down
9 changes: 8 additions & 1 deletion ta_lib/price/src/median.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
use core::series::Series;

pub fn median_price(high: &[f64], low: &[f64]) -> Vec<f64> {
high.iter().zip(low).map(|(&h, &l)| (h + l) / 2.0).collect()
let high = Series::from(high);
let low = Series::from(low);

let median_price = (high + &low) / 2.0;

median_price.into()
}

#[cfg(test)]
Expand Down
14 changes: 9 additions & 5 deletions ta_lib/price/src/typical.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use core::series::Series;

pub fn typical_price(high: &[f64], low: &[f64], close: &[f64]) -> Vec<f64> {
high.iter()
.zip(low)
.zip(close)
.map(|((&h, &l), &c)| (h + l + c) / 3.0)
.collect()
let high = Series::from(high);
let low = Series::from(low);
let close = Series::from(close);

let typical_price = (high + &low + &close) / 3.0;

typical_price.into()
}

#[cfg(test)]
Expand Down
14 changes: 9 additions & 5 deletions ta_lib/price/src/wcl.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use core::series::Series;

pub fn wcl(high: &[f64], low: &[f64], close: &[f64]) -> Vec<f64> {
high.iter()
.zip(low)
.zip(close)
.map(|((&h, &l), &c)| (h + l + (c * 2.0)) / 4.0)
.collect()
let high = Series::from(high);
let low = Series::from(low);
let close = Series::from(close);

let wcl = (high + &low + &(close * 2.0)) / 4.0;

wcl.into()
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion ta_lib/trend/src/zlema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn zlema(source: &[f64], period: usize) -> Series<f64> {

let diff = &ema_first - &ema_second;

let zlema = &ema_first + &diff;
let zlema = ema_first + &diff;

zlema
}
Expand Down
8 changes: 4 additions & 4 deletions ta_lib/utils/src/stoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use core::series::Series;
pub fn stoch(high: &[f64], low: &[f64], close: &[f64], period: usize) -> Series<f64> {
let high = Series::from(high);
let low = Series::from(low);
let close = Series::from(close);

let hh = &high.highest(period);
let ll = &low.lowest(period);
let close = &Series::from(close);
let hh = high.highest(period);
let ll = low.lowest(period);

let stoch = &(100.0 * &(close - ll)) / &(hh - ll);
let stoch = 100.0 * (close - &ll) / &(hh - &ll);

stoch
}
Expand Down
Loading

0 comments on commit 9171bd9

Please sign in to comment.