queue_aware_smdp 0.1.2

Controlled execution probes and immutable telemetry for Queue-Aware SMDP research.
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
use std::collections::HashMap;

use serde::{Deserialize, Serialize};
use serde_json::{Value, json};

use extrema_infra::prelude::{Market, WsChannel};

#[derive(Clone, Debug)]
pub struct FeedPlan {
    pub task_id: u64,
    pub market: Market,
    pub channel: WsChannel,
    pub source: &'static str,
    pub feed: &'static str,
    pub url: String,
    pub subscriptions: Vec<String>,
    pub is_private: bool,
    pub is_action: bool,
}

#[derive(Clone, Debug, Default)]
pub struct ConnectionState {
    pub generation: u64,
    pub connection_id: String,
    pub frame_seq: u64,
    pub last_receive_monotonic_ns: Option<u64>,
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ProbeSide {
    Bid,
    Ask,
}

impl ProbeSide {
    pub fn as_action_side(self) -> &'static str {
        match self {
            Self::Bid => "buy",
            Self::Ask => "sell",
        }
    }

    pub fn order_sign(self) -> f64 {
        match self {
            Self::Bid => 1.0,
            Self::Ask => -1.0,
        }
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ProbePhase {
    PlacePending,
    PostAccepted,
    Resting,
    Partial,
    CancelPending,
    Filled,
    MarkoutPending,
    FlattenPending,
    Recovering,
}

#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct QueueBelief {
    pub queue_ahead_lower: f64,
    pub queue_ahead_p10: f64,
    pub queue_ahead_p50: f64,
    pub queue_ahead_p90: f64,
    pub queue_ahead_upper: f64,
    pub same_price_visible_size: f64,
    pub same_price_order_count: Option<u64>,
    pub same_price_public_trades_since_ack: f64,
    pub visible_add_since_ack: f64,
    pub visible_remove_since_ack: f64,
    pub method_version: String,
}

impl QueueBelief {
    pub fn initialize(visible_size: f64, order_count: Option<u64>, own_size: f64) -> Self {
        let possible_ahead = (visible_size - own_size).max(0.0);
        Self {
            queue_ahead_lower: 0.0,
            queue_ahead_p10: possible_ahead * 0.1,
            queue_ahead_p50: possible_ahead * 0.5,
            queue_ahead_p90: possible_ahead * 0.9,
            queue_ahead_upper: possible_ahead,
            same_price_visible_size: visible_size,
            same_price_order_count: order_count,
            same_price_public_trades_since_ack: 0.0,
            visible_add_since_ack: 0.0,
            visible_remove_since_ack: 0.0,
            method_version: "interval_fifo_v1".into(),
        }
    }

    pub fn observe_trade(&mut self, size: f64) {
        if !size.is_finite() || size <= 0.0 {
            return;
        }
        self.same_price_public_trades_since_ack += size;
        self.queue_ahead_lower = (self.queue_ahead_lower - size).max(0.0);
        self.queue_ahead_p10 = (self.queue_ahead_p10 - size).max(0.0);
        self.queue_ahead_p50 = (self.queue_ahead_p50 - size).max(0.0);
        self.queue_ahead_p90 = (self.queue_ahead_p90 - size).max(0.0);
        self.queue_ahead_upper = (self.queue_ahead_upper - size).max(0.0);
    }

    pub fn observe_visible(&mut self, visible_size: f64, order_count: Option<u64>) {
        if !visible_size.is_finite() || visible_size < 0.0 {
            return;
        }
        let delta = visible_size - self.same_price_visible_size;
        if delta > 0.0 {
            self.visible_add_since_ack += delta;
        } else if delta < 0.0 {
            let removed = -delta;
            self.visible_remove_since_ack += removed;
            self.queue_ahead_lower = (self.queue_ahead_lower - removed).max(0.0);
            self.queue_ahead_p10 = (self.queue_ahead_p10 - removed * 0.1).max(0.0);
            self.queue_ahead_p50 = (self.queue_ahead_p50 - removed * 0.5).max(0.0);
            self.queue_ahead_p90 = (self.queue_ahead_p90 - removed * 0.9).max(0.0);
        }
        self.same_price_visible_size = visible_size;
        self.same_price_order_count = order_count;
        self.clamp();
    }

    pub fn observe_partial_fill(&mut self) {
        self.queue_ahead_lower = 0.0;
        self.queue_ahead_p10 = 0.0;
        self.queue_ahead_p50 = 0.0;
        self.queue_ahead_p90 = 0.0;
        self.queue_ahead_upper = 0.0;
    }

    fn clamp(&mut self) {
        self.queue_ahead_p10 = self
            .queue_ahead_p10
            .clamp(self.queue_ahead_lower, self.queue_ahead_upper);
        self.queue_ahead_p50 = self
            .queue_ahead_p50
            .clamp(self.queue_ahead_p10, self.queue_ahead_upper);
        self.queue_ahead_p90 = self
            .queue_ahead_p90
            .clamp(self.queue_ahead_p50, self.queue_ahead_upper);
    }
}

#[derive(Clone, Debug, Default)]
pub struct BookState {
    pub received_monotonic_ns: u64,
    pub exchange_ts_ns: Option<u64>,
    pub bids: Vec<BookLevel>,
    pub asks: Vec<BookLevel>,
}

impl BookState {
    pub fn best_bid(&self) -> Option<f64> {
        self.bids.first().map(|level| level.price)
    }

    pub fn best_ask(&self) -> Option<f64> {
        self.asks.first().map(|level| level.price)
    }

    pub fn midpoint(&self) -> Option<f64> {
        let bid = self.best_bid()?;
        let ask = self.best_ask()?;
        (bid > 0.0 && ask >= bid).then_some((bid + ask) * 0.5)
    }

    pub fn level(&self, side: ProbeSide, price: f64, tick: f64) -> Option<&BookLevel> {
        let levels = match side {
            ProbeSide::Bid => &self.bids,
            ProbeSide::Ask => &self.asks,
        };
        levels
            .iter()
            .find(|level| (level.price - price).abs() <= tick * 0.1)
    }
}

#[derive(Clone, Debug, Default)]
pub struct BookLevel {
    pub price: f64,
    pub size: f64,
    pub order_count: Option<u64>,
}

#[derive(Clone, Debug, Default)]
pub struct MarketState {
    pub feed_last_receive_ns: HashMap<&'static str, u64>,
    pub hl_bbo: BookState,
    pub hl_fast_l2: BookState,
    pub hl_default_l2: BookState,
    pub binance_bid: Option<f64>,
    pub binance_ask: Option<f64>,
    pub okx_bid: Option<f64>,
    pub okx_ask: Option<f64>,
    pub hl_trade_count: u64,
    pub hl_signed_trade_size: f64,
}

impl MarketState {
    pub fn mark_feed(&mut self, feed: &'static str, received_monotonic_ns: u64) {
        self.feed_last_receive_ns
            .insert(feed, received_monotonic_ns);
    }

    pub fn required_quotes_fresh(
        &self,
        now_ns: u64,
        freshness_ns: u64,
        default_l2_freshness_ns: u64,
    ) -> bool {
        let hl_quote_fresh = (self.hl_bbo.midpoint().is_some()
            && self.feed_fresh("hl_bbo", now_ns, freshness_ns))
            || (self.hl_fast_l2.midpoint().is_some()
                && self.feed_fresh("hl_fast_l2", now_ns, freshness_ns));
        let queue_book_fresh = (self.hl_fast_l2.midpoint().is_some()
            && self.feed_fresh("hl_fast_l2", now_ns, freshness_ns))
            || (self.hl_default_l2.midpoint().is_some()
                && self.feed_fresh("hl_default_l2", now_ns, default_l2_freshness_ns));
        let binance_quote_valid = valid_quote(self.binance_bid, self.binance_ask);
        let okx_quote_valid = valid_quote(self.okx_bid, self.okx_ask);

        hl_quote_fresh && queue_book_fresh && binance_quote_valid && okx_quote_valid
    }

    pub fn fair_value(&self) -> Option<f64> {
        self.freshest_hl_book()?.1.midpoint()
    }

    pub fn freshest_hl_book(&self) -> Option<(&'static str, &BookState)> {
        [
            ("hl_bbo", &self.hl_bbo),
            ("hl_fast_l2", &self.hl_fast_l2),
            ("hl_default_l2", &self.hl_default_l2),
        ]
        .into_iter()
        .filter(|(_, book)| book.midpoint().is_some())
        .max_by_key(|(_, book)| book.received_monotonic_ns)
    }

    pub fn clear_reconnecting_feed(&mut self, feed: &str) {
        match feed {
            "hl_standard" => {
                self.hl_bbo = BookState::default();
                self.hl_default_l2 = BookState::default();
                for name in [
                    "hl_bbo",
                    "hl_default_l2",
                    "hl_trades",
                    "hl_active_asset_ctx",
                ] {
                    self.feed_last_receive_ns.remove(name);
                }
            }
            "hl_fast_l2" => {
                self.hl_fast_l2 = BookState::default();
                self.feed_last_receive_ns.remove("hl_fast_l2");
            }
            "binance_lob" => {
                self.binance_bid = None;
                self.binance_ask = None;
                self.feed_last_receive_ns.remove("binance_bbo");
                self.feed_last_receive_ns.remove("binance_depth");
            }
            "binance_agg_trade" => {
                self.feed_last_receive_ns.remove("binance_agg_trade");
            }
            "okx_public_lob" => {
                self.okx_bid = None;
                self.okx_ask = None;
                self.feed_last_receive_ns.remove("okx_bbo");
                self.feed_last_receive_ns.remove("okx_books");
            }
            "okx_trades_all" => {
                self.feed_last_receive_ns.remove("okx_trades_all");
            }
            _ => {}
        }
    }

    fn feed_fresh(&self, feed: &'static str, now_ns: u64, freshness_ns: u64) -> bool {
        self.feed_last_receive_ns
            .get(feed)
            .is_some_and(|timestamp| now_ns.saturating_sub(*timestamp) <= freshness_ns)
    }

    pub fn book_for_queue(&self) -> &BookState {
        if self.hl_fast_l2.received_monotonic_ns >= self.hl_default_l2.received_monotonic_ns {
            &self.hl_fast_l2
        } else {
            &self.hl_default_l2
        }
    }

    pub fn snapshot(&self, now_ns: u64) -> Value {
        let age = |feed: &'static str| {
            self.feed_last_receive_ns
                .get(feed)
                .map(|timestamp| now_ns.saturating_sub(*timestamp) / 1_000_000)
        };
        let freshest_hl = self.freshest_hl_book();
        json!({
            "fair_value": self.fair_value(),
            "fair_value_source": freshest_hl.map(|(source, _)| source),
            "hl_best_bid": freshest_hl.and_then(|(_, book)| book.best_bid()),
            "hl_best_ask": freshest_hl.and_then(|(_, book)| book.best_ask()),
            "binance_bid": self.binance_bid,
            "binance_ask": self.binance_ask,
            "okx_bid": self.okx_bid,
            "okx_ask": self.okx_ask,
            "feed_age_ms": {
                "hl_bbo": age("hl_bbo"),
                "hl_fast_l2": age("hl_fast_l2"),
                "hl_default_l2": age("hl_default_l2"),
                "hl_trades": age("hl_trades"),
                "hl_active_asset_ctx": age("hl_active_asset_ctx"),
                "binance_bbo": age("binance_bbo"),
                "binance_depth": age("binance_depth"),
                "binance_agg_trade": age("binance_agg_trade"),
                "okx_bbo": age("okx_bbo"),
                "okx_books": age("okx_books"),
                "okx_trades_all": age("okx_trades_all"),
            },
            "fast_l2_age_ms": now_ns.saturating_sub(self.hl_fast_l2.received_monotonic_ns) / 1_000_000,
            "default_l2_age_ms": now_ns.saturating_sub(self.hl_default_l2.received_monotonic_ns) / 1_000_000,
            "hl_trade_count": self.hl_trade_count,
            "hl_signed_trade_size": self.hl_signed_trade_size,
        })
    }
}

fn valid_quote(bid: Option<f64>, ask: Option<f64>) -> bool {
    matches!((bid, ask), (Some(bid), Some(ask)) if bid > 0.0 && ask >= bid)
}

#[derive(Clone, Debug)]
pub struct ActiveProbe {
    pub probe_id: String,
    pub decision_id: String,
    pub action_id: String,
    pub cloid: String,
    pub oid: Option<u64>,
    pub side: ProbeSide,
    pub price_level_ticks: u32,
    pub price: f64,
    pub tick_size: f64,
    pub size: f64,
    pub remaining_size: f64,
    pub cumulative_filled_size: f64,
    pub phase: ProbePhase,
    pub created_monotonic_ns: u64,
    pub send_monotonic_ns: u64,
    pub post_accepted_monotonic_ns: Option<u64>,
    pub open_monotonic_ns: Option<u64>,
    pub first_fill_monotonic_ns: Option<u64>,
    pub planned_expiry_monotonic_ns: u64,
    pub planned_dwell_ms: u64,
    pub last_keep_monotonic_ns: u64,
    pub cancel_send_monotonic_ns: Option<u64>,
    pub post_request_id: u64,
    pub cancel_request_id: Option<u64>,
    pub flatten_request_id: Option<u64>,
    pub flatten_oid: Option<u64>,
    pub flatten_action_id: Option<String>,
    pub flatten_send_monotonic_ns: Option<u64>,
    pub terminal_order_update_monotonic_ns: Option<u64>,
    pub recovery_not_before_monotonic_ns: u64,
    pub end_reason: Option<String>,
    pub initial_queue_belief: Option<QueueBelief>,
    pub queue_belief: QueueBelief,
}

#[derive(Clone, Debug)]
pub struct PendingMarkout {
    pub probe_id: String,
    pub oid: u64,
    pub fill_id: String,
    pub side: ProbeSide,
    pub fill_price: f64,
    pub fill_size: f64,
    pub fill_monotonic_ns: u64,
    pub horizons_ms: Vec<u64>,
    pub next_horizon_index: usize,
}

impl PendingMarkout {
    pub fn is_complete(&self) -> bool {
        self.next_horizon_index >= self.horizons_ms.len()
    }
}

#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct AccountState {
    pub snapshot_monotonic_ns: u64,
    pub rest_snapshot_healthy: bool,
    pub confirmed_inventory: f64,
    pub account_value: Option<f64>,
    pub withdrawable: Option<f64>,
    pub open_order_count: usize,
    pub open_orders: Vec<AccountOpenOrder>,
    pub requests_used: Option<u64>,
    pub requests_cap: Option<u64>,
    pub requests_surplus: Option<u64>,
    pub maker_fee: Option<f64>,
    pub taker_fee: Option<f64>,
    pub active_referral_discount: Option<f64>,
    pub lot_size: Option<f64>,
    pub size_decimals: Option<u32>,
    pub growth_mode: Option<String>,
    pub deployer_fee_scale: Option<String>,
}

#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct AccountOpenOrder {
    pub oid: u64,
    pub cloid: Option<String>,
    pub price: f64,
    pub size: f64,
    pub executed_size: f64,
}

impl AccountState {
    pub fn action_budget_remaining(&self) -> Option<u64> {
        Some(self.requests_cap?.saturating_sub(self.requests_used?))
    }
}

#[derive(Clone, Debug)]
pub struct OrderUpdate {
    pub coin: String,
    pub side: ProbeSide,
    pub price: f64,
    pub remaining_size: f64,
    pub original_size: f64,
    pub oid: u64,
    pub cloid: Option<String>,
    pub order_timestamp_ms: u64,
    pub status: String,
    pub status_timestamp_ms: u64,
}

#[derive(Clone, Debug)]
pub struct UserFill {
    pub oid: u64,
    pub trade_id: String,
    pub coin: String,
    pub side: ProbeSide,
    pub price: f64,
    pub size: f64,
    pub timestamp_ms: u64,
    pub fee: Option<f64>,
    pub fee_token: Option<String>,
    pub raw: Value,
}

pub fn frame_channel(value: &Value) -> Option<&str> {
    value.get("channel").and_then(Value::as_str).or_else(|| {
        value
            .pointer("/arg/channel")
            .and_then(Value::as_str)
            .or_else(|| value.get("e").and_then(Value::as_str))
    })
}

pub fn exchange_timestamp_ns(value: &Value) -> Option<u64> {
    let timestamp = [
        value.pointer("/data/time"),
        value.pointer("/data/0/time"),
        value.pointer("/data/0/ts"),
        value.get("E"),
        value.get("T"),
        value.get("ts"),
    ]
    .into_iter()
    .flatten()
    .find_map(json_u64)?;
    Some(timestamp_to_ns(timestamp))
}

pub fn parse_hl_book(value: &Value, received_monotonic_ns: u64) -> Option<BookState> {
    let data = value.get("data")?;
    let exchange_ts_ns = data.get("time").and_then(json_u64).map(timestamp_to_ns);
    if let Some(levels) = data.get("levels").and_then(Value::as_array) {
        return Some(BookState {
            received_monotonic_ns,
            exchange_ts_ns,
            bids: parse_hl_levels(levels.first()),
            asks: parse_hl_levels(levels.get(1)),
        });
    }
    let bbo = data.get("bbo")?.as_array()?;
    Some(BookState {
        received_monotonic_ns,
        exchange_ts_ns,
        bids: bbo
            .first()
            .filter(|value| !value.is_null())
            .and_then(parse_hl_level)
            .into_iter()
            .collect(),
        asks: bbo
            .get(1)
            .filter(|value| !value.is_null())
            .and_then(parse_hl_level)
            .into_iter()
            .collect(),
    })
}

pub fn parse_hl_trades(value: &Value) -> Vec<(ProbeSide, f64, f64)> {
    value
        .get("data")
        .and_then(Value::as_array)
        .into_iter()
        .flatten()
        .filter_map(|trade| {
            let side = match trade.get("side")?.as_str()? {
                "B" => ProbeSide::Bid,
                "A" => ProbeSide::Ask,
                _ => return None,
            };
            Some((
                side,
                json_f64(trade.get("px")?)?,
                json_f64(trade.get("sz")?)?,
            ))
        })
        .collect()
}

pub fn parse_order_updates(value: &Value) -> Vec<OrderUpdate> {
    value
        .get("data")
        .and_then(Value::as_array)
        .into_iter()
        .flatten()
        .filter_map(|entry| {
            let order = entry.get("order")?;
            Some(OrderUpdate {
                coin: order.get("coin")?.as_str()?.to_string(),
                side: match order.get("side")?.as_str()? {
                    "B" => ProbeSide::Bid,
                    "A" => ProbeSide::Ask,
                    _ => return None,
                },
                price: json_f64(order.get("limitPx")?)?,
                remaining_size: json_f64(order.get("sz")?)?,
                original_size: json_f64(order.get("origSz")?)?,
                oid: json_u64(order.get("oid")?)?,
                cloid: order
                    .get("cloid")
                    .and_then(Value::as_str)
                    .filter(|value| !value.is_empty())
                    .map(str::to_string),
                order_timestamp_ms: json_u64(order.get("timestamp")?)?,
                status: entry.get("status")?.as_str()?.to_string(),
                status_timestamp_ms: json_u64(entry.get("statusTimestamp")?)?,
            })
        })
        .collect()
}

pub fn parse_user_fills(value: &Value) -> Vec<UserFill> {
    let fills = value
        .pointer("/data/fills")
        .or_else(|| value.get("data"))
        .and_then(Value::as_array);
    fills
        .into_iter()
        .flatten()
        .filter_map(|fill| {
            Some(UserFill {
                oid: json_u64(fill.get("oid")?)?,
                trade_id: fill.get("tid").map(value_to_string).unwrap_or_default(),
                coin: fill.get("coin")?.as_str()?.to_string(),
                side: match fill.get("side")?.as_str()? {
                    "B" => ProbeSide::Bid,
                    "A" => ProbeSide::Ask,
                    _ => return None,
                },
                price: json_f64(fill.get("px")?)?,
                size: json_f64(fill.get("sz")?)?,
                timestamp_ms: json_u64(fill.get("time")?)?,
                fee: fill.get("fee").and_then(json_f64),
                fee_token: fill
                    .get("feeToken")
                    .and_then(Value::as_str)
                    .map(str::to_string),
                raw: fill.clone(),
            })
        })
        .collect()
}

pub fn parse_xyz_inventory(value: &Value, coin: &str) -> Option<f64> {
    let positions = value
        .pointer("/data/clearinghouseState/assetPositions")
        .or_else(|| value.pointer("/data/assetPositions"))?
        .as_array()?;
    positions.iter().find_map(|entry| {
        let position = entry.get("position")?;
        (position.get("coin")?.as_str()? == coin)
            .then(|| position.get("szi").and_then(json_f64))
            .flatten()
    })
}

pub fn parse_binance_bbo(value: &Value) -> Option<(f64, f64)> {
    (value.get("e").and_then(Value::as_str) == Some("bookTicker"))
        .then(|| Some((json_f64(value.get("b")?)?, json_f64(value.get("a")?)?)))?
}

pub fn parse_okx_bbo(value: &Value) -> Option<(f64, f64)> {
    if value.pointer("/arg/channel").and_then(Value::as_str) != Some("bbo-tbt") {
        return None;
    }
    let row = value.pointer("/data/0")?;
    let bid = row
        .get("bids")?
        .as_array()?
        .first()?
        .as_array()?
        .first()
        .and_then(json_f64)?;
    let ask = row
        .get("asks")?
        .as_array()?
        .first()?
        .as_array()?
        .first()
        .and_then(json_f64)?;
    Some((bid, ask))
}

pub fn json_u64(value: &Value) -> Option<u64> {
    value
        .as_u64()
        .or_else(|| value.as_i64().and_then(|number| number.try_into().ok()))
        .or_else(|| value.as_str().and_then(|text| text.parse().ok()))
}

pub fn json_f64(value: &Value) -> Option<f64> {
    value
        .as_f64()
        .or_else(|| value.as_str().and_then(|text| text.parse().ok()))
        .filter(|number| number.is_finite())
}

fn value_to_string(value: &Value) -> String {
    value
        .as_str()
        .map(str::to_string)
        .unwrap_or_else(|| value.to_string())
}

fn timestamp_to_ns(timestamp: u64) -> u64 {
    match timestamp {
        0..=9_999_999_999 => timestamp.saturating_mul(1_000_000_000),
        10_000_000_000..=9_999_999_999_999 => timestamp.saturating_mul(1_000_000),
        10_000_000_000_000..=9_999_999_999_999_999 => timestamp.saturating_mul(1_000),
        _ => timestamp,
    }
}

fn parse_hl_levels(value: Option<&Value>) -> Vec<BookLevel> {
    value
        .and_then(Value::as_array)
        .into_iter()
        .flatten()
        .filter_map(parse_hl_level)
        .collect()
}

fn parse_hl_level(value: &Value) -> Option<BookLevel> {
    Some(BookLevel {
        price: json_f64(value.get("px")?)?,
        size: json_f64(value.get("sz")?)?,
        order_count: value.get("n").and_then(json_u64),
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn queue_belief_preserves_uncertainty() {
        let mut belief = QueueBelief::initialize(100.0, Some(10), 1.0);
        belief.observe_trade(20.0);
        assert_eq!(belief.queue_ahead_upper, 79.0);
        assert_eq!(belief.queue_ahead_p50, 29.5);
        belief.observe_visible(70.0, Some(7));
        assert!(belief.queue_ahead_lower <= belief.queue_ahead_p10);
        assert!(belief.queue_ahead_p10 <= belief.queue_ahead_p50);
        assert!(belief.queue_ahead_p50 <= belief.queue_ahead_p90);
        assert!(belief.queue_ahead_p90 <= belief.queue_ahead_upper);
    }

    #[test]
    fn parses_private_order_and_fill_frames() {
        let order = json!({
            "channel": "orderUpdates",
            "data": [{
                "order": {"coin":"xyz:XYZ100","side":"B","limitPx":"29500","sz":"0.0003","origSz":"0.0005","oid":42,"timestamp":1,"cloid":"0xabc"},
                "status":"open","statusTimestamp":2
            }]
        });
        let fills = json!({
            "channel":"userFills",
            "data":{"fills":[{"oid":42,"tid":99,"coin":"xyz:XYZ100","side":"B","px":"29500","sz":"0.0002","time":3,"fee":"0.001","feeToken":"USDC"}]}
        });

        let parsed_order = parse_order_updates(&order);
        assert_eq!(parsed_order[0].oid, 42);
        assert_eq!(parsed_order[0].remaining_size, 0.0003);
        let parsed_fill = parse_user_fills(&fills);
        assert_eq!(parsed_fill[0].trade_id, "99");
        assert_eq!(parsed_fill[0].size, 0.0002);
    }

    #[test]
    fn parses_hl_books_without_losing_order_count() {
        let value = json!({
            "channel":"l2Book",
            "data":{"time":10,"levels":[
                [{"px":"29500","sz":"12.5","n":44}],
                [{"px":"29501","sz":"13.5","n":45}]
            ]}
        });
        let book = parse_hl_book(&value, 100).unwrap();
        assert_eq!(book.bids[0].order_count, Some(44));
        assert_eq!(book.asks[0].price, 29_501.0);
    }

    #[test]
    fn markout_is_complete_only_after_every_horizon() {
        let mut markout = PendingMarkout {
            probe_id: "probe-1".into(),
            oid: 42,
            fill_id: "42:1".into(),
            side: ProbeSide::Bid,
            fill_price: 100.0,
            fill_size: 0.1,
            fill_monotonic_ns: 10,
            horizons_ms: vec![100, 500, 1_000, 5_000],
            next_horizon_index: 3,
        };
        assert!(!markout.is_complete());
        markout.next_horizon_index = 4;
        assert!(markout.is_complete());
    }

    #[test]
    fn quiet_external_quote_remains_valid_without_new_market_events() {
        let mut market = MarketState {
            hl_bbo: test_book(100, 100.0, 101.0),
            hl_fast_l2: test_book(1_000, 100.0, 101.0),
            hl_default_l2: test_book(1, 100.0, 101.0),
            binance_bid: Some(99.0),
            binance_ask: Some(100.0),
            okx_bid: Some(99.0),
            okx_ask: Some(100.0),
            ..Default::default()
        };
        market.mark_feed("hl_bbo", 100);
        market.mark_feed("hl_fast_l2", 1_000);
        market.mark_feed("hl_default_l2", 1);
        market.mark_feed("binance_bbo", 100);
        market.mark_feed("binance_depth", 100);
        market.mark_feed("okx_bbo", 100);
        market.mark_feed("okx_books", 100);

        assert!(market.required_quotes_fresh(2_000, 1_000, 10_000));
    }

    #[test]
    fn venue_requires_an_initialized_quote_even_when_depth_is_live() {
        let mut market = MarketState {
            hl_fast_l2: test_book(1_000, 100.0, 101.0),
            hl_default_l2: test_book(1_000, 100.0, 101.0),
            okx_bid: Some(99.0),
            okx_ask: Some(100.0),
            ..Default::default()
        };
        for feed in ["hl_fast_l2", "hl_default_l2", "binance_depth", "okx_books"] {
            market.mark_feed(feed, 1_000);
        }

        assert!(!market.required_quotes_fresh(2_000, 1_000, 10_000));
    }

    #[test]
    fn fair_value_uses_freshest_valid_hl_book() {
        let mut market = MarketState {
            hl_bbo: test_book(100, 100.0, 102.0),
            hl_fast_l2: test_book(200, 103.0, 104.0),
            hl_default_l2: test_book(300, 106.0, 105.0),
            ..Default::default()
        };

        assert_eq!(market.fair_value(), Some(103.5));
        market.clear_reconnecting_feed("hl_fast_l2");
        assert_eq!(market.fair_value(), Some(101.0));
    }

    #[test]
    fn reconnect_clears_old_quote_and_liveness() {
        let mut market = MarketState {
            binance_bid: Some(99.0),
            binance_ask: Some(100.0),
            ..Default::default()
        };
        market.mark_feed("binance_bbo", 100);
        market.mark_feed("binance_depth", 200);

        market.clear_reconnecting_feed("binance_lob");

        assert_eq!(market.binance_bid, None);
        assert!(!market.feed_last_receive_ns.contains_key("binance_bbo"));
        assert!(!market.feed_last_receive_ns.contains_key("binance_depth"));
    }

    fn test_book(received_monotonic_ns: u64, bid: f64, ask: f64) -> BookState {
        BookState {
            received_monotonic_ns,
            bids: vec![BookLevel {
                price: bid,
                size: 1.0,
                order_count: Some(1),
            }],
            asks: vec![BookLevel {
                price: ask,
                size: 1.0,
                order_count: Some(1),
            }],
            ..Default::default()
        }
    }
}