1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//! Tick-imbalance bar builder (simplified López de Prado) — sample on cumulative signed order flow.
use crate::error::{Error, Result};
use crate::ohlcv::Candle;
use crate::traits::BarBuilder;
/// One completed imbalance bar.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ImbalanceBar {
/// Open of the first candle in the bar.
pub open: f64,
/// Highest high across the bar.
pub high: f64,
/// Lowest low across the bar.
pub low: f64,
/// Close of the candle that closed the bar.
pub close: f64,
/// Signed cumulative tick imbalance at the close (`Σ sign`).
pub imbalance: f64,
/// `+1` if buy-side imbalance closed the bar, `-1` if sell-side.
pub direction: i8,
}
/// Tick-imbalance bar builder — a **simplified** form of López de Prado's
/// imbalance bars.
///
/// Each candle is assigned a tick sign by the tick rule: `+1` if its close is above
/// the previous close, `-1` if below, and the previous sign is carried on an
/// unchanged close. The signed imbalance `θ = Σ sign` accumulates until its absolute
/// value reaches a fixed `threshold`, at which point a bar closes. Imbalance bars
/// therefore sample the market when order flow becomes *one-sided* — a burst of
/// persistent buying or selling — rather than on time, count, or volume. This makes
/// them sensitive to informed, directional trading.
///
/// **Simplification.** The full method estimates a *dynamic* threshold
/// `E[T] · |2P − 1|` from an EWMA of the expected bar length `E[T]` and the buy-tick
/// probability `P`, and can weight each sign by volume (volume-imbalance bars) or
/// traded value (dollar-imbalance bars). This builder uses a **fixed** threshold on
/// the unweighted tick imbalance. For the adaptive estimator and the volume/dollar
/// variants, see López de Prado (2018), ch. 2.
///
/// At most one bar closes per candle, so [`BarBuilder::update`] returns either an
/// empty vector or a single [`ImbalanceBar`].
///
/// # Example
///
/// ```
/// use wickra_core::{BarBuilder, Candle, ImbalanceBars};
///
/// let flat = |price: f64| Candle::new(price, price, price, price, 1.0, 0).unwrap();
/// let mut bars = ImbalanceBars::new(3.0).unwrap();
/// bars.update(flat(10.0)); // seed, no sign
/// bars.update(flat(11.0)); // +1
/// bars.update(flat(12.0)); // +2
/// let out = bars.update(flat(13.0)); // +3 -> close
/// assert_eq!(out.len(), 1);
/// assert_eq!(out[0].direction, 1);
/// ```
#[derive(Debug, Clone)]
pub struct ImbalanceBars {
threshold: f64,
count: usize,
open: f64,
high: f64,
low: f64,
close: f64,
prev_close: Option<f64>,
last_sign: i8,
theta: f64,
}
impl ImbalanceBars {
/// Construct an imbalance-bar builder with the given absolute imbalance threshold.
///
/// # Errors
///
/// Returns [`Error::InvalidPeriod`] if `threshold` is not finite and positive.
pub fn new(threshold: f64) -> Result<Self> {
if !threshold.is_finite() || threshold <= 0.0 {
return Err(Error::InvalidPeriod {
message: "threshold must be finite and positive",
});
}
Ok(Self {
threshold,
count: 0,
open: 0.0,
high: 0.0,
low: 0.0,
close: 0.0,
prev_close: None,
last_sign: 0,
theta: 0.0,
})
}
/// Configured absolute imbalance threshold.
pub const fn threshold(&self) -> f64 {
self.threshold
}
/// Signed imbalance accumulated into the in-progress bar.
pub const fn imbalance(&self) -> f64 {
self.theta
}
}
impl BarBuilder for ImbalanceBars {
type Bar = ImbalanceBar;
fn update(&mut self, candle: Candle) -> Vec<ImbalanceBar> {
if self.count == 0 {
self.open = candle.open;
self.high = candle.high;
self.low = candle.low;
} else {
self.high = self.high.max(candle.high);
self.low = self.low.min(candle.low);
}
self.close = candle.close;
self.count += 1;
if let Some(prev) = self.prev_close {
let sign = if candle.close > prev {
1
} else if candle.close < prev {
-1
} else {
self.last_sign
};
self.last_sign = sign;
self.theta += f64::from(sign);
}
self.prev_close = Some(candle.close);
if self.theta.abs() < self.threshold {
return Vec::new();
}
let direction = if self.theta > 0.0 { 1 } else { -1 };
let bar = ImbalanceBar {
open: self.open,
high: self.high,
low: self.low,
close: self.close,
imbalance: self.theta,
direction,
};
self.count = 0;
self.theta = 0.0;
vec![bar]
}
fn reset(&mut self) {
self.count = 0;
self.prev_close = None;
self.last_sign = 0;
self.theta = 0.0;
}
fn name(&self) -> &'static str {
"ImbalanceBars"
}
}
#[cfg(test)]
mod tests {
use super::*;
use approx::assert_relative_eq;
fn flat(price: f64) -> Candle {
Candle::new(price, price, price, price, 1.0, 0).unwrap()
}
#[test]
fn rejects_invalid_threshold() {
assert!(matches!(
ImbalanceBars::new(0.0),
Err(Error::InvalidPeriod { .. })
));
assert!(matches!(
ImbalanceBars::new(-3.0),
Err(Error::InvalidPeriod { .. })
));
assert!(matches!(
ImbalanceBars::new(f64::NAN),
Err(Error::InvalidPeriod { .. })
));
}
#[test]
fn accessors_and_metadata() {
let bars = ImbalanceBars::new(10.0).unwrap();
assert_relative_eq!(bars.threshold(), 10.0, epsilon = 1e-12);
assert_relative_eq!(bars.imbalance(), 0.0, epsilon = 1e-12);
assert_eq!(bars.name(), "ImbalanceBars");
}
#[test]
fn buy_imbalance_closes_up_bar() {
let mut bars = ImbalanceBars::new(3.0).unwrap();
bars.update(flat(10.0)); // seed
bars.update(flat(11.0)); // +1
bars.update(flat(12.0)); // +2
let out = bars.update(flat(13.0)); // +3
assert_eq!(out.len(), 1);
assert_eq!(out[0].direction, 1);
assert_relative_eq!(out[0].imbalance, 3.0, epsilon = 1e-12);
}
#[test]
fn sell_imbalance_closes_down_bar() {
let mut bars = ImbalanceBars::new(3.0).unwrap();
bars.update(flat(10.0));
bars.update(flat(9.0)); // -1
bars.update(flat(8.0)); // -2
let out = bars.update(flat(7.0)); // -3
assert_eq!(out.len(), 1);
assert_eq!(out[0].direction, -1);
}
#[test]
fn flat_tick_carries_previous_sign() {
let mut bars = ImbalanceBars::new(3.0).unwrap();
bars.update(flat(10.0));
bars.update(flat(11.0)); // +1
bars.update(flat(11.0)); // flat -> carries +1 -> +2
assert_relative_eq!(bars.imbalance(), 2.0, epsilon = 1e-12);
}
#[test]
fn oscillation_does_not_reach_threshold() {
let mut bars = ImbalanceBars::new(3.0).unwrap();
bars.update(flat(10.0));
bars.update(flat(11.0)); // +1
bars.update(flat(10.0)); // -1 -> theta 0
assert!(bars.update(flat(11.0)).is_empty()); // +1
assert_relative_eq!(bars.imbalance(), 1.0, epsilon = 1e-12);
}
#[test]
fn reset_clears_state() {
let mut bars = ImbalanceBars::new(3.0).unwrap();
bars.update(flat(10.0));
bars.update(flat(11.0));
bars.reset();
assert_relative_eq!(bars.imbalance(), 0.0, epsilon = 1e-12);
// After reset the next candle re-seeds (no previous close).
assert!(bars.update(flat(50.0)).is_empty());
}
#[test]
fn batch_concatenates_completed_bars() {
let mut bars = ImbalanceBars::new(2.0).unwrap();
let candles = [
flat(10.0),
flat(11.0), // +1
flat(12.0), // +2 -> close
flat(13.0), // +1
flat(14.0), // +2 -> close
];
let out = bars.batch(&candles);
assert_eq!(out.len(), 2);
assert!(out.iter().all(|b| b.direction == 1));
}
}