How a Matching Engine Processes an Order

A matching engine validates, sequences, and matches every order against the book in a fixed order that decides who trades first.

A matching engine processes an order in three steps, always in the same sequence. Every order passes pre-trade validation, receives a time-stamped sequence number, and is compared against the resting book to see whether it can trade immediately. An order that cannot trade immediately does not vanish. It joins the book at its limit price, in line behind everyone who arrived earlier at that same price, which is the starting point for reading a limit order book the way a trading desk does rather than the way a textbook does.

Before an order reaches the matching logic it passes through an exchange gateway that runs pre-trade risk checks. These include price collars that reject orders priced too far from the last trade, maximum order size limits, and self-trade prevention logic that stops a firm's own resting order from trading against its own incoming order. A rejected order never receives a sequence number and never touches the book.

These checks exist because a matching engine applies no judgment of its own. It executes whatever crosses at whatever price the logic allows, as fast as the logic allows. The gateway is the only point where a mistyped price or a runaway algorithm gets stopped before it moves the market, which is why firms building their own order management systems replicate a version of these checks on their own network, ahead of the exchange's copy.

Once an order clears the gateway it receives a sequence number and is inserted into the book at its limit price. The book is organized by price level, and within each level, orders are ordered by arrival time. This is price-time priority, the rule governing almost every listed equity book in the United States, and the rule that turns a resting order into a position in a queue rather than a static instruction.

A market order carries no limit price, so it never joins that queue. It matches immediately against the best available opposing price, walking through successive price levels until it is filled or the book runs out of liquidity to trade against. A marketable limit order, one priced through the current best opposing price, behaves the same way for the portion that can trade right away, and rests for any remainder.