Case study demo

Stock exchange matching

Match buy and sell orders in an order book by price and time.

An interactive demo of how this system behaves. It runs in your browser.

Case study · Live model

How a stock exchange matches orders

Traders send orders to buy or sell one stock. A matching engine keeps the waiting orders in a list called the book, best price first. It makes a trade when a buyer and a seller agree on a price. There are two engines: a main one and a backup. Send a big order, or turn off the part that puts orders in one line, and watch what changes.

Try it

Lab time 3.0 s
in number orderTraders, 12 orders a secondTraders12 orders a second38 sentOrder gateway, takes each order inOrder gatewaytakes each order inthen passes it onSequencer, numbers every orderSequencernumbers every ordernext number 38Main engine, 15 tradesMain engine15 tradesbest $95 buy, $96 sellBackup engine, 15 tradesBackup engine15 tradesthe same book
  • Traders — 12 orders a second — healthy — 38 sent
  • Order gateway — takes each order in — healthy — then passes it on
  • Sequencer — numbers every order — healthy — next number 38
  • Main engine — 15 trades — healthy — best $95 buy, $96 sell
  • Backup engine — 15 trades — healthy — the same book
  • An order from a trader
  • The order on its way to the engines
Last trade
$96
15 trades, 430 shares
Best buy and sell
$95 / $96
$1 apart
Orders waiting
21
1,190 shares in the book
Last big order
–
press a "500 at any price" button
Matched differently
0/ 37
orders the two engines did not match alike
Backup engine's book
the same
as the main engine's, order for order

Numbers add up:2,050 shares placed = 1,190 waiting + 0 cancelled + 2 × 430 tradedThe lab checks this sum as it runs, so a counting mistake would show here.

Controls

Orders
12 a second
Two engines

Gives every order a number. Both engines take orders by number, so they see the same orders in the same order. This is called a total order. Turning it on copies the main book to the backup.

Orders waiting

SidePlace in linePriceSharesFromWaiting for
Sell6$9970trader 290.4 s
Sell5$9980trader 72.3 s
Sell4$9820trader 861.9 s
Sell3$9780trader 361.1 s
Sell2$9770trader 921.6 s
Sell1$9610trader 860.4 s
Buy1$9580trader 761.8 s
Buy2$9470trader 671.2 s
Buy3$9340trader 152.3 s
Buy4$9310trader 351.6 s
Buy5$9330trader 171.5 s
Buy6$9340trader 621.1 s

The first 6 sell orders and the first 6 buy orders in line. Place 1 on each side trades next: the lowest sell price, and the highest buy price.

Last trades

WhenPriceSharesOrder that arrived
2.8 s$9640trader 86
2.7 s$9640trader 73
2.3 s$9730trader 70
2.3 s$9640trader 70
1.9 s$9710trader 53
1.9 s$9630trader 53
1.9 s$9510trader 67
1.9 s$9520trader 67
Price of the last trade, with the best buy and sell prices ($)
Last tradeBest buyBest sell
0501001502001s2s3s
Show data table
TimeLast tradeBest buyBest sell
1s10096—
1s969699
2s969697
2s979597
3s979597
3s969596

What just happened

Nothing yet. Start traffic or run an experiment.

What this model simplifies
  • There is one stock, and prices are whole dollars. A real exchange has thousands of stocks, each with its own book, and prices in cents.
  • Nine orders in ten are limit orders, which name a price. One that nobody takes is cancelled after 15 s. Real traders cancel and change orders all the time.
  • One trip over the network takes 40 ms, and the last trip to an engine up to 60 ms more. A real exchange measures these trips in millionths of a second, but they still differ.
  • The backup takes the same orders as the main engine and stays ready. This model never fails over to it; it only shows whether it could. Keeping two engines alike by giving them the same orders in the same order is called state machine replication.
  • There are no checks on a trader's money or shares, and no fees. A real gateway refuses an order the trader cannot pay for.
  • The sequencer is one part and never stops here. In a real exchange it is the part that must never be lost, so it has its own backups.