flowsurface-data 0.8.8

Data aggregation, indexing, and configuration utilities for Flowsurface
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
use crate::chart::kline::KlineTrades;
use crate::util::ok_or_default;
use exchange::{
    Trade,
    unit::price::{Price, PriceStep},
    unit::qty::Qty,
};

use serde::{Deserialize, Serialize};
use std::{
    collections::{BTreeMap, VecDeque},
    time::Duration,
};

const TRADE_RETENTION_MS: u64 = 8 * 60_000;
const CHASE_MIN_VISIBLE_OPACITY: f32 = 0.15;

#[derive(Debug, Clone, Copy, PartialEq, Deserialize, Serialize)]
pub struct Config {
    pub show_spread: bool,
    #[serde(deserialize_with = "ok_or_default", default)]
    pub show_chase_tracker: bool,
    pub trade_retention: Duration,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            show_spread: false,
            show_chase_tracker: true,
            trade_retention: Duration::from_millis(TRADE_RETENTION_MS),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Direction {
    Up,
    Down,
}

#[derive(Copy, Clone)]
pub enum Side {
    Bid,
    Ask,
}

impl Side {
    pub fn idx(self) -> usize {
        match self {
            Side::Bid => 0,
            Side::Ask => 1,
        }
    }

    pub fn is_bid(self) -> bool {
        matches!(self, Side::Bid)
    }
}

#[derive(Default)]
pub struct GroupedDepth {
    pub orders: BTreeMap<Price, Qty>,
    pub chase: ChaseTracker,
}

impl GroupedDepth {
    pub fn new() -> Self {
        Self {
            orders: BTreeMap::new(),
            chase: ChaseTracker::default(),
        }
    }

    pub fn regroup_from_raw(&mut self, levels: &BTreeMap<Price, Qty>, side: Side, step: PriceStep) {
        self.orders.clear();
        for (price, qty) in levels.iter() {
            let grouped_price = price.round_to_side_step(side.is_bid(), step);
            *self.orders.entry(grouped_price).or_default() += *qty;
        }
    }

    pub fn best_price(&self, side: Side) -> Option<Price> {
        match side {
            Side::Bid => self.orders.last_key_value().map(|(p, _)| *p),
            Side::Ask => self.orders.first_key_value().map(|(p, _)| *p),
        }
    }
}

#[derive(Debug)]
pub struct TradeStore {
    pub raw: VecDeque<Trade>,
    pub grouped: KlineTrades,
}

impl Default for TradeStore {
    fn default() -> Self {
        Self {
            raw: VecDeque::new(),
            grouped: KlineTrades::new(),
        }
    }
}

impl TradeStore {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn is_empty(&self) -> bool {
        self.raw.is_empty()
    }

    pub fn insert_trades(&mut self, buffer: &[Trade], step: PriceStep) {
        for trade in buffer {
            self.grouped.add_trade_to_side_bin(trade, step);
            self.raw.push_back(*trade);
        }
    }

    pub fn rebuild_grouped(&mut self, step: PriceStep) {
        self.grouped.clear();
        for trade in &self.raw {
            self.grouped.add_trade_to_side_bin(trade, step);
        }
    }

    pub fn trade_qty_at(&self, price: Price) -> (Qty, Qty) {
        if let Some(g) = self.grouped.trades.get(&price) {
            (g.buy_qty, g.sell_qty)
        } else {
            (Qty::default(), Qty::default())
        }
    }

    pub fn price_range(&self) -> Option<(Price, Price)> {
        let mut min_p: Option<Price> = None;
        let mut max_p: Option<Price> = None;
        for &p in self.grouped.trades.keys() {
            min_p = Some(min_p.map_or(p, |cur| cur.min(p)));
            max_p = Some(max_p.map_or(p, |cur| cur.max(p)));
        }
        match (min_p, max_p) {
            (Some(a), Some(b)) => Some((a, b)),
            _ => None,
        }
    }

    /// Returns true if it removed trades and regrouped.
    pub fn maybe_cleanup(&mut self, now_ms: u64, retention: Duration, step: PriceStep) -> bool {
        let Some(oldest) = self.raw.front() else {
            return false;
        };

        let retention_ms = retention.as_millis() as u64;
        if retention_ms == 0 {
            return false;
        }

        // ~1/10th of retention, min 5s
        let cleanup_step_ms = (retention_ms / 10).max(5_000);
        let threshold_ms = retention_ms + cleanup_step_ms;
        if now_ms.saturating_sub(oldest.time) < threshold_ms {
            return false;
        }

        let keep_from_ms = now_ms.saturating_sub(retention_ms);
        let mut removed = 0usize;
        while let Some(trade) = self.raw.front() {
            if trade.time < keep_from_ms {
                self.raw.pop_front();
                removed += 1;
            } else {
                break;
            }
        }

        if removed > 0 {
            self.rebuild_grouped(step);
            return true;
        }
        false
    }
}

#[derive(Debug, Clone, Copy, Default)]
enum ChaseProgress {
    #[default]
    Idle,
    Chasing {
        direction: Direction,
        start: Price,
        end: Price,
        /// Number of consecutive moves in the current direction
        consecutive: u32,
    },
    Fading {
        direction: Direction,
        start: Price,
        end: Price,
        /// Consecutive count at the moment fading started
        start_consecutive: u32,
        /// How many unchanged updates we have been fading
        fade_steps: u32,
    },
}

#[derive(Debug, Default)]
pub struct ChaseTracker {
    /// Last known best price (raw ungrouped)
    last_best: Option<Price>,
    state: ChaseProgress,
    last_update_ms: Option<u64>,
}

impl ChaseTracker {
    pub fn update(
        &mut self,
        current_best: Option<Price>,
        is_bid: bool,
        now_ms: u64,
        max_interval: Duration,
    ) {
        let max_ms = max_interval.as_millis() as u64;
        if let Some(prev) = self.last_update_ms
            && max_ms > 0
            && now_ms.saturating_sub(prev) > max_ms
        {
            self.reset();
        }

        self.last_update_ms = Some(now_ms);

        let Some(current) = current_best else {
            self.reset();
            return;
        };

        if let Some(last) = self.last_best {
            let direction = if is_bid {
                Direction::Up
            } else {
                Direction::Down
            };

            let is_continue = match direction {
                Direction::Up => current > last,
                Direction::Down => current < last,
            };
            let is_reverse = match direction {
                Direction::Up => current < last,
                Direction::Down => current > last,
            };
            let is_unchanged = current == last;

            self.state = match (&self.state, is_continue, is_reverse, is_unchanged) {
                // Continue in same direction while already chasing: extend chase
                (
                    ChaseProgress::Chasing {
                        direction: sdir,
                        start,
                        consecutive,
                        ..
                    },
                    true,
                    _,
                    _,
                ) if *sdir == direction => ChaseProgress::Chasing {
                    direction,
                    start: *start,
                    end: current,
                    consecutive: consecutive.saturating_add(1),
                },
                // Start or restart a chase (from idle or from fading)
                (ChaseProgress::Idle, true, _, _) | (ChaseProgress::Fading { .. }, true, _, _) => {
                    ChaseProgress::Chasing {
                        direction,
                        start: last,
                        end: current,
                        consecutive: 1,
                    }
                }
                // Reversal while chasing -> start fading from the last chase extreme (freeze end)
                (
                    ChaseProgress::Chasing {
                        direction: sdir,
                        start,
                        end,
                        consecutive,
                    },
                    _,
                    true,
                    _,
                ) if *consecutive > 0 => ChaseProgress::Fading {
                    direction: *sdir,
                    start: *start,
                    end: *end, // keep the extreme reached during the chase
                    start_consecutive: *consecutive,
                    fade_steps: 0,
                },
                // Unchanged while chasing -> start fading from the last chase extreme (freeze end)
                (
                    ChaseProgress::Chasing {
                        direction: sdir,
                        start,
                        end,
                        consecutive,
                    },
                    _,
                    _,
                    true,
                ) if *consecutive > 0 => ChaseProgress::Fading {
                    direction: *sdir,
                    start: *start,
                    end: *end, // keep the extreme reached during the chase
                    start_consecutive: *consecutive,
                    fade_steps: 0,
                },
                // Unchanged while fading -> keep fading (decay)
                (
                    ChaseProgress::Fading {
                        direction: sdir,
                        start,
                        end,
                        start_consecutive,
                        fade_steps,
                    },
                    _,
                    _,
                    true,
                ) => ChaseProgress::Fading {
                    direction: *sdir,
                    start: *start,
                    end: *end,
                    start_consecutive: *start_consecutive,
                    fade_steps: fade_steps.saturating_add(1),
                },
                // Reversal while fading -> keep fading and decay
                (
                    ChaseProgress::Fading {
                        direction: sdir,
                        start,
                        end,
                        start_consecutive,
                        fade_steps,
                    },
                    _,
                    true,
                    _,
                ) => ChaseProgress::Fading {
                    direction: *sdir,
                    start: *start,
                    end: *end, // freeze
                    start_consecutive: *start_consecutive,
                    fade_steps: fade_steps.saturating_add(1),
                },
                // Unchanged when idle -> no change
                (ChaseProgress::Idle, _, _, true) => ChaseProgress::Idle,
                _ => self.state,
            };

            if let ChaseProgress::Fading {
                start_consecutive,
                fade_steps,
                ..
            } = self.state
            {
                let base = Self::consecutive_to_alpha(start_consecutive);
                let alpha = base / (1.0 + fade_steps as f32);
                if alpha < CHASE_MIN_VISIBLE_OPACITY {
                    self.state = ChaseProgress::Idle;
                }
            }
        }

        self.last_best = Some(current);
    }

    pub fn reset(&mut self) {
        self.last_best = None;
        self.state = ChaseProgress::Idle;
        self.last_update_ms = None;
    }

    /// Maps consecutive steps n to [0,1): 1 - 1/(1+n)
    fn consecutive_to_alpha(n: u32) -> f32 {
        let nf = n as f32;
        1.0 - 1.0 / (1.0 + nf)
    }

    pub fn segment(&self) -> Option<(Price, Price, f32)> {
        match self.state {
            ChaseProgress::Chasing {
                start,
                end,
                consecutive,
                ..
            } => Some((start, end, Self::consecutive_to_alpha(consecutive))),
            ChaseProgress::Fading {
                start,
                end,
                start_consecutive,
                fade_steps,
                ..
            } => {
                let alpha = {
                    let base = Self::consecutive_to_alpha(start_consecutive);
                    base / (1.0 + fade_steps as f32)
                };
                Some((start, end, alpha))
            }
            _ => None,
        }
    }
}