Limit Order Book Mechanics

Medium·20 min read
Market MicrostructureLimit Order BookMarket MakingAdverse Selection

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: M=(Pbid+Pask)/2M = (P_{\mathrm{bid}} + P_{\mathrm{ask}})/2.
  • Spread: s=PaskPbids = P_{\mathrm{ask}} - P_{\mathrm{bid}}. Quoted in ticks (minimum price increments) or basis points.
  • Tick size: the minimum allowed price increment δ\delta. For US equities, δ=0.01 USD\delta = 0.01\ \text{USD}.
  • All prices are in units of the tick unless stated otherwise.
  • Time is measured in microseconds (μs\mu s) to milliseconds (ms\mathrm{ms}) 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 pp, there is a queue of resting limit orders, each with a quantity qiq_i and a timestamp tit_i. The full structure:

Bid side (buyers, sorted descending by price): {(Pbid,Qbid1),(Pbidδ,Qbid2),}\{(P_{\mathrm{bid}}, Q_{\mathrm{bid}}^1), (P_{\mathrm{bid}} - \delta, Q_{\mathrm{bid}}^2), \ldots\}

Ask side (sellers, sorted ascending by price): {(Pask,Qask1),(Pask+δ,Qask2),}\{(P_{\mathrm{ask}}, Q_{\mathrm{ask}}^1), (P_{\mathrm{ask}} + \delta, Q_{\mathrm{ask}}^2), \ldots\}

where QkQ^k is the total resting quantity at the kk-th level. A Level-1 snapshot contains only the best bid and ask (k=1k=1). 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 pp has priority over a later limit order at the same price pp. A limit order at a better price p>pp' > p (on the bid side) always has priority over any order at pp.

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 qq units at price pp is a commitment to buy at most qq units at a price of pp or better (i.e., at pp 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 pp. 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 pεp - \varepsilon (below the bid limit), the market maker who posted a bid at pp 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 qq 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 qq:

Pexec=kpkmin(qk,remaining)kmin(qk,remaining),P_{\mathrm{exec}} = \frac{\sum_{k} p_k \cdot \min(q_k, \text{remaining})}{\sum_k \min(q_k, \text{remaining})},

where (pk,qk)(p_k, q_k) are the ask levels traversed in order, and "remaining" is the unfilled quantity.

Immediate-or-Cancel (IOC)

This topic requires Premium

Only today's featured topic is free. Unlock the full Today's Focus archive with Premium.

Read the theory? Verify your understanding.

Take the Quiz