Where you are. Lesson 10 sold a security into the world at an auction price. From that moment on nobody runs an auction; the price is remade continuously, trade by trade, by a mechanism so simple it fits in forty lines and so consequential that entire industries are built on being one microsecond earlier to it. This lesson builds it.
Where does the price on the screen come from
Your app shows a share at 100.50. Ask where the number comes from and the intuitive answer is that somebody decided it, or that it is an average, or that it comes from the exchange.
None of those. There is no price in the sense of a value stored somewhere. There is a list of people who have said what they would buy at, and a list of people who have said what they would sell at, and the number on your screen is a fact about those two lists: the best offer to buy is 100, the best offer to sell is 101, and the last time somebody crossed the gap the trade printed at 100.50.
Nobody set it. It is the state of a queue.
The idea in one paragraph
An order book is two sorted queues, one of buyers and one of sellers, and a price is what happens when they meet. A limit order says what you will pay, or accept, and waits in the queue; a market order says take whatever is available now, and consumes the queue from the best price inward. The sort order is the rule that makes the whole thing fair and mechanical: best price first, and among orders at the same price, whoever arrived first. That is price-time priority, and it is why being early matters as much as being aggressive. The best bid and the best ask are what everyone means by “the market”, the gap between them is the spread, and every trade you have ever seen printed was one order eating into the other side of somebody’s queue.
The rule that decides everything
Two orders arrive to buy at 100, thirty units then twenty. A third arrives at 99 for fifty. Sort them: both 100s ahead of the 99, and between the two 100s, the earlier one first.
Now a market sell of sixty arrives. It takes thirty from the first 100, twenty from the second 100, and ten from the 99. Three prints, in that order, and the last one is worse than the first two - which is what it means to sell more than the best price can absorb.
Wider than the screen; scroll it sideways.
Why microseconds became worth millions
Price-time priority has a consequence nobody legislated. If two participants want the same queue position at the same price, the only thing separating them is arrival time. Being a microsecond earlier is not a marginal advantage; it is the difference between filling and not filling.
That is the whole economic engine behind colocation, microwave links between exchanges, and the arms race in low-latency trading. It follows from the tiebreak rule, not from anybody’s intent, which is a fine example of a mechanism’s incentives outrunning its designers.
Check yourself
1. Your app shows 100.50. In what sense does that number not exist anywhere?
There is no stored value that anyone set. There are two queues of orders, and the quoted figures are facts about them: the best price someone will buy at, the best someone will sell at, and the price at which the most recent crossing trade printed. The price is a summary of a queue’s state, recomputed continuously.
2. Two buy orders rest at 100, submitted at different times, and a market sell arrives for less than their combined size. Which fills, and what rule decides?
The earlier one fills first, because price-time priority breaks ties at the same price by arrival order. The later order fills only with whatever quantity remains, which may be nothing.
3. A market sell of sixty prints at 100, 100 and then 99. What happened, and what would have prevented the worst print?
The order exhausted both orders resting at 100 and took the rest from the next level down. Splitting the order into smaller pieces over time, or using a limit order that refuses to trade below 100, would have avoided the 99 print - at the cost of possibly not filling at all.
4. How does a tiebreak rule end up justifying microwave towers between cities?
Because when two participants want the same price, arrival time is the only thing that separates them, so a microsecond decides who fills. That turns latency into direct profit and makes almost any spend on speed rational for the participants competing for those positions. The arms race follows from the comparator, not from anyone’s design intent.
Do this
Build the matching engine and watch price-time priority decide the fills.
python3 code/order_book.py
The starter provides the book, already sorted by price then arrival, and leaves you match_market_order: take from the front of the resting side, fill as much of each order as you can, log each fill as a price and quantity, and stop when the order is done or the side is empty. The assertions check the sort, that a market buy of fifty produces two fills at 101 in arrival order, and that a market sell of sixty walks down through 100, 100 and 99. Success prints both trade logs and the line:
no auctioneer decided anything: the price is wherever the two sides of the book happen to meet, order by order
The completed version is in solutions/order_book.py.
What you can now do. You can build the machinery that forms a price and explain any print as the state of a queue. What the book cannot explain by itself is why anyone would rest orders on both sides of it at all, absorbing everyone else’s urgency. The next lesson is about the people who do that for a living, and what they are paid for.