Skip to content

Commit

Permalink
Merge pull request #2 from Ohkthx/develop
Browse files Browse the repository at this point in the history
Version 0.3.0
Fixes #1
  • Loading branch information
Ohkthx committed Oct 31, 2023
2 parents 74af697 + aab1485 commit 632d206
Show file tree
Hide file tree
Showing 40 changed files with 1,102 additions and 687 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/target
/Cargo.lock
*target/
*Cargo.lock
commit_msg
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "tatk"
license = "MIT"
version = "0.2.4"
version = "0.3.0"
edition = "2021"
description = "Technical Analysis Toolkit"
readme = "README.md"
Expand Down Expand Up @@ -74,3 +74,6 @@ required-features = ["test-data"]
[[example]]
name = "traits"
path = "examples/user_traits.rs"

[dependencies]
tatk_derive = { path = "./tatk_derive" }
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ tatk = { git = "https://github.com/ohkthx/tatk-rs" }
- Variance (Var(X))
- Standard Deviation (SD/STDEV)
- Cross (Cross), checks two lines for Golden or Death cross.
- **Macros, Traits, and Derives**
- [macros.rs](https://github.com/Ohkthx/tatk-rs/tree/main/src/macros.rs)
- [traits.rs](https://github.com/Ohkthx/tatk-rs/tree/main/src/traits.rs)
- [tatk_derive/lib.rs](https://github.com/Ohkthx/tatk-rs/tree/main/tatk_derive/src/lib.rs)

## Documentation

Expand All @@ -59,7 +63,6 @@ The following traits are either used by the crate on indicators or to be defined
- **Indicator**
- Stats - Basic statistics for the indicator such as: sum, mean, variance, and standard deviation.
- Period - Period of window of the data for the indicator.
- Value - Current value held by the indicator.
- Next - Add a new data point to the indicator to recalculate value.
- **User Defined**
- AsValue - Alternative value that can be passed to an Indicators `Next`.
Expand All @@ -69,9 +72,9 @@ The following traits are either used by the crate on indicators or to be defined
- High - Highest value for the data type.
- Volume - Total volume for the data type.
- **Others**
- HL2 - Average of the Highest and Lowest values, requires `High` and `Low` to be defined.
- HLC3 - Average of the Highest, Lowest, and Close values, requires `High`, `Low`, and `Close` to be defined.
- OHLC4 - Average of the Open, Highest, Lowest, and Close values, requires `Open`, `High`, `Low`, and `Close` to be defined.
- Hl2 - Average of the Highest and Lowest values, requires `High` and `Low` to be defined.
- Hlc3 - Average of the Highest, Lowest, and Close values, requires `High`, `Low`, and `Close` to be defined.
- Ohlc4 - Average of the Open, Highest, Lowest, and Close values, requires `Open`, `High`, `Low`, and `Close` to be defined.

## Examples

Expand Down
17 changes: 10 additions & 7 deletions examples/atr.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Demonstrates how to initialize and use an ATR.
use tatk::indicators::ATR;
//! Demonstrates how to initialize and use an Average True Range.
use tatk::atr;
use tatk::test_data::TestData;
use tatk::traits::{Next, Value};
use tatk::traits::Next;

fn main() {
let period: usize = 10;
Expand All @@ -10,15 +10,18 @@ fn main() {
println!("Data (total): {:?}", candles.len());
println!("Period: {}", period);

// Create the ATR.
let mut atr = match ATR::new(period, &candles[..candles.len() - 1]) {
// Create the Average True Range.
let mut indicator = match atr!(period, &candles[..candles.len() - 1]) {
Ok(value) => value,
Err(error) => panic!("{}", error),
};

// Extract last candle.
let last_candle = candles[candles.len() - 1];

println!("\nATR: {}", atr.value());
println!("Adding last candle. New ATR: {}", atr.next(last_candle));
println!("\nAverage True Range: {}", indicator.value());
println!(
"Adding last candle. New ATR: {}",
indicator.next(last_candle)
);
}
23 changes: 12 additions & 11 deletions examples/bbands.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! Demonstrates how to initialize and use Bollinger Bands (BB).
use tatk::indicators::{BBands, EMA};
use tatk::ema;
use tatk::indicators::BollingerBands;
use tatk::test_data::TestData;
use tatk::traits::{Next, Value};
use tatk::traits::Next;

fn main() {
let period: usize = 10;
Expand All @@ -10,13 +11,13 @@ fn main() {
println!("Data (total): {:?}", data.len());
println!("Period: {}", period);

// Use EMA as the line instead of the default EMA.
let ema = match EMA::new(period, &data[..data.len() - 1]) {
// Use EMA as the line instead of the default SMA.
let ema_indicator = match ema!(period, &data[..data.len() - 1]) {
Ok(value) => value,
Err(error) => panic!("{}", error),
};

let mut bbands = match BBands::with_line(ema, 2.0) {
let mut indicator = match BollingerBands::with_line(ema_indicator, 2.0) {
Ok(value) => value,
Err(error) => panic!("{}", error),
};
Expand All @@ -25,15 +26,15 @@ fn main() {
let last_data = data[data.len() - 1];

println!(
"\nBollinger Band (BBands): {}, lower: {}, upper: {}",
bbands.value(),
bbands.lower(),
bbands.upper()
"\nBollinger Band (BB): {}, lower: {}, upper: {}",
indicator.value(),
indicator.lower(),
indicator.upper()
);

let next = bbands.next(last_data);
let next = indicator.next(last_data);
println!(
"Adding {}. New BBands: {}, lower: {}, upper: {}",
"Adding {}. New BBands value: {}, lower: {}, upper: {}",
last_data, next.1, next.0, next.2
);
}
18 changes: 11 additions & 7 deletions examples/dema.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Demonstrates how to initialize and use a DEMA.
use tatk::indicators::DEMA;
//! Demonstrates how to initialize and use a Double Exponential Moving Average.
use tatk::dema;
use tatk::test_data::TestData;
use tatk::traits::{Next, Value};
use tatk::traits::Next;

fn main() {
let period: usize = 10;
Expand All @@ -10,15 +10,19 @@ fn main() {
println!("Data (total): {:?}", data.len());
println!("Period: {}", period);

// Create the DEMA.
let mut dema = match DEMA::new(period, &data[..data.len() - 1]) {
// Create the Double Exponential Moving Average.
let mut indicator = match dema!(period, &data[..data.len() - 1]) {
Ok(value) => value,
Err(error) => panic!("{}", error),
};

// Extract last data point.
let last_data = data[data.len() - 1];

println!("\nDEMA: {}", dema.value());
println!("Adding {}. New DEMA: {}", last_data, dema.next(last_data));
println!("\nDouble Exponential Moving Average: {}", indicator.value());
println!(
"Adding {}. New DEMA: {}",
last_data,
indicator.next(last_data)
);
}
18 changes: 11 additions & 7 deletions examples/ema.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Demonstrates how to initialize and use a EMA.
use tatk::indicators::EMA;
//! Demonstrates how to initialize and use a Exponential Moving Average.
use tatk::ema;
use tatk::test_data::TestData;
use tatk::traits::{Next, Value};
use tatk::traits::Next;

fn main() {
let period: usize = 10;
Expand All @@ -10,15 +10,19 @@ fn main() {
println!("Data (total): {:?}", data.len());
println!("Period: {}", period);

// Create the EMA.
let mut ema = match EMA::new(period, &data[..data.len() - 1]) {
// Create the Exponential Moving Average.
let mut indicator = match ema!(period, &data[..data.len() - 1]) {
Ok(value) => value,
Err(error) => panic!("{}", error),
};

// Extract last data point.
let last_data = data[data.len() - 1];

println!("\nEMA: {}", ema.value());
println!("Adding {}. New EMA: {}", last_data, ema.next(last_data));
println!("\nExponential Moving Average: {}", indicator.value());
println!(
"Adding {}. New EMA: {}",
last_data,
indicator.next(last_data)
);
}
18 changes: 9 additions & 9 deletions examples/linereg.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Demonstrates how to initialize and use a LineReg.
use tatk::indicators::LineReg;
//! Demonstrates how to initialize and use a Linear Regression.
use tatk::lr;
use tatk::test_data::TestData;
use tatk::traits::{Next, Value};
use tatk::traits::Next;

fn main() {
let period: usize = 10;
Expand All @@ -10,22 +10,22 @@ fn main() {
println!("Data (total): {:?}", data.len());
println!("Period: {}", period);

// Create the LineReg.
let mut linereg = match LineReg::new(period, &data[..data.len() - 1]) {
// Create the Linear Regression indicator.
let mut indicator = match lr!(period, &data[..data.len() - 1]) {
Ok(value) => value,
Err(error) => panic!("{}", error),
};

// Extract last data point.
let last_data = data[data.len() - 1];

println!("\nbest fit: {}", linereg.value());
println!("\nbest fit: {}", indicator.value());
println!(
"Adding {}. New best fit: {}, r_squared: {:.4}",
last_data,
linereg.next(last_data),
linereg.r_sq()
indicator.next(last_data),
indicator.r_sq()
);

println!("Forecasted next: {}", linereg.forecast(1));
println!("Forecasted next: {}", indicator.forecast(1));
}
22 changes: 15 additions & 7 deletions examples/macd.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Demonstrates how to initialize and use a MACD.
use tatk::indicators::MACD;
//! Demonstrates how to initialize and use a Moving Average Convergence and Divergence.
use tatk::macd;
use tatk::test_data::TestData;
use tatk::traits::{Next, Value};
use tatk::traits::Next;

fn main() {
let short: usize = 8;
Expand All @@ -13,15 +13,23 @@ fn main() {
println!("Periods:");
println!("short: {}, long: {}, signal: {}", short, long, signal);

// Create the MACD.
let mut macd = match MACD::new(short, long, signal, &data[..data.len() - 1]) {
// Create the Moving Average Convergence and Divergence.
let mut indicator = match macd!(short, long, signal, &data[..data.len() - 1]) {
Ok(value) => value,
Err(error) => panic!("{}", error),
};

// Extract last data point.
let last_data = data[data.len() - 1];

println!("\nMACD: {}, signal: {}", macd.value(), macd.signal_value());
println!("Adding {}. New MACD: {}", last_data, macd.next(last_data).0);
println!(
"\nMoving Average Convergence and Divergence: {}, signal: {}",
indicator.value(),
indicator.signal_value()
);
println!(
"Adding {}. New MACD: {}",
last_data,
indicator.next(last_data).0
);
}
18 changes: 11 additions & 7 deletions examples/md.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Demonstrates how to initialize and use a MD.
use tatk::indicators::MD;
//! Demonstrates how to initialize and use a McGinley Dynamic Indicator.
use tatk::mdi;
use tatk::test_data::TestData;
use tatk::traits::{Next, Value};
use tatk::traits::Next;

fn main() {
let period: usize = 10;
Expand All @@ -10,15 +10,19 @@ fn main() {
println!("Data (total): {:?}", data.len());
println!("Period: {}", period);

// Create the MD.
let mut md = match MD::new(period, &data[..data.len() - 1], 0.6) {
// Create the McGinley Dynamic Indicator.
let mut indicator = match mdi!(period, &data[..data.len() - 1], 0.6) {
Ok(value) => value,
Err(error) => panic!("{}", error),
};

// Extract last data point.
let last_data = data[data.len() - 1];

println!("\nMD: {}", md.value());
println!("Adding {}. New MD: {}", last_data, md.next(last_data));
println!("\nMcGinley Dynamic Indicator: {}", indicator.value());
println!(
"Adding {}. New MDI: {}",
last_data,
indicator.next(last_data)
);
}
17 changes: 10 additions & 7 deletions examples/obv.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Demonstrates how to initialize and use an OBV.
use tatk::indicators::OBV;
//! Demonstrates how to initialize and use an On-Balance Volume.
use tatk::obv;
use tatk::test_data::TestData;
use tatk::traits::{Next, Value};
use tatk::traits::Next;

fn main() {
let period: usize = 10;
Expand All @@ -10,15 +10,18 @@ fn main() {
println!("Data (total): {:?}", candles.len());
println!("Period: {}", period);

// Create the OBV.
let mut atr = match OBV::new(period, &candles[..candles.len() - 1]) {
// Create the On-Balance Volume.
let mut indicator = match obv!(period, &candles[..candles.len() - 1]) {
Ok(value) => value,
Err(error) => panic!("{}", error),
};

// Extract last candle.
let last_candle = candles[candles.len() - 1];

println!("\nOBV: {}", atr.value());
println!("Adding last candle. New OBV: {}", atr.next(last_candle));
println!("\nOn-Balance Volume: {}", indicator.value());
println!(
"Adding last candle. New OBV: {}",
indicator.next(last_candle)
);
}
17 changes: 10 additions & 7 deletions examples/roc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Demonstrates how to initialize and use a ROC.
use tatk::indicators::ROC;
//! Demonstrates how to initialize and use a Rate of Change.
use tatk::roc;
use tatk::test_data::TestData;
use tatk::traits::{Next, Value};
use tatk::traits::Next;

fn main() {
let period: usize = 10;
Expand All @@ -10,15 +10,18 @@ fn main() {
println!("Data (total): {:?}", data.len());
println!("Period: {}", period);

// Create the ROC.
let mut roc = match ROC::new(period, &data[..data.len() - 1]) {
// Create the Rate of Change.
let mut indicator = match roc!(period, &data[..data.len() - 1]) {
Ok(value) => value,
Err(error) => panic!("{}", error),
};

// Extract last candle.
let last_candle = data[data.len() - 1];

println!("\nROC: {}", roc.value());
println!("Adding last candle. New ROC: {}", roc.next(last_candle));
println!("\nRate of Change: {}", indicator.value());
println!(
"Adding last candle. New ROC: {}",
indicator.next(last_candle)
);
}
Loading

0 comments on commit 632d206

Please sign in to comment.