6 · The book Draft

Orders, matching, and price priority

5 min read

On this page

6.1 What a market is to a program

The running example is BTC-USDT-PERP, a contract that tracks the price of one bitcoin in USDT, a token meant to be worth one US dollar. For everything in this chapter it behaves like bitcoin bought and sold for dollars, and nothing below depends on what it is. What matters is how a price for it comes to exist, because a program that copies a market has to copy that mechanism’s output exactly, and it cannot do so without knowing what the mechanism is.

Two people and no price

Start with no price at all. Somebody wants to buy bitcoin, somebody else wants to sell it, and neither knows what the other will accept. A price is not something the venue owns and announces; it is what comes out when the two meet. So each of them says what they are willing to do, in a form a program can compare. That form is an order, and it has three fields: a side, buy or sell; a price, the worst price its owner will accept, which for a buyer is the most they will pay and for a seller the least they will take; and a quantity, how much. “Buy 0.5 BTC at no more than $68,123.45” is an order. So is “sell 1.2 BTC at no less than $68,123.50”. These two do not trade, because the buyer’s limit is below the seller’s, and each would rather wait than give way.

Limit orders and market orders

An order with a price, like both of those, is a limit order: the price is a limit the trade will never go past. When nothing on the other side meets it, the venue does not throw it away. It keeps it, waiting in its memory, until somebody arrives who will accept it or until its owner cancels it. A market order carries no price. “Buy 1.2 BTC now” means buy from whoever is waiting to sell, cheapest first, at whatever they ask. It never waits: it trades against what is there, and whatever is left over cannot trade and is cancelled. The difference has a consequence worth stating early. Every price a market shows came from a limit order someone left waiting; market orders only consume them.

The matching engine

The program at the venue that does this is the matching engine. It holds every waiting order for an instrument, receives new orders and cancels one at a time in the order they arrive, decides which meet, and records the outcome. It is usually a single thread per instrument or per group of instruments, for the reason Chapter 3 gave for having one writer: the question “who was first?” must have exactly one answer, and one thread taking requests from a queue gives it one without a lock. The matching engine owns the truth. Whatever it holds is the market, by definition. Everything anyone else holds, including the book in our program, is a copy made from what the engine chooses to publish, and the engine publishes changes as it makes them.

Price levels and the queue

Orders waiting on one side at one price form a price level. The level’s quantity is the sum of the quantities of the orders in it, and that sum is the number most feeds publish: “at $68,123.53 there are 0.420 BTC for sale” may be one order of 0.420 or forty orders of about 0.010. Inside a level the engine keeps the orders in a queue, in the order they arrived. When an order arrives that can trade, the engine applies price-time priority: it trades first against the best price on the other side, and at that price against the order at the front of the queue. A better price always goes first; between equal prices, earlier goes first. A few futures markets share a level’s trading among its orders in proportion to their size instead, but price-time is the rule on crypto venues and on most others, and the only one this chapter uses. It is why a place near the front of a queue is worth something, and why a cancel followed by a new order at the same price goes to the back.

Bid, ask, spread and mid

A waiting buy order is a bid, and the buy side of the book is called the bids. A waiting sell order is an ask, also called an offer, and the sell side is the asks. The best bid is the highest price anyone is waiting to buy at; the best ask is the lowest price anyone is waiting to sell at. The best bid is always below the best ask, because if a buy price ever reached a sell price the engine would have matched the two the moment the second of them arrived. The distance between them is the spread: best ask minus best bid, always above zero in a working book. The mid is the average of the two, a reference for “where the market is”; it is often not a price anyone can trade at, and it can fall between two prices the venue allows.