Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Principle:Avhz RustQuant Order Management

From Leeroopedia


Knowledge Sources
Domains Trading_Systems, Market_Microstructure
Last Updated 2026-02-07 21:00 GMT

Overview

Order management infrastructure providing order representations, order book data structures, and a limit order book implementation for modeling exchange-level trading mechanics.

Description

Order Management in RustQuant models the full lifecycle of trading orders through a set of types that represent orders, their attributes, and the books that hold them. The module provides two order book implementations at different levels of abstraction.

Order is the primary struct representing a single order in the system. It contains fields for the order ID (u64), symbol ID, order type, order side, price, stop price, quantity, executed quantity, leaves quantity (remaining unfilled), time-in-force policy, and a timestamp. A validate() method is defined for future order validation logic.

OrderType is an enum with six variants reflecting standard exchange order types:

  • Market -- executes immediately at the current bid/ask price
  • Limit -- executes at a specified price or better
  • Stop -- submits a market order when a trigger price is reached
  • StopLimit -- submits a limit order when a trigger price is reached
  • TrailingStop -- a stop order where the trigger price trails the market price by a fixed amount
  • TrailingStopLimit -- combines trailing stop mechanics with a limit order

OrderSide is an enum with two variants: BID (buy) and ASK (sell). It implements the Not trait so that !BID yields ASK and vice versa.

OrderTimeInForce (OrderLifespan) defines how long an order remains active:

  • GoodTillCancelled (GTC) -- persists until filled or explicitly cancelled
  • ImmediateOrCancel (IOC) -- any unfilled portion is immediately cancelled
  • FillOrKill (FOK) -- the entire order must fill immediately or be cancelled entirely
  • AllOrNone (AON) -- must be filled in its entirety but may remain on the book

OrderBook is a basic implementation using two VecDeque<Order> collections for the bid and ask sides. It supports inserting orders, checking emptiness, and querying the book size.

Book (LimitOrderBook) is a more sophisticated implementation using BTreeMap<u64, Limit> for price-ordered limit levels and a HashMap<u64, Order> for fast order lookup. It supports adding orders (with duplicate ID detection), cancelling orders (removing from both the limit level and the order map, and cleaning up empty levels), and executing market orders by walking through the opposite side of the book at successive price levels until the requested volume is filled.

Usage

Use the Order and OrderBook types for simple order management scenarios or simulations. Use the Book (LimitOrderBook) implementation when modeling realistic exchange mechanics with price-time priority, market order execution, and order cancellation. The OrderType and OrderTimeInForce enums provide the vocabulary for specifying order behavior consistent with real-world exchange conventions.

Theoretical Basis

A limit order book (LOB) is the fundamental data structure used by modern electronic exchanges. It maintains two sorted collections of resting orders: bids (buy orders) sorted in descending price order and asks (sell orders) sorted in ascending price order. The best bid and best ask define the bid-ask spread.

Market orders execute by consuming liquidity from the opposite side of the book, starting at the best available price and walking through successive price levels until the order is fully filled. This price-time priority matching is the standard model for most equity and derivatives exchanges.

The time-in-force policies control order lifecycle and are standardized across major exchanges, with definitions sourced from Interactive Brokers and NASDAQ documentation.

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment