Skip to content

Commit

Permalink
Add Entropy CandleComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
MathisWellmann committed May 19, 2023
1 parent 05cb4d5 commit 3599096
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "trade_aggregation"
version = "9.0.0"
version = "9.1.0"
authors = ["MathisWellmann <wellmannmathis@gmail.com>"]
edition = "2021"
license-file = "LICENSE"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ WeightedPrice | The volume weighted price
StdDevPrices | Keeps track of the standard deviation of prices
StdDevSizes | Keeps track of the standard deviaton of sizes
TimeVelocity | Essentially how fast the candle was created time wise
Entropy | Binary shannon entropy using the trade side as inputs

And again, if these don't satisfy your needs, just bring your own by implementing the
[CandleComponent](src/candle_components/candle_component_trait.rs) trait and you can plug them into your own candle struct.
Expand Down
37 changes: 37 additions & 0 deletions src/candle_components/entropy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use crate::{CandleComponent, CandleComponentUpdate, TakerTrade};

/// A `CandleComponent` that computes the binary entropy of whether a trade is a buy or a sell.
#[derive(Default, Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Entropy {
buys: usize,
total_observed_trades: usize,
}

impl CandleComponent<f64> for Entropy {
fn value(&self) -> f64 {
let pt = self.buys as f64 / self.total_observed_trades as f64;
let pn = 1_f64 - pt;

let mut h = pt * pt.log2() + pn * pn.log2();
if h.is_nan() {
h = 0.0;
}

-h
}

fn reset(&mut self) {
self.buys = 0;
self.total_observed_trades = 0;
}
}

impl<T: TakerTrade> CandleComponentUpdate<T> for Entropy {
fn update(&mut self, trade: &T) {
if trade.size() > 0.0 {
self.buys += 1
}
self.total_observed_trades += 1;
}
}
2 changes: 2 additions & 0 deletions src/candle_components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod candle_component_trait;
mod close;
mod directional_trade_ratio;
mod directional_volume_ratio;
mod entropy;
mod high;
mod low;
mod median_price;
Expand All @@ -25,6 +26,7 @@ pub use candle_component_trait::{CandleComponent, CandleComponentUpdate};
pub use close::Close;
pub use directional_trade_ratio::DirectionalTradeRatio;
pub use directional_volume_ratio::DirectionalVolumeRatio;
pub use entropy::Entropy;
pub use high::High;
pub use low::Low;
pub use median_price::MedianPrice;
Expand Down
6 changes: 2 additions & 4 deletions src/candle_components/open_datetime.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use crate::CandleComponent;
use crate::CandleComponentUpdate;
use crate::TakerTrade;
use crate::TimestampResolution;
use chrono::{DateTime, TimeZone, Utc};

use crate::{CandleComponent, CandleComponentUpdate, TakerTrade, TimestampResolution};

/// This 'CandleComponent' keeps track of the opening [DateTime<Utc>] of a Candle.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand Down

0 comments on commit 3599096

Please # to comment.