mylittleindicators 0.1.8

Multi-stream financial indicators library — 556 bar indicators + 21 event primitives across 35 categories. Consumes 27 stream kinds from digdigdig3 exchange connectors: OHLCV bars, ticks, orderbook (snapshot/delta/L3), funding/predicted funding/funding settlement, mark price, index price, open interest, liquidations, ticker, agg trades, long/short ratio, option greeks, volatility index, historical volatility, basis (derived), composite index, settlement events, block trades, insurance fund, risk limit, market warning, and three kline-family variants. Live-verified on 12 exchanges (89% pass-rate on a 150s BTC slice).
Documentation
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//! Order Flow Imbalance — buy/sell pressure from L2 orderbook deltas.
//!
//! Primary path: `update_orderbook(&OrderBook)` — computes real OFI by comparing
//! current bid/ask depth against the previous snapshot.
//!
//! Fallback path: `update_bar(o,h,l,c,v)` — SYNTHETIC ESTIMATE only. Uses
//! close > open heuristic to split volume into buy/sell. Accuracy is limited;
//! prefer the L2 path when orderbook snapshots are available.

use crate::bar_indicators::indicator_value::IndicatorValue;
use crate::bar_indicators::order_book_consumer::OrderBookConsumer;
use crate::core::types::OrderBook;
use crate::types::Bar;
use std::collections::HashMap;

/// One price level with accumulated buy/sell volume split.
#[derive(Debug, Clone)]
pub struct PriceLevel {
    pub price: f64,
    pub buy_volume: f64,
    pub sell_volume: f64,
    pub total_volume: f64,
    /// (buy - sell) / total, range [-1, 1].
    pub imbalance: f64,
    /// buy / sell ratio (inf when sell == 0 and buy > 0).
    pub imbalance_ratio: f64,
}

impl PriceLevel {
    pub fn new(price: f64) -> Self {
        Self {
            price,
            buy_volume: 0.0,
            sell_volume: 0.0,
            total_volume: 0.0,
            imbalance: 0.0,
            imbalance_ratio: 1.0,
        }
    }

    pub fn add_volume(&mut self, volume: f64, is_buy: bool) {
        if is_buy {
            self.buy_volume += volume;
        } else {
            self.sell_volume += volume;
        }
        self.total_volume += volume;
        self.update_metrics();
    }

    fn update_metrics(&mut self) {
        if self.total_volume > 0.0 {
            self.imbalance = (self.buy_volume - self.sell_volume) / self.total_volume;
        }
        self.imbalance_ratio = if self.sell_volume > 0.0 {
            self.buy_volume / self.sell_volume
        } else if self.buy_volume > 0.0 {
            f64::INFINITY
        } else {
            1.0
        };
    }
}

/// Order Flow Imbalance indicator.
#[derive(Clone)]
pub struct OrderFlowImbalance {
    period: usize,
    tick_size: f64,

    // OHLCV-path state
    volume_bars: Vec<Bar>,
    price_levels: HashMap<i64, PriceLevel>,

    // L2-path state: depth snapshots for OFI delta
    prev_bid_depth: f64,
    prev_ask_depth: f64,
    l2_depth_levels: usize,

    // Metrics (shared output)
    total_imbalance: f64,
    avg_imbalance: f64,
    max_imbalance: f64,
    min_imbalance: f64,
    dominant_side: i8,
    imbalance_strength: f64,
    flow_acceleration: f64,
    prev_imbalance: f64,

    max_buy_level: Option<PriceLevel>,
    max_sell_level: Option<PriceLevel>,
    strongest_imbalance_level: Option<PriceLevel>,

    /// Number of `update_orderbook` calls received on the L2 path.
    l2_updates: usize,
}

impl OrderFlowImbalance {
    pub fn new(period: usize, tick_size: f64) -> Self {
        Self {
            period,
            tick_size,
            volume_bars: Vec::with_capacity(period),
            price_levels: HashMap::new(),
            prev_bid_depth: 0.0,
            prev_ask_depth: 0.0,
            l2_depth_levels: 10,
            total_imbalance: 0.0,
            avg_imbalance: 0.0,
            max_imbalance: 0.0,
            min_imbalance: 0.0,
            dominant_side: 0,
            imbalance_strength: 0.0,
            flow_acceleration: 0.0,
            prev_imbalance: 0.0,
            max_buy_level: None,
            max_sell_level: None,
            strongest_imbalance_level: None,
            l2_updates: 0,
        }
    }

    // -------------------------------------------------------------------------
    // L2 path helpers
    // -------------------------------------------------------------------------

    /// Standard OFI formula:
    ///   Δbid = current_bid_depth - prev_bid_depth  (positive → buy pressure)
    ///   Δask = current_ask_depth - prev_ask_depth  (positive → sell pressure)
    ///   OFI  = Δbid - Δask, normalised to [-1, 1] by total depth change.
    fn apply_l2_ofi(&mut self, bid_depth: f64, ask_depth: f64) {
        let delta_bid = bid_depth - self.prev_bid_depth;
        let delta_ask = ask_depth - self.prev_ask_depth;
        let raw_ofi = delta_bid - delta_ask;
        let normaliser = (delta_bid.abs() + delta_ask.abs()).max(1e-12);
        let ofi = raw_ofi / normaliser;

        // Blend into rolling avg_imbalance
        let old = self.avg_imbalance;
        self.avg_imbalance = ofi;
        self.total_imbalance = ofi;
        self.flow_acceleration = self.avg_imbalance - old;
        self.prev_imbalance = old;

        self.dominant_side = if ofi > 0.1 {
            1
        } else if ofi < -0.1 {
            -1
        } else {
            0
        };
        self.imbalance_strength = ofi.abs();

        self.prev_bid_depth = bid_depth;
        self.prev_ask_depth = ask_depth;
    }

    // -------------------------------------------------------------------------
    // OHLCV path helpers (synthetic — kept for pipelines without L2)
    // -------------------------------------------------------------------------

    pub fn update_volume_bar(&mut self, volume_bar: &Bar) -> f64 {
        if self.volume_bars.len() >= self.period {
            self.volume_bars.remove(0);
        }
        self.volume_bars.push(*volume_bar);
        self.analyze_price_levels(volume_bar);
        self.recalculate_metrics();
        self.total_imbalance
    }

    fn analyze_price_levels(&mut self, volume_bar: &Bar) {
        if self.volume_bars.len() >= self.period {
            self.price_levels.clear();
            let bars_to_process = self.volume_bars.clone();
            for bar in &bars_to_process {
                self.process_bar_levels(bar);
            }
        } else {
            self.process_bar_levels(volume_bar);
        }
    }

    /// SYNTHETIC: price above mid → buy volume, below mid → sell volume.
    fn process_bar_levels(&mut self, volume_bar: &Bar) {
        let prices = [volume_bar.open, volume_bar.high, volume_bar.low, volume_bar.close];
        let volume_per_price = volume_bar.volume / 4.0;
        let mid = (volume_bar.open + volume_bar.close) / 2.0;

        for price in &prices {
            let price_key = self.price_to_key(*price);
            // SYNTHETIC ESTIMATE: price >= mid → buy side
            let is_buy = *price >= mid;
            let level = self.price_levels.entry(price_key).or_insert_with(|| PriceLevel::new(*price));
            level.add_volume(volume_per_price, is_buy);
        }
    }

    fn price_to_key(&self, price: f64) -> i64 {
        (price / self.tick_size).round() as i64
    }

    fn recalculate_metrics(&mut self) {
        if self.price_levels.is_empty() {
            return;
        }

        let mut total_imbalance = 0.0;
        let mut max_imbalance = f64::NEG_INFINITY;
        let mut min_imbalance = f64::INFINITY;
        let mut max_buy_volume = 0.0f64;
        let mut max_sell_volume = 0.0f64;
        let mut strongest_imbalance = 0.0f64;

        self.max_buy_level = None;
        self.max_sell_level = None;
        self.strongest_imbalance_level = None;

        for level in self.price_levels.values() {
            total_imbalance += level.imbalance;

            if level.imbalance > max_imbalance {
                max_imbalance = level.imbalance;
            }
            if level.imbalance < min_imbalance {
                min_imbalance = level.imbalance;
            }

            if level.buy_volume > max_buy_volume {
                max_buy_volume = level.buy_volume;
                self.max_buy_level = Some(level.clone());
            }
            if level.sell_volume > max_sell_volume {
                max_sell_volume = level.sell_volume;
                self.max_sell_level = Some(level.clone());
            }
            if level.imbalance.abs() > strongest_imbalance {
                strongest_imbalance = level.imbalance.abs();
                self.strongest_imbalance_level = Some(level.clone());
            }
        }

        self.total_imbalance = total_imbalance;
        self.avg_imbalance = total_imbalance / self.price_levels.len() as f64;
        self.max_imbalance = max_imbalance;
        self.min_imbalance = min_imbalance;

        self.dominant_side = if self.avg_imbalance > 0.1 {
            1
        } else if self.avg_imbalance < -0.1 {
            -1
        } else {
            0
        };
        self.imbalance_strength = self.avg_imbalance.abs();
        self.flow_acceleration = self.avg_imbalance - self.prev_imbalance;
        self.prev_imbalance = self.avg_imbalance;
    }

    // -------------------------------------------------------------------------
    // Public accessors
    // -------------------------------------------------------------------------

    pub fn total_imbalance(&self) -> f64 { self.total_imbalance }
    pub fn avg_imbalance(&self) -> f64 { self.avg_imbalance }
    pub fn dominant_side(&self) -> i8 { self.dominant_side }
    pub fn imbalance_strength(&self) -> f64 { self.imbalance_strength }
    pub fn flow_acceleration(&self) -> f64 { self.flow_acceleration }
    pub fn max_buy_level(&self) -> Option<&PriceLevel> { self.max_buy_level.as_ref() }
    pub fn max_sell_level(&self) -> Option<&PriceLevel> { self.max_sell_level.as_ref() }
    pub fn strongest_imbalance_level(&self) -> Option<&PriceLevel> { self.strongest_imbalance_level.as_ref() }
    pub fn price_levels_count(&self) -> usize { self.price_levels.len() }

    pub fn flow_state(&self) -> &'static str {
        match (self.dominant_side, self.imbalance_strength) {
            (1, s) if s > 0.5 => "Strong Buy Flow",
            (1, s) if s > 0.2 => "Moderate Buy Flow",
            (-1, s) if s > 0.5 => "Strong Sell Flow",
            (-1, s) if s > 0.2 => "Moderate Sell Flow",
            (0, s) if s < 0.1 => "Balanced Flow",
            _ => "Weak Flow",
        }
    }

    pub fn analysis_quality(&self) -> &'static str {
        match self.price_levels.len() {
            n if n >= 20 => "Excellent",
            n if n >= 10 => "Good",
            n if n >= 5 => "Fair",
            _ => "Poor",
        }
    }

    /// SYNTHETIC ESTIMATE: OHLCV bar fallback. close > open → all volume is buy.
    /// Use `update_orderbook` for accurate OFI from L2 data.
    pub fn update_bar(&mut self, open: f64, high: f64, low: f64, close: f64, volume: f64) -> IndicatorValue {
        let bar = Bar { time: 0, open, high, low, close, volume };
        self.update_volume_bar(&bar);
        self.value()
    }

    #[inline]
    pub fn value(&self) -> IndicatorValue {
        IndicatorValue::Single(self.total_imbalance)
    }

    pub fn is_ready(&self) -> bool {
        (self.volume_bars.len() >= (self.period / 2).max(1) && !self.price_levels.is_empty())
            || self.l2_updates >= 5
    }

    pub fn reset(&mut self) {
        self.volume_bars.clear();
        self.price_levels.clear();
        self.prev_bid_depth = 0.0;
        self.prev_ask_depth = 0.0;
        self.total_imbalance = 0.0;
        self.avg_imbalance = 0.0;
        self.max_imbalance = 0.0;
        self.min_imbalance = 0.0;
        self.dominant_side = 0;
        self.imbalance_strength = 0.0;
        self.flow_acceleration = 0.0;
        self.prev_imbalance = 0.0;
        self.max_buy_level = None;
        self.max_sell_level = None;
        self.strongest_imbalance_level = None;
        self.l2_updates = 0;
    }
}

impl OrderBookConsumer for OrderFlowImbalance {
    /// Real OFI from L2 deltas. Compares current bid/ask depth against
    /// previous snapshot. Δbid - Δask normalised to [-1, 1].
    fn update_orderbook(&mut self, book: &OrderBook) -> IndicatorValue {
        let bid_depth = book.bid_depth(self.l2_depth_levels);
        let ask_depth = book.ask_depth(self.l2_depth_levels);
        self.apply_l2_ofi(bid_depth, ask_depth);
        self.l2_updates += 1;
        self.value()
    }

    fn value(&self) -> IndicatorValue { self.value() }
    fn reset(&mut self) { self.reset() }
    fn is_ready(&self) -> bool { self.is_ready() }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::types::OrderBook;

    #[test]
    fn test_order_flow_imbalance_creation() {
        let ind = OrderFlowImbalance::new(20, 0.01);
        assert!(!ind.is_ready());
        assert_eq!(ind.total_imbalance(), 0.0);
    }

    #[test]
    fn test_order_flow_imbalance_warmup() {
        let mut ind = OrderFlowImbalance::new(10, 0.01);
        for i in 0..15 {
            let bar = Bar {
                time: i as i64,
                open: 100.0 + (i as f64 * 0.1).sin(),
                high: 101.0 + (i as f64 * 0.1).sin(),
                low: 99.0 + (i as f64 * 0.1).sin(),
                close: 100.5 + (i as f64 * 0.1).sin(),
                volume: 1000.0 + i as f64 * 10.0,
            };
            ind.update_volume_bar(&bar);
        }
        assert!(ind.is_ready());
    }

    #[test]
    fn test_order_flow_imbalance_values() {
        let mut ind = OrderFlowImbalance::new(10, 0.01);
        for i in 0..20 {
            let bar = Bar {
                time: i as i64,
                open: 100.0,
                high: 102.0,
                low: 98.0,
                close: 101.0,
                volume: 1000.0,
            };
            ind.update_volume_bar(&bar);
        }
        assert!(ind.total_imbalance().is_finite());
        let side = ind.dominant_side();
        assert!(side >= -1 && side <= 1);
    }

    #[test]
    fn test_order_flow_imbalance_reset() {
        let mut ind = OrderFlowImbalance::new(10, 0.01);
        for i in 0..15 {
            let bar = Bar {
                time: i as i64,
                open: 100.0,
                high: 102.0,
                low: 98.0,
                close: 101.0,
                volume: 1000.0,
            };
            ind.update_volume_bar(&bar);
        }
        ind.reset();
        assert!(!ind.is_ready());
        assert_eq!(ind.total_imbalance(), 0.0);
    }

    #[test]
    fn test_ofi_l2_buy_pressure() {
        let mut ind = OrderFlowImbalance::new(10, 0.01);
        // First snapshot: balanced
        let book1 = OrderBook::from_tuples(
            &[(100.0, 10.0), (99.0, 8.0)],
            &[(101.0, 10.0), (102.0, 8.0)],
            1000,
        );
        ind.update_orderbook(&book1);

        // Second snapshot: bids grew → buy pressure
        let book2 = OrderBook::from_tuples(
            &[(100.0, 20.0), (99.0, 16.0)], // bid_depth doubled
            &[(101.0, 10.0), (102.0, 8.0)],
            2000,
        );
        let val = ind.update_orderbook(&book2);
        assert!(val.main() > 0.0, "bid growth should produce positive OFI");
    }

    #[test]
    fn test_ofi_l2_sell_pressure() {
        let mut ind = OrderFlowImbalance::new(10, 0.01);
        let book1 = OrderBook::from_tuples(
            &[(100.0, 10.0), (99.0, 8.0)],
            &[(101.0, 10.0), (102.0, 8.0)],
            1000,
        );
        ind.update_orderbook(&book1);

        // asks grew → sell pressure
        let book2 = OrderBook::from_tuples(
            &[(100.0, 10.0), (99.0, 8.0)],
            &[(101.0, 20.0), (102.0, 16.0)],
            2000,
        );
        let val = ind.update_orderbook(&book2);
        assert!(val.main() < 0.0, "ask growth should produce negative OFI");
    }
}