An interactive visualisation  ::  five experiments & a live market

lobster: a stock exchange you can take apart

An interactive visualisation of a limit order book. Start with a live market, change who is trading in it, then run five experiments that measure what changed. Every number on this page is computed here, in this tab, by a matching engine compiled to WebAssembly.

00
Live market · running now

The whole engine, in one panel

A market with no question attached. Change who is in it and watch the book change shape underneath you.

Every band below is a resting queue at one price, brighter where more size is waiting, with time running left to right. The line threading through is the mid, and the marks are prints: above the line a buyer crossed the spread, below it a seller did. Nothing here is a recording. The engine is stepping in this tab, and every control rebuilds the market from tick zero.

Noise traders2
Momentum chaseron
Market makers1
Latent liquidity2.0
Maker half-spread0.40
Maker wire delay0.00
   starting

01
Experiment one of five · the queue

Who gets filled first

Two makers quote the same price in the same size. Does sitting closer to the exchange buy anything, and if so, what exactly does it buy?

An order book is a queue, and the only way to the front of a queue is to arrive early. Below is the bid side of one. Each row is a price; each block inside a row is a single resting order, drawn left to right in the order it will be filled. Most book views collapse a row to a single number, which hides the only thing that matters here. This one keeps the orders apart and colours them by who sent them:

Fast maker0.05
Slow maker0.15
booting

Both handles set how long a maker's orders spend on the wire before the exchange sees them. Price–time priority does the rest: at a shared price, whoever arrived first is filled first, so shortening one wire slides that maker's block to the left and keeps it there. Give them the same delay and the advantage disappears; overshoot, and it changes hands.

That's the whole mechanism, and all of it is arrival order. The two makers quote the same price, in the same size, off the same model. One run of fifteen hundred ticks wouldn't tell you whether the effect were real or whether that seed happened to be kind, so here are twelve, each a separate market:

So being early wins the queue. The obvious next thought is that it therefore wins money, and that turns out to be wrong. It's the more interesting half of this.

Markout is the average move in the mid price over the twenty ticks after one of your orders fills, signed by whether you bought or sold. Positive means the market kept going your way. Negative means you were the one who got run over, which has a name: you were adversely selected. Here is every seed, one dot each:

The reason is plain once you see it. A sweep arrives when somebody wants the price moved, and it eats the queue from the front. Being first in line guarantees you that trade. The maker behind you fills only on the sweeps big enough to reach it, and skips every small one that was about to go against you both. That a passive maker loses to the flow it fills is the oldest result in the field, from Glosten and Milgrom (1985); what the sliders add is that buying your way to the front of the queue buys you more of it.

Finding · experiment one

02
Experiment two of five · impact

What a big order costs

Cost grows like the square root of size in every market anyone has measured. Does it here, and what in the book makes that happen?

That was one order at a time. Now a different question, and a different set of agents in the book: you want to buy fifteen hundred lots, far more than is resting, so you cut it into eight-lot children and work them in. What do you pay per share, and how does that scale?

The answer is one of the most reliable measurements in finance. Cost grows roughly like the square root of the size, an exponent near 0.5 to 0.6, measured across decades, venues and asset classes (Almgren et al. 2005; Gatheral 2010). Doubling your order costs you less than twice as much per share, which is why splitting a big trade helps less than a linear model promises. Both axes below are logarithmic, so a power law is a straight line and its exponent is the slope:

Latent liquidity, shares per rung2.00
waiting

Flip the switch and the law breaks. The only thing it removes is an agent that ladders passive size around what it thinks the stock is worth, showing a little close to that price and more the further the market runs away from it. Every other agent quotes off the mid, so with the ladder gone nothing pushes back: the parent order walks the book, the market maker re-quotes around a mid the parent has already moved, and the parent chases its own footprint.

That shape is the whole mechanism, and it's arithmetic. If the size resting at distance d from fair value grows like d, then the shares available within D grow like , so buying Q means walking out to D ~ √Q. Depth that's linear in distance is the square-root law. Refill it too fast and the book becomes perfectly elastic, price snaps back between children, and impact vanishes instead, which is wrong in the other direction.

Finding · experiment two

03
Experiment three of five · the tape

Does the tape look real?

Real returns have fat tails and clustered volatility. A simulator that misses those is a toy, however good its plumbing.

A simulator is worth reasoning with only if what comes out of it has the statistical shape of the thing it imitates. Two of those shapes are cheap to check and hard to fake. Real returns have heavy tails: the big moves are far more common than a bell curve allows. And their size is autocorrelated even though their direction isn't, which is volatility clustering, the reason quiet weeks and violent weeks come in runs.

Past the end of the side there is nothing to extrapolate with, so a sweep that can't be filled comes back marked incomplete and reports how much it managed, rather than inventing a price for the remainder. That's the difference between this figure and the one above it. Here the whole trade goes at once and pays what the book is asking; the second experiment works the same quantity in over three hundred ticks and lets liquidity come back between children. The gap between those two numbers is the reason execution algorithms exist.

Finding · experiment three

04
Experiment four of five · the book

What the book charges, right now

The only question here that needs no simulation. Walking a resting book is arithmetic, so this one answers as fast as you can drag.

The first three questions all needed a simulation to run. This one doesn't. Below is a single frozen book, and buying against it is arithmetic: walk the ask side in price order, take what's resting at each level, stop when you have enough. That's what cost_to_trade does, without touching the book, and it answers in microseconds. Drag the size:

Order size120 lots
Finding · experiment four

05
Experiment five of five · the feed

Rebuild the book from an exchange feed

Everything above ran a simulation. This one takes an exchange's own event stream and reconstructs what was resting, message by message.

NASDAQ order flow is distributed by LOBSTER as a message file: one event per line, six columns, no book. Time, event type, order id, size, price in ten-thousandths, and direction. That's the whole feed. The book isn't in the file; it is what you get by applying the events in order, and getting that reconstruction right is the difference between analysing a market and analysing your own bookkeeping.

Seven event types, and three of them deliberately leave the visible book alone. A hidden execution fills against size that was never displayed, a cross prints at an auction, and a halt isn't a trade at all. Skipping them is correct rather than lazy, and the counters below report how many were skipped so the claim is checkable.

The feed on the left is generated rather than downloaded, because the real files are licensed and gigabytes wide, but the format and the event vocabulary are LOBSTER's and parse_lobster_line reads these exactly as it reads NASDAQ's. The book on the right is being rebuilt from it as you watch:

   waiting
Finding · experiment five

All five, together

The scorecard

Every row below was computed in this tab while you scrolled. Rows still reading not yet run belong to a section you haven't reached.

What was measuredHereReference

Five questions, one engine, and every number above computed while you read. The same engine is scored against published results in validation.md, where eleven of fourteen rows agree. The three that don't are written up rather than dropped: order-flow memory dies after about a hundred trades where real flow lasts thousands, returns carry a small negative autocorrelation where the reference is zero, and the mid mean reverts harder than a real one. A simulator that only listed its wins would be a brochure.