Setup
Market Context
Every liquid financial instrument traded on an electronic venue — equities, futures, options, FX spot — executes through a limit order book (LOB). The LOB is the matching engine at the heart of modern markets. Understanding its mechanics is a prerequisite for algorithmic trading, market making, optimal execution, and any quantitative work that touches execution quality, transaction costs, or intraday price dynamics.
This module covers the Level-2 data structure of the book, order types, queue dynamics, and how the LOB generates the bid-ask spread and the microprice — the starting points for any execution model.
Conventions
- Bid: the highest price at which a buyer is willing to buy (best bid, or BBO bid).
- Ask (offer): the lowest price at which a seller is willing to sell (best ask, or BBO ask).
- Mid-price: .
- Spread: . Quoted in ticks (minimum price increments) or basis points.
- Tick size: the minimum allowed price increment . For US equities, .
- All prices are in units of the tick unless stated otherwise.
- Time is measured in microseconds () to milliseconds () for HFT contexts; seconds to minutes for slower strategies.
The Limit Order Book Data Structure
Level-2 Architecture
The LOB is a price-ordered queue on each side. At each price level , there is a queue of resting limit orders, each with a quantity and a timestamp . The full structure:
Bid side (buyers, sorted descending by price):
Ask side (sellers, sorted ascending by price):
where is the total resting quantity at the -th level. A Level-1 snapshot contains only the best bid and ask (). A Level-2 snapshot contains multiple levels.
Price-time priority. Within each price level, orders are executed in the order they arrived. An earlier limit order at price has priority over a later limit order at the same price . A limit order at a better price (on the bid side) always has priority over any order at .
C++ LOB Data Structure
#include <map>
#include <queue>
#include <cstdint>
// A single resting limit order
struct Order {
uint64_t order_id;
double price;
int quantity; // signed: positive = buy, negative = sell
uint64_t timestamp_ns; // nanosecond timestamp
};
// A price level: ordered queue of orders at the same price
struct PriceLevel {
double price;
std::queue<Order> orders; // FIFO: front() has highest priority
int total_quantity() const {
int total = 0;
// Note: in production, maintain a running total (O(1))
auto q = orders;
while (!q.empty()) { total += q.front().quantity; q.pop(); }
return total;
}
};
// The full LOB
// Bid side: map ordered descending (highest price = best bid)
// Ask side: map ordered ascending (lowest price = best ask)
using BidBook = std::map<double, PriceLevel, std::greater<double>>;
using AskBook = std::map<double, PriceLevel>;
struct LimitOrderBook {
BidBook bid_book;
AskBook ask_book;
double best_bid() const { return bid_book.empty() ? 0.0 : bid_book.begin()->first; }
double best_ask() const { return ask_book.empty() ? 0.0 : ask_book.begin()->first; }
double mid() const { return (best_bid() + best_ask()) / 2.0; }
double spread() const { return best_ask() - best_bid(); }
};
Order Types
Limit Order
A limit order to buy units at price is a commitment to buy at most units at a price of or better (i.e., at or any lower price on the ask side). It rests in the book until filled, cancelled, or expired.
Execution risk: the limit order may not be filled if the market never reaches price . In a fast-moving market, by the time the exchange acknowledges a limit order, the price may have moved away.
Adverse selection risk: a resting limit order is a free option granted to the market. If the true value of the asset moves to (below the bid limit), the market maker who posted a bid at is filled at a price now worse than fair value. This is the fundamental adverse selection problem of liquidity provision.
Market Order
A market order to buy units executes immediately at the best available ask prices, walking up the book if necessary. It pays the spread and any market impact above the best ask.
Guaranteed fill, uncertain price. The execution price is the volume-weighted average of the levels consumed. For a buy market order of size :
where are the ask levels traversed in order, and "remaining" is the unfilled quantity.