otterbook_core 0.1.3

A high-performance, lock-free price level implementation for limit order books in Rust. This library provides the building blocks for creating efficient trading systems with support for multiple order types and concurrent access patterns.
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
#[cfg(test)]
mod tests {
    use crate::{OrderBook, OrderBookError};
    use pricelevel::{OrderId, Side, TimeInForce};
    use uuid::Uuid;

    // Helper function to create a random OrderId
    fn new_order_id() -> OrderId {
        OrderId(Uuid::new_v4())
    }

    // Helper function to create an order book for testing
    fn create_test_order_book() -> OrderBook {
        OrderBook::new("TEST-SYMBOL")
    }

    #[test]
    fn test_add_limit_order() {
        let order_book = create_test_order_book();
        let id = new_order_id();
        let price = 1000;
        let quantity = 10;
        let side = Side::Buy;
        let time_in_force = TimeInForce::Gtc;

        let result = order_book.add_limit_order(id, price, quantity, side, time_in_force);
        assert!(result.is_ok(), "Adding a limit order should succeed");

        let order = result.unwrap();
        assert_eq!(order.id(), id, "Order ID should match");
        assert_eq!(order.price(), price, "Price should match");
        assert_eq!(order.visible_quantity(), quantity, "Quantity should match");
        assert_eq!(order.side(), side, "Side should match");
        assert_eq!(
            order.time_in_force(),
            time_in_force,
            "Time in force should match"
        );

        // Verify the order is in the book
        let book_order = order_book.get_order(id);
        assert!(book_order.is_some(), "Order should be in the book");
    }

    #[test]
    fn test_add_iceberg_order() {
        let order_book = create_test_order_book();
        let id = new_order_id();
        let price = 1000;
        let visible_quantity = 10;
        let hidden_quantity = 90;
        let side = Side::Sell;
        let time_in_force = TimeInForce::Gtc;

        let result = order_book.add_iceberg_order(
            id,
            price,
            visible_quantity,
            hidden_quantity,
            side,
            time_in_force,
        );
        assert!(result.is_ok(), "Adding an iceberg order should succeed");

        let order = result.unwrap();
        assert_eq!(order.id(), id, "Order ID should match");
        assert_eq!(order.price(), price, "Price should match");
        assert_eq!(
            order.visible_quantity(),
            visible_quantity,
            "Visible quantity should match"
        );
        assert_eq!(
            order.hidden_quantity(),
            hidden_quantity,
            "Hidden quantity should match"
        );
        assert_eq!(order.side(), side, "Side should match");
        assert_eq!(
            order.time_in_force(),
            time_in_force,
            "Time in force should match"
        );

        // Verify the order is in the book
        let book_order = order_book.get_order(id);
        assert!(book_order.is_some(), "Order should be in the book");
    }

    #[test]
    fn test_add_post_only_order() {
        let order_book = create_test_order_book();
        let id = new_order_id();
        let price = 1000;
        let quantity = 10;
        let side = Side::Buy;
        let time_in_force = TimeInForce::Gtc;

        let result = order_book.add_post_only_order(id, price, quantity, side, time_in_force);
        assert!(result.is_ok(), "Adding a post-only order should succeed");

        let order = result.unwrap();
        assert_eq!(order.id(), id, "Order ID should match");
        assert_eq!(order.price(), price, "Price should match");
        assert_eq!(order.visible_quantity(), quantity, "Quantity should match");
        assert_eq!(order.side(), side, "Side should match");
        assert_eq!(
            order.time_in_force(),
            time_in_force,
            "Time in force should match"
        );
        assert!(order.is_post_only(), "Order should be post-only");

        // Verify the order is in the book
        let book_order = order_book.get_order(id);
        assert!(book_order.is_some(), "Order should be in the book");
    }

    #[test]
    fn test_post_only_order_price_crossing() {
        let order_book = create_test_order_book();

        // First add a sell order at price 1000
        let sell_id = new_order_id();
        let sell_result =
            order_book.add_limit_order(sell_id, 1000, 10, Side::Sell, TimeInForce::Gtc);
        assert!(sell_result.is_ok(), "Adding sell order should succeed");

        // Now try to add a post-only buy order at price 1000 (would cross)
        let buy_id = new_order_id();
        let buy_result =
            order_book.add_post_only_order(buy_id, 1000, 10, Side::Buy, TimeInForce::Gtc);

        assert!(
            buy_result.is_err(),
            "Post-only order that would cross should fail"
        );
        match buy_result {
            Err(OrderBookError::PriceCrossing {
                price,
                side,
                opposite_price,
            }) => {
                assert_eq!(price, 1000, "Price should match");
                assert_eq!(side, Side::Buy, "Side should be buy");
                assert_eq!(opposite_price, 1000, "Opposite price should match");
            }
            _ => panic!("Expected PriceCrossing error"),
        }
    }

    #[test]
    fn test_submit_market_order() {
        let order_book = create_test_order_book();

        // First add a limit sell order
        let sell_id = new_order_id();
        let sell_result =
            order_book.add_limit_order(sell_id, 1000, 10, Side::Sell, TimeInForce::Gtc);
        assert!(sell_result.is_ok(), "Adding sell order should succeed");

        // Now submit a market buy order
        let buy_id = new_order_id();
        let market_result = order_book.submit_market_order(
            buy_id,
            5, // Half of the available quantity
            Side::Buy,
        );

        assert!(market_result.is_ok(), "Market order should succeed");
        let match_result = market_result.unwrap();

        // Check match result
        assert_eq!(match_result.order_id, buy_id, "Order ID should match");
        assert_eq!(
            match_result.executed_quantity(),
            5,
            "Should execute requested quantity"
        );
        assert_eq!(match_result.remaining_quantity, 0, "No remaining quantity");
        assert!(match_result.is_complete, "Order should be complete");
        assert_eq!(
            match_result.transactions.len(),
            1,
            "Should have one transaction"
        );

        // Check transaction details
        let transaction = &match_result.transactions.as_vec()[0];
        assert_eq!(
            transaction.taker_order_id, buy_id,
            "Taker should be market order"
        );
        assert_eq!(
            transaction.maker_order_id, sell_id,
            "Maker should be limit order"
        );
        assert_eq!(
            transaction.price, 1000,
            "Price should match limit order price"
        );
        assert_eq!(
            transaction.quantity, 5,
            "Quantity should match market order size"
        );
        assert_eq!(
            transaction.taker_side,
            Side::Buy,
            "Taker side should be buy"
        );

        // Verify the sell order is still in the book with reduced quantity
        let updated_sell = order_book.get_order(sell_id);
        assert!(
            updated_sell.is_some(),
            "Sell order should still be in the book"
        );
        assert_eq!(
            updated_sell.unwrap().visible_quantity(),
            5,
            "Sell order should have reduced quantity"
        );
    }

    #[test]
    fn test_submit_market_order_full_fill() {
        let order_book = create_test_order_book();

        // First add a limit sell order
        let sell_id = new_order_id();
        let sell_result =
            order_book.add_limit_order(sell_id, 1000, 10, Side::Sell, TimeInForce::Gtc);
        assert!(sell_result.is_ok(), "Adding sell order should succeed");

        // Now submit a market buy order for the full amount
        let buy_id = new_order_id();
        let market_result = order_book.submit_market_order(buy_id, 10, Side::Buy);

        assert!(market_result.is_ok(), "Market order should succeed");
        let match_result = market_result.unwrap();

        assert_eq!(
            match_result.executed_quantity(),
            10,
            "Should execute full quantity"
        );
        assert!(
            match_result.filled_order_ids.contains(&sell_id),
            "Sell order should be marked as filled"
        );

        // Verify the sell order is no longer in the book
        let updated_sell = order_book.get_order(sell_id);
        assert!(
            updated_sell.is_none(),
            "Sell order should be removed from the book"
        );
    }

    #[test]
    fn test_submit_market_order_insufficient_liquidity() {
        let order_book = create_test_order_book();

        // First add a limit sell order
        let sell_id = new_order_id();
        let sell_result =
            order_book.add_limit_order(sell_id, 1000, 10, Side::Sell, TimeInForce::Gtc);
        assert!(sell_result.is_ok(), "Adding sell order should succeed");

        // Now submit a market buy order for more than available
        let buy_id = new_order_id();
        let market_result = order_book.submit_market_order(buy_id, 20, Side::Buy);

        // The market order should execute partially
        assert!(
            market_result.is_ok(),
            "Market order should succeed with partial fill"
        );
        let match_result = market_result.unwrap();

        assert_eq!(
            match_result.executed_quantity(),
            10,
            "Should execute available quantity"
        );
        assert_eq!(
            match_result.remaining_quantity, 10,
            "Should have remaining quantity"
        );
        assert!(!match_result.is_complete, "Order should not be complete");
    }

    #[test]
    fn test_market_order_no_liquidity() {
        let order_book = create_test_order_book();

        // Submit a market buy order with no matching orders
        let buy_id = new_order_id();
        let market_result = order_book.submit_market_order(buy_id, 10, Side::Buy);

        assert!(
            market_result.is_err(),
            "Market order with no liquidity should fail"
        );
        match market_result {
            Err(OrderBookError::InsufficientLiquidity {
                side,
                requested,
                available,
            }) => {
                assert_eq!(side, Side::Buy, "Side should be buy");
                assert_eq!(requested, 10, "Requested quantity should match");
                assert_eq!(available, 0, "Available should be zero");
            }
            _ => panic!("Expected InsufficientLiquidity error"),
        }
    }

    #[test]
    fn test_limit_order_immediate_or_cancel() {
        let order_book = create_test_order_book();

        // Add a sell order
        let sell_id = new_order_id();
        let sell_result =
            order_book.add_limit_order(sell_id, 1000, 10, Side::Sell, TimeInForce::Gtc);
        assert!(sell_result.is_ok(), "Adding sell order should succeed");

        // Add a buy IOC order
        let buy_id = new_order_id();
        let buy_result = order_book.add_limit_order(
            buy_id,
            1000, // Same price so will match
            5,    // Half of the available quantity
            Side::Buy,
            TimeInForce::Ioc,
        );

        assert!(buy_result.is_ok(), "Adding IOC buy order should succeed");

        // IOC order should not be in the book
        let buy_order = order_book.get_order(buy_id);
        assert!(
            buy_order.is_none(),
            "IOC order should not be in the book after execution"
        );

        // Sell order should be partially filled
        let sell_order = order_book.get_order(sell_id);
        assert!(
            sell_order.is_some(),
            "Sell order should still be in the book"
        );
        assert_eq!(
            sell_order.unwrap().visible_quantity(),
            5,
            "Sell order should have reduced quantity"
        );
    }

    #[test]
    fn test_limit_order_fill_or_kill_success() {
        let order_book = create_test_order_book();

        // Add a sell order
        let sell_id = new_order_id();
        let sell_result =
            order_book.add_limit_order(sell_id, 1000, 10, Side::Sell, TimeInForce::Gtc);
        assert!(sell_result.is_ok(), "Adding sell order should succeed");

        // Add a buy FOK order that can be fully filled
        let buy_id = new_order_id();
        let buy_result = order_book.add_limit_order(
            buy_id,
            1000,
            10, // Full quantity available
            Side::Buy,
            TimeInForce::Fok,
        );

        assert!(
            buy_result.is_ok(),
            "Adding FOK buy order should succeed when fully fillable"
        );

        // FOK order should not be in the book after execution
        let buy_order = order_book.get_order(buy_id);
        assert!(
            buy_order.is_none(),
            "FOK order should not be in the book after execution"
        );

        // Sell order should be fully filled and removed
        let sell_order = order_book.get_order(sell_id);
        assert!(
            sell_order.is_none(),
            "Sell order should be removed from the book"
        );
    }

    #[test]
    fn test_limit_order_fill_or_kill_failure() {
        let order_book = create_test_order_book();

        // Add a sell order
        let sell_id = new_order_id();
        let sell_result = order_book.add_limit_order(
            sell_id,
            1000,
            5, // Only 5 units available
            Side::Sell,
            TimeInForce::Gtc,
        );
        assert!(sell_result.is_ok(), "Adding sell order should succeed");

        // Add a buy FOK order that cannot be fully filled
        let buy_id = new_order_id();
        let buy_result = order_book.add_limit_order(
            buy_id,
            1000,
            10, // Requires more than available
            Side::Buy,
            TimeInForce::Fok,
        );

        assert!(
            buy_result.is_err(),
            "Adding FOK buy order should fail when not fully fillable"
        );
        match buy_result {
            Err(OrderBookError::InsufficientLiquidity {
                side,
                requested,
                available,
            }) => {
                assert_eq!(side, Side::Buy, "Side should be buy");
                assert_eq!(requested, 10, "Requested quantity should match");
                assert_eq!(available, 5, "Available quantity should match");
            }
            _ => panic!("Expected InsufficientLiquidity error"),
        }

        // Sell order should remain unchanged
        let sell_order = order_book.get_order(sell_id);
        assert!(
            sell_order.is_some(),
            "Sell order should still be in the book"
        );
        assert_eq!(
            sell_order.unwrap().visible_quantity(),
            5,
            "Sell order quantity should be unchanged"
        );
    }
}

#[cfg(test)]
mod test_operations_remaining {
    use crate::OrderBook;
    use pricelevel::{OrderId, Side, TimeInForce};
    use uuid::Uuid;

    fn create_order_id() -> OrderId {
        OrderId(Uuid::new_v4())
    }

    #[test]
    fn test_add_limit_order_with_trace() {
        let book = OrderBook::new("TEST");

        // Add a limit order with detailed tracing
        let id = create_order_id();
        let price = 1000;
        let quantity = 10;
        let side = Side::Buy;
        let time_in_force = TimeInForce::Gtc;

        let result = book.add_limit_order(id, price, quantity, side, time_in_force);
        assert!(result.is_ok());

        // Verify order was added correctly
        let order = result.unwrap();
        assert_eq!(order.id(), id);
        assert_eq!(order.price(), price);
        assert_eq!(order.visible_quantity(), quantity);
    }

    #[test]
    fn test_add_iceberg_order_with_trace() {
        let book = OrderBook::new("TEST");

        // Add an iceberg order with detailed tracing
        let id = create_order_id();
        let price = 1000;
        let visible_quantity = 10;
        let hidden_quantity = 90;
        let side = Side::Sell;
        let time_in_force = TimeInForce::Gtc;

        let result = book.add_iceberg_order(
            id,
            price,
            visible_quantity,
            hidden_quantity,
            side,
            time_in_force,
        );
        assert!(result.is_ok());

        // Verify order was added correctly
        let order = result.unwrap();
        assert_eq!(order.id(), id);
        assert_eq!(order.price(), price);
        assert_eq!(order.visible_quantity(), visible_quantity);
        assert_eq!(order.hidden_quantity(), hidden_quantity);
    }

    #[test]
    fn test_add_post_only_order_with_trace() {
        let book = OrderBook::new("TEST");

        // Add a post-only order with detailed tracing
        let id = create_order_id();
        let price = 1000;
        let quantity = 10;
        let side = Side::Buy;
        let time_in_force = TimeInForce::Gtc;

        let result = book.add_post_only_order(id, price, quantity, side, time_in_force);
        assert!(result.is_ok());

        // Verify order was added correctly
        let order = result.unwrap();
        assert_eq!(order.id(), id);
        assert_eq!(order.price(), price);
        assert_eq!(order.visible_quantity(), quantity);
        assert!(order.is_post_only());
    }
}

#[cfg(test)]
mod test_operations_specific {
    use crate::OrderBook;
    use pricelevel::{OrderId, Side, TimeInForce};
    use uuid::Uuid;

    use tracing::trace;

    fn create_order_id() -> OrderId {
        OrderId(Uuid::new_v4())
    }

    #[test]
    fn test_submit_market_order_with_tracing() {
        let book = OrderBook::new("TEST");

        // Add a sell order
        let sell_id = create_order_id();
        let _ = book.add_limit_order(sell_id, 1000, 10, Side::Sell, TimeInForce::Gtc);

        // Submit a market buy order with tracing enabled
        let buy_id = create_order_id();

        // Use trace macro to generate some trace output
        trace!(
            "About to submit market order {} for {} at side {:?}",
            buy_id,
            5,
            Side::Buy
        );

        let result = book.submit_market_order(buy_id, 5, Side::Buy);
        assert!(result.is_ok());

        // Verify order was matched correctly
        let remaining_sell = book.get_order(sell_id);
        assert!(remaining_sell.is_some());
        assert_eq!(remaining_sell.unwrap().visible_quantity(), 5);
    }
}