obsidiate.
Market mechanics
AdvancedLesson 1 of 107 min read

Inside the matching engine

Every trade on a modern exchange ends up in the same place: a matching engine, which is a surprisingly small piece of software with one job — take a stream of incoming orders and decide, unambiguously, who trades with whom, at what price, in what order. Everything else an exchange does (risk checks, balances, market data, the pretty charts) exists upstream or downstream of this core. If you understand the engine, you understand why your order filled, why it didn’t, and why the price you saw on screen wasn’t quite the price you got.

The engine’s data structure is the central limit order book (CLOB), and its rulebook is price-time priority. Neither is complicated. What makes them powerful is that they are applied with total consistency, millions of times a day, with no exceptions and no judgment calls. Markets work because the matching rules are boring.

The central limit order book

Picture two sorted lists. On one side, the bids: every resting order to buy, sorted from highest price to lowest. On the other, the asks: every resting order to sell, sorted from lowest price to highest. The highest bid and the lowest ask are the top of book, and the gap between them is the spread. Each price level holds a queue of orders, and each order in the queue remembers when it arrived.

Suppose the best bid for an instrument is 100.00 with 8 units queued, and the best ask is 100.05 with 5 units. Nothing trades, because no buyer is willing to pay what any seller demands. The book is in equilibrium — a fragile one. The moment someone submits an order that crosses the spread, the engine springs into action.

  • A limit order states a worst acceptable price. It executes immediately against anything at that price or better, and any remainder rests in the book.
  • A market order states no price at all. It consumes whatever is on the opposite side, level by level, until it is filled or the book runs out.
  • A stop-loss order sleeps outside the book entirely. When the trigger price trades, the engine converts it into a live order and it joins the fray like any other.

Price-time priority: the only fairness rule you need

When an incoming sell order crosses the spread, who among the buyers gets filled? Price-time priority answers in two steps. First, price: better-priced orders always trade first. A bid at 100.02 beats every bid at 100.00, no matter when it arrived. Second, time: among orders at the same price, the one that arrived first trades first. First in, first out — a queue at the deli counter, except the deli moves a few hundred thousand tickets a second.

This rule has a sharp consequence: there are exactly two ways to get filled faster. Pay a better price, or arrive earlier. There is no third option, no priority for size, no favoritism for account tier. On Obsidiate, as on any serious venue, the order you watch joining the book in the live feed is competing under precisely these rules.

If you modify a resting order’s price or increase its size, most engines treat it as a cancel-and-replace — you lose your place in the queue. Reducing size in place usually preserves priority. Know which one your venue does before you fiddle with live orders.

The life of an order

An order is a tiny state machine, and every transition is recorded. The lifecycle looks like this:

  1. Submitted. Your order arrives at the gateway, where risk checks run: do you have the balance, is the price sane, is the instrument trading. Fail any check and the order is rejected before the engine ever sees it.
  2. Accepted. The engine receives the order and checks whether it crosses the book. If it can trade immediately, it does — possibly in several pieces.
  3. Resting. Any unfilled remainder of a limit order sits in the book at its price level, holding its queue position, until something happens to it.
  4. Partially filled. A counterparty consumed some of your size. The rest keeps resting, same queue position for the remainder.
  5. Filled. All size executed. The order is done and leaves the book.
  6. Canceled. You (or an expiry condition) pulled the order. Whatever filled before the cancel still stands — cancellation is not an undo button.

Partial fills are information

Say you rest a bid for 10 units at 100.00 and receive a fill for 3. Mechanically, a seller crossed the spread with 3 units and your order was at the front of the queue. But read it as a message: sellers are willing to hit your price, just not in size — yet. A string of small partial fills on your bid while the ask side keeps refilling is the book telling you that patient supply sits above you. Traders who treat fills as data, not just outcomes, tend to make better second decisions.

Partial fills also explain the most common beginner confusion: a market order can fill at several prices at once. If you buy 12 units at market and the ask side shows 5 at 100.05, 4 at 100.06, and 6 at 100.07, you receive 5, 4, and 3 at those respective prices. Your average is 100.058 — worse than the 100.05 on screen. That difference is slippage, and it was visible in the book depth before you clicked.

Before sending a market order in a thin instrument, glance at the depth in the order book panel. If your size exceeds the top level, you already know your fill will be worse than the quoted price — the only question is how much.

Why determinism matters

A matching engine must be deterministic: given the same sequence of inputs, it must produce exactly the same trades, every time. This sounds like an engineering nicety. It is actually the foundation of market trust. Determinism means the exchange can replay any disputed moment and prove what happened. It means two market data subscribers see the same book. It means there is no code path where an operator, a big customer, or a bug quietly reorders the queue.

It is also why matching engines are often single-threaded per instrument, which surprises engineers raised on parallelism. Concurrency introduces ordering ambiguity, and ordering ambiguity is the one thing an engine may not have. Sequencing first, speed second — the engine processes one event at a time, in arrival order, and the entire market’s fairness rests on that monotony.

Key takeaways

  • The order book is two sorted queues — bids descending, asks ascending — and trades happen only when an order crosses the spread.
  • Price-time priority means better prices fill first, and at equal prices the earliest order wins. There is no third way to jump the queue.
  • Orders move through a strict lifecycle: submitted, accepted, resting, partially filled, filled, or canceled — and fills that happened before a cancel are permanent.
  • Market orders can sweep multiple price levels; the depth visible in the book predicts your slippage before you trade.
  • Modifying an order usually resets your queue position; reducing size in place usually does not.
  • Determinism — same inputs, same trades, every time — is what makes an exchange auditable and fair, and it is why engines prize sequencing over raw parallelism.