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
//! Level 3 orderbook implementation
//!
//! This module provides order-level (L3) orderbook tracking, which maintains
//! individual orders rather than just aggregated price levels like L2.
//!
//! # Key Features
//!
//! - **Order-level tracking**: Each order is tracked with its unique ID
//! - **FIFO queue semantics**: Orders at the same price are queued in FIFO order
//! - **Queue position**: Calculate your position in the queue and quantity ahead
//! - **O(1) order lookup**: HashMap index for fast order operations
//! - **L2 compatibility**: Can generate aggregated L2 levels for checksum validation
//!
//! # Use Cases
//!
//! - **Market making**: Track your order's queue position to estimate fills
//! - **Order flow analysis**: See individual orders entering/leaving the book
//! - **Fill prediction**: Estimate probability of being filled based on queue position
//!
//! # Example
//!
//! ```
//! use kraken_book::l3::{L3Book, L3Order, L3Side};
//! use rust_decimal_macros::dec;
//!
//! // Create an L3 orderbook
//! let mut book = L3Book::new("BTC/USD", 10);
//!
//! // Add orders
//! book.add_order(L3Order::new("order1", dec!(100), dec!(1)), L3Side::Bid);
//! book.add_order(L3Order::new("order2", dec!(100), dec!(2)), L3Side::Bid);
//! book.add_order(L3Order::new("order3", dec!(100), dec!(3)), L3Side::Bid);
//!
//! // Check queue position for order2
//! if let Some(pos) = book.queue_position("order2") {
//! println!("Position: {}", pos.position); // 1 (0-indexed)
//! println!("Orders ahead: {}", pos.orders_ahead); // 1
//! println!("Qty ahead: {}", pos.qty_ahead); // 1.0
//! println!("Fill probability: {:.2}", pos.fill_probability()); // 0.67
//! }
//!
//! // Get aggregated L2 view
//! let bids = book.aggregated_bids();
//! assert_eq!(bids[0].qty, dec!(6)); // 1 + 2 + 3 = 6 at price 100
//! ```
//!
//! # Kraken L3 Subscription
//!
//! L3 data requires authentication and has rate limits based on depth:
//!
//! | Depth | Rate Counter | Use Case |
//! |-------|--------------|----------|
//! | 10 | 5 | Low latency scalping |
//! | 100 | 25 | Day trading |
//! | 1000 | 100 | Market making, full depth |
// Re-export main types
pub use ;
pub use ;