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
// Allow our dollar.cents digit grouping convention (e.g., 100_00 = $100.00)
//! # nanobook
//!
//! A deterministic limit order book and matching engine for testing trading algorithms.
//!
//! ## Features
//!
//! - **Order types**: Limit, Market, Cancel, Modify
//! - **Time-in-force**: GTC (Good-til-cancelled), IOC (Immediate-or-cancel), FOK (Fill-or-kill)
//! - **Price-time priority**: FIFO matching at each price level
//! - **Deterministic replay**: Record events and replay to reconstruct exact state
//! - **Fixed-point prices**: Avoid floating-point errors with integer cents
//!
//! ## Quick Start
//!
//! ```
//! use nanobook::{Exchange, Side, Price, TimeInForce};
//!
//! let mut exchange = Exchange::new();
//!
//! // Place some resting asks (sell orders)
//! exchange.submit_limit(Side::Sell, Price(101_00), 100, TimeInForce::GTC);
//! exchange.submit_limit(Side::Sell, Price(102_00), 200, TimeInForce::GTC);
//!
//! // Place a bid that crosses — this will match!
//! let result = exchange.submit_limit(Side::Buy, Price(101_00), 50, TimeInForce::GTC);
//!
//! assert_eq!(result.filled_quantity, 50);
//! assert_eq!(result.trades.len(), 1);
//! assert_eq!(result.trades[0].price, Price(101_00));
//! ```
//!
//! ## Price Representation
//!
//! Prices are stored as [`i64`] in the smallest unit (e.g., cents):
//!
//! ```
//! use nanobook::Price;
//!
//! let price = Price(100_50); // $100.50
//! assert_eq!(format!("{}", price), "$100.50");
//! ```
//!
//! ## Time-in-Force
//!
//! | TIF | Behavior |
//! |-----|----------|
//! | **GTC** | Rests on book until filled or cancelled |
//! | **IOC** | Fill immediately, cancel unfilled remainder |
//! | **FOK** | Fill entirely or cancel entirely (no partial fills) |
//!
//! ```
//! use nanobook::{Exchange, Side, Price, TimeInForce};
//!
//! let mut exchange = Exchange::new();
//!
//! // IOC: Fill what's available, cancel the rest
//! 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.filled_quantity, 30);
//! assert_eq!(result.cancelled_quantity, 70);
//!
//! // FOK: Must fill entirely or nothing happens
//! exchange.submit_limit(Side::Sell, Price(100_00), 50, TimeInForce::GTC);
//! let result = exchange.submit_limit(Side::Buy, Price(100_00), 100, TimeInForce::FOK);
//! assert_eq!(result.filled_quantity, 0); // Rejected: only 50 available
//! assert!(result.trades.is_empty());
//! ```
//!
//! ## Market Orders
//!
//! Market orders execute at the best available prices:
//!
//! ```
//! use nanobook::{Exchange, Side, Price, TimeInForce};
//!
//! let mut exchange = Exchange::new();
//! exchange.submit_limit(Side::Sell, Price(100_00), 50, TimeInForce::GTC);
//! exchange.submit_limit(Side::Sell, Price(101_00), 50, TimeInForce::GTC);
//!
//! // Market buy sweeps through price levels
//! let result = exchange.submit_market(Side::Buy, 75);
//! assert_eq!(result.trades.len(), 2);
//! assert_eq!(result.trades[0].price, Price(100_00)); // Best price first
//! assert_eq!(result.trades[1].price, Price(101_00));
//! ```
//!
//! ## Cancel and Modify
//!
//! ```
//! use nanobook::{Exchange, Side, Price, TimeInForce};
//!
//! let mut exchange = Exchange::new();
//!
//! let order = exchange.submit_limit(Side::Buy, Price(99_00), 100, TimeInForce::GTC);
//!
//! // Cancel: removes the order from the book
//! let cancel = exchange.cancel(order.order_id);
//! assert!(cancel.success);
//!
//! // Modify: cancel-and-replace (new order gets new ID, loses time priority)
//! let order2 = exchange.submit_limit(Side::Buy, Price(99_00), 100, TimeInForce::GTC);
//! let modify = exchange.modify(order2.order_id, Price(98_00), 150);
//! assert!(modify.success);
//! assert_ne!(modify.new_order_id, Some(order2.order_id));
//! ```
//!
//! ## Event Replay
//!
//! All operations are recorded as events for deterministic replay
//! (requires the `event-log` feature, enabled by default):
//!
//! ```ignore
//! use nanobook::{Exchange, Side, Price, TimeInForce};
//!
//! let mut exchange = Exchange::new();
//! exchange.submit_limit(Side::Sell, Price(101_00), 100, TimeInForce::GTC);
//! exchange.submit_limit(Side::Buy, Price(100_00), 100, TimeInForce::GTC);
//! exchange.submit_limit(Side::Buy, Price(101_00), 50, TimeInForce::GTC);
//!
//! // Save events
//! let events = exchange.events().to_vec();
//!
//! // Replay on a fresh exchange — produces identical state
//! let replayed = Exchange::replay(&events);
//! assert_eq!(exchange.best_bid_ask(), replayed.best_bid_ask());
//! assert_eq!(exchange.trades().len(), replayed.trades().len());
//! ```
//!
//! ## Book Snapshots
//!
//! Get market data snapshots:
//!
//! ```
//! use nanobook::{Exchange, Side, Price, TimeInForce};
//!
//! let mut exchange = Exchange::new();
//! exchange.submit_limit(Side::Buy, Price(99_00), 100, TimeInForce::GTC);
//! exchange.submit_limit(Side::Buy, Price(100_00), 200, TimeInForce::GTC);
//! exchange.submit_limit(Side::Sell, Price(101_00), 150, TimeInForce::GTC);
//!
//! let snap = exchange.depth(10); // Top 10 levels each side
//!
//! assert_eq!(snap.best_bid(), Some(Price(100_00)));
//! assert_eq!(snap.best_ask(), Some(Price(101_00)));
//! assert_eq!(snap.spread(), Some(100)); // $1.00
//! ```
// Re-export public API
pub use OrderBook;
pub use ValidationError;
pub use ;
pub use Exchange;
pub use Level;
pub use MatchResult;
pub use MultiExchange;
pub use ;
pub use PriceLevels;
pub use ;
pub use Side;
pub use ;
pub use ;
pub use TimeInForce;
pub use Trade;
pub use ;