chronicle-core 0.1.0

Single-threaded deterministic limit order book and matching engine with zero steady-state allocation
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
//! A deliberately simple reference order book, compiled only under
//! `cfg(test)`, used as the property-test oracle.
//!
//! It favours obviousness over speed: orders sit in a `BTreeMap`, and
//! every match step rescans all orders to find the best maker by
//! (price, arrival). It shares no code with the engine, so a bug in the
//! arena, bitmap, or lookup cannot hide by being made twice. It works
//! in raw price space and assumes its inputs were validated (the
//! generators only produce valid, grid-aligned commands).
//!
//! Milestone 2: the oracle implements the full command set
//! independently: every order type, the three self-trade prevention
//! policies, amend, and cancel-by-participant. Where the engine rejects
//! in preflight (`PostOnly` crossing, `Fok` shortfall, amend crossing), the
//! oracle reports the same rejection through [`OracleReject`] and
//! mutates nothing, so the harness can compare errors as well as event
//! streams.

use std::collections::BTreeMap;

use crate::events::{CancelReason, ExecutionEvent};
use crate::types::{OrderId, OrderType, Side, StpPolicy};

/// One resting order in the reference book.
#[derive(Clone, Copy, Debug)]
pub struct RefOrder {
    /// Participant that owns the order.
    pub participant: u32,
    /// Side the order rests on.
    pub side: Side,
    /// Raw price.
    pub price: u64,
    /// Remaining quantity.
    pub qty: u64,
    /// Monotonic arrival counter, the FIFO tiebreak.
    pub arrival: u64,
}

/// A preflight rejection the oracle computes independently, mirroring
/// the engine's typed errors.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum OracleReject {
    /// `PostOnly` price would cross the opposite best.
    PostOnlyWouldCross {
        /// The raw opposite best price.
        opposite_best: u64,
    },
    /// `Fok` order cannot fill in full; `available` is the STP-adjusted
    /// executable quantity.
    FokInsufficient {
        /// STP-adjusted executable quantity.
        available: u64,
    },
    /// Amend price would cross the opposite best.
    AmendWouldCross {
        /// The raw opposite best price.
        opposite_best: u64,
    },
    /// Cancel or amend named an order that is not live.
    NotFound,
}

/// The oracle book.
#[derive(Default)]
pub struct ReferenceBook {
    /// Live orders by id.
    pub orders: BTreeMap<u64, RefOrder>,
    /// Next arrival counter value.
    arrival: u64,
    /// Next sequence number, mirroring the engine rule (starts at 1,
    /// one per accepted command).
    next_seq: u64,
}

impl ReferenceBook {
    /// An empty oracle.
    pub fn new() -> Self {
        Self {
            orders: BTreeMap::new(),
            arrival: 0,
            next_seq: 1,
        }
    }

    /// True when an order with this id is live.
    pub fn contains(&self, order_id: u64) -> bool {
        self.orders.contains_key(&order_id)
    }

    /// Add a Limit order and return its full event stream. Inputs are
    /// assumed valid and the id unused.
    pub fn add_limit(
        &mut self,
        order_id: u64,
        participant: u32,
        side: Side,
        price: u64,
        qty: u64,
    ) -> Vec<ExecutionEvent> {
        self.add_typed(
            order_id,
            participant,
            side,
            OrderType::Limit,
            price,
            qty,
            StpPolicy::CancelIncoming,
        )
        .expect("a Limit add never rejects in preflight")
    }

    /// Add an order of any type under an already-resolved STP policy
    /// (the harness resolves the engine's `Option` against the book
    /// default before calling). On an `Err` the oracle has mutated
    /// nothing, including its sequence number.
    #[allow(
        clippy::too_many_arguments,
        reason = "mirrors the engine add_order parameter list"
    )]
    pub fn add_typed(
        &mut self,
        order_id: u64,
        participant: u32,
        side: Side,
        order_type: OrderType,
        price: u64,
        qty: u64,
        stp: StpPolicy,
    ) -> Result<Vec<ExecutionEvent>, OracleReject> {
        if order_type == OrderType::PostOnly
            && let Some(best) = self.best(side.opposite())
        {
            let crosses = match side {
                Side::Bid => price >= best.0,
                Side::Ask => price <= best.0,
            };
            if crosses {
                return Err(OracleReject::PostOnlyWouldCross {
                    opposite_best: best.0,
                });
            }
        }
        // Work on a scratch copy so a Fok shortfall mutates nothing,
        // not even the resting orders STP would have cancelled.
        let mut orders = self.orders.clone();
        let seq = self.next_seq;
        let (mut events, remaining, stp_stopped) = if order_type == OrderType::PostOnly {
            (Vec::new(), qty, false)
        } else {
            Self::sweep(
                &mut orders,
                seq,
                order_id,
                participant,
                side,
                price,
                qty,
                stp,
            )
        };
        if order_type == OrderType::Fok && remaining > 0 {
            return Err(OracleReject::FokInsufficient {
                available: qty - remaining,
            });
        }
        self.next_seq += 1;
        if remaining == 0 {
            events.push(ExecutionEvent::Filled {
                seq,
                order_id: OrderId(order_id),
            });
        } else if stp_stopped {
            events.push(ExecutionEvent::SelfTradePrevented {
                seq,
                order_id: OrderId(order_id),
            });
        } else {
            match order_type {
                OrderType::Limit | OrderType::PostOnly => {
                    self.arrival += 1;
                    orders.insert(
                        order_id,
                        RefOrder {
                            participant,
                            side,
                            price,
                            qty: remaining,
                            arrival: self.arrival,
                        },
                    );
                    events.push(ExecutionEvent::Accepted {
                        seq,
                        order_id: OrderId(order_id),
                    });
                }
                OrderType::Ioc => {
                    events.push(ExecutionEvent::Expired {
                        seq,
                        order_id: OrderId(order_id),
                        remaining_qty: remaining,
                    });
                }
                OrderType::Fok => {
                    unreachable!("a Fok order with a remainder rejected above")
                }
            }
        }
        self.orders = orders;
        Ok(events)
    }

    /// The matching sweep over a scratch order map: trades in
    /// price-time order at maker prices, STP applied at own orders.
    /// Returns the sweep events, the taker remainder, and whether STP
    /// stopped the sweep.
    #[allow(
        clippy::too_many_arguments,
        reason = "mirrors the engine add_order parameter list"
    )]
    fn sweep(
        orders: &mut BTreeMap<u64, RefOrder>,
        seq: u64,
        order_id: u64,
        participant: u32,
        side: Side,
        price: u64,
        qty: u64,
        stp: StpPolicy,
    ) -> (Vec<ExecutionEvent>, u64, bool) {
        let mut events = Vec::new();
        let mut remaining = qty;
        let mut stp_stopped = false;
        while remaining > 0 {
            let Some(maker_id) = Self::best_maker_in(orders, side, price) else {
                break;
            };
            let maker = *orders.get(&maker_id).expect("maker exists");
            if maker.participant == participant {
                match stp {
                    StpPolicy::CancelIncoming => {
                        stp_stopped = true;
                        break;
                    }
                    StpPolicy::CancelResting => {
                        orders.remove(&maker_id);
                        events.push(ExecutionEvent::Cancelled {
                            seq,
                            order_id: OrderId(maker_id),
                            reason: CancelReason::SelfTradePrevention,
                        });
                        continue;
                    }
                    StpPolicy::CancelBoth => {
                        orders.remove(&maker_id);
                        events.push(ExecutionEvent::Cancelled {
                            seq,
                            order_id: OrderId(maker_id),
                            reason: CancelReason::SelfTradePrevention,
                        });
                        stp_stopped = true;
                        break;
                    }
                }
            }
            let trade_qty = remaining.min(maker.qty);
            remaining -= trade_qty;
            events.push(ExecutionEvent::Trade {
                seq,
                maker_order_id: OrderId(maker_id),
                taker_order_id: OrderId(order_id),
                price: maker.price,
                qty: trade_qty,
            });
            if trade_qty == maker.qty {
                orders.remove(&maker_id);
                events.push(ExecutionEvent::Filled {
                    seq,
                    order_id: OrderId(maker_id),
                });
            } else if let Some(m) = orders.get_mut(&maker_id) {
                m.qty -= trade_qty;
            }
        }
        (events, remaining, stp_stopped)
    }

    /// Amend a live order. Inputs are assumed validated. Amending to a
    /// larger quantity or any different price moves the order behind
    /// every order already resting at the target level (a fresh arrival
    /// stamp); a reduction or an identical amend keeps its stamp.
    pub fn amend(
        &mut self,
        order_id: u64,
        new_price: u64,
        new_qty: u64,
    ) -> Result<Vec<ExecutionEvent>, OracleReject> {
        let order = *self.orders.get(&order_id).ok_or(OracleReject::NotFound)?;
        if let Some(best) = self.best(order.side.opposite()) {
            let crosses = match order.side {
                Side::Bid => new_price >= best.0,
                Side::Ask => new_price <= best.0,
            };
            if crosses {
                return Err(OracleReject::AmendWouldCross {
                    opposite_best: best.0,
                });
            }
        }
        let seq = self.next_seq;
        self.next_seq += 1;
        let entry = self.orders.get_mut(&order_id).expect("checked above");
        if new_price != order.price || new_qty > order.qty {
            self.arrival += 1;
            entry.arrival = self.arrival;
        }
        entry.price = new_price;
        entry.qty = new_qty;
        Ok(vec![ExecutionEvent::Amended {
            seq,
            order_id: OrderId(order_id),
            price: new_price,
            qty: new_qty,
        }])
    }

    /// Cancel every live order of one participant. The command consumes
    /// a sequence number even when nothing is cancelled. The oracle
    /// emits in ascending id order; the engine emits in ascending slot
    /// order, and the harness compares the two as sets (INV-CANCEL-1).
    pub fn cancel_by_participant(&mut self, participant: u32) -> Vec<ExecutionEvent> {
        let seq = self.next_seq;
        self.next_seq += 1;
        let ids: Vec<u64> = self
            .orders
            .iter()
            .filter(|(_, o)| o.participant == participant)
            .map(|(id, _)| *id)
            .collect();
        let mut events = Vec::new();
        for id in ids {
            self.orders.remove(&id);
            events.push(ExecutionEvent::Cancelled {
                seq,
                order_id: OrderId(id),
                reason: CancelReason::ByParticipant,
            });
        }
        events
    }

    /// Cancel a live order, returning its events and its remaining
    /// quantity at cancel time.
    pub fn cancel(&mut self, order_id: u64) -> Option<(Vec<ExecutionEvent>, u64)> {
        let order = self.orders.remove(&order_id)?;
        let seq = self.next_seq;
        self.next_seq += 1;
        Some((
            vec![ExecutionEvent::Cancelled {
                seq,
                order_id: OrderId(order_id),
                reason: CancelReason::Requested,
            }],
            order.qty,
        ))
    }

    /// Find the best maker for an incoming order in an arbitrary order
    /// map: on the opposite side, crossing the limit price, best price
    /// first, then earliest arrival.
    fn best_maker_in(
        orders: &BTreeMap<u64, RefOrder>,
        taker_side: Side,
        taker_price: u64,
    ) -> Option<u64> {
        let mut best: Option<(u64, u64, u64)> = None; // (price key, arrival, id)
        for (id, order) in orders {
            if order.side == taker_side {
                continue;
            }
            let crosses = match taker_side {
                Side::Bid => order.price <= taker_price,
                Side::Ask => order.price >= taker_price,
            };
            if !crosses {
                continue;
            }
            // Price key sorts best first for the taker's perspective:
            // a buyer wants the lowest ask, a seller the highest bid.
            let key = match taker_side {
                Side::Bid => order.price,
                Side::Ask => u64::MAX - order.price,
            };
            let candidate = (key, order.arrival, *id);
            if best.is_none_or(|b| candidate < b) {
                best = Some(candidate);
            }
        }
        best.map(|(_, _, id)| id)
    }

    /// Best price and aggregate quantity for one side, by naive scan.
    pub fn best(&self, side: Side) -> Option<(u64, u64)> {
        let mut prices: Vec<u64> = self
            .orders
            .values()
            .filter(|o| o.side == side)
            .map(|o| o.price)
            .collect();
        prices.sort_unstable();
        let price = match side {
            Side::Bid => prices.last().copied(),
            Side::Ask => prices.first().copied(),
        }?;
        let qty = self
            .orders
            .values()
            .filter(|o| o.side == side && o.price == price)
            .map(|o| o.qty)
            .sum();
        Some((price, qty))
    }

    /// Full depth for one side as (price, total qty, order count),
    /// best price first, by naive grouping.
    pub fn depth(&self, side: Side) -> Vec<(u64, u64, u32)> {
        let mut levels: BTreeMap<u64, (u64, u32)> = BTreeMap::new();
        for order in self.orders.values().filter(|o| o.side == side) {
            let entry = levels.entry(order.price).or_insert((0, 0));
            entry.0 += order.qty;
            entry.1 += 1;
        }
        let mut out: Vec<(u64, u64, u32)> = levels
            .into_iter()
            .map(|(price, (qty, count))| (price, qty, count))
            .collect();
        if side == Side::Bid {
            out.reverse();
        }
        out
    }
}