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
//!
//! A limit order book is a record of outstanding limit orders maintained by the security specialist
//! who works at the exchange. The book is organized by Limit level and contains the Limit and
//! volume of each limit order. The specialist is responsible for maintaining a fair and orderly
//! market in the security and uses the book to help determine the best Limit at which to execute
//! orders.
//!
//! The limit order book is a key component of the market microstructure and is used by traders to
//! help make trading decisions. The book is also used by the exchange to help determine the best
//! Limit at which to execute orders. The book is updated in real-time as orders are placed and
//! executed.
//!

mod primitives;
use itertools::Itertools;
use stable_vec::StableVec;
use std::collections::{HashMap, VecDeque};
use thiserror::Error;

pub use primitives::{Oid, OrderSide, OrderType, Price, Timestamp, Volume};

/// LevelIndex is an index to a Level in a stable vec
type LevelIndex = usize;

// stable vec of levels, once added level will not change its index
// it will be removed only when the level is empty
// so when looking up the index we will get None
type LevelVec = StableVec<Level>;

// map of Limit -> LevelIndex
// this will allow for O(1) lookup of Limit levels
// this will only grow, since each limit need to point to a stable index in the stable level vec
type LevelMap = HashMap<Price, LevelIndex>;

// map of Order ID -> LimitOrder that contains full order data
type OrderMap = HashMap<Oid, LimitOrder>;

/// Order
#[derive(Debug, PartialEq, PartialOrd, Clone)]
pub struct Order {
    id: Oid,
    pub side: OrderSide,
    kind: OrderType,
    pub price: Option<Price>,
    pub volume: Volume,
    timestamp: Timestamp,
}

impl Order {
    /// Create a new order
    pub fn new_limit(
        id: Oid,
        side: OrderSide,
        timestamp: Timestamp,
        price: Price,
        volume: Volume,
    ) -> Self {
        Order {
            id,
            side,
            kind: OrderType::Limit,
            timestamp,
            price: Some(price),
            volume,
        }
    }
    pub fn new_market(id: Oid, side: OrderSide, timestamp: Timestamp, volume: Volume) -> Self {
        Order {
            id,
            side,
            kind: OrderType::Market,
            timestamp,
            price: None,
            volume,
        }
    }
}

/// Limit Order
#[derive(Debug, PartialEq, PartialOrd, Clone)]
pub struct LimitOrder {
    id: Oid,
    side: OrderSide,
    timestamp: Timestamp,
    price: Price,
    volume: Volume,
    filled_volume: Option<Volume>,
}

pub enum TryFromOrderError {
    OrderTypeNotLimit,
}

impl TryFrom<&Order> for LimitOrder {
    type Error = TryFromOrderError;

    fn try_from(order: &Order) -> Result<Self, Self::Error> {
        match order.kind {
            OrderType::Limit => Ok(LimitOrder {
                id: order.id,
                side: order.side,
                timestamp: order.timestamp,
                price: order.price.unwrap(), // we can unwrap since we know it is a limit order
                volume: order.volume,
                filled_volume: None,
            }),
            _ => Err(TryFromOrderError::OrderTypeNotLimit),
        }
    }
}

impl LimitOrder {
    /// Create a new order
    pub fn new(
        id: Oid,
        side: OrderSide,
        timestamp: Timestamp,
        price: Price,
        volume: Volume,
    ) -> Self {
        LimitOrder {
            id,
            side,
            timestamp,
            price,
            volume,
            filled_volume: None,
        }
    }
}

/// Limit level
/// represents Price level and list of orders in FIFO order
#[derive(Debug, Clone)]
pub struct Level {
    price: Price,
    total_volume: Volume,
    orders: VecDeque<Oid>,
    depth: usize, // number of orders at the level
}

impl Eq for Level {}
impl PartialEq for Level {
    fn eq(&self, other: &Self) -> bool {
        self.price == other.price
    }
}

impl PartialOrd for Level {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Level {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.price.cmp(&other.price)
    }
}

impl Level {
    /// Create a new Limit level
    pub fn new(price: Price) -> Level {
        Level {
            price,
            total_volume: 0.into(),
            orders: VecDeque::new(),
            depth: 0,
        }
    }

    /// Add an order to the Limit level
    pub fn add_order(&mut self, order: &LimitOrder) {
        {
            self.total_volume += order.volume;
            self.depth += 1;
        }
        self.orders.push_back(order.id);
    }

    /// Remove an order from the Limit level
    /// order will be cancelled and removed from the map
    /// therefore we need to update the total volume
    pub fn cancell_order(&mut self, order: &LimitOrder) {
        self.total_volume -= order.volume;
        self.depth -= 1;
    }
}

/// Limits (i.e. Price): 21.0453 to orders at that price
#[derive(Debug, Default)]
pub struct Limits {
    /// map of LimitIndex -> Level (i.e. queue of orders at that Limit level)
    /// this will allow for O(1) lookup of Limit levels
    /// when inserting an order at a specific Limit level
    levels: LevelVec,
    // stable vector of Limit levels
    // once inserted limit will not change its index
    // and hence we will be able to make O(1) lookup to access orders at that level
    level_map: LevelMap,
    // for bids is max for asks is min limit
    best: Option<LevelIndex>,
}

impl Limits {
    /// depends on the side, i.e. for ask find smallest Limit, for bid find largest Limit
    pub fn get_best_limit(&self) -> Option<Price> {
        if let Some(index) = self.best {
            self.levels.get(index).map(|l| l.price)
        } else {
            None
        }
    }

    /// add an order to the Limit map
    pub fn add_order(&mut self, order: &LimitOrder) {
        let price = &order.price;
        match self.level_map.get(price) {
            None => {
                // create a new limit level
                let mut level = Level::new(*price);
                level.add_order(order);
                let index = self.levels.push(level);
                self.level_map.insert(*price, index);
            }
            Some(index) => {
                // add the order to the existing Limit level
                if let Some(level) = self.levels.get_mut(*index) {
                    level.add_order(order);
                }
            }
        }
    }

    /// cancell order
    /// since we postopne removal of cancelled orders when filling the new order
    /// all we need to do is to update the total level volume so it is in sync
    pub fn cancel_order(&mut self, order: &LimitOrder) {
        if let Some(index) = self.level_map.get(&order.price) {
            if let Some(level) = self.levels.get_mut(*index) {
                level.cancell_order(order);
            }
        }
    }
}

/// Place order error
#[derive(Error, Debug, PartialEq, PartialOrd, Clone)]
pub enum PlaceOrderError {
    /// Order cannot be placed
    #[error("Order cannot be placed: {0}")]
    OrderCannotBePlaced(String),
}

/// Trade
#[derive(Debug)]
#[allow(dead_code)]
pub struct Trade {
    order_id: Oid,
    volume: Volume,
    filled_volume: Volume,
    executions: Vec<Execution>,
}

impl Trade {
    /// Create a new trade
    pub fn new(order_id: Oid, volume: Volume) -> Self {
        Trade {
            order_id,
            volume,
            filled_volume: Volume::ZERO,
            executions: Vec::new(),
        }
    }

    /// Add an execution to the trade
    pub fn add_execution(&mut self, execution: Execution) {
        self.filled_volume += execution.volume;
        self.executions.push(execution)
    }
}

/// Execution
#[derive(Debug, PartialEq, PartialOrd, Clone)]
pub struct Execution {
    order_id: Oid,
    price: Price,
    volume: Volume,
}

impl Execution {
    /// Create a new execution
    pub fn new(order_id: Oid, price: Price, volume: Volume) -> Self {
        Execution {
            order_id,
            price,
            volume,
        }
    }
}

/// Cancellation status
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CancellationStatus {
    /// Order was cancelled
    Cancelled,
    /// Order was not cancelled
    NotCancelled(String),
}

/// Cancellation report
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct CancellationReport {
    order_id: Oid,
    status: CancellationStatus,
}

/// Cancel order error  
#[derive(Error, Debug, PartialEq, PartialOrd, Clone)]
pub enum CancelOrderError {
    /// Order not found
    #[error("Order {0} not found")]
    NotFound(Oid),
    /// Order already cancelled
    #[error("Order {0} already cancelled")]
    AlreadyCancelled(Oid),
}

/// Spread
#[derive(Debug, PartialEq, PartialOrd, Clone)]
pub struct Spread(f64);

/// Limit Order Book
/// Trades are made when highest bid Limit is greater than or equal to the lowest ask Limit (spread is crossed)
/// If order cannot be filled immediately, it is added to the book
#[derive(Debug, Default)]
pub struct OrderBook {
    // Bid side of the book, represents open offers to buy an asset
    bids: Limits,
    // Ask side of the book, represents open offers to sell an asset
    asks: Limits,
    // this will allow for O(1) lookup of orders for cancellation
    orders: OrderMap,
    // spread is the diff between min ask and max bid
    spread: Option<Spread>,
}

impl OrderBook {
    /// takes an order object and either fills it or places it in the limit
    /// book, prints trades that have taken place as a result of the order
    pub fn execute(&mut self, order: &Order) -> Result<Trade, PlaceOrderError> {
        // we start with implementing market order

        let trade = match order.side {
            OrderSide::Buy => self.execute_buy(order),
            OrderSide::Sell => self.execute_sell(order),
        }?;

        // update min/max limits
        self.update_best_limit();

        // update spread
        self.update_spreads();
        Ok(trade)
    }

    fn update_spreads(&mut self) {
        let ask_best_limit = self.asks.get_best_limit();
        let bid_best_limit = self.bids.get_best_limit();
        match (ask_best_limit, bid_best_limit) {
            (Some(ask_limit), Some(bid_limit)) => {
                self.spread = Some(Spread((ask_limit - bid_limit).into()));
            }
            _ => {
                self.spread = None;
            }
        }
    }

    fn execute_buy(&mut self, order: &Order) -> Result<Trade, PlaceOrderError> {
        let trade = self.fill_buy_order(order)?;
        match order.kind {
            OrderType::Market => {
                // we do not need to add the order to the book
            }
            OrderType::Limit => {
                if trade.filled_volume < order.volume {
                    // add the order to the book
                    let limit_order = LimitOrder::try_from(order).map_err(|_| {
                        PlaceOrderError::OrderCannotBePlaced("not an market order".to_string())
                    })?;
                    self.bids.add_order(&limit_order);
                    self.orders.insert(limit_order.id, limit_order);
                }
            }
        }
        Ok(trade)
    }

    fn update_best_limit(&mut self) {
        if let Some(max) = self
            .bids
            .levels
            .values()
            .filter(|l| l.total_volume > 0.into())
            .max()
        {
            self.bids.best = self.bids.level_map.get(&max.price).copied();
        }
        if let Some(min) = self
            .asks
            .levels
            .values()
            .filter(|l| l.total_volume > 0.into())
            .min()
        {
            self.asks.best = self.asks.level_map.get(&min.price).copied();
        }
    }

    fn execute_sell(&mut self, order: &Order) -> Result<Trade, PlaceOrderError> {
        let trade = self.fill_sell_order(order)?;
        match order.kind {
            OrderType::Market => {
                // we do not need to add the order to the book
            }
            OrderType::Limit => {
                if trade.filled_volume < order.volume {
                    // add the order to the book
                    let limit_order = LimitOrder::try_from(order).map_err(|_| {
                        PlaceOrderError::OrderCannotBePlaced("not an market order".to_string())
                    })?;
                    self.asks.add_order(&limit_order);
                    self.orders.insert(limit_order.id, limit_order);
                }
            }
        }
        Ok(trade)
    }

    fn fill_buy_order(&mut self, buy_order: &Order) -> Result<Trade, PlaceOrderError> {
        // find the lowest sell Limit
        // if the lowest sell Limit is less than or equal to the buy Limit, we can fill the order, substracting the volume
        // if the lowest sell Limit is greater than the buy Limit, we add the order to the book, with the volume
        // equal to the order quantity

        let mut buy_volume = buy_order.volume;
        let mut trade = Trade::new(buy_order.id, buy_order.volume);

        let sorted = self
            .asks
            .levels
            .values_mut()
            .filter(|l| filter_limit_for_buy(l, &buy_order.price))
            .sorted();

        'top: for l in sorted {
            // peek order at front of the level
            while let Some(oid) = l.orders.front() {
                // todo: remove might trigger memcpy
                // although we need to get the owned value otherwise we will be borrowing self hence problem with borrow checker
                let Some(mut sell_order) = self.orders.remove(oid) else {
                    // if there is no order then it might have been cancelled
                    // and removed from the map, and since we pospone the removal of orders from the level
                    // till we encounter such order, we can safely remove the order from the level
                    l.orders.pop_front();
                    continue;
                };
                let sell_volume = sell_order.volume;
                if sell_volume <= buy_volume {
                    // fill the sell order
                    trade.add_execution(Execution::new(
                        sell_order.id,
                        sell_order.price,
                        sell_volume,
                    ));
                    // remove order from the level
                    l.orders.pop_front();
                    l.cancell_order(&sell_order);
                    sell_order.volume = Volume::ZERO;
                    buy_volume -= sell_volume;
                } else {
                    // fill the buy order, put the order back to the book
                    let execution = Execution::new(sell_order.id, sell_order.price, buy_volume);
                    trade.add_execution(execution);
                    sell_order.volume -= buy_volume;
                    buy_volume = Volume::ZERO;
                }
                // we should put back the sell order if it was not completely filled
                if !sell_order.volume.is_zero() {
                    self.orders.insert(sell_order.id, sell_order);
                }
                // if buy order was filled completely, we can break the loop
                if buy_volume.is_zero() {
                    break 'top;
                }
                // otherwise we still have volume to fill
            } // no more orders at the level, we can move to the next level
        }
        Ok(trade)
    }

    fn fill_sell_order(&mut self, sell_order: &Order) -> Result<Trade, PlaceOrderError> {
        // find the highest bid Limit
        // if the highest bid Limit is greater than or equal to the ask Limit, we can fill the order, substracting the volume
        // if the highest bid Limit is less than the ask Limit, we add the order to the book, with the volume
        // equal to the order quantity

        let mut sell_volume = sell_order.volume;
        let mut trade = Trade::new(sell_order.id, sell_order.volume);

        let sorted = self
            .bids
            .levels
            .values_mut()
            .filter(|l| filter_limit_for_sell(l, &sell_order.price))
            .sorted_by(sort_limit_descending);

        'top: for l in sorted {
            // peek order at front of the level
            while let Some(oid) = l.orders.front() {
                // todo: remove might trigger memcpy
                // although we need to get the owned value otherwise we will be borrowing self hence problem with borrow checker
                let Some(mut buy_order) = self.orders.remove(oid) else {
                    // if there is no order then it might have been cancelled
                    // and removed from the map, and since we pospone the removal of orders from the level
                    // till we encounter such order, we can safely remove the order from the level
                    l.orders.pop_front();
                    continue;
                };
                let buy_volume = buy_order.volume;
                if buy_volume <= sell_volume {
                    // fill the sell order
                    trade.add_execution(Execution::new(buy_order.id, buy_order.price, buy_volume));
                    // remove order from the level
                    l.orders.pop_front();
                    l.cancell_order(&buy_order);
                    buy_order.volume = Volume::ZERO;
                    sell_volume -= buy_volume;
                } else {
                    // fill the buy order, put the order back to the book
                    let execution = Execution::new(buy_order.id, buy_order.price, sell_volume);
                    trade.add_execution(execution);
                    buy_order.volume -= sell_volume;
                    sell_volume = Volume::ZERO;
                }
                // we should put back the sell order if it was not completely filled
                if !buy_order.volume.is_zero() {
                    self.orders.insert(buy_order.id, buy_order);
                }
                // if sell order was filled completely, we can break the loop
                if sell_volume.is_zero() {
                    break 'top;
                }
                // otherwise we still have volume to fill
            }
        }
        Ok(trade)
    }

    /// cancellation does not modify any of the underlying collections. Order is marked as cancelled and will be removed
    /// at the time of order filling, when we iterate over the orders
    pub fn cancel_order(&mut self, order_id: Oid) -> Result<CancellationReport, CancelOrderError> {
        // immutable borrows of self, therefore the need for new scope
        // so if we do not return err then the immutable borrow will go out of scope
        // and will allow for mutable borrow to allow for removal of the order from hashmap
        match self.orders.remove(&order_id) {
            None => return Err(CancelOrderError::NotFound(order_id)),
            Some(order) => {
                // update the level so the level volume is updated
                match order.side {
                    OrderSide::Buy => self.bids.cancel_order(&order),
                    OrderSide::Sell => self.asks.cancel_order(&order),
                }
            }
        }
        Ok(CancellationReport {
            order_id,
            status: CancellationStatus::Cancelled,
        })
    }

    /// get volume of open orders for either buying or selling side of the book
    pub fn get_volume_at_limit(&self, limit: Price, side: OrderSide) -> Option<Volume> {
        let limit_map = match side {
            OrderSide::Buy => &self.bids,
            OrderSide::Sell => &self.asks,
        };
        limit_map
            .level_map
            .get(&limit)
            .map(|index| limit_map.levels[*index].total_volume)
    }
}

// we want to inline since this is a small function and we want to avoid the overhead of a function call
#[inline]
#[allow(clippy::needless_lifetimes)]
fn sort_limit_descending<'a, 'b>(l: &'a &mut Level, r: &'b &mut Level) -> std::cmp::Ordering {
    l.price.cmp(&r.price).reverse()
}
#[inline]
#[allow(clippy::needless_lifetimes)]
fn filter_limit_for_buy<'a>(l: &'a &mut Level, price: &Option<Price>) -> bool {
    if l.total_volume > 0.into() {
        // in case price is none, we want to return true since we are in market order which has no price
        return price.map(|p| l.price <= p).unwrap_or(true);
    }
    false
}
#[inline]
#[allow(clippy::needless_lifetimes)]
fn filter_limit_for_sell<'a>(l: &'a &mut Level, price: &Option<Price>) -> bool {
    if l.total_volume > 0.into() {
        // in case price is none, we want to return true since we are in market order which has no price
        return price.map(|p| l.price >= p).unwrap_or(true);
    }
    false
}

mod tests_limit_map {

    #[test]
    fn test_limit_map() {
        let mut limit_map = crate::Limits::default();
        let order = crate::LimitOrder::new(
            crate::primitives::Oid::new(1),
            crate::OrderSide::Buy,
            crate::primitives::Timestamp::new(1),
            21.0453.into(),
            100.into(),
        );
        limit_map.add_order(&order);
    }
}

mod tests_order_book {

    #[test]
    fn test_order_book_new() {
        let order_book = crate::OrderBook::default();
        assert_eq!(order_book.bids.best, None);
        assert_eq!(order_book.asks.best, None);
        assert_eq!(order_book.orders.len(), 0);
        assert_eq!(order_book.spread, None);
    }

    #[test]
    fn test_cancel_order() {
        let mut order_book = crate::OrderBook::default();
        let order = &crate::Order::new_limit(
            crate::primitives::Oid::new(1),
            crate::OrderSide::Buy,
            chrono::Utc::now().into(),
            21.0453.into(),
            100.into(),
        );
        let _ = order_book.execute(order);
        assert_eq!(order_book.orders.len(), 1);
        let order = order_book
            .cancel_order(crate::primitives::Oid::new(1))
            .unwrap();
        assert_eq!(order_book.orders.len(), 0);
        assert_eq!(order.order_id, crate::primitives::Oid::new(1));
        assert_eq!(order.status, crate::CancellationStatus::Cancelled);

        let order = &crate::Order::new_limit(
            crate::primitives::Oid::new(2),
            crate::OrderSide::Buy,
            chrono::Utc::now().into(),
            21.0453.into(),
            50.into(),
        );
        let _ = order_book.execute(order);
        assert_eq!(order_book.orders.len(), 1);
        let order = order_book
            .cancel_order(crate::primitives::Oid::new(2))
            .unwrap();
        assert_eq!(order_book.orders.len(), 0);
        assert_eq!(order.order_id, crate::primitives::Oid::new(2));
        assert_eq!(order.status, crate::CancellationStatus::Cancelled);
    }

    #[test]
    fn test_execute_buy_order() {
        let mut order_book = crate::OrderBook::default();
        let order = &crate::Order::new_limit(
            crate::primitives::Oid::new(1),
            crate::OrderSide::Sell,
            chrono::Utc::now().into(),
            21.0453.into(),
            100.into(),
        );
        let trade = order_book.execute(order).unwrap();
        assert_eq!(trade.order_id, crate::primitives::Oid::new(1));
        assert_eq!(trade.volume, 100.into());
        assert_eq!(trade.filled_volume, 0.into());
        assert_eq!(trade.executions.len(), 0);

        let order = &crate::Order::new_limit(
            crate::primitives::Oid::new(3),
            crate::OrderSide::Sell,
            chrono::Utc::now().into(),
            21.0454.into(),
            50.into(),
        );
        let trade = order_book.execute(order).unwrap();
        assert_eq!(trade.order_id, crate::primitives::Oid::new(3));
        assert_eq!(trade.volume, 50.into());
        assert_eq!(trade.filled_volume, 0.into());
        assert_eq!(trade.executions.len(), 0);

        let order = &crate::Order::new_limit(
            crate::primitives::Oid::new(2),
            crate::OrderSide::Buy,
            chrono::Utc::now().into(),
            21.0455.into(),
            125.into(),
        );

        let trade = order_book.execute(order).unwrap();
        assert_eq!(trade.order_id, crate::primitives::Oid::new(2));
        assert_eq!(trade.volume, 125.into());
        assert_eq!(trade.filled_volume, 125.into());
        assert_eq!(trade.executions.len(), 2);
        let execution = &trade.executions[0];
        assert_eq!(execution.order_id, crate::primitives::Oid::new(1));
        assert_eq!(execution.price, 21.0453.into());
        assert_eq!(execution.volume, 100.into());
        let execution = &trade.executions[1];
        assert_eq!(execution.order_id, crate::primitives::Oid::new(3));
        assert_eq!(execution.price, 21.0454.into());
        assert_eq!(execution.volume, 25.into());
    }

    #[test]
    fn test_market_order_should_result_in_empty_order_book() {
        let mut order_book = crate::OrderBook::default();
        let order = &crate::Order::new_limit(
            crate::primitives::Oid::new(1),
            crate::OrderSide::Sell,
            chrono::Utc::now().into(),
            21.0453.into(),
            100.into(),
        );
        let _ = order_book.execute(order);

        let order = &crate::Order::new_limit(
            crate::primitives::Oid::new(2),
            crate::OrderSide::Sell,
            chrono::Utc::now().into(),
            21.0454.into(),
            50.into(),
        );
        let _ = order_book.execute(order);

        let order = &crate::Order::new_market(
            crate::primitives::Oid::new(3),
            crate::OrderSide::Buy,
            chrono::Utc::now().into(),
            150.into(),
        );
        let trade = order_book.execute(order).unwrap();
        assert_eq!(trade.order_id, crate::primitives::Oid::new(3));
        assert_eq!(trade.volume, 150.into());
        assert_eq!(trade.filled_volume, 150.into());
        assert_eq!(trade.executions.len(), 2);
        let execution = &trade.executions[0];
        assert_eq!(execution.order_id, crate::primitives::Oid::new(1));
        assert_eq!(execution.price, 21.0453.into());
        assert_eq!(execution.volume, 100.into());
        let execution = &trade.executions[1];
        assert_eq!(execution.order_id, crate::primitives::Oid::new(2));
        assert_eq!(execution.price, 21.0454.into());
        assert_eq!(execution.volume, 50.into());

        assert_eq!(order_book.orders.len(), 0);
    }

    #[test]
    fn test_sell_market_order_should_result_in_empty_order_book() {
        let mut order_book = crate::OrderBook::default();
        let order = &crate::Order::new_limit(
            crate::primitives::Oid::new(1),
            crate::OrderSide::Buy,
            chrono::Utc::now().into(),
            21.0453.into(),
            100.into(),
        );
        let _ = order_book.execute(order);

        let order = &crate::Order::new_limit(
            crate::primitives::Oid::new(2),
            crate::OrderSide::Buy,
            chrono::Utc::now().into(),
            21.0454.into(),
            50.into(),
        );
        let _ = order_book.execute(order);

        let order = &crate::Order::new_market(
            crate::primitives::Oid::new(3),
            crate::OrderSide::Sell,
            chrono::Utc::now().into(),
            150.into(),
        );
        let trade = order_book.execute(order).unwrap();

        assert_eq!(trade.order_id, crate::primitives::Oid::new(3));
        assert_eq!(trade.volume, 150.into());
        assert_eq!(trade.filled_volume, 150.into());
        assert_eq!(trade.executions.len(), 2);
        let execution = &trade.executions[0];
        assert_eq!(execution.order_id, crate::primitives::Oid::new(2));
        assert_eq!(execution.price, 21.0454.into());
        assert_eq!(execution.volume, 50.into());
        let execution = &trade.executions[1];
        assert_eq!(execution.order_id, crate::primitives::Oid::new(1));
        assert_eq!(execution.price, 21.0453.into());
        assert_eq!(execution.volume, 100.into());

        assert_eq!(order_book.orders.len(), 0);
    }
}