Skip to content

Commit d3f7fb6

Browse files
Add VolumeBuys and VolumeSells candle component
1 parent edcf5f7 commit d3f7fb6

File tree

6 files changed

+65
-3
lines changed

6 files changed

+65
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "trade_aggregation"
3-
version = "12.0.1"
3+
version = "12.0.2"
44
authors = ["MathisWellmann <[email protected]>"]
55
edition = "2021"
66
license-file = "LICENSE"

src/candle_components/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ mod std_dev_sizes;
2121
mod time_velocity;
2222
mod trades;
2323
mod volume;
24+
mod volume_buys;
25+
mod volume_sells;
2426
mod weighted_price;
2527

2628
pub use average_price::AveragePrice;
@@ -43,6 +45,8 @@ pub use std_dev_sizes::StdDevSizes;
4345
pub use time_velocity::TimeVelocity;
4446
pub use trades::Trades;
4547
pub use volume::Volume;
48+
pub use volume_buys::VolumeBuys;
49+
pub use volume_sells::VolumeSells;
4650
pub use weighted_price::WeightedPrice;
4751

4852
#[cfg(test)]

src/candle_components/volume.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{CandleComponent, CandleComponentUpdate, TakerTrade};
22

3-
/// This 'CandleComponent' keeps track of the cumulative volume
3+
/// This 'CandleComponent' keeps track of the cumulative volume of trades.
44
#[derive(Debug, Default, Clone)]
55
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
66
pub struct Volume {

src/candle_components/volume_buys.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use crate::{CandleComponent, CandleComponentUpdate, TakerTrade};
2+
3+
/// This 'CandleComponent' keeps track of the cumulative buy volume of trades.
4+
#[derive(Debug, Default, Clone)]
5+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6+
pub struct VolumeBuys {
7+
buy_volume: f64,
8+
}
9+
10+
impl CandleComponent<f64> for VolumeBuys {
11+
#[inline(always)]
12+
fn value(&self) -> f64 {
13+
self.buy_volume
14+
}
15+
16+
#[inline(always)]
17+
fn reset(&mut self) {
18+
self.buy_volume = 0.0;
19+
}
20+
}
21+
22+
impl<T: TakerTrade> CandleComponentUpdate<T> for VolumeBuys {
23+
#[inline(always)]
24+
fn update(&mut self, trade: &T) {
25+
if trade.size().is_sign_positive() {
26+
self.buy_volume += trade.size().abs()
27+
}
28+
}
29+
}

src/candle_components/volume_sells.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use crate::{CandleComponent, CandleComponentUpdate, TakerTrade};
2+
3+
/// This 'CandleComponent' keeps track of the cumulative volume of trades.
4+
#[derive(Debug, Default, Clone)]
5+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6+
pub struct VolumeSells {
7+
sell_volume: f64,
8+
}
9+
10+
impl CandleComponent<f64> for VolumeSells {
11+
#[inline(always)]
12+
fn value(&self) -> f64 {
13+
self.sell_volume
14+
}
15+
16+
#[inline(always)]
17+
fn reset(&mut self) {
18+
self.sell_volume = 0.0;
19+
}
20+
}
21+
22+
impl<T: TakerTrade> CandleComponentUpdate<T> for VolumeSells {
23+
#[inline(always)]
24+
fn update(&mut self, trade: &T) {
25+
if trade.size().is_sign_negative() {
26+
self.sell_volume += trade.size().abs()
27+
}
28+
}
29+
}

src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,6 @@ pub trait TakerTrade {
6262

6363
/// Number of shares or contracts in this trade.
6464
/// A negative value indicates
65-
/// that the trade was executed on the bid (market sell order).
65+
/// that the trade was a sell order, taking liquidity from the bid.
6666
fn size(&self) -> f64;
6767
}

0 commit comments

Comments
 (0)