Skip to main content

nautilus_model/data/
depth.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! An `OrderBookDepth10` aggregated top-of-book data type with a fixed depth of 10 levels per side.
17
18use std::{collections::HashMap, fmt::Display};
19
20use indexmap::IndexMap;
21use nautilus_core::{UnixNanos, serialization::Serializable};
22use serde::{Deserialize, Serialize};
23
24use super::{HasTsInit, order::BookOrder};
25use crate::{identifiers::InstrumentId, types::fixed::FIXED_SIZE_BINARY};
26
27pub const DEPTH10_LEN: usize = 10;
28
29/// Represents an aggregated order book update with a fixed depth of 10 levels per side.
30///
31/// This structure is specifically designed for scenarios where a snapshot of the top 10 bid and
32/// ask levels in an order book is needed. It differs from `OrderBookDelta` or `OrderBookDeltas`
33/// in its fixed-depth nature and is optimized for cases where a full depth representation is not
34/// required or practical.
35///
36/// Note: This type is not compatible with `OrderBookDelta` or `OrderBookDeltas` due to
37/// its specialized structure and limited depth use case.
38///
39/// Per-level [`BookOrder::order_id`] values are non-semantic for this aggregated MBP data.
40/// Parquet catalog decoding canonicalizes them to zero.
41#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
42#[cfg_attr(
43    feature = "python",
44    pyo3::pyclass(module = "nautilus_trader.model", from_py_object)
45)]
46#[cfg_attr(
47    feature = "python",
48    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.model")
49)]
50pub struct OrderBookDepth10 {
51    /// The instrument ID for the book.
52    pub instrument_id: InstrumentId,
53    /// The bid orders for the depth update.
54    pub bids: [BookOrder; DEPTH10_LEN],
55    /// The ask orders for the depth update.
56    pub asks: [BookOrder; DEPTH10_LEN],
57    /// The count of bid orders per level for the depth update.
58    pub bid_counts: [u32; DEPTH10_LEN],
59    /// The count of ask orders per level for the depth update.
60    pub ask_counts: [u32; DEPTH10_LEN],
61    /// The record flags bit field, indicating event end and data information.
62    pub flags: u8,
63    /// The message sequence number assigned at the venue.
64    pub sequence: u64,
65    /// UNIX timestamp (nanoseconds) when the book event occurred.
66    pub ts_event: UnixNanos,
67    /// UNIX timestamp (nanoseconds) when the instance was created.
68    pub ts_init: UnixNanos,
69}
70
71impl OrderBookDepth10 {
72    /// Creates a new [`OrderBookDepth10`] instance.
73    #[expect(clippy::too_many_arguments)]
74    #[must_use]
75    pub fn new(
76        instrument_id: InstrumentId,
77        bids: [BookOrder; DEPTH10_LEN],
78        asks: [BookOrder; DEPTH10_LEN],
79        bid_counts: [u32; DEPTH10_LEN],
80        ask_counts: [u32; DEPTH10_LEN],
81        flags: u8,
82        sequence: u64,
83        ts_event: UnixNanos,
84        ts_init: UnixNanos,
85    ) -> Self {
86        Self {
87            instrument_id,
88            bids,
89            asks,
90            bid_counts,
91            ask_counts,
92            flags,
93            sequence,
94            ts_event,
95            ts_init,
96        }
97    }
98
99    /// Returns the metadata for the type, for use with serialization formats.
100    #[must_use]
101    pub fn get_metadata(
102        instrument_id: &InstrumentId,
103        price_precision: u8,
104        size_precision: u8,
105    ) -> HashMap<String, String> {
106        let mut metadata = HashMap::new();
107        metadata.insert("instrument_id".to_string(), instrument_id.to_string());
108        metadata.insert("price_precision".to_string(), price_precision.to_string());
109        metadata.insert("size_precision".to_string(), size_precision.to_string());
110        metadata
111    }
112
113    /// Returns the field map for the type, for use with Arrow schemas.
114    #[must_use]
115    pub fn get_fields() -> IndexMap<String, String> {
116        let mut metadata = IndexMap::new();
117        metadata.insert("bid_price_0".to_string(), FIXED_SIZE_BINARY.to_string());
118        metadata.insert("bid_price_1".to_string(), FIXED_SIZE_BINARY.to_string());
119        metadata.insert("bid_price_2".to_string(), FIXED_SIZE_BINARY.to_string());
120        metadata.insert("bid_price_3".to_string(), FIXED_SIZE_BINARY.to_string());
121        metadata.insert("bid_price_4".to_string(), FIXED_SIZE_BINARY.to_string());
122        metadata.insert("bid_price_5".to_string(), FIXED_SIZE_BINARY.to_string());
123        metadata.insert("bid_price_6".to_string(), FIXED_SIZE_BINARY.to_string());
124        metadata.insert("bid_price_7".to_string(), FIXED_SIZE_BINARY.to_string());
125        metadata.insert("bid_price_8".to_string(), FIXED_SIZE_BINARY.to_string());
126        metadata.insert("bid_price_9".to_string(), FIXED_SIZE_BINARY.to_string());
127        metadata.insert("ask_price_0".to_string(), FIXED_SIZE_BINARY.to_string());
128        metadata.insert("ask_price_1".to_string(), FIXED_SIZE_BINARY.to_string());
129        metadata.insert("ask_price_2".to_string(), FIXED_SIZE_BINARY.to_string());
130        metadata.insert("ask_price_3".to_string(), FIXED_SIZE_BINARY.to_string());
131        metadata.insert("ask_price_4".to_string(), FIXED_SIZE_BINARY.to_string());
132        metadata.insert("ask_price_5".to_string(), FIXED_SIZE_BINARY.to_string());
133        metadata.insert("ask_price_6".to_string(), FIXED_SIZE_BINARY.to_string());
134        metadata.insert("ask_price_7".to_string(), FIXED_SIZE_BINARY.to_string());
135        metadata.insert("ask_price_8".to_string(), FIXED_SIZE_BINARY.to_string());
136        metadata.insert("ask_price_9".to_string(), FIXED_SIZE_BINARY.to_string());
137        metadata.insert("bid_size_0".to_string(), FIXED_SIZE_BINARY.to_string());
138        metadata.insert("bid_size_1".to_string(), FIXED_SIZE_BINARY.to_string());
139        metadata.insert("bid_size_2".to_string(), FIXED_SIZE_BINARY.to_string());
140        metadata.insert("bid_size_3".to_string(), FIXED_SIZE_BINARY.to_string());
141        metadata.insert("bid_size_4".to_string(), FIXED_SIZE_BINARY.to_string());
142        metadata.insert("bid_size_5".to_string(), FIXED_SIZE_BINARY.to_string());
143        metadata.insert("bid_size_6".to_string(), FIXED_SIZE_BINARY.to_string());
144        metadata.insert("bid_size_7".to_string(), FIXED_SIZE_BINARY.to_string());
145        metadata.insert("bid_size_8".to_string(), FIXED_SIZE_BINARY.to_string());
146        metadata.insert("bid_size_9".to_string(), FIXED_SIZE_BINARY.to_string());
147        metadata.insert("ask_size_0".to_string(), FIXED_SIZE_BINARY.to_string());
148        metadata.insert("ask_size_1".to_string(), FIXED_SIZE_BINARY.to_string());
149        metadata.insert("ask_size_2".to_string(), FIXED_SIZE_BINARY.to_string());
150        metadata.insert("ask_size_3".to_string(), FIXED_SIZE_BINARY.to_string());
151        metadata.insert("ask_size_4".to_string(), FIXED_SIZE_BINARY.to_string());
152        metadata.insert("ask_size_5".to_string(), FIXED_SIZE_BINARY.to_string());
153        metadata.insert("ask_size_6".to_string(), FIXED_SIZE_BINARY.to_string());
154        metadata.insert("ask_size_7".to_string(), FIXED_SIZE_BINARY.to_string());
155        metadata.insert("ask_size_8".to_string(), FIXED_SIZE_BINARY.to_string());
156        metadata.insert("ask_size_9".to_string(), FIXED_SIZE_BINARY.to_string());
157        metadata.insert("bid_count_0".to_string(), "UInt32".to_string());
158        metadata.insert("bid_count_1".to_string(), "UInt32".to_string());
159        metadata.insert("bid_count_2".to_string(), "UInt32".to_string());
160        metadata.insert("bid_count_3".to_string(), "UInt32".to_string());
161        metadata.insert("bid_count_4".to_string(), "UInt32".to_string());
162        metadata.insert("bid_count_5".to_string(), "UInt32".to_string());
163        metadata.insert("bid_count_6".to_string(), "UInt32".to_string());
164        metadata.insert("bid_count_7".to_string(), "UInt32".to_string());
165        metadata.insert("bid_count_8".to_string(), "UInt32".to_string());
166        metadata.insert("bid_count_9".to_string(), "UInt32".to_string());
167        metadata.insert("ask_count_0".to_string(), "UInt32".to_string());
168        metadata.insert("ask_count_1".to_string(), "UInt32".to_string());
169        metadata.insert("ask_count_2".to_string(), "UInt32".to_string());
170        metadata.insert("ask_count_3".to_string(), "UInt32".to_string());
171        metadata.insert("ask_count_4".to_string(), "UInt32".to_string());
172        metadata.insert("ask_count_5".to_string(), "UInt32".to_string());
173        metadata.insert("ask_count_6".to_string(), "UInt32".to_string());
174        metadata.insert("ask_count_7".to_string(), "UInt32".to_string());
175        metadata.insert("ask_count_8".to_string(), "UInt32".to_string());
176        metadata.insert("ask_count_9".to_string(), "UInt32".to_string());
177        metadata.insert("flags".to_string(), "UInt8".to_string());
178        metadata.insert("sequence".to_string(), "UInt64".to_string());
179        metadata.insert("ts_event".to_string(), "UInt64".to_string());
180        metadata.insert("ts_init".to_string(), "UInt64".to_string());
181        metadata
182    }
183}
184
185// TODO: Exact format for Debug and Display TBD
186impl Display for OrderBookDepth10 {
187    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
188        write!(
189            f,
190            "{},flags={},sequence={},ts_event={},ts_init={}",
191            self.instrument_id, self.flags, self.sequence, self.ts_event, self.ts_init
192        )
193    }
194}
195
196impl Serializable for OrderBookDepth10 {}
197
198impl HasTsInit for OrderBookDepth10 {
199    fn ts_init(&self) -> UnixNanos {
200        self.ts_init
201    }
202}
203
204#[cfg(test)]
205mod tests {
206    use std::{
207        collections::hash_map::DefaultHasher,
208        hash::{Hash, Hasher},
209    };
210
211    use rstest::rstest;
212    use serde_json;
213
214    use super::*;
215    use crate::{
216        data::{order::BookOrder, stubs::*},
217        enums::OrderSide,
218        types::{Price, Quantity},
219    };
220
221    fn create_test_book_order(
222        side: OrderSide,
223        price: &str,
224        size: &str,
225        order_id: u64,
226    ) -> BookOrder {
227        BookOrder::new(side, Price::from(price), Quantity::from(size), order_id)
228    }
229
230    fn create_test_depth10() -> OrderBookDepth10 {
231        let instrument_id = InstrumentId::from("EURUSD.SIM");
232
233        // Create bid orders (descending prices)
234        let bids = [
235            create_test_book_order(OrderSide::Buy, "1.0500", "100000", 1),
236            create_test_book_order(OrderSide::Buy, "1.0499", "150000", 2),
237            create_test_book_order(OrderSide::Buy, "1.0498", "200000", 3),
238            create_test_book_order(OrderSide::Buy, "1.0497", "125000", 4),
239            create_test_book_order(OrderSide::Buy, "1.0496", "175000", 5),
240            create_test_book_order(OrderSide::Buy, "1.0495", "100000", 6),
241            create_test_book_order(OrderSide::Buy, "1.0494", "225000", 7),
242            create_test_book_order(OrderSide::Buy, "1.0493", "150000", 8),
243            create_test_book_order(OrderSide::Buy, "1.0492", "300000", 9),
244            create_test_book_order(OrderSide::Buy, "1.0491", "175000", 10),
245        ];
246
247        // Create ask orders (ascending prices)
248        let asks = [
249            create_test_book_order(OrderSide::Sell, "1.0501", "100000", 11),
250            create_test_book_order(OrderSide::Sell, "1.0502", "125000", 12),
251            create_test_book_order(OrderSide::Sell, "1.0503", "150000", 13),
252            create_test_book_order(OrderSide::Sell, "1.0504", "175000", 14),
253            create_test_book_order(OrderSide::Sell, "1.0505", "200000", 15),
254            create_test_book_order(OrderSide::Sell, "1.0506", "100000", 16),
255            create_test_book_order(OrderSide::Sell, "1.0507", "250000", 17),
256            create_test_book_order(OrderSide::Sell, "1.0508", "125000", 18),
257            create_test_book_order(OrderSide::Sell, "1.0509", "300000", 19),
258            create_test_book_order(OrderSide::Sell, "1.0510", "175000", 20),
259        ];
260
261        let bid_counts = [1, 2, 1, 3, 1, 2, 1, 4, 1, 2];
262        let ask_counts = [2, 1, 3, 1, 2, 1, 4, 1, 2, 3];
263
264        OrderBookDepth10::new(
265            instrument_id,
266            bids,
267            asks,
268            bid_counts,
269            ask_counts,
270            32,                             // flags
271            12345,                          // sequence
272            UnixNanos::from(1_000_000_000), // ts_event
273            UnixNanos::from(2_000_000_000), // ts_init
274        )
275    }
276
277    fn create_empty_depth10() -> OrderBookDepth10 {
278        let instrument_id = InstrumentId::from("EMPTY.TEST");
279
280        // Create empty orders with zero prices and quantities
281        let empty_bid = create_test_book_order(OrderSide::Buy, "0.0", "0", 0);
282        let empty_ask = create_test_book_order(OrderSide::Sell, "0.0", "0", 0);
283
284        OrderBookDepth10::new(
285            instrument_id,
286            [empty_bid; DEPTH10_LEN],
287            [empty_ask; DEPTH10_LEN],
288            [0; DEPTH10_LEN],
289            [0; DEPTH10_LEN],
290            0,
291            0,
292            UnixNanos::from(0),
293            UnixNanos::from(0),
294        )
295    }
296
297    #[rstest]
298    fn test_order_book_depth10_new() {
299        let depth = create_test_depth10();
300
301        assert_eq!(depth.instrument_id, InstrumentId::from("EURUSD.SIM"));
302        assert_eq!(depth.bids.len(), DEPTH10_LEN);
303        assert_eq!(depth.asks.len(), DEPTH10_LEN);
304        assert_eq!(depth.bid_counts.len(), DEPTH10_LEN);
305        assert_eq!(depth.ask_counts.len(), DEPTH10_LEN);
306        assert_eq!(depth.flags, 32);
307        assert_eq!(depth.sequence, 12345);
308        assert_eq!(depth.ts_event, UnixNanos::from(1_000_000_000));
309        assert_eq!(depth.ts_init, UnixNanos::from(2_000_000_000));
310    }
311
312    #[rstest]
313    fn test_order_book_depth10_new_with_all_parameters() {
314        let instrument_id = InstrumentId::from("GBPUSD.SIM");
315        let bid = create_test_book_order(OrderSide::Buy, "1.2500", "50000", 1);
316        let ask = create_test_book_order(OrderSide::Sell, "1.2501", "75000", 2);
317        let flags = 64u8;
318        let sequence = 999u64;
319        let ts_event = UnixNanos::from(5_000_000_000);
320        let ts_init = UnixNanos::from(6_000_000_000);
321
322        let depth = OrderBookDepth10::new(
323            instrument_id,
324            [bid; DEPTH10_LEN],
325            [ask; DEPTH10_LEN],
326            [5; DEPTH10_LEN],
327            [3; DEPTH10_LEN],
328            flags,
329            sequence,
330            ts_event,
331            ts_init,
332        );
333
334        assert_eq!(depth.instrument_id, instrument_id);
335        assert_eq!(depth.bids[0], bid);
336        assert_eq!(depth.asks[0], ask);
337        assert_eq!(depth.bid_counts[0], 5);
338        assert_eq!(depth.ask_counts[0], 3);
339        assert_eq!(depth.flags, flags);
340        assert_eq!(depth.sequence, sequence);
341        assert_eq!(depth.ts_event, ts_event);
342        assert_eq!(depth.ts_init, ts_init);
343    }
344
345    #[rstest]
346    fn test_order_book_depth10_fixed_array_sizes() {
347        let depth = create_test_depth10();
348
349        // Verify arrays are exactly DEPTH10_LEN (10)
350        assert_eq!(depth.bids.len(), 10);
351        assert_eq!(depth.asks.len(), 10);
352        assert_eq!(depth.bid_counts.len(), 10);
353        assert_eq!(depth.ask_counts.len(), 10);
354
355        // Verify DEPTH10_LEN constant
356        assert_eq!(DEPTH10_LEN, 10);
357    }
358
359    #[rstest]
360    fn test_order_book_depth10_array_indexing() {
361        let depth = create_test_depth10();
362
363        // Test first and last elements of each array
364        assert_eq!(depth.bids[0].price, Price::from("1.0500"));
365        assert_eq!(depth.bids[9].price, Price::from("1.0491"));
366        assert_eq!(depth.asks[0].price, Price::from("1.0501"));
367        assert_eq!(depth.asks[9].price, Price::from("1.0510"));
368        assert_eq!(depth.bid_counts[0], 1);
369        assert_eq!(depth.bid_counts[9], 2);
370        assert_eq!(depth.ask_counts[0], 2);
371        assert_eq!(depth.ask_counts[9], 3);
372    }
373
374    #[rstest]
375    fn test_order_book_depth10_bid_ask_ordering() {
376        let depth = create_test_depth10();
377
378        // Verify bid prices are in descending order (highest to lowest)
379        for i in 0..9 {
380            assert!(
381                depth.bids[i].price >= depth.bids[i + 1].price,
382                "Bid prices should be in descending order: {} >= {}",
383                depth.bids[i].price,
384                depth.bids[i + 1].price
385            );
386        }
387
388        // Verify ask prices are in ascending order (lowest to highest)
389        for i in 0..9 {
390            assert!(
391                depth.asks[i].price <= depth.asks[i + 1].price,
392                "Ask prices should be in ascending order: {} <= {}",
393                depth.asks[i].price,
394                depth.asks[i + 1].price
395            );
396        }
397
398        // Verify bid-ask spread (best bid < best ask)
399        assert!(
400            depth.bids[0].price < depth.asks[0].price,
401            "Best bid {} should be less than best ask {}",
402            depth.bids[0].price,
403            depth.asks[0].price
404        );
405    }
406
407    #[rstest]
408    fn test_order_book_depth10_clone() {
409        let depth1 = create_test_depth10();
410        let depth2 = depth1;
411
412        assert_eq!(depth1.instrument_id, depth2.instrument_id);
413        assert_eq!(depth1.bids, depth2.bids);
414        assert_eq!(depth1.asks, depth2.asks);
415        assert_eq!(depth1.bid_counts, depth2.bid_counts);
416        assert_eq!(depth1.ask_counts, depth2.ask_counts);
417        assert_eq!(depth1.flags, depth2.flags);
418        assert_eq!(depth1.sequence, depth2.sequence);
419        assert_eq!(depth1.ts_event, depth2.ts_event);
420        assert_eq!(depth1.ts_init, depth2.ts_init);
421    }
422
423    #[rstest]
424    fn test_order_book_depth10_copy() {
425        let depth1 = create_test_depth10();
426        let depth2 = depth1;
427
428        // Verify Copy trait by modifying one and ensuring the other is unchanged
429        // Since we're using Copy, this should work without explicit clone
430        assert_eq!(depth1, depth2);
431    }
432
433    #[rstest]
434    fn test_order_book_depth10_debug() {
435        let depth = create_test_depth10();
436        let debug_str = format!("{depth:?}");
437
438        assert!(debug_str.contains("OrderBookDepth10"));
439        assert!(debug_str.contains("EURUSD.SIM"));
440        assert!(debug_str.contains("flags: 32"));
441        assert!(debug_str.contains("sequence: 12345"));
442    }
443
444    #[rstest]
445    fn test_order_book_depth10_partial_eq() {
446        let depth1 = create_test_depth10();
447        let depth2 = create_test_depth10();
448        let depth3 = create_empty_depth10();
449
450        assert_eq!(depth1, depth2); // Same data
451        assert_ne!(depth1, depth3); // Different data
452        assert_ne!(depth2, depth3); // Different data
453    }
454
455    #[rstest]
456    fn test_order_book_depth10_eq_consistency() {
457        let depth1 = create_test_depth10();
458        let depth2 = create_test_depth10();
459
460        assert_eq!(depth1, depth2);
461        assert_eq!(depth2, depth1); // Symmetry
462        assert_eq!(depth1, depth1); // Reflexivity
463    }
464
465    #[rstest]
466    fn test_order_book_depth10_hash() {
467        let depth1 = create_test_depth10();
468        let depth2 = create_test_depth10();
469
470        let mut hasher1 = DefaultHasher::new();
471        let mut hasher2 = DefaultHasher::new();
472
473        depth1.hash(&mut hasher1);
474        depth2.hash(&mut hasher2);
475
476        assert_eq!(hasher1.finish(), hasher2.finish()); // Equal objects have equal hashes
477    }
478
479    #[rstest]
480    fn test_order_book_depth10_hash_different_objects() {
481        let depth1 = create_test_depth10();
482        let depth2 = create_empty_depth10();
483
484        let mut hasher1 = DefaultHasher::new();
485        let mut hasher2 = DefaultHasher::new();
486
487        depth1.hash(&mut hasher1);
488        depth2.hash(&mut hasher2);
489
490        assert_ne!(hasher1.finish(), hasher2.finish()); // Different objects should have different hashes
491    }
492
493    #[rstest]
494    fn test_order_book_depth10_display() {
495        let depth = create_test_depth10();
496        let display_str = format!("{depth}");
497
498        assert!(display_str.contains("EURUSD.SIM"));
499        assert!(display_str.contains("flags=32"));
500        assert!(display_str.contains("sequence=12345"));
501        assert!(display_str.contains("ts_event=1000000000"));
502        assert!(display_str.contains("ts_init=2000000000"));
503    }
504
505    #[rstest]
506    fn test_order_book_depth10_display_format() {
507        let depth = create_test_depth10();
508        let expected = "EURUSD.SIM,flags=32,sequence=12345,ts_event=1000000000,ts_init=2000000000";
509
510        assert_eq!(format!("{depth}"), expected);
511    }
512
513    #[rstest]
514    fn test_order_book_depth10_serialization() {
515        let depth = create_test_depth10();
516
517        // Test JSON serialization
518        let json = serde_json::to_string(&depth).unwrap();
519        let deserialized: OrderBookDepth10 = serde_json::from_str(&json).unwrap();
520
521        assert_eq!(depth, deserialized);
522    }
523
524    #[rstest]
525    fn test_order_book_depth10_serializable_trait() {
526        fn assert_serializable<T: Serializable>(_: &T) {}
527
528        let depth = create_test_depth10();
529
530        // Verify Serializable trait is implemented (compile-time check)
531        assert_serializable(&depth);
532    }
533
534    #[rstest]
535    fn test_order_book_depth10_has_ts_init() {
536        let depth = create_test_depth10();
537
538        assert_eq!(depth.ts_init(), UnixNanos::from(2_000_000_000));
539    }
540
541    #[rstest]
542    fn test_order_book_depth10_get_metadata() {
543        let instrument_id = InstrumentId::from("EURUSD.SIM");
544        let price_precision = 5u8;
545        let size_precision = 0u8;
546
547        let metadata =
548            OrderBookDepth10::get_metadata(&instrument_id, price_precision, size_precision);
549
550        assert_eq!(
551            metadata.get("instrument_id"),
552            Some(&"EURUSD.SIM".to_string())
553        );
554        assert_eq!(metadata.get("price_precision"), Some(&"5".to_string()));
555        assert_eq!(metadata.get("size_precision"), Some(&"0".to_string()));
556        assert_eq!(metadata.len(), 3);
557    }
558
559    #[rstest]
560    fn test_order_book_depth10_get_fields() {
561        let fields = OrderBookDepth10::get_fields();
562
563        // Verify all 10 bid and ask price fields
564        for i in 0..10 {
565            assert_eq!(
566                fields.get(&format!("bid_price_{i}")),
567                Some(&FIXED_SIZE_BINARY.to_string())
568            );
569            assert_eq!(
570                fields.get(&format!("ask_price_{i}")),
571                Some(&FIXED_SIZE_BINARY.to_string())
572            );
573        }
574
575        // Verify all 10 bid and ask size fields
576        for i in 0..10 {
577            assert_eq!(
578                fields.get(&format!("bid_size_{i}")),
579                Some(&FIXED_SIZE_BINARY.to_string())
580            );
581            assert_eq!(
582                fields.get(&format!("ask_size_{i}")),
583                Some(&FIXED_SIZE_BINARY.to_string())
584            );
585        }
586
587        // Verify all 10 bid and ask count fields
588        for i in 0..10 {
589            assert_eq!(
590                fields.get(&format!("bid_count_{i}")),
591                Some(&"UInt32".to_string())
592            );
593            assert_eq!(
594                fields.get(&format!("ask_count_{i}")),
595                Some(&"UInt32".to_string())
596            );
597        }
598
599        // Verify metadata fields
600        assert_eq!(fields.get("flags"), Some(&"UInt8".to_string()));
601        assert_eq!(fields.get("sequence"), Some(&"UInt64".to_string()));
602        assert_eq!(fields.get("ts_event"), Some(&"UInt64".to_string()));
603        assert_eq!(fields.get("ts_init"), Some(&"UInt64".to_string()));
604
605        // Verify total field count:
606        // 10 bid_price + 10 ask_price + 10 bid_size + 10 ask_size + 10 bid_count + 10 ask_count + 4 metadata = 64
607        assert_eq!(fields.len(), 64);
608    }
609
610    #[rstest]
611    fn test_order_book_depth10_get_fields_order() {
612        let fields = OrderBookDepth10::get_fields();
613        let keys: Vec<&String> = fields.keys().collect();
614
615        // Verify the ordering of fields matches expectations
616        assert_eq!(keys[0], "bid_price_0");
617        assert_eq!(keys[9], "bid_price_9");
618        assert_eq!(keys[10], "ask_price_0");
619        assert_eq!(keys[19], "ask_price_9");
620        assert_eq!(keys[20], "bid_size_0");
621        assert_eq!(keys[29], "bid_size_9");
622        assert_eq!(keys[30], "ask_size_0");
623        assert_eq!(keys[39], "ask_size_9");
624        assert_eq!(keys[40], "bid_count_0");
625        assert_eq!(keys[41], "bid_count_1");
626    }
627
628    #[rstest]
629    fn test_order_book_depth10_empty_values() {
630        let depth = create_empty_depth10();
631
632        assert_eq!(depth.instrument_id, InstrumentId::from("EMPTY.TEST"));
633        assert_eq!(depth.flags, 0);
634        assert_eq!(depth.sequence, 0);
635        assert_eq!(depth.ts_event, UnixNanos::from(0));
636        assert_eq!(depth.ts_init, UnixNanos::from(0));
637
638        // Verify all orders have zero prices and quantities
639        for bid in &depth.bids {
640            assert_eq!(bid.price, Price::from("0.0"));
641            assert_eq!(bid.size, Quantity::from("0"));
642            assert_eq!(bid.order_id, 0);
643        }
644
645        for ask in &depth.asks {
646            assert_eq!(ask.price, Price::from("0.0"));
647            assert_eq!(ask.size, Quantity::from("0"));
648            assert_eq!(ask.order_id, 0);
649        }
650
651        // Verify all counts are zero
652        for &count in &depth.bid_counts {
653            assert_eq!(count, 0);
654        }
655
656        for &count in &depth.ask_counts {
657            assert_eq!(count, 0);
658        }
659    }
660
661    #[rstest]
662    fn test_order_book_depth10_max_values() {
663        let instrument_id = InstrumentId::from("MAX.TEST");
664        let max_bid = create_test_book_order(OrderSide::Buy, "999999.99", "999999999", u64::MAX);
665        let max_ask = create_test_book_order(OrderSide::Sell, "1000000.00", "999999999", u64::MAX);
666
667        let depth = OrderBookDepth10::new(
668            instrument_id,
669            [max_bid; DEPTH10_LEN],
670            [max_ask; DEPTH10_LEN],
671            [u32::MAX; DEPTH10_LEN],
672            [u32::MAX; DEPTH10_LEN],
673            u8::MAX,
674            u64::MAX,
675            UnixNanos::from(u64::MAX),
676            UnixNanos::from(u64::MAX),
677        );
678
679        assert_eq!(depth.flags, u8::MAX);
680        assert_eq!(depth.sequence, u64::MAX);
681        assert_eq!(depth.ts_event, UnixNanos::from(u64::MAX));
682        assert_eq!(depth.ts_init, UnixNanos::from(u64::MAX));
683
684        for &count in &depth.bid_counts {
685            assert_eq!(count, u32::MAX);
686        }
687
688        for &count in &depth.ask_counts {
689            assert_eq!(count, u32::MAX);
690        }
691    }
692
693    #[rstest]
694    fn test_order_book_depth10_different_instruments() {
695        let instruments = [
696            "EURUSD.SIM",
697            "GBPUSD.SIM",
698            "USDJPY.SIM",
699            "AUDUSD.SIM",
700            "USDCHF.SIM",
701        ];
702
703        for instrument_str in &instruments {
704            let instrument_id = InstrumentId::from(*instrument_str);
705            let bid = create_test_book_order(OrderSide::Buy, "1.0000", "100000", 1);
706            let ask = create_test_book_order(OrderSide::Sell, "1.0001", "100000", 2);
707
708            let depth = OrderBookDepth10::new(
709                instrument_id,
710                [bid; DEPTH10_LEN],
711                [ask; DEPTH10_LEN],
712                [1; DEPTH10_LEN],
713                [1; DEPTH10_LEN],
714                0,
715                1,
716                UnixNanos::from(1_000_000_000),
717                UnixNanos::from(2_000_000_000),
718            );
719
720            assert_eq!(depth.instrument_id, instrument_id);
721            assert!(format!("{depth}").contains(instrument_str));
722        }
723    }
724
725    #[rstest]
726    fn test_order_book_depth10_realistic_forex_spread() {
727        let instrument_id = InstrumentId::from("EURUSD.SIM");
728
729        // Realistic EUR/USD spread with 0.1 pip spread
730        let best_bid = create_test_book_order(OrderSide::Buy, "1.08500", "1000000", 1);
731        let best_ask = create_test_book_order(OrderSide::Sell, "1.08501", "1000000", 2);
732
733        let depth = OrderBookDepth10::new(
734            instrument_id,
735            [best_bid; DEPTH10_LEN],
736            [best_ask; DEPTH10_LEN],
737            [5; DEPTH10_LEN], // Realistic order count
738            [3; DEPTH10_LEN],
739            16,                                         // Realistic flags
740            123_456,                                    // Realistic sequence
741            UnixNanos::from(1_672_531_200_000_000_000), // Jan 1, 2023 timestamp
742            UnixNanos::from(1_672_531_200_000_100_000),
743        );
744
745        assert_eq!(depth.bids[0].price, Price::from("1.08500"));
746        assert_eq!(depth.asks[0].price, Price::from("1.08501"));
747        assert!(depth.bids[0].price < depth.asks[0].price); // Positive spread
748
749        // Verify realistic quantities and counts
750        assert_eq!(depth.bids[0].size, Quantity::from("1000000"));
751        assert_eq!(depth.bid_counts[0], 5);
752        assert_eq!(depth.ask_counts[0], 3);
753    }
754
755    #[rstest]
756    fn test_order_book_depth10_with_stub(stub_depth10: OrderBookDepth10) {
757        let depth = stub_depth10;
758
759        assert_eq!(depth.instrument_id, InstrumentId::from("AAPL.XNAS"));
760        assert_eq!(depth.bids.len(), 10);
761        assert_eq!(depth.asks.len(), 10);
762        assert_eq!(depth.asks[9].price, Price::from("109.0"));
763        assert_eq!(depth.asks[0].price, Price::from("100.0"));
764        assert_eq!(depth.bids[0].price, Price::from("99.0"));
765        assert_eq!(depth.bids[9].price, Price::from("90.0"));
766        assert_eq!(depth.bid_counts.len(), 10);
767        assert_eq!(depth.ask_counts.len(), 10);
768        assert_eq!(depth.bid_counts[0], 1);
769        assert_eq!(depth.ask_counts[0], 1);
770        assert_eq!(depth.flags, 0);
771        assert_eq!(depth.sequence, 0);
772        assert_eq!(depth.ts_event, UnixNanos::from(1));
773        assert_eq!(depth.ts_init, UnixNanos::from(2));
774    }
775
776    #[rstest]
777    fn test_new(stub_depth10: OrderBookDepth10) {
778        let depth = stub_depth10;
779        let instrument_id = InstrumentId::from("AAPL.XNAS");
780        let flags = 0;
781        let sequence = 0;
782        let ts_event = 1;
783        let ts_init = 2;
784
785        assert_eq!(depth.instrument_id, instrument_id);
786        assert_eq!(depth.bids.len(), 10);
787        assert_eq!(depth.asks.len(), 10);
788        assert_eq!(depth.asks[9].price, Price::from("109.0"));
789        assert_eq!(depth.asks[0].price, Price::from("100.0"));
790        assert_eq!(depth.bids[0].price, Price::from("99.0"));
791        assert_eq!(depth.bids[9].price, Price::from("90.0"));
792        assert_eq!(depth.bid_counts.len(), 10);
793        assert_eq!(depth.ask_counts.len(), 10);
794        assert_eq!(depth.bid_counts[0], 1);
795        assert_eq!(depth.ask_counts[0], 1);
796        assert_eq!(depth.flags, flags);
797        assert_eq!(depth.sequence, sequence);
798        assert_eq!(depth.ts_event, ts_event);
799        assert_eq!(depth.ts_init, ts_init);
800    }
801
802    #[rstest]
803    fn test_display(stub_depth10: OrderBookDepth10) {
804        let depth = stub_depth10;
805        assert_eq!(
806            format!("{depth}"),
807            "AAPL.XNAS,flags=0,sequence=0,ts_event=1,ts_init=2".to_string()
808        );
809    }
810}