Skip to content

Commit

Permalink
prices
Browse files Browse the repository at this point in the history
  • Loading branch information
m5l14i11 committed Aug 3, 2023
1 parent 029de10 commit ad7c5ac
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
3 changes: 3 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/strategy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ version = "0.1.0"
edition = "2021"

[dependencies]
price = { path = "../price" }
47 changes: 46 additions & 1 deletion ta_lib/strategy/src/base.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use price::{average::average_price, median::median_price, typical::typical_price, wcl::wcl};
use std::collections::{HashMap, VecDeque};

pub struct OHLCV {
Expand Down Expand Up @@ -62,6 +63,10 @@ pub trait StrategySeries {
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 {
Expand All @@ -84,6 +89,38 @@ impl StrategySeries for BaseStrategy {
fn volume(&self) -> Vec<f64> {
self.data.iter().map(|ohlcv| ohlcv.volume).collect()
}

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

median_price(&high, &low)
}

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

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

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

wcl(&high, &low, &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)
}
}

#[cfg(test)]
Expand Down Expand Up @@ -128,7 +165,7 @@ mod tests {
}

#[test]
fn test_ohlcv_data() {
fn test_strategy_data() {
let mut strategy = BaseStrategy::new(3);
let ohlcvs = vec![
OHLCV {
Expand Down Expand Up @@ -170,5 +207,13 @@ mod tests {
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]);

assert_eq!(strategy.hl2(), vec![2.25, 3.25, 4.25]);
assert_eq!(
strategy.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]);
}
}

0 comments on commit ad7c5ac

Please sign in to comment.