nanobook 0.9.2

Production-grade Rust execution infrastructure for automated trading: LOB engine, portfolio simulator, broker abstraction, risk engine
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
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
//! Exchange: The high-level API for order submission and management.
//!
//! This is the main entry point for users of the library. It wraps the
//! OrderBook and provides methods for submitting orders with proper
//! time-in-force handling.

#[cfg(feature = "event-log")]
use crate::event::Event;
use crate::{
    Order, OrderBook, OrderId, OrderStatus, Price, Quantity, Side, TimeInForce, Trade,
    error::ValidationError,
    result::{
        CancelError, CancelResult, ModifyError, ModifyResult, StopSubmitResult, SubmitResult,
    },
    snapshot::BookSnapshot,
    stop::{StopBook, StopOrder, StopStatus, TrailMethod},
};

/// The exchange: processes orders and maintains the order book.
///
/// This is the main interface for interacting with the limit order book.
/// It handles:
/// - Order submission with time-in-force semantics
/// - Order cancellation and modification
/// - Book snapshots for market data
/// - Trade history
/// - Event logging for replay
#[derive(Clone, Debug)]
pub struct Exchange {
    /// The underlying order book
    pub(crate) book: OrderBook,
    /// Complete trade history
    pub(crate) trades: Vec<Trade>,
    /// Stop order book
    pub(crate) stop_book: StopBook,
    /// Last trade price (for stop order triggers)
    pub(crate) last_trade_price: Option<Price>,
    /// Event log for replay (only with "event-log" feature)
    #[cfg(feature = "event-log")]
    pub(crate) events: Vec<crate::event::Event>,
}

impl Exchange {
    /// Create a new exchange with an empty order book.
    pub fn new() -> Self {
        Self {
            book: OrderBook::new(),
            trades: Vec::new(),
            stop_book: StopBook::new(),
            last_trade_price: None,
            #[cfg(feature = "event-log")]
            events: Vec::new(),
        }
    }

    // === Order Submission ===

    /// Submit a limit order.
    ///
    /// The order is matched against the opposite side of the book.
    /// Remaining quantity is handled according to time-in-force:
    /// - **GTC**: Rests on book until filled or cancelled
    /// - **IOC**: Cancelled (never rests)
    /// - **FOK**: If cannot fill entirely, order is rejected (no trades)
    pub fn submit_limit(
        &mut self,
        side: Side,
        price: Price,
        quantity: Quantity,
        tif: TimeInForce,
    ) -> SubmitResult {
        #[cfg(feature = "event-log")]
        self.events.push(Event::SubmitLimit {
            side,
            price,
            quantity,
            time_in_force: tif,
        });

        let result = self.submit_limit_internal(side, price, quantity, tif);
        if !result.trades.is_empty() {
            let last_price = result.trades.last().unwrap().price;
            self.last_trade_price = Some(last_price);
            self.process_trade_triggers();
        }
        result
    }

    /// Submit a market order.
    ///
    /// Market orders execute immediately at the best available prices.
    /// Any unfilled quantity is cancelled (IOC semantics).
    ///
    /// This is equivalent to a limit order at the worst possible price
    /// with IOC time-in-force.
    pub fn submit_market(&mut self, side: Side, quantity: Quantity) -> SubmitResult {
        #[cfg(feature = "event-log")]
        self.events.push(Event::SubmitMarket { side, quantity });

        // Market order = limit at worst price + IOC
        let price = match side {
            Side::Buy => Price::MAX,
            Side::Sell => Price::MIN,
        };
        let result = self.submit_limit_internal(side, price, quantity, TimeInForce::IOC);
        if !result.trades.is_empty() {
            let last_price = result.trades.last().unwrap().price;
            self.last_trade_price = Some(last_price);
            self.process_trade_triggers();
        }
        result
    }

    /// Submit a limit order with input validation.
    ///
    /// Returns `Err(ValidationError::ZeroQuantity)` if quantity is 0,
    /// or `Err(ValidationError::ZeroPrice)` if price is <= 0.
    pub fn try_submit_limit(
        &mut self,
        side: Side,
        price: Price,
        quantity: Quantity,
        tif: TimeInForce,
    ) -> Result<SubmitResult, ValidationError> {
        if quantity == 0 {
            return Err(ValidationError::ZeroQuantity);
        }
        if price.0 <= 0 {
            return Err(ValidationError::ZeroPrice);
        }
        Ok(self.submit_limit(side, price, quantity, tif))
    }

    /// Submit a market order with input validation.
    ///
    /// Returns `Err(ValidationError::ZeroQuantity)` if quantity is 0.
    pub fn try_submit_market(
        &mut self,
        side: Side,
        quantity: Quantity,
    ) -> Result<SubmitResult, ValidationError> {
        if quantity == 0 {
            return Err(ValidationError::ZeroQuantity);
        }
        Ok(self.submit_market(side, quantity))
    }

    /// Internal: submit limit order without recording event.
    pub(crate) fn submit_limit_internal(
        &mut self,
        side: Side,
        price: Price,
        quantity: Quantity,
        tif: TimeInForce,
    ) -> SubmitResult {
        // FOK: Check feasibility before doing anything
        if tif == TimeInForce::FOK && !self.book.can_fully_fill(side, price, quantity) {
            // Reject the order. We still consume an OrderId for consistency
            // (the caller gets a valid ID even for rejected orders).
            // Note: This creates gaps in the OrderId sequence for rejected FOKs,
            // and the order is not stored (get_order returns None).
            let order = self.book.create_order(side, price, quantity, tif);
            return SubmitResult {
                order_id: order.id,
                status: OrderStatus::Cancelled,
                trades: Vec::new(),
                filled_quantity: 0,
                resting_quantity: 0,
                cancelled_quantity: quantity,
            };
        }

        // Create the order
        let mut order = self.book.create_order(side, price, quantity, tif);
        let order_id = order.id;

        // Match against the book
        let match_result = self.book.match_order(&mut order);

        // Record trades
        self.trades.extend(match_result.trades.iter().cloned());

        let filled = order.filled_quantity;
        let remaining = order.remaining_quantity;

        // Handle remaining quantity based on TIF
        let (status, resting, cancelled) = if remaining == 0 {
            // Fully filled
            order.status = OrderStatus::Filled;
            self.book.orders.insert(order_id, order);
            (OrderStatus::Filled, 0, 0)
        } else if tif == TimeInForce::GTC {
            // Rest on book
            let status = if filled > 0 {
                OrderStatus::PartiallyFilled
            } else {
                OrderStatus::New
            };
            order.status = status;
            self.book.add_order(order);
            (status, remaining, 0)
        } else {
            // IOC/FOK: cancel remainder (FOK shouldn't reach here with remainder)
            let status = if filled > 0 {
                OrderStatus::PartiallyFilled
            } else {
                OrderStatus::Cancelled
            };
            order.status = status;
            self.book.orders.insert(order_id, order);
            (status, 0, remaining)
        };

        SubmitResult {
            order_id,
            status,
            trades: match_result.trades,
            filled_quantity: filled,
            resting_quantity: resting,
            cancelled_quantity: cancelled,
        }
    }

    // === Order Management ===

    /// Cancel an order.
    ///
    /// Returns the cancelled quantity if successful.
    pub fn cancel(&mut self, order_id: OrderId) -> CancelResult {
        #[cfg(feature = "event-log")]
        self.events.push(Event::Cancel { order_id });

        self.cancel_internal(order_id)
    }

    /// Internal: cancel without recording event.
    pub(crate) fn cancel_internal(&mut self, order_id: OrderId) -> CancelResult {
        // Check stop book first
        if self.stop_book.contains_pending(order_id) {
            if let Some(stop) = self.stop_book.get(order_id) {
                let qty = stop.quantity;
                self.stop_book.cancel(order_id);
                return CancelResult::success(qty);
            }
        }

        // Check if order exists in regular book
        let order = match self.book.get_order(order_id) {
            Some(o) => o,
            None => return CancelResult::failure(CancelError::OrderNotFound),
        };

        // Check if order is active
        if !order.is_active() {
            return CancelResult::failure(CancelError::OrderNotActive);
        }

        // Cancel it
        match self.book.cancel_order(order_id) {
            Some(qty) => CancelResult::success(qty),
            None => CancelResult::failure(CancelError::OrderNotActive),
        }
    }

    /// Modify an order (cancel and replace).
    ///
    /// The old order is cancelled and a new order is submitted with
    /// the new price and quantity. The new order gets a new ID and
    /// **loses time priority**.
    ///
    /// The new order inherits the original order's time-in-force.
    pub fn modify(
        &mut self,
        order_id: OrderId,
        new_price: Price,
        new_quantity: Quantity,
    ) -> ModifyResult {
        #[cfg(feature = "event-log")]
        self.events.push(Event::Modify {
            order_id,
            new_price,
            new_quantity,
        });

        self.modify_internal(order_id, new_price, new_quantity)
    }

    /// Internal: modify without recording event.
    pub(crate) fn modify_internal(
        &mut self,
        order_id: OrderId,
        new_price: Price,
        new_quantity: Quantity,
    ) -> ModifyResult {
        // Validate quantity
        if new_quantity == 0 {
            return ModifyResult::failure(order_id, ModifyError::InvalidQuantity);
        }

        // Get the old order's details
        let (side, tif) = match self.book.get_order(order_id) {
            Some(o) if o.is_active() => (o.side, o.time_in_force),
            Some(_) => return ModifyResult::failure(order_id, ModifyError::OrderNotActive),
            None => return ModifyResult::failure(order_id, ModifyError::OrderNotFound),
        };

        // Cancel the old order
        let cancelled = match self.book.cancel_order(order_id) {
            Some(qty) => qty,
            None => return ModifyResult::failure(order_id, ModifyError::OrderNotActive),
        };

        // Submit the new order
        let result = self.submit_limit_internal(side, new_price, new_quantity, tif);

        ModifyResult::success(order_id, result.order_id, cancelled, result.trades)
    }

    // === Stop Orders ===

    /// Maximum cascade depth to prevent infinite stop-trigger loops.
    const MAX_CASCADE_DEPTH: usize = 100;

    /// Submit a stop-market order.
    ///
    /// The order becomes a market order when `last_trade_price` reaches `stop_price`.
    /// - Buy stop: triggers when `last_trade_price >= stop_price`
    /// - Sell stop: triggers when `last_trade_price <= stop_price`
    pub fn submit_stop_market(
        &mut self,
        side: Side,
        stop_price: Price,
        quantity: Quantity,
    ) -> StopSubmitResult {
        #[cfg(feature = "event-log")]
        self.events.push(Event::SubmitStopMarket {
            side,
            stop_price,
            quantity,
        });

        self.submit_stop_internal(side, stop_price, None, quantity, TimeInForce::GTC)
    }

    /// Submit a stop-limit order.
    ///
    /// The order becomes a limit order at `limit_price` when `last_trade_price`
    /// reaches `stop_price`.
    pub fn submit_stop_limit(
        &mut self,
        side: Side,
        stop_price: Price,
        limit_price: Price,
        quantity: Quantity,
        tif: TimeInForce,
    ) -> StopSubmitResult {
        #[cfg(feature = "event-log")]
        self.events.push(Event::SubmitStopLimit {
            side,
            stop_price,
            limit_price,
            quantity,
            time_in_force: tif,
        });

        self.submit_stop_internal(side, stop_price, Some(limit_price), quantity, tif)
    }

    /// Submit a trailing stop-market order.
    ///
    /// The stop price adjusts as the market moves favorably:
    /// - Sell trailing: stop follows the market UP (protects long positions)
    /// - Buy trailing: stop follows the market DOWN (protects short positions)
    ///
    /// `initial_stop_price` is the starting stop price before any trailing.
    pub fn submit_trailing_stop_market(
        &mut self,
        side: Side,
        initial_stop_price: Price,
        quantity: Quantity,
        trail_method: TrailMethod,
    ) -> StopSubmitResult {
        #[cfg(feature = "event-log")]
        self.events.push(Event::SubmitTrailingStopMarket {
            side,
            stop_price: initial_stop_price,
            quantity,
            trail_method: trail_method.clone(),
        });

        self.submit_trailing_stop_internal(
            side,
            initial_stop_price,
            None,
            quantity,
            TimeInForce::GTC,
            trail_method,
        )
    }

    /// Submit a trailing stop-limit order.
    ///
    /// Like a trailing stop-market, but when triggered becomes a limit order
    /// at `limit_price`.
    pub fn submit_trailing_stop_limit(
        &mut self,
        side: Side,
        initial_stop_price: Price,
        limit_price: Price,
        quantity: Quantity,
        tif: TimeInForce,
        trail_method: TrailMethod,
    ) -> StopSubmitResult {
        #[cfg(feature = "event-log")]
        self.events.push(Event::SubmitTrailingStopLimit {
            side,
            stop_price: initial_stop_price,
            limit_price,
            quantity,
            time_in_force: tif,
            trail_method: trail_method.clone(),
        });

        self.submit_trailing_stop_internal(
            side,
            initial_stop_price,
            Some(limit_price),
            quantity,
            tif,
            trail_method,
        )
    }

    /// Internal: submit trailing stop order.
    pub(crate) fn submit_trailing_stop_internal(
        &mut self,
        side: Side,
        stop_price: Price,
        limit_price: Option<Price>,
        quantity: Quantity,
        tif: TimeInForce,
        trail_method: TrailMethod,
    ) -> StopSubmitResult {
        self.insert_stop_order(
            side,
            stop_price,
            limit_price,
            quantity,
            tif,
            Some(trail_method),
        )
    }

    /// Internal: submit stop order without recording event.
    pub(crate) fn submit_stop_internal(
        &mut self,
        side: Side,
        stop_price: Price,
        limit_price: Option<Price>,
        quantity: Quantity,
        tif: TimeInForce,
    ) -> StopSubmitResult {
        self.insert_stop_order(side, stop_price, limit_price, quantity, tif, None)
    }

    /// Shared logic for inserting stop/trailing-stop orders.
    fn insert_stop_order(
        &mut self,
        side: Side,
        stop_price: Price,
        limit_price: Option<Price>,
        quantity: Quantity,
        tif: TimeInForce,
        trail_method: Option<TrailMethod>,
    ) -> StopSubmitResult {
        let id = self.book.next_order_id();
        let timestamp = self.book.next_timestamp();
        let is_trailing = trail_method.is_some();

        let order = StopOrder {
            id,
            side,
            stop_price,
            limit_price,
            quantity,
            time_in_force: tif,
            timestamp,
            status: StopStatus::Pending,
            trail_method,
            watermark: None,
        };

        self.stop_book.insert(order);

        // Trailing stops don't trigger immediately — they need price movement to
        // establish the watermark first. update_trailing_stops() will adjust the
        // stop price relative to the watermark, so the raw stop_price check would
        // be misleading.
        if !is_trailing {
            if let Some(last_price) = self.last_trade_price {
                let should_trigger = match side {
                    Side::Buy => last_price >= stop_price,
                    Side::Sell => last_price <= stop_price,
                };
                if should_trigger {
                    self.process_trade_triggers();
                    let status = self
                        .stop_book
                        .get(id)
                        .map(|o| o.status)
                        .unwrap_or(StopStatus::Triggered);
                    return StopSubmitResult {
                        order_id: id,
                        status,
                    };
                }
            }
        }

        StopSubmitResult {
            order_id: id,
            status: StopStatus::Pending,
        }
    }

    /// Process stop order triggers after trades occur.
    ///
    /// Trailing stops are updated BEFORE checking triggers, so their
    /// stop prices reflect the latest market move.
    ///
    /// Triggered stops may produce trades that trigger more stops (cascade).
    /// Limited to `MAX_CASCADE_DEPTH` iterations to prevent infinite loops.
    pub(crate) fn process_trade_triggers(&mut self) {
        for _ in 0..Self::MAX_CASCADE_DEPTH {
            let trade_price = match self.last_trade_price {
                Some(p) => p,
                None => return,
            };

            // Update trailing stops before checking triggers
            self.stop_book.update_trailing_stops(trade_price);

            let triggered = self.stop_book.collect_triggered(trade_price);
            if triggered.is_empty() {
                return;
            }

            let mut new_last_price = None;

            for stop in triggered {
                let result = match stop.limit_price {
                    Some(limit) => self.submit_limit_internal(
                        stop.side,
                        limit,
                        stop.quantity,
                        stop.time_in_force,
                    ),
                    None => {
                        let price = match stop.side {
                            Side::Buy => Price::MAX,
                            Side::Sell => Price::MIN,
                        };
                        self.submit_limit_internal(
                            stop.side,
                            price,
                            stop.quantity,
                            TimeInForce::IOC,
                        )
                    }
                };

                // submit_limit_internal already records trades in self.trades
                if let Some(last_trade) = result.trades.last() {
                    new_last_price = Some(last_trade.price);
                }
            }

            match new_last_price {
                Some(p) => self.last_trade_price = Some(p),
                None => return, // No new trades, no more triggers possible
            }
        }
    }

    // === Queries ===

    /// Get an order by ID.
    pub fn get_order(&self, order_id: OrderId) -> Option<&Order> {
        self.book.get_order(order_id)
    }

    /// Get the best bid and ask prices.
    pub fn best_bid_ask(&self) -> (Option<Price>, Option<Price>) {
        self.book.best_bid_ask()
    }

    /// Get the best bid price.
    pub fn best_bid(&self) -> Option<Price> {
        self.book.best_bid()
    }

    /// Get the best ask price.
    pub fn best_ask(&self) -> Option<Price> {
        self.book.best_ask()
    }

    /// Get the spread (best ask - best bid).
    pub fn spread(&self) -> Option<i64> {
        self.book.spread()
    }

    /// Get a snapshot of the top N levels on each side.
    pub fn depth(&self, levels: usize) -> BookSnapshot {
        self.book.snapshot(levels)
    }

    /// Get a full snapshot of the order book.
    pub fn full_book(&self) -> BookSnapshot {
        self.book.full_snapshot()
    }

    /// Get all trades that have occurred.
    pub fn trades(&self) -> &[Trade] {
        &self.trades
    }

    /// Get the underlying order book (for advanced queries).
    pub fn book(&self) -> &OrderBook {
        &self.book
    }

    /// Get mutable access to the underlying order book.
    pub fn book_mut(&mut self) -> &mut OrderBook {
        &mut self.book
    }

    /// Get a stop order by ID.
    pub fn get_stop_order(&self, order_id: OrderId) -> Option<&StopOrder> {
        self.stop_book.get(order_id)
    }

    /// Get the number of pending stop orders.
    pub fn pending_stop_count(&self) -> usize {
        self.stop_book.pending_count()
    }

    /// Get the last trade price.
    pub fn last_trade_price(&self) -> Option<Price> {
        self.last_trade_price
    }

    /// Get the stop book (for advanced queries).
    pub fn stop_book(&self) -> &StopBook {
        &self.stop_book
    }

    // === Memory Management ===

    /// Clear trade history to free memory.
    ///
    /// Use periodically for long-running instances.
    pub fn clear_trades(&mut self) {
        self.trades.clear();
    }

    /// Remove filled and cancelled orders from history.
    ///
    /// Active orders (on the book) are preserved. Returns the number
    /// of orders removed. Also clears triggered/cancelled stop orders.
    pub fn clear_order_history(&mut self) -> usize {
        self.stop_book.clear_history();
        self.book.clear_history()
    }

    /// Remove all tombstones from the order book.
    ///
    /// Useful after heavy cancellation activity to reclaim memory and
    /// maintain iteration performance.
    pub fn compact(&mut self) {
        self.book.compact();
    }
}

impl Default for Exchange {
    fn default() -> Self {
        Self::new()
    }
}

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

    // === Basic submission ===

    #[test]
    fn submit_limit_no_match() {
        let mut exchange = Exchange::new();

        let result = exchange.submit_limit(Side::Buy, Price(100_00), 100, TimeInForce::GTC);

        assert_eq!(result.order_id, OrderId(1));
        assert_eq!(result.status, OrderStatus::New);
        assert!(result.trades.is_empty());
        assert_eq!(result.filled_quantity, 0);
        assert_eq!(result.resting_quantity, 100);
        assert_eq!(result.cancelled_quantity, 0);

        // Order should be on the book
        assert_eq!(exchange.best_bid(), Some(Price(100_00)));
    }

    #[test]
    fn submit_limit_full_fill() {
        let mut exchange = Exchange::new();

        // Place a resting ask
        exchange.submit_limit(Side::Sell, Price(100_00), 100, TimeInForce::GTC);

        // Place a crossing bid
        let result = exchange.submit_limit(Side::Buy, Price(100_00), 100, TimeInForce::GTC);

        assert_eq!(result.status, OrderStatus::Filled);
        assert_eq!(result.trades.len(), 1);
        assert_eq!(result.filled_quantity, 100);
        assert_eq!(result.resting_quantity, 0);
        assert_eq!(result.cancelled_quantity, 0);

        // Book should be empty
        assert_eq!(exchange.best_bid(), None);
        assert_eq!(exchange.best_ask(), None);
    }

    #[test]
    fn submit_limit_partial_fill_gtc() {
        let mut exchange = Exchange::new();

        // Place a small ask
        exchange.submit_limit(Side::Sell, Price(100_00), 30, TimeInForce::GTC);

        // Place a larger bid
        let result = exchange.submit_limit(Side::Buy, Price(100_00), 100, TimeInForce::GTC);

        assert_eq!(result.status, OrderStatus::PartiallyFilled);
        assert_eq!(result.filled_quantity, 30);
        assert_eq!(result.resting_quantity, 70);
        assert_eq!(result.cancelled_quantity, 0);

        // Remainder should be on book
        assert_eq!(exchange.best_bid(), Some(Price(100_00)));
    }

    // === IOC ===

    #[test]
    fn submit_ioc_full_fill() {
        let mut exchange = Exchange::new();

        exchange.submit_limit(Side::Sell, Price(100_00), 100, TimeInForce::GTC);

        let result = exchange.submit_limit(Side::Buy, Price(100_00), 100, TimeInForce::IOC);

        assert_eq!(result.status, OrderStatus::Filled);
        assert_eq!(result.filled_quantity, 100);
        assert_eq!(result.resting_quantity, 0);
    }

    #[test]
    fn submit_ioc_partial_fill() {
        let mut exchange = Exchange::new();

        exchange.submit_limit(Side::Sell, Price(100_00), 30, TimeInForce::GTC);

        let result = exchange.submit_limit(Side::Buy, Price(100_00), 100, TimeInForce::IOC);

        assert_eq!(result.status, OrderStatus::PartiallyFilled);
        assert_eq!(result.filled_quantity, 30);
        assert_eq!(result.resting_quantity, 0); // IOC never rests
        assert_eq!(result.cancelled_quantity, 70);

        // Nothing on bid side
        assert_eq!(exchange.best_bid(), None);
    }

    #[test]
    fn submit_ioc_no_fill() {
        let mut exchange = Exchange::new();

        let result = exchange.submit_limit(Side::Buy, Price(100_00), 100, TimeInForce::IOC);

        assert_eq!(result.status, OrderStatus::Cancelled);
        assert_eq!(result.filled_quantity, 0);
        assert_eq!(result.cancelled_quantity, 100);
        assert_eq!(exchange.best_bid(), None);
    }

    // === FOK ===

    #[test]
    fn submit_fok_full_fill() {
        let mut exchange = Exchange::new();

        exchange.submit_limit(Side::Sell, Price(100_00), 100, TimeInForce::GTC);

        let result = exchange.submit_limit(Side::Buy, Price(100_00), 100, TimeInForce::FOK);

        assert_eq!(result.status, OrderStatus::Filled);
        assert_eq!(result.filled_quantity, 100);
        assert_eq!(result.trades.len(), 1);
    }

    #[test]
    fn submit_fok_rejected_insufficient_liquidity() {
        let mut exchange = Exchange::new();

        exchange.submit_limit(Side::Sell, Price(100_00), 50, TimeInForce::GTC);

        // Try to buy 100 but only 50 available
        let result = exchange.submit_limit(Side::Buy, Price(100_00), 100, TimeInForce::FOK);

        assert_eq!(result.status, OrderStatus::Cancelled);
        assert_eq!(result.filled_quantity, 0);
        assert_eq!(result.cancelled_quantity, 100);
        assert!(result.trades.is_empty()); // No trades!

        // Ask should still be there
        assert_eq!(exchange.best_ask(), Some(Price(100_00)));
    }

    #[test]
    fn submit_fok_rejected_no_liquidity() {
        let mut exchange = Exchange::new();

        let result = exchange.submit_limit(Side::Buy, Price(100_00), 100, TimeInForce::FOK);

        assert_eq!(result.status, OrderStatus::Cancelled);
        assert!(result.trades.is_empty());
    }

    // === Market orders ===

    #[test]
    fn submit_market_full_fill() {
        let mut exchange = Exchange::new();

        exchange.submit_limit(Side::Sell, Price(100_00), 100, TimeInForce::GTC);

        let result = exchange.submit_market(Side::Buy, 100);

        assert_eq!(result.status, OrderStatus::Filled);
        assert_eq!(result.filled_quantity, 100);
    }

    #[test]
    fn submit_market_partial_fill() {
        let mut exchange = Exchange::new();

        exchange.submit_limit(Side::Sell, Price(100_00), 50, TimeInForce::GTC);

        let result = exchange.submit_market(Side::Buy, 100);

        assert_eq!(result.status, OrderStatus::PartiallyFilled);
        assert_eq!(result.filled_quantity, 50);
        assert_eq!(result.cancelled_quantity, 50);
    }

    #[test]
    fn submit_market_no_liquidity() {
        let mut exchange = Exchange::new();

        let result = exchange.submit_market(Side::Buy, 100);

        assert_eq!(result.status, OrderStatus::Cancelled);
        assert_eq!(result.filled_quantity, 0);
    }

    // === Cancel ===

    #[test]
    fn cancel_order() {
        let mut exchange = Exchange::new();

        let submit = exchange.submit_limit(Side::Buy, Price(100_00), 100, TimeInForce::GTC);
        let result = exchange.cancel(submit.order_id);

        assert!(result.success);
        assert_eq!(result.cancelled_quantity, 100);
        assert_eq!(exchange.best_bid(), None);
    }

    #[test]
    fn cancel_nonexistent() {
        let mut exchange = Exchange::new();

        let result = exchange.cancel(OrderId(999));

        assert!(!result.success);
        assert_eq!(result.error, Some(CancelError::OrderNotFound));
    }

    #[test]
    fn cancel_already_filled() {
        let mut exchange = Exchange::new();

        exchange.submit_limit(Side::Sell, Price(100_00), 100, TimeInForce::GTC);
        let buy = exchange.submit_limit(Side::Buy, Price(100_00), 100, TimeInForce::GTC);

        // Order is filled, can't cancel
        let result = exchange.cancel(buy.order_id);

        assert!(!result.success);
        assert_eq!(result.error, Some(CancelError::OrderNotActive));
    }

    // === Modify ===

    #[test]
    fn modify_order() {
        let mut exchange = Exchange::new();

        let submit = exchange.submit_limit(Side::Buy, Price(100_00), 100, TimeInForce::GTC);
        let result = exchange.modify(submit.order_id, Price(99_00), 150);

        assert!(result.success);
        assert_eq!(result.old_order_id, submit.order_id);
        assert!(result.new_order_id.is_some());
        assert_ne!(result.new_order_id.unwrap(), submit.order_id);
        assert_eq!(result.cancelled_quantity, 100);

        // New order should be on book at new price
        assert_eq!(exchange.best_bid(), Some(Price(99_00)));
        let new_order = exchange.get_order(result.new_order_id.unwrap()).unwrap();
        assert_eq!(new_order.remaining_quantity, 150);
    }

    #[test]
    fn modify_with_immediate_fill() {
        let mut exchange = Exchange::new();

        // Resting ask
        exchange.submit_limit(Side::Sell, Price(100_00), 50, TimeInForce::GTC);

        // Resting bid that doesn't cross
        let submit = exchange.submit_limit(Side::Buy, Price(99_00), 100, TimeInForce::GTC);

        // Modify to cross
        let result = exchange.modify(submit.order_id, Price(100_00), 100);

        assert!(result.success);
        assert_eq!(result.trades.len(), 1);
        assert_eq!(result.trades[0].quantity, 50);
    }

    #[test]
    fn modify_nonexistent() {
        let mut exchange = Exchange::new();

        let result = exchange.modify(OrderId(999), Price(100_00), 100);

        assert!(!result.success);
        assert_eq!(result.error, Some(ModifyError::OrderNotFound));
    }

    #[test]
    fn modify_zero_quantity() {
        let mut exchange = Exchange::new();

        let submit = exchange.submit_limit(Side::Buy, Price(100_00), 100, TimeInForce::GTC);
        let result = exchange.modify(submit.order_id, Price(100_00), 0);

        assert!(!result.success);
        assert_eq!(result.error, Some(ModifyError::InvalidQuantity));
    }

    // === Validation ===

    #[test]
    fn try_submit_limit_zero_quantity() {
        let mut exchange = Exchange::new();
        let result = exchange.try_submit_limit(Side::Buy, Price(100_00), 0, TimeInForce::GTC);
        assert_eq!(result.unwrap_err(), ValidationError::ZeroQuantity);
    }

    #[test]
    fn try_submit_limit_zero_price() {
        let mut exchange = Exchange::new();
        let result = exchange.try_submit_limit(Side::Buy, Price(0), 100, TimeInForce::GTC);
        assert_eq!(result.unwrap_err(), ValidationError::ZeroPrice);
    }

    #[test]
    fn try_submit_limit_negative_price() {
        let mut exchange = Exchange::new();
        let result = exchange.try_submit_limit(Side::Buy, Price(-100), 100, TimeInForce::GTC);
        assert_eq!(result.unwrap_err(), ValidationError::ZeroPrice);
    }

    #[test]
    fn try_submit_limit_valid() {
        let mut exchange = Exchange::new();
        let result = exchange.try_submit_limit(Side::Buy, Price(100_00), 100, TimeInForce::GTC);
        assert!(result.is_ok());
        assert_eq!(result.unwrap().order_id, OrderId(1));
    }

    #[test]
    fn try_submit_market_zero_quantity() {
        let mut exchange = Exchange::new();
        let result = exchange.try_submit_market(Side::Buy, 0);
        assert_eq!(result.unwrap_err(), ValidationError::ZeroQuantity);
    }

    #[test]
    fn try_submit_market_valid() {
        let mut exchange = Exchange::new();
        exchange.submit_limit(Side::Sell, Price(100_00), 100, TimeInForce::GTC);
        let result = exchange.try_submit_market(Side::Buy, 50);
        assert!(result.is_ok());
    }

    // === Stop Orders ===

    #[test]
    fn submit_stop_market_pending() {
        let mut exchange = Exchange::new();

        let result = exchange.submit_stop_market(Side::Buy, Price(105_00), 100);
        assert_eq!(result.status, StopStatus::Pending);
        assert_eq!(exchange.pending_stop_count(), 1);
    }

    #[test]
    fn stop_market_triggers_on_trade() {
        let mut exchange = Exchange::new();

        // Set up a resting ask
        exchange.submit_limit(Side::Sell, Price(100_00), 50, TimeInForce::GTC);
        // Set up asks for the triggered order to fill against
        exchange.submit_limit(Side::Sell, Price(105_00), 100, TimeInForce::GTC);

        // Place buy stop at 100
        exchange.submit_stop_market(Side::Buy, Price(100_00), 100);

        // Now submit a buy that crosses the ask and produces a trade at 100
        let result = exchange.submit_limit(Side::Buy, Price(100_00), 50, TimeInForce::GTC);
        assert_eq!(result.trades.len(), 1);

        // Stop should have triggered and filled against the 105 ask
        assert_eq!(exchange.pending_stop_count(), 0);
        assert_eq!(exchange.last_trade_price(), Some(Price(105_00)));
    }

    #[test]
    fn stop_limit_triggers_with_limit_price() {
        let mut exchange = Exchange::new();

        // Set up asks
        exchange.submit_limit(Side::Sell, Price(100_00), 50, TimeInForce::GTC);
        exchange.submit_limit(Side::Sell, Price(106_00), 100, TimeInForce::GTC);

        // Place buy stop-limit: triggers at 100, but only buy up to 105
        exchange.submit_stop_limit(
            Side::Buy,
            Price(100_00),
            Price(105_00),
            100,
            TimeInForce::GTC,
        );

        // Trigger with a trade at 100
        exchange.submit_limit(Side::Buy, Price(100_00), 50, TimeInForce::GTC);

        // Stop triggered, but limit price 105 doesn't cross ask at 106
        // So it should rest on the book
        assert_eq!(exchange.pending_stop_count(), 0);
        assert_eq!(exchange.best_bid(), Some(Price(105_00)));
    }

    #[test]
    fn cancel_stop_order() {
        let mut exchange = Exchange::new();

        let stop = exchange.submit_stop_market(Side::Buy, Price(105_00), 100);
        assert_eq!(exchange.pending_stop_count(), 1);

        let result = exchange.cancel(stop.order_id);
        assert!(result.success);
        assert_eq!(result.cancelled_quantity, 100);
        assert_eq!(exchange.pending_stop_count(), 0);
    }

    #[test]
    fn sell_stop_triggers_on_price_drop() {
        let mut exchange = Exchange::new();

        // Set up a resting bid to establish a price
        exchange.submit_limit(Side::Buy, Price(100_00), 50, TimeInForce::GTC);
        // Set up bids for the triggered sell to fill against
        exchange.submit_limit(Side::Buy, Price(95_00), 100, TimeInForce::GTC);

        // Sell stop at 100: triggers when price drops to 100
        exchange.submit_stop_market(Side::Sell, Price(100_00), 100);

        // Trade at 100 triggers the sell stop
        exchange.submit_limit(Side::Sell, Price(100_00), 50, TimeInForce::GTC);

        assert_eq!(exchange.pending_stop_count(), 0);
    }

    #[test]
    fn immediate_trigger_if_price_already_past() {
        let mut exchange = Exchange::new();

        // Create a trade to establish last_trade_price at 100
        exchange.submit_limit(Side::Sell, Price(100_00), 50, TimeInForce::GTC);
        exchange.submit_limit(Side::Buy, Price(100_00), 50, TimeInForce::GTC);
        assert_eq!(exchange.last_trade_price(), Some(Price(100_00)));

        // Set up more asks for the stop to fill against
        exchange.submit_limit(Side::Sell, Price(105_00), 100, TimeInForce::GTC);

        // Submit buy stop at 99 — already past, should trigger immediately
        let result = exchange.submit_stop_market(Side::Buy, Price(99_00), 100);
        assert_eq!(result.status, StopStatus::Triggered);
        assert_eq!(exchange.pending_stop_count(), 0);
    }

    #[test]
    fn stop_cascade() {
        let mut exchange = Exchange::new();

        // Set up asks at different levels
        exchange.submit_limit(Side::Sell, Price(100_00), 50, TimeInForce::GTC);
        exchange.submit_limit(Side::Sell, Price(102_00), 50, TimeInForce::GTC);
        exchange.submit_limit(Side::Sell, Price(104_00), 50, TimeInForce::GTC);

        // Buy stop at 100 — when triggered, will trade at 102
        exchange.submit_stop_market(Side::Buy, Price(100_00), 50);
        // Buy stop at 102 — cascading trigger from first stop's trade
        exchange.submit_stop_market(Side::Buy, Price(102_00), 50);

        // Trigger cascade: trade at 100 -> stop1 triggers -> trades at 102 -> stop2 triggers
        exchange.submit_limit(Side::Buy, Price(100_00), 50, TimeInForce::GTC);

        assert_eq!(exchange.pending_stop_count(), 0);
    }

    // === Queries ===

    #[test]
    fn trades_are_recorded() {
        let mut exchange = Exchange::new();

        exchange.submit_limit(Side::Sell, Price(100_00), 100, TimeInForce::GTC);
        exchange.submit_limit(Side::Buy, Price(100_00), 100, TimeInForce::GTC);

        assert_eq!(exchange.trades().len(), 1);
        assert_eq!(exchange.trades()[0].quantity, 100);
    }

    #[test]
    fn depth_snapshot() {
        let mut exchange = Exchange::new();

        exchange.submit_limit(Side::Buy, Price(100_00), 100, TimeInForce::GTC);
        exchange.submit_limit(Side::Buy, Price(99_00), 200, TimeInForce::GTC);
        exchange.submit_limit(Side::Sell, Price(101_00), 150, TimeInForce::GTC);

        let snap = exchange.depth(10);

        assert_eq!(snap.bids.len(), 2);
        assert_eq!(snap.asks.len(), 1);
        assert_eq!(snap.best_bid(), Some(Price(100_00)));
        assert_eq!(snap.best_ask(), Some(Price(101_00)));
    }

    // === Trailing Stop Orders ===

    #[test]
    fn trailing_stop_market_sell() {
        let mut exchange = Exchange::new();

        // Set up order book: asks at 100 and bids at 90 for the triggered sell
        exchange.submit_limit(Side::Sell, Price(100_00), 100, TimeInForce::GTC);
        exchange.submit_limit(Side::Buy, Price(90_00), 200, TimeInForce::GTC);

        // Place trailing sell stop: initial stop at 95, trail by $3
        let result = exchange.submit_trailing_stop_market(
            Side::Sell,
            Price(95_00),
            100,
            TrailMethod::Fixed(3_00),
        );
        assert_eq!(result.status, StopStatus::Pending);

        // Trade at 100 (buy crosses the ask) — watermark should move up
        exchange.submit_limit(Side::Buy, Price(100_00), 100, TimeInForce::GTC);

        // The trailing stop should have adjusted: watermark=100, stop=97
        // It should not have triggered (price 100 > stop 97 for sell)
        let stop = exchange.get_stop_order(result.order_id).unwrap();
        assert_eq!(stop.watermark, Some(Price(100_00)));
        assert_eq!(stop.stop_price, Price(97_00));
    }

    #[test]
    fn trailing_stop_triggers_on_reversal() {
        let mut exchange = Exchange::new();

        // Build book: asks and bids for trading
        exchange.submit_limit(Side::Sell, Price(100_00), 50, TimeInForce::GTC);
        exchange.submit_limit(Side::Sell, Price(105_00), 50, TimeInForce::GTC);
        exchange.submit_limit(Side::Buy, Price(90_00), 200, TimeInForce::GTC);

        // Place trailing sell stop: initial stop at 98, trail by $2
        exchange.submit_trailing_stop_market(
            Side::Sell,
            Price(98_00),
            50,
            TrailMethod::Fixed(2_00),
        );

        // Trade at 100 — trailing updates to stop=98, watermark=100
        exchange.submit_limit(Side::Buy, Price(100_00), 50, TimeInForce::GTC);
        assert_eq!(exchange.pending_stop_count(), 1);

        // Trade at 105 — trailing updates to stop=103, watermark=105
        exchange.submit_limit(Side::Buy, Price(105_00), 50, TimeInForce::GTC);

        // Now set up a sell at 103 and buy at 90 to drop the price
        exchange.submit_limit(Side::Buy, Price(103_00), 50, TimeInForce::GTC);
        exchange.submit_limit(Side::Sell, Price(103_00), 50, TimeInForce::GTC);
        // Trade at 103 should trigger the trailing stop (stop_price=103)
        assert_eq!(exchange.pending_stop_count(), 0);
    }

    #[test]
    fn trailing_stop_percentage_method() {
        let mut exchange = Exchange::new();

        exchange.submit_limit(Side::Sell, Price(100_00), 100, TimeInForce::GTC);
        exchange.submit_limit(Side::Buy, Price(80_00), 200, TimeInForce::GTC);

        // Trailing sell stop: 5% trailing distance
        let result = exchange.submit_trailing_stop_market(
            Side::Sell,
            Price(90_00),
            50,
            TrailMethod::Percentage(0.05),
        );
        assert_eq!(result.status, StopStatus::Pending);

        // Trade at 100 — watermark=100, offset=5% of 100 = $5, stop=95
        exchange.submit_limit(Side::Buy, Price(100_00), 100, TimeInForce::GTC);

        let stop = exchange.get_stop_order(result.order_id).unwrap();
        assert_eq!(stop.watermark, Some(Price(100_00)));
        assert_eq!(stop.stop_price, Price(95_00));
    }

    #[test]
    fn trailing_stop_does_not_trigger_immediately() {
        let mut exchange = Exchange::new();

        // Establish last_trade_price at 90 via a trade
        exchange.submit_limit(Side::Sell, Price(90_00), 50, TimeInForce::GTC);
        exchange.submit_limit(Side::Buy, Price(90_00), 50, TimeInForce::GTC);
        assert_eq!(exchange.last_trade_price(), Some(Price(90_00)));

        // Submit trailing sell stop with stop_price=95 — although 90 <= 95,
        // trailing stops wait for price movement to establish the watermark first
        let result = exchange.submit_trailing_stop_market(
            Side::Sell,
            Price(95_00),
            50,
            TrailMethod::Fixed(3_00),
        );
        assert_eq!(result.status, StopStatus::Pending);
        assert_eq!(exchange.pending_stop_count(), 1);
    }

    #[test]
    fn cancel_trailing_stop() {
        let mut exchange = Exchange::new();

        let result = exchange.submit_trailing_stop_market(
            Side::Sell,
            Price(95_00),
            100,
            TrailMethod::Fixed(3_00),
        );

        let cancel = exchange.cancel(result.order_id);
        assert!(cancel.success);
        assert_eq!(exchange.pending_stop_count(), 0);
    }
}