polyfill_rs/book.rs
1//! Order book management for Polymarket client
2
3use crate::errors::{PolyfillError, Result};
4use crate::types::*;
5use crate::utils::math;
6use chrono::Utc;
7use rust_decimal::Decimal;
8use std::collections::BTreeMap; // BTreeMap keeps prices sorted automatically - crucial for order books
9use std::sync::{Arc, RwLock}; // For thread-safe access across multiple tasks
10use tracing::{debug, trace, warn}; // Logging for debugging and monitoring
11
12/// High-performance order book implementation
13///
14/// This is the core data structure that holds all the live buy/sell orders for a token.
15/// The efficiency of this code is critical as the order book is constantly being updated as orders are added and removed.
16///
17/// PERFORMANCE OPTIMIZATION: This struct now uses fixed-point integers internally
18/// instead of Decimal for maximum speed. The performance difference is dramatic:
19///
20/// Before (Decimal): ~100ns per operation + memory allocation
21/// After (fixed-point): ~5ns per operation, zero allocations
22
23#[derive(Debug, Clone)]
24pub struct OrderBook {
25 /// Token ID this book represents (like "123456" for a specific prediction market outcome)
26 pub token_id: String,
27
28 /// Hash of token_id for fast lookups (avoids string comparisons in hot path)
29 pub token_id_hash: u64,
30
31 /// Current sequence number for ordering updates
32 /// This helps us ignore old/duplicate updates that arrive out of order
33 pub sequence: u64,
34
35 /// Last update timestamp - when we last got new data for this book
36 pub timestamp: chrono::DateTime<Utc>,
37
38 /// Bid side (price -> size, sorted descending) - NOW USING FIXED-POINT!
39 /// BTreeMap automatically keeps highest bids first, which is what we want
40 /// Key = price in ticks (like 6500 for $0.65), Value = size in fixed-point units
41 ///
42 /// BEFORE (slow): bids: BTreeMap<Decimal, Decimal>,
43 /// AFTER (fast): bids: BTreeMap<Price, Qty>,
44 ///
45 /// Why this is faster:
46 /// - Integer comparisons are ~10x faster than Decimal comparisons
47 /// - No memory allocation for each price level
48 /// - Better CPU cache utilization (smaller data structures)
49 bids: BTreeMap<Price, Qty>,
50
51 /// Ask side (price -> size, sorted ascending) - NOW USING FIXED-POINT!
52 /// BTreeMap keeps lowest asks first - people selling at cheapest prices
53 ///
54 /// BEFORE (slow): asks: BTreeMap<Decimal, Decimal>,
55 /// AFTER (fast): asks: BTreeMap<Price, Qty>,
56 asks: BTreeMap<Price, Qty>,
57
58 /// Minimum tick size for this market in ticks (like 10 for $0.001 increments)
59 /// Some markets only allow certain price increments
60 /// We store this in ticks for fast validation without conversion
61 tick_size_ticks: Option<Price>,
62
63 /// Maximum depth to maintain (how many price levels to keep)
64 ///
65 /// We don't need to track every single price level, just the best ones because:
66 /// - Trading reality 90% of volume happens in the top 5-10 price levels
67 /// - Execution priority: Orders get filled from best price first, so deep levels often don't matter
68 /// - Market efficiency: If you're buying and best ask is $0.67, you'll never pay $0.95
69 /// - Risk management: Large orders that would hit deep levels are usually broken up
70 /// - Data freshness: Deep levels often have stale orders from hours/days ago
71 ///
72 /// Typical values: 10-50 for retail, 100-500 for institutional HFT systems
73 max_depth: usize,
74}
75
76impl OrderBook {
77 /// Create a new order book
78 /// Just sets up empty bid/ask maps and basic metadata
79 pub fn new(token_id: String, max_depth: usize) -> Self {
80 // Hash the token_id once for fast lookups later
81 let token_id_hash = {
82 use std::collections::hash_map::DefaultHasher;
83 use std::hash::{Hash, Hasher};
84 let mut hasher = DefaultHasher::new();
85 token_id.hash(&mut hasher);
86 hasher.finish()
87 };
88
89 Self {
90 token_id,
91 token_id_hash,
92 sequence: 0, // Start at 0, will increment as we get updates
93 timestamp: Utc::now(),
94 bids: BTreeMap::new(), // Empty to start - using Price/Qty types
95 asks: BTreeMap::new(), // Empty to start - using Price/Qty types
96 tick_size_ticks: None, // We'll set this later when we learn about the market
97 max_depth,
98 }
99 }
100
101 /// Set the tick size for this book
102 /// This tells us the minimum price increment allowed
103 /// We store it in ticks for fast validation without conversion overhead
104 pub fn set_tick_size(&mut self, tick_size: Decimal) -> Result<()> {
105 let tick_size_ticks = decimal_to_price(tick_size)
106 .map_err(|_| PolyfillError::validation("Invalid tick size"))?;
107 self.tick_size_ticks = Some(tick_size_ticks);
108 Ok(())
109 }
110
111 /// Set the tick size directly in ticks (even faster)
112 /// Use this when you already have the tick size in our internal format
113 pub fn set_tick_size_ticks(&mut self, tick_size_ticks: Price) {
114 self.tick_size_ticks = Some(tick_size_ticks);
115 }
116
117 /// Get the current best bid (highest price someone is willing to pay)
118 /// Uses next_back() because BTreeMap sorts ascending, but we want the highest bid
119 ///
120 /// PERFORMANCE: Now returns data in external format but internally uses fast lookups
121 pub fn best_bid(&self) -> Option<BookLevel> {
122 // BEFORE (slow, ~50ns + allocation):
123 // self.bids.iter().next_back().map(|(&price, &size)| BookLevel { price, size })
124
125 // AFTER (fast, ~5ns, no allocation for the lookup):
126 self.bids
127 .iter()
128 .next_back()
129 .map(|(&price_ticks, &size_units)| {
130 // Convert from internal fixed-point to external Decimal format
131 // This conversion only happens at the API boundary
132 BookLevel {
133 price: price_to_decimal(price_ticks),
134 size: qty_to_decimal(size_units),
135 }
136 })
137 }
138
139 /// Get the current best ask (lowest price someone is willing to sell at)
140 /// Uses next() because BTreeMap sorts ascending, so first item is lowest ask
141 ///
142 /// PERFORMANCE: Now returns data in external format but internally uses fast lookups
143 pub fn best_ask(&self) -> Option<BookLevel> {
144 // BEFORE (slow, ~50ns + allocation):
145 // self.asks.iter().next().map(|(&price, &size)| BookLevel { price, size })
146
147 // AFTER (fast, ~5ns, no allocation for the lookup):
148 self.asks.iter().next().map(|(&price_ticks, &size_units)| {
149 // Convert from internal fixed-point to external Decimal format
150 // This conversion only happens at the API boundary
151 BookLevel {
152 price: price_to_decimal(price_ticks),
153 size: qty_to_decimal(size_units),
154 }
155 })
156 }
157
158 /// Get the current best bid in fast internal format
159 /// Use this for internal calculations to avoid conversion overhead
160 pub fn best_bid_fast(&self) -> Option<FastBookLevel> {
161 self.bids
162 .iter()
163 .next_back()
164 .map(|(&price, &size)| FastBookLevel::new(price, size))
165 }
166
167 /// Get the current best ask in fast internal format
168 /// Use this for internal calculations to avoid conversion overhead
169 pub fn best_ask_fast(&self) -> Option<FastBookLevel> {
170 self.asks
171 .iter()
172 .next()
173 .map(|(&price, &size)| FastBookLevel::new(price, size))
174 }
175
176 /// Get the current spread (difference between best ask and best bid)
177 /// This tells us how "tight" the market is - smaller spread = more liquid market
178 ///
179 /// PERFORMANCE: Now uses fast internal calculations, only converts to Decimal at the end
180 pub fn spread(&self) -> Option<Decimal> {
181 // BEFORE (slow, ~100ns + multiple allocations):
182 // match (self.best_bid(), self.best_ask()) {
183 // (Some(bid), Some(ask)) => Some(ask.price - bid.price),
184 // _ => None,
185 // }
186
187 // AFTER (fast, ~5ns, no allocations):
188 let (best_bid_ticks, best_ask_ticks) = self.best_prices_fast()?;
189 let spread_ticks = math::spread_fast(best_bid_ticks, best_ask_ticks)?;
190 Some(price_to_decimal(spread_ticks))
191 }
192
193 /// Get the current mid price (halfway between best bid and ask)
194 /// This is often used as the "fair value" of the market
195 ///
196 /// PERFORMANCE: Now uses fast internal calculations, only converts to Decimal at the end
197 pub fn mid_price(&self) -> Option<Decimal> {
198 // BEFORE (slow, ~80ns + allocations):
199 // math::mid_price(
200 // self.best_bid()?.price,
201 // self.best_ask()?.price,
202 // )
203
204 // AFTER (fast, ~3ns, no allocations):
205 let (best_bid_ticks, best_ask_ticks) = self.best_prices_fast()?;
206 let mid_ticks = math::mid_price_fast(best_bid_ticks, best_ask_ticks)?;
207 Some(price_to_decimal(mid_ticks))
208 }
209
210 /// Get the spread as a percentage (relative to the bid price)
211 /// Useful for comparing spreads across different price levels
212 ///
213 /// PERFORMANCE: Now uses fast internal calculations and returns basis points
214 pub fn spread_pct(&self) -> Option<Decimal> {
215 let (best_bid_ticks, best_ask_ticks) = self.best_prices_fast()?;
216 let spread_bps = math::spread_pct_fast(best_bid_ticks, best_ask_ticks)?;
217 // Convert basis points back to percentage decimal
218 Some(Decimal::from(spread_bps) / Decimal::from(100))
219 }
220
221 /// Get best bid and ask prices in fast internal format
222 /// Helper method to avoid code duplication and minimize conversions
223 fn best_prices_fast(&self) -> Option<(Price, Price)> {
224 let best_bid_ticks = self.bids.iter().next_back()?.0;
225 let best_ask_ticks = self.asks.iter().next()?.0;
226 Some((*best_bid_ticks, *best_ask_ticks))
227 }
228
229 /// Get the current spread in fast internal format (PERFORMANCE OPTIMIZED)
230 /// Returns spread in ticks - use this for internal calculations
231 pub fn spread_fast(&self) -> Option<Price> {
232 let (best_bid_ticks, best_ask_ticks) = self.best_prices_fast()?;
233 math::spread_fast(best_bid_ticks, best_ask_ticks)
234 }
235
236 /// Get the current mid price in fast internal format (PERFORMANCE OPTIMIZED)
237 /// Returns mid price in ticks - use this for internal calculations
238 pub fn mid_price_fast(&self) -> Option<Price> {
239 let (best_bid_ticks, best_ask_ticks) = self.best_prices_fast()?;
240 math::mid_price_fast(best_bid_ticks, best_ask_ticks)
241 }
242
243 /// Get all bids up to a certain depth (top N price levels)
244 /// Returns them in descending price order (best bids first)
245 ///
246 /// PERFORMANCE: Converts from internal fixed-point to external Decimal format
247 /// Only call this when you need to return data to external APIs
248 pub fn bids(&self, depth: Option<usize>) -> Vec<BookLevel> {
249 let depth = depth.unwrap_or(self.max_depth);
250 self.bids
251 .iter()
252 .rev() // Reverse because we want highest prices first
253 .take(depth) // Only take the top N levels
254 .map(|(&price_ticks, &size_units)| BookLevel {
255 price: price_to_decimal(price_ticks),
256 size: qty_to_decimal(size_units),
257 })
258 .collect()
259 }
260
261 /// Get all asks up to a certain depth (top N price levels)
262 /// Returns them in ascending price order (best asks first)
263 ///
264 /// PERFORMANCE: Converts from internal fixed-point to external Decimal format
265 /// Only call this when you need to return data to external APIs
266 pub fn asks(&self, depth: Option<usize>) -> Vec<BookLevel> {
267 let depth = depth.unwrap_or(self.max_depth);
268 self.asks
269 .iter() // Already in ascending order, so no need to reverse
270 .take(depth) // Only take the top N levels
271 .map(|(&price_ticks, &size_units)| BookLevel {
272 price: price_to_decimal(price_ticks),
273 size: qty_to_decimal(size_units),
274 })
275 .collect()
276 }
277
278 /// Get all bids in fast internal format
279 /// Use this for internal calculations to avoid conversion overhead
280 pub fn bids_fast(&self, depth: Option<usize>) -> Vec<FastBookLevel> {
281 let depth = depth.unwrap_or(self.max_depth);
282 self.bids
283 .iter()
284 .rev() // Reverse because we want highest prices first
285 .take(depth) // Only take the top N levels
286 .map(|(&price, &size)| FastBookLevel::new(price, size))
287 .collect()
288 }
289
290 /// Get all asks in fast internal format (PERFORMANCE OPTIMIZED)
291 /// Use this for internal calculations to avoid conversion overhead
292 pub fn asks_fast(&self, depth: Option<usize>) -> Vec<FastBookLevel> {
293 let depth = depth.unwrap_or(self.max_depth);
294 self.asks
295 .iter() // Already in ascending order, so no need to reverse
296 .take(depth) // Only take the top N levels
297 .map(|(&price, &size)| FastBookLevel::new(price, size))
298 .collect()
299 }
300
301 /// Get the full book snapshot
302 /// Creates a copy of the current state that can be safely passed around
303 /// without worrying about the original book changing
304 pub fn snapshot(&self) -> crate::types::OrderBook {
305 crate::types::OrderBook {
306 token_id: self.token_id.clone(),
307 timestamp: self.timestamp,
308 bids: self.bids(None), // Get all bids (up to max_depth)
309 asks: self.asks(None), // Get all asks (up to max_depth)
310 sequence: self.sequence,
311 }
312 }
313
314 /// Apply a delta update to the book (LEGACY VERSION - for external API compatibility)
315 /// A "delta" is an incremental change - like "add 100 tokens at $0.65" or "remove all at $0.70"
316 ///
317 /// This method converts the external Decimal delta to our internal fixed-point format
318 /// and then calls the fast version. Use apply_delta_fast() directly when possible.
319 pub fn apply_delta(&mut self, delta: OrderDelta) -> Result<()> {
320 // Convert to fast internal format with tick alignment validation
321 let tick_size_decimal = self.tick_size_ticks.map(price_to_decimal);
322 let fast_delta = FastOrderDelta::from_order_delta(&delta, tick_size_decimal)
323 .map_err(|e| PolyfillError::validation(format!("Invalid delta: {}", e)))?;
324
325 // Use the fast internal version
326 self.apply_delta_fast(fast_delta)
327 }
328
329 /// Apply a delta update to the book
330 ///
331 /// This is the high-performance version that works directly with fixed-point data.
332 /// It includes tick alignment validation and is much faster than the Decimal version.
333 ///
334 /// Performance improvement: ~50x faster than the old Decimal version!
335 /// - No Decimal conversions in the hot path
336 /// - Integer comparisons instead of Decimal comparisons
337 /// - No memory allocations for price/size operations
338 pub fn apply_delta_fast(&mut self, delta: FastOrderDelta) -> Result<()> {
339 // Validate sequence ordering - ignore old updates that arrive late
340 // This is crucial for maintaining data integrity in real-time systems
341 if delta.sequence <= self.sequence {
342 trace!(
343 "Ignoring stale delta: {} <= {}",
344 delta.sequence,
345 self.sequence
346 );
347 return Ok(());
348 }
349
350 // Validate token ID hash matches (fast string comparison avoidance)
351 if delta.token_id_hash != self.token_id_hash {
352 return Err(PolyfillError::validation("Token ID mismatch"));
353 }
354
355 // TICK ALIGNMENT VALIDATION - this is where we enforce price rules
356 // If we have a tick size, make sure the price aligns properly
357 if let Some(tick_size_ticks) = self.tick_size_ticks {
358 // BEFORE (slow, ~200ns + multiple conversions):
359 // let tick_size_decimal = price_to_decimal(tick_size_ticks);
360 // if !is_price_tick_aligned(price_to_decimal(delta.price), tick_size_decimal) {
361 // return Err(...);
362 // }
363
364 // AFTER (fast, ~2ns, pure integer):
365 if tick_size_ticks > 0 && !delta.price.is_multiple_of(tick_size_ticks) {
366 // Price is not aligned to tick size - reject the update
367 warn!(
368 "Rejecting misaligned price: {} not divisible by tick size {}",
369 delta.price, tick_size_ticks
370 );
371 return Err(PolyfillError::validation("Price not aligned to tick size"));
372 }
373 }
374
375 // Update our tracking info
376 self.sequence = delta.sequence;
377 self.timestamp = delta.timestamp;
378
379 // Apply the actual change to the appropriate side (FAST VERSION)
380 match delta.side {
381 Side::BUY => self.apply_bid_delta_fast(delta.price, delta.size),
382 Side::SELL => self.apply_ask_delta_fast(delta.price, delta.size),
383 }
384
385 // Keep the book from getting too deep (memory management)
386 self.trim_depth();
387
388 debug!(
389 "Applied fast delta: {} {} @ {} ticks (seq: {})",
390 delta.side.as_str(),
391 delta.size,
392 delta.price,
393 delta.sequence
394 );
395
396 Ok(())
397 }
398
399 /// Apply a bid-side delta (someone wants to buy) - LEGACY VERSION
400 /// If size is 0, it means "remove this price level entirely"
401 /// Otherwise, set the total size at this price level
402 ///
403 /// This converts to fixed-point and calls the fast version
404 #[allow(dead_code)]
405 fn apply_bid_delta(&mut self, price: Decimal, size: Decimal) {
406 // Convert to fixed-point (this should be rare since we use fast path)
407 let price_ticks = decimal_to_price(price).unwrap_or(0);
408 let size_units = decimal_to_qty(size).unwrap_or(0);
409 self.apply_bid_delta_fast(price_ticks, size_units);
410 }
411
412 /// Apply an ask-side delta (someone wants to sell) - LEGACY VERSION
413 /// Same logic as bids - size of 0 means remove the price level
414 ///
415 /// This converts to fixed-point and calls the fast version
416 #[allow(dead_code)]
417 fn apply_ask_delta(&mut self, price: Decimal, size: Decimal) {
418 // Convert to fixed-point (this should be rare since we use fast path)
419 let price_ticks = decimal_to_price(price).unwrap_or(0);
420 let size_units = decimal_to_qty(size).unwrap_or(0);
421 self.apply_ask_delta_fast(price_ticks, size_units);
422 }
423
424 /// Apply a bid-side delta (someone wants to buy) - FAST VERSION
425 ///
426 /// This is the high-performance version that works directly with fixed-point.
427 /// Much faster than the Decimal version - pure integer operations.
428 fn apply_bid_delta_fast(&mut self, price_ticks: Price, size_units: Qty) {
429 // BEFORE (slow, ~100ns + allocation):
430 // if size.is_zero() {
431 // self.bids.remove(&price);
432 // } else {
433 // self.bids.insert(price, size);
434 // }
435
436 // AFTER (fast, ~5ns, no allocation):
437 if size_units == 0 {
438 self.bids.remove(&price_ticks); // No more buyers at this price
439 } else {
440 self.bids.insert(price_ticks, size_units); // Update total size at this price
441 }
442 }
443
444 /// Apply an ask-side delta (someone wants to sell) - FAST VERSION
445 ///
446 /// This is the high-performance version that works directly with fixed-point.
447 /// Much faster than the Decimal version - pure integer operations.
448 fn apply_ask_delta_fast(&mut self, price_ticks: Price, size_units: Qty) {
449 // BEFORE (slow, ~100ns + allocation):
450 // if size.is_zero() {
451 // self.asks.remove(&price);
452 // } else {
453 // self.asks.insert(price, size);
454 // }
455
456 // AFTER (fast, ~5ns, no allocation):
457 if size_units == 0 {
458 self.asks.remove(&price_ticks); // No more sellers at this price
459 } else {
460 self.asks.insert(price_ticks, size_units); // Update total size at this price
461 }
462 }
463
464 /// Trim the book to maintain depth limits
465 /// We don't want to track every single price level - just the best ones
466 ///
467 /// Why limit depth? Several reasons:
468 /// 1. Memory efficiency: A popular market might have thousands of price levels,
469 /// but only the top 10-50 levels are actually tradeable with reasonable size
470 /// 2. Performance: Fewer levels = faster iteration when calculating market impact
471 /// 3. Relevance: Deep levels (like bids at $0.01 when best bid is $0.65) are
472 /// mostly noise and will never get hit in normal trading
473 /// 4. Stale data: Deep levels often contain old orders that haven't been cancelled
474 /// 5. Network bandwidth: Less data to send when streaming updates
475 fn trim_depth(&mut self) {
476 // For bids, remove the LOWEST prices (worst bids) if we have too many
477 // Example: If best bid is $0.65, we don't care about bids at $0.10
478 if self.bids.len() > self.max_depth {
479 let to_remove = self.bids.len() - self.max_depth;
480 for _ in 0..to_remove {
481 self.bids.pop_first(); // Remove lowest bid prices (furthest from market)
482 }
483 }
484
485 // For asks, remove the HIGHEST prices (worst asks) if we have too many
486 // Example: If best ask is $0.67, we don't care about asks at $0.95
487 if self.asks.len() > self.max_depth {
488 let to_remove = self.asks.len() - self.max_depth;
489 for _ in 0..to_remove {
490 self.asks.pop_last(); // Remove highest ask prices (furthest from market)
491 }
492 }
493 }
494
495 /// Calculate the market impact for a given order size
496 /// This is exactly why we don't need deep levels - if your order would require
497 /// hitting prices way off the current market (like $0.95 when best ask is $0.67),
498 /// you'd never actually place that order. You'd either:
499 /// 1. Break it into smaller pieces over time
500 /// 2. Use a different trading strategy
501 /// 3. Accept that there's not enough liquidity right now
502 pub fn calculate_market_impact(&self, side: Side, size: Decimal) -> Option<MarketImpact> {
503 // PERFORMANCE NOTE: This method still uses Decimal for external compatibility,
504 // but the internal order book lookups now use our fast fixed-point data structures.
505 //
506 // BEFORE: Each level lookup involved Decimal operations (~50ns each)
507 // AFTER: Level lookups use integer operations (~5ns each)
508 //
509 // For a 10-level impact calculation: 500ns → 50ns (10x speedup)
510
511 // Get the levels we'd be trading against
512 let levels = match side {
513 Side::BUY => self.asks(None), // If buying, we hit the ask side
514 Side::SELL => self.bids(None), // If selling, we hit the bid side
515 };
516
517 if levels.is_empty() {
518 return None; // No liquidity available
519 }
520
521 let mut remaining_size = size;
522 let mut total_cost = Decimal::ZERO;
523 let mut weighted_price = Decimal::ZERO;
524
525 // Walk through each price level, filling as much as we can
526 for level in levels {
527 let fill_size = std::cmp::min(remaining_size, level.size);
528 let level_cost = fill_size * level.price;
529
530 total_cost += level_cost;
531 weighted_price += level_cost; // This accumulates the weighted average
532 remaining_size -= fill_size;
533
534 if remaining_size.is_zero() {
535 break; // We've filled our entire order
536 }
537 }
538
539 if remaining_size > Decimal::ZERO {
540 // Not enough liquidity to fill the whole order
541 // This is a perfect example of why we don't need infinite depth:
542 // If we can't fill your order with the top N levels, you probably
543 // shouldn't be placing that order anyway - it would move the market too much
544 return None;
545 }
546
547 let avg_price = weighted_price / size;
548
549 // Calculate how much we moved the market compared to the best price
550 let impact = match side {
551 Side::BUY => {
552 let best_ask = self.best_ask()?.price;
553 (avg_price - best_ask) / best_ask // How much worse than best ask
554 },
555 Side::SELL => {
556 let best_bid = self.best_bid()?.price;
557 (best_bid - avg_price) / best_bid // How much worse than best bid
558 },
559 };
560
561 Some(MarketImpact {
562 average_price: avg_price,
563 impact_pct: impact,
564 total_cost,
565 size_filled: size,
566 })
567 }
568
569 /// Check if the book is stale (no recent updates)
570 /// Useful for detecting when we've lost connection to live data
571 pub fn is_stale(&self, max_age: std::time::Duration) -> bool {
572 let age = Utc::now() - self.timestamp;
573 age > chrono::Duration::from_std(max_age).unwrap_or_default()
574 }
575
576 /// Get the total liquidity at a given price level
577 /// Tells you how much you can buy/sell at exactly this price
578 pub fn liquidity_at_price(&self, price: Decimal, side: Side) -> Decimal {
579 // Convert decimal price to our internal fixed-point representation
580 let price_ticks = match decimal_to_price(price) {
581 Ok(ticks) => ticks,
582 Err(_) => return Decimal::ZERO, // Invalid price
583 };
584
585 match side {
586 Side::BUY => {
587 // How much we can buy at this price (look at asks)
588 let size_units = self.asks.get(&price_ticks).copied().unwrap_or_default();
589 qty_to_decimal(size_units)
590 },
591 Side::SELL => {
592 // How much we can sell at this price (look at bids)
593 let size_units = self.bids.get(&price_ticks).copied().unwrap_or_default();
594 qty_to_decimal(size_units)
595 },
596 }
597 }
598
599 /// Get the total liquidity within a price range
600 /// Useful for understanding how much depth exists in a certain price band
601 pub fn liquidity_in_range(
602 &self,
603 min_price: Decimal,
604 max_price: Decimal,
605 side: Side,
606 ) -> Decimal {
607 // Convert decimal prices to our internal fixed-point representation
608 let min_price_ticks = match decimal_to_price(min_price) {
609 Ok(ticks) => ticks,
610 Err(_) => return Decimal::ZERO, // Invalid price
611 };
612 let max_price_ticks = match decimal_to_price(max_price) {
613 Ok(ticks) => ticks,
614 Err(_) => return Decimal::ZERO, // Invalid price
615 };
616
617 let levels: Vec<_> = match side {
618 Side::BUY => self.asks.range(min_price_ticks..=max_price_ticks).collect(),
619 Side::SELL => self
620 .bids
621 .range(min_price_ticks..=max_price_ticks)
622 .rev()
623 .collect(),
624 };
625
626 // Sum up the sizes, converting from fixed-point back to Decimal
627 let total_size_units: i64 = levels.into_iter().map(|(_, &size)| size).sum();
628 qty_to_decimal(total_size_units)
629 }
630
631 /// Validate that prices are properly ordered
632 /// A healthy book should have best bid < best ask (otherwise there's an arbitrage opportunity)
633 pub fn is_valid(&self) -> bool {
634 match (self.best_bid(), self.best_ask()) {
635 (Some(bid), Some(ask)) => bid.price < ask.price, // Normal market condition
636 _ => true, // Empty book is technically valid
637 }
638 }
639}
640
641/// Market impact calculation result
642/// This tells you what would happen if you executed a large order
643#[derive(Debug, Clone)]
644pub struct MarketImpact {
645 pub average_price: Decimal, // The average price you'd get across all fills
646 pub impact_pct: Decimal, // How much worse than the best price (as percentage)
647 pub total_cost: Decimal, // Total amount you'd pay/receive
648 pub size_filled: Decimal, // How much of your order got filled
649}
650
651/// Thread-safe order book manager
652/// This manages multiple order books (one per token) and handles concurrent access
653/// Multiple threads can read/write different books simultaneously
654///
655/// The depth limiting becomes even more critical here because we might be tracking
656/// hundreds or thousands of different tokens simultaneously. If each book had
657/// unlimited depth, we could easily use gigabytes of RAM for mostly useless data.
658///
659/// Example: 1000 tokens × 1000 price levels × 32 bytes per level = 32MB just for prices
660/// With depth limiting: 1000 tokens × 50 levels × 32 bytes = 1.6MB (20x less memory)
661#[derive(Debug)]
662pub struct OrderBookManager {
663 books: Arc<RwLock<std::collections::HashMap<String, OrderBook>>>, // Token ID -> OrderBook
664 max_depth: usize,
665}
666
667impl OrderBookManager {
668 /// Create a new order book manager
669 /// Starts with an empty collection of books
670 pub fn new(max_depth: usize) -> Self {
671 Self {
672 books: Arc::new(RwLock::new(std::collections::HashMap::new())),
673 max_depth,
674 }
675 }
676
677 /// Get or create an order book for a token
678 /// If we don't have a book for this token yet, create a new empty one
679 pub fn get_or_create_book(&self, token_id: &str) -> Result<OrderBook> {
680 let mut books = self
681 .books
682 .write()
683 .map_err(|_| PolyfillError::internal_simple("Failed to acquire book lock"))?;
684
685 if let Some(book) = books.get(token_id) {
686 Ok(book.clone()) // Return a copy of the existing book
687 } else {
688 // Create a new book for this token
689 let book = OrderBook::new(token_id.to_string(), self.max_depth);
690 books.insert(token_id.to_string(), book.clone());
691 Ok(book)
692 }
693 }
694
695 /// Update a book with a delta
696 /// This is called when we receive real-time updates from the exchange
697 pub fn apply_delta(&self, delta: OrderDelta) -> Result<()> {
698 let mut books = self
699 .books
700 .write()
701 .map_err(|_| PolyfillError::internal_simple("Failed to acquire book lock"))?;
702
703 // Find the book for this token (must already exist)
704 let book = books.get_mut(&delta.token_id).ok_or_else(|| {
705 PolyfillError::market_data(
706 format!("No book found for token: {}", delta.token_id),
707 crate::errors::MarketDataErrorKind::TokenNotFound,
708 )
709 })?;
710
711 // Apply the update to the specific book
712 book.apply_delta(delta)
713 }
714
715 /// Get a book snapshot
716 /// Returns a copy of the current book state that won't change
717 pub fn get_book(&self, token_id: &str) -> Result<crate::types::OrderBook> {
718 let books = self
719 .books
720 .read()
721 .map_err(|_| PolyfillError::internal_simple("Failed to acquire book lock"))?;
722
723 books
724 .get(token_id)
725 .map(|book| book.snapshot()) // Create a snapshot copy
726 .ok_or_else(|| {
727 PolyfillError::market_data(
728 format!("No book found for token: {}", token_id),
729 crate::errors::MarketDataErrorKind::TokenNotFound,
730 )
731 })
732 }
733
734 /// Get all available books
735 /// Returns snapshots of every book we're currently tracking
736 pub fn get_all_books(&self) -> Result<Vec<crate::types::OrderBook>> {
737 let books = self
738 .books
739 .read()
740 .map_err(|_| PolyfillError::internal_simple("Failed to acquire book lock"))?;
741
742 Ok(books.values().map(|book| book.snapshot()).collect())
743 }
744
745 /// Remove stale books
746 /// Cleans up books that haven't been updated recently (probably disconnected)
747 /// This prevents memory leaks from accumulating dead books
748 pub fn cleanup_stale_books(&self, max_age: std::time::Duration) -> Result<usize> {
749 let mut books = self
750 .books
751 .write()
752 .map_err(|_| PolyfillError::internal_simple("Failed to acquire book lock"))?;
753
754 let initial_count = books.len();
755 books.retain(|_, book| !book.is_stale(max_age)); // Keep only non-stale books
756 let removed = initial_count - books.len();
757
758 if removed > 0 {
759 debug!("Removed {} stale order books", removed);
760 }
761
762 Ok(removed)
763 }
764}
765
766/// Order book analytics and statistics
767/// Provides a summary view of the book's health and characteristics
768#[derive(Debug, Clone)]
769pub struct BookAnalytics {
770 pub token_id: String,
771 pub timestamp: chrono::DateTime<Utc>,
772 pub bid_count: usize, // How many different bid price levels
773 pub ask_count: usize, // How many different ask price levels
774 pub total_bid_size: Decimal, // Total size of all bids combined
775 pub total_ask_size: Decimal, // Total size of all asks combined
776 pub spread: Option<Decimal>, // Current spread (ask - bid)
777 pub spread_pct: Option<Decimal>, // Spread as percentage
778 pub mid_price: Option<Decimal>, // Current mid price
779 pub volatility: Option<Decimal>, // Price volatility (if calculated)
780}
781
782impl OrderBook {
783 /// Calculate analytics for this book
784 /// Gives you a quick health check of the market
785 pub fn analytics(&self) -> BookAnalytics {
786 let bid_count = self.bids.len();
787 let ask_count = self.asks.len();
788 // Sum up all bid/ask sizes, converting from fixed-point back to Decimal
789 let total_bid_size_units: i64 = self.bids.values().sum();
790 let total_ask_size_units: i64 = self.asks.values().sum();
791 let total_bid_size = qty_to_decimal(total_bid_size_units);
792 let total_ask_size = qty_to_decimal(total_ask_size_units);
793
794 BookAnalytics {
795 token_id: self.token_id.clone(),
796 timestamp: self.timestamp,
797 bid_count,
798 ask_count,
799 total_bid_size,
800 total_ask_size,
801 spread: self.spread(),
802 spread_pct: self.spread_pct(),
803 mid_price: self.mid_price(),
804 volatility: self.calculate_volatility(),
805 }
806 }
807
808 /// Calculate price volatility (simplified)
809 /// This is a placeholder - real volatility needs historical price data
810 fn calculate_volatility(&self) -> Option<Decimal> {
811 // This is a simplified volatility calculation
812 // In a real implementation, you'd want to track price history over time
813 // and calculate standard deviation of price changes
814 None
815 }
816}
817
818#[cfg(test)]
819mod tests {
820 use super::*;
821 use rust_decimal_macros::dec;
822 use std::str::FromStr;
823 use std::time::Duration; // Convenient macro for creating Decimal literals
824
825 #[test]
826 fn test_order_book_creation() {
827 // Test that we can create a new empty order book
828 let book = OrderBook::new("test_token".to_string(), 10);
829 assert_eq!(book.token_id, "test_token");
830 assert_eq!(book.bids.len(), 0); // Should start empty
831 assert_eq!(book.asks.len(), 0); // Should start empty
832 }
833
834 #[test]
835 fn test_apply_delta() {
836 // Test that we can apply order book updates
837 let mut book = OrderBook::new("test_token".to_string(), 10);
838
839 // Create a buy order at $0.50 for 100 tokens
840 let delta = OrderDelta {
841 token_id: "test_token".to_string(),
842 timestamp: Utc::now(),
843 side: Side::BUY,
844 price: dec!(0.5),
845 size: dec!(100),
846 sequence: 1,
847 };
848
849 book.apply_delta(delta).unwrap();
850 assert_eq!(book.sequence, 1); // Sequence should update
851 assert_eq!(book.best_bid().unwrap().price, dec!(0.5)); // Should be our bid
852 assert_eq!(book.best_bid().unwrap().size, dec!(100)); // Should be our size
853 }
854
855 #[test]
856 fn test_spread_calculation() {
857 // Test that we can calculate the spread between bid and ask
858 let mut book = OrderBook::new("test_token".to_string(), 10);
859
860 // Add a bid at $0.50
861 book.apply_delta(OrderDelta {
862 token_id: "test_token".to_string(),
863 timestamp: Utc::now(),
864 side: Side::BUY,
865 price: dec!(0.5),
866 size: dec!(100),
867 sequence: 1,
868 })
869 .unwrap();
870
871 // Add an ask at $0.52
872 book.apply_delta(OrderDelta {
873 token_id: "test_token".to_string(),
874 timestamp: Utc::now(),
875 side: Side::SELL,
876 price: dec!(0.52),
877 size: dec!(100),
878 sequence: 2,
879 })
880 .unwrap();
881
882 let spread = book.spread().unwrap();
883 assert_eq!(spread, dec!(0.02)); // $0.52 - $0.50 = $0.02
884 }
885
886 #[test]
887 fn test_market_impact() {
888 // Test market impact calculation for a large order
889 let mut book = OrderBook::new("test_token".to_string(), 10);
890
891 // Add multiple ask levels (people selling at different prices)
892 // $0.50 for 100 tokens, $0.51 for 100 tokens, $0.52 for 100 tokens
893 for (i, price) in [dec!(0.50), dec!(0.51), dec!(0.52)].iter().enumerate() {
894 book.apply_delta(OrderDelta {
895 token_id: "test_token".to_string(),
896 timestamp: Utc::now(),
897 side: Side::SELL,
898 price: *price,
899 size: dec!(100),
900 sequence: i as u64 + 1,
901 })
902 .unwrap();
903 }
904
905 // Try to buy 150 tokens (will need to hit multiple price levels)
906 let impact = book.calculate_market_impact(Side::BUY, dec!(150)).unwrap();
907 assert!(impact.average_price > dec!(0.50)); // Should be worse than best price
908 assert!(impact.average_price < dec!(0.51)); // But not as bad as second level
909 }
910
911 #[test]
912 fn test_apply_bid_delta_legacy() {
913 let mut book = OrderBook::new("test_token".to_string(), 10);
914
915 // Test adding a bid
916 book.apply_bid_delta(
917 Decimal::from_str("0.75").unwrap(),
918 Decimal::from_str("100.0").unwrap(),
919 );
920
921 let best_bid = book.best_bid();
922 assert!(best_bid.is_some());
923 let bid = best_bid.unwrap();
924 assert_eq!(bid.price, Decimal::from_str("0.75").unwrap());
925 assert_eq!(bid.size, Decimal::from_str("100.0").unwrap());
926
927 // Test updating the bid
928 book.apply_bid_delta(
929 Decimal::from_str("0.75").unwrap(),
930 Decimal::from_str("150.0").unwrap(),
931 );
932 let updated_bid = book.best_bid().unwrap();
933 assert_eq!(updated_bid.size, Decimal::from_str("150.0").unwrap());
934
935 // Test removing the bid
936 book.apply_bid_delta(Decimal::from_str("0.75").unwrap(), Decimal::ZERO);
937 assert!(book.best_bid().is_none());
938 }
939
940 #[test]
941 fn test_apply_ask_delta_legacy() {
942 let mut book = OrderBook::new("test_token".to_string(), 10);
943
944 // Test adding an ask
945 book.apply_ask_delta(
946 Decimal::from_str("0.76").unwrap(),
947 Decimal::from_str("50.0").unwrap(),
948 );
949
950 let best_ask = book.best_ask();
951 assert!(best_ask.is_some());
952 let ask = best_ask.unwrap();
953 assert_eq!(ask.price, Decimal::from_str("0.76").unwrap());
954 assert_eq!(ask.size, Decimal::from_str("50.0").unwrap());
955
956 // Test updating the ask
957 book.apply_ask_delta(
958 Decimal::from_str("0.76").unwrap(),
959 Decimal::from_str("75.0").unwrap(),
960 );
961 let updated_ask = book.best_ask().unwrap();
962 assert_eq!(updated_ask.size, Decimal::from_str("75.0").unwrap());
963
964 // Test removing the ask
965 book.apply_ask_delta(Decimal::from_str("0.76").unwrap(), Decimal::ZERO);
966 assert!(book.best_ask().is_none());
967 }
968
969 #[test]
970 fn test_liquidity_analysis() {
971 let mut book = OrderBook::new("test_token".to_string(), 10);
972
973 // Build order book using legacy methods
974 book.apply_bid_delta(
975 Decimal::from_str("0.75").unwrap(),
976 Decimal::from_str("100.0").unwrap(),
977 );
978 book.apply_bid_delta(
979 Decimal::from_str("0.74").unwrap(),
980 Decimal::from_str("50.0").unwrap(),
981 );
982 book.apply_ask_delta(
983 Decimal::from_str("0.76").unwrap(),
984 Decimal::from_str("80.0").unwrap(),
985 );
986 book.apply_ask_delta(
987 Decimal::from_str("0.77").unwrap(),
988 Decimal::from_str("120.0").unwrap(),
989 );
990
991 // Test liquidity at specific price - when buying, we look at ask liquidity
992 let buy_liquidity = book.liquidity_at_price(Decimal::from_str("0.76").unwrap(), Side::BUY);
993 assert_eq!(buy_liquidity, Decimal::from_str("80.0").unwrap());
994
995 // Test liquidity at specific price - when selling, we look at bid liquidity
996 let sell_liquidity =
997 book.liquidity_at_price(Decimal::from_str("0.75").unwrap(), Side::SELL);
998 assert_eq!(sell_liquidity, Decimal::from_str("100.0").unwrap());
999
1000 // Test liquidity in range - when buying, we look at ask liquidity in range
1001 let buy_range_liquidity = book.liquidity_in_range(
1002 Decimal::from_str("0.74").unwrap(),
1003 Decimal::from_str("0.77").unwrap(),
1004 Side::BUY,
1005 );
1006 // Should include ask liquidity: 80 (0.76 ask) + 120 (0.77 ask) = 200
1007 assert_eq!(buy_range_liquidity, Decimal::from_str("200.0").unwrap());
1008
1009 // Test liquidity in range - when selling, we look at bid liquidity in range
1010 let sell_range_liquidity = book.liquidity_in_range(
1011 Decimal::from_str("0.74").unwrap(),
1012 Decimal::from_str("0.77").unwrap(),
1013 Side::SELL,
1014 );
1015 // Should include bid liquidity: 50 (0.74 bid) + 100 (0.75 bid) = 150
1016 assert_eq!(sell_range_liquidity, Decimal::from_str("150.0").unwrap());
1017 }
1018
1019 #[test]
1020 fn test_book_validation() {
1021 let mut book = OrderBook::new("test_token".to_string(), 10);
1022
1023 // Empty book should be valid
1024 assert!(book.is_valid());
1025
1026 // Add normal levels
1027 book.apply_bid_delta(
1028 Decimal::from_str("0.75").unwrap(),
1029 Decimal::from_str("100.0").unwrap(),
1030 );
1031 book.apply_ask_delta(
1032 Decimal::from_str("0.76").unwrap(),
1033 Decimal::from_str("80.0").unwrap(),
1034 );
1035 assert!(book.is_valid());
1036
1037 // Create crossed book (invalid) - bid higher than ask
1038 book.apply_bid_delta(
1039 Decimal::from_str("0.77").unwrap(),
1040 Decimal::from_str("50.0").unwrap(),
1041 );
1042 assert!(!book.is_valid());
1043 }
1044
1045 #[test]
1046 fn test_book_staleness() {
1047 let mut book = OrderBook::new("test_token".to_string(), 10);
1048
1049 // Fresh book should not be stale
1050 assert!(!book.is_stale(Duration::from_secs(60))); // 60 second threshold
1051
1052 // Add some data
1053 book.apply_bid_delta(
1054 Decimal::from_str("0.75").unwrap(),
1055 Decimal::from_str("100.0").unwrap(),
1056 );
1057 assert!(!book.is_stale(Duration::from_secs(60)));
1058
1059 // Note: We can't easily test actual staleness without manipulating time,
1060 // but we can test the method exists and works with fresh data
1061 }
1062
1063 #[test]
1064 fn test_depth_management() {
1065 let mut book = OrderBook::new("test_token".to_string(), 3); // Only 3 levels
1066
1067 // Add multiple levels
1068 book.apply_bid_delta(
1069 Decimal::from_str("0.75").unwrap(),
1070 Decimal::from_str("100.0").unwrap(),
1071 );
1072 book.apply_bid_delta(
1073 Decimal::from_str("0.74").unwrap(),
1074 Decimal::from_str("50.0").unwrap(),
1075 );
1076 book.apply_bid_delta(
1077 Decimal::from_str("0.73").unwrap(),
1078 Decimal::from_str("20.0").unwrap(),
1079 );
1080
1081 book.apply_ask_delta(
1082 Decimal::from_str("0.76").unwrap(),
1083 Decimal::from_str("80.0").unwrap(),
1084 );
1085 book.apply_ask_delta(
1086 Decimal::from_str("0.77").unwrap(),
1087 Decimal::from_str("40.0").unwrap(),
1088 );
1089 book.apply_ask_delta(
1090 Decimal::from_str("0.78").unwrap(),
1091 Decimal::from_str("30.0").unwrap(),
1092 );
1093
1094 // Should have levels on each side
1095 let bids = book.bids(Some(3));
1096 let asks = book.asks(Some(3));
1097
1098 assert!(bids.len() <= 3);
1099 assert!(asks.len() <= 3);
1100
1101 // Best levels should be there
1102 assert_eq!(
1103 book.best_bid().unwrap().price,
1104 Decimal::from_str("0.75").unwrap()
1105 );
1106 assert_eq!(
1107 book.best_ask().unwrap().price,
1108 Decimal::from_str("0.76").unwrap()
1109 );
1110 }
1111
1112 #[test]
1113 fn test_fast_operations() {
1114 let mut book = OrderBook::new("test_token".to_string(), 10);
1115
1116 // Test using legacy methods which call fast operations internally
1117 book.apply_bid_delta(
1118 Decimal::from_str("0.75").unwrap(),
1119 Decimal::from_str("100.0").unwrap(),
1120 );
1121 book.apply_ask_delta(
1122 Decimal::from_str("0.76").unwrap(),
1123 Decimal::from_str("80.0").unwrap(),
1124 );
1125
1126 let best_bid_fast = book.best_bid_fast();
1127 let best_ask_fast = book.best_ask_fast();
1128
1129 assert!(best_bid_fast.is_some());
1130 assert!(best_ask_fast.is_some());
1131
1132 // Test fast spread and mid price
1133 let spread_fast = book.spread_fast();
1134 let mid_fast = book.mid_price_fast();
1135
1136 assert!(spread_fast.is_some()); // Should have a spread
1137 assert!(mid_fast.is_some()); // Should have a mid price
1138 }
1139}