pub struct Book { /* private fields */ }Expand description
A single-instrument limit order book on one tick grid.
Implementations§
Source§impl Book
impl Book
Sourcepub fn capture_snapshot_state(&self) -> SnapshotState
pub fn capture_snapshot_state(&self) -> SnapshotState
Capture the complete structural state of this book for persistence. Live orders are reported in ascending slot order; the free list is reported exactly, head and per-slot links, so a restore allocates slots in the same order this book would have.
Sourcepub fn from_snapshot_state(state: SnapshotState) -> Result<Self, BookError>
pub fn from_snapshot_state(state: SnapshotState) -> Result<Self, BookError>
Rebuild a book from a captured state. The state must already
have passed chronicle-wal’s checked decode (section 10.4):
this constructor trusts the state’s internal consistency,
guards it with debug assertions, and runs the full structural
invariant check on the rebuilt book in debug builds.
§Errors
Returns BookError::InvalidConfig when the state’s own
configuration fails validation, or when a section length
disagrees with the configuration it carries. Both are
unreachable for a state produced by capture or by the checked
snapshot decode.
§Panics
Panics when the state fails the structural invariant check in debug builds, which a state produced by capture or by the checked snapshot decode cannot do.
Source§impl Book
impl Book
Sourcepub fn new(config: BookConfig) -> Result<Self, BookError>
pub fn new(config: BookConfig) -> Result<Self, BookError>
Build an empty book.
§Examples
use chronicle_core::{Book, BookConfig, StpPolicy};
let book = Book::new(BookConfig {
max_orders: 16,
grid_len: 640,
tick_size: 1,
grid_start_tick: 1,
default_stp: StpPolicy::CancelIncoming,
})
.unwrap();
assert_eq!(book.live_order_count(), 0);
assert_eq!(book.best_bid(), None);§Errors
Returns BookError::InvalidConfig when the configuration
fails any rule in ARCHITECTURE.md section 3.2.
Sourcepub const fn config(&self) -> BookConfig
pub const fn config(&self) -> BookConfig
The configuration this book was built with.
Sourcepub const fn live_order_count(&self) -> u32
pub const fn live_order_count(&self) -> u32
Number of live orders.
Sourcepub fn best_bid(&self) -> Option<(u64, u64)>
pub fn best_bid(&self) -> Option<(u64, u64)>
Best bid as (price, aggregate qty), or None when no bid
rests.
Sourcepub fn best_ask(&self) -> Option<(u64, u64)>
pub fn best_ask(&self) -> Option<(u64, u64)>
Best ask as (price, aggregate qty), or None when no ask
rests.
Sourcepub fn add_order(
&mut self,
order_id: OrderId,
participant: ParticipantId,
side: Side,
order_type: OrderType,
price: u64,
qty: u64,
stp: Option<StpPolicy>,
events: &mut EventBuffer<'_>,
) -> Result<usize, BookError>
pub fn add_order( &mut self, order_id: OrderId, participant: ParticipantId, side: Side, order_type: OrderType, price: u64, qty: u64, stp: Option<StpPolicy>, events: &mut EventBuffer<'_>, ) -> Result<usize, BookError>
Add an order. The order sweeps the opposite side in price-time
priority under its self-trade prevention policy (stp, falling
back to the book default when None), then the order type
decides the remainder: Limit rests it, Ioc expires it, Fok was
already proven fillable in preflight, and PostOnly rests in full
because preflight proved it does not cross.
Returns the number of events written into events.
§Examples
use chronicle_core::{
Book, BookConfig, EventBuffer, ExecutionEvent, OrderId, OrderType, ParticipantId,
Side, StpPolicy,
};
let mut book = Book::new(BookConfig {
max_orders: 16,
grid_len: 640,
tick_size: 1,
grid_start_tick: 1,
default_stp: StpPolicy::CancelIncoming,
})
.unwrap();
let mut storage = Vec::with_capacity(8);
let mut events = EventBuffer::new(&mut storage);
let written = book
.add_order(
OrderId(1),
ParticipantId(1),
Side::Bid,
OrderType::Limit,
100,
10,
None,
&mut events,
)
.unwrap();
assert_eq!(written, 1);
assert!(matches!(storage[0], ExecutionEvent::Accepted { .. }));
assert_eq!(book.best_bid(), Some((100, 10)));§Errors
Returns a BookError before mutating anything when validation
or preflight fails: invalid or duplicate order id, zero
quantity, invalid price tick, capacity, a PostOnly price that
would cross, a Fok quantity the book cannot fill, or an event
buffer below the exact preflight bound.
Sourcepub fn cancel_order(
&mut self,
order_id: OrderId,
events: &mut EventBuffer<'_>,
) -> Result<usize, BookError>
pub fn cancel_order( &mut self, order_id: OrderId, events: &mut EventBuffer<'_>, ) -> Result<usize, BookError>
Cancel a live order by id.
Returns the number of events written into events (one
Cancelled event on success).
§Errors
Returns BookError::OrderNotFound with no side effects when
the id is missing, filled, or already cancelled, and
BookError::EventBufferFull when the buffer has no free slot.
Sourcepub fn cancel_by_participant(
&mut self,
participant: ParticipantId,
events: &mut EventBuffer<'_>,
) -> Result<usize, BookError>
pub fn cancel_by_participant( &mut self, participant: ParticipantId, events: &mut EventBuffer<'_>, ) -> Result<usize, BookError>
Cancel every live order belonging to one participant
(ARCHITECTURE.md section 7.8). Events are emitted in ascending
slot order, one Cancelled with reason
CancelReason::ByParticipant per order. A participant with no
live orders succeeds with zero events; the command still
consumes a sequence number, as every accepted command does.
Returns the number of events written into events.
§Errors
Returns BookError::EventBufferFull before mutating anything
when the buffer holds fewer free slots than the participant has
live orders.
Sourcepub fn amend_order(
&mut self,
order_id: OrderId,
new_price: u64,
new_qty: u64,
events: &mut EventBuffer<'_>,
) -> Result<usize, BookError>
pub fn amend_order( &mut self, order_id: OrderId, new_price: u64, new_qty: u64, events: &mut EventBuffer<'_>, ) -> Result<usize, BookError>
Amend a live order to a new price and a new remaining quantity
(ARCHITECTURE.md section 7.4). A smaller quantity at the same
price, or an identical price and quantity, preserves the order’s
exact FIFO position; a larger quantity or any price change moves
it to the tail of the target level. An amend never triggers a
match. Every accepted amend emits one Amended event and
consumes one sequence number, including the identical no-op.
Returns the number of events written into events (one on
success).
§Errors
Returns BookError::OrderNotFound when the id is not live,
BookError::InvalidPriceTick or BookError::InvalidQuantity
for an off-grid price or a zero quantity,
BookError::AmendWouldCross when the new price would leave
the book crossed, and BookError::EventBufferFull when the
buffer has no free slot. Every error is returned before any
mutation.
Sourcepub fn depth(
&self,
side: Side,
max_levels: usize,
out: &mut [DepthLevel],
) -> usize
pub fn depth( &self, side: Side, max_levels: usize, out: &mut [DepthLevel], ) -> usize
Write up to max_levels aggregated levels for one side into
out, best price first, and return how many were written.
Mutates nothing and allocates nothing.
Sourcepub fn debug_assert_invariants(&self)
pub fn debug_assert_invariants(&self)
Full structural self-check (ARCHITECTURE.md section 11): crossed book, per-level aggregates recomputed from the arena, bitmap summaries recomputed from level counts, lookup reachability and generation agreement for every live order, free list exactness, and level list link symmetry. Intended for tests and debug builds; it is O(capacity + grid).
§Panics
Panics on the first invariant violation found, with a message naming the broken invariant and the offending slot or level. That is the intended behaviour: this is a checker, and a failed check must never be mistaken for a passing one.