Market Mechanics – Part 4, How Order Books work

Part of a 6-part series on market mechanics:

  1. Introduction
  2. Functions of an exchange
    1. Custody
    2. Settlement
    3. Tax reporting
    4. Matching
    5. Market data
  3. Types of exchanges
    1. Order book
    2. Over-the-counter (OTC)
    3. Derivatives
  4. How order books work
    1. Mechanics
    2. Types of orders
      1. Market
      2. Limit
      3. Stop
    3. Matching
  5. What’s in a price?
  6. Market pitfalls
    1. Slippage
    2. Market cap
    3. Hidden supply
    4. Flash crash

Mechanics

An order book, fundamentally, is just a sorted list of price levels that people are willing to buy or sell at, along with the quantity available at that price. Internally, the exchange will also keep other data, including:

  • Who has placed the order, so it can match up buyers and sellers when a trade occurs.
  • When was the order placed?
  • Is there a specific expiration time on the order, after which it should be removed from the order book? (If not, it’s called a “good till cancel” order, which is the default on most exchanges.)
  • Are partial fills allowed, where only some of the desired quantity is matched up? This is usually the default; if not, it’s called a “fill-or-kill” order, which gives the order the same all-or-nothing property that OTC exchanges provide.
  • Should the order be hidden from public market data feeds? This is an option on some exchanges, but not all offer it.

This extra data is not usually exposed to the public. Market data services like Cryptolazza can sometimes infer when by watching the order book continuously, and occasionally can identify if orders were placed by the same person (but not who that person is).

Coinbase Pro order book. Asks are at top, bids at bottom.

The order book is usually divided into two sides. Bids are all the orders looking to buy the coin, and asks are orders looking to sell the coin. The two sides never cross, within a single exchange – if there is an ask that’s lower than the highest bid, a trade happens and the orders are removed from the book. The spread is the distance between the highest ask and the lowest bid. Spreads are typically low or non-existent with common cryptocurrencies like Bitcoin, but can be very high with thinly-traded coins.

Types of orders

When you enter an order into an exchange, you’re often given a choice of order type:

Binance.us order entry screen

What’s the difference between those types? This section will explain:

Market orders

A market order is a request to trade at a set time, almost always “now”. You get whatever the best price is on the market at the time the order is filled. Market orders are sometimes called “takers”, because they take whatever the best price is on the market at a particular time.

You are not guaranteed to get the current price listed on the exchange when you place a market order. If several people are trading at the same time (almost always the case – most exchanges have dozens to hundreds of trades per second), other orders might get processed first and fill several price levels in the order book before yours gets its turn. Also, remember that a single market order may be filled by multiple entries in the order book: if you’re placing a large market order, you may eat up that liquidity and be forced to fill the remainder of your order at prices you didn’t anticipate. This tendency for market orders to get worse prices than they anticipate is called slippage.

Market orders are often used in reaction to current news or events, when you want to make sure that you trade as quickly as possible and are willing to take the risk that the price may move against you.

Limit orders

A limit order is a request to buy or sell at a set price. When you enter a limit order, it goes into the order book. It does not get executed immediately, unless there’s an order already in the order book that can be matched with it. If you post a limit buy that’s higher than the best ask, or a limit sell that’s lower than the best bid, it acts like a market order and is executed immediately at the best price on the market.

Limit orders are sometimes called “makers” (because together they make up the market), and you will sometimes see exchanges posting different fees for limit and market orders. Oftentimes maker fees are lower, because exchanges want to incentivize a deep order book.

When you issue a limit order, you know exactly what price you will get for your coins. You do not know when the order will be filled, or even if it will be. Limit orders stay in the order book until you cancel them; it’s possible to keep them open forever.

Limit orders are usually used if:

  • You’ve previously decided upon a value that you’d be happy buying the coin at, and want to buy if it happens to reach that price without needing to watch the markets every day.
  • You’ve done some fundamental analysis to arrive at a value for the coin, and want to buy only at that price or better.
  • You want to see if you can get a slightly better price than the current market offers, and don’t mind waiting a while and taking the risk that the market will never fill your order.
  • You want to avoid the risk of the market moving against you. Placing an out-of-the-money limit order (a bid slightly higher than the best ask, or an ask slightly lower than the best bid) limits your downside to the price on your order, while if the market doesn’t move, it’s filled at the current best price and you lose nothing.

Stop orders

Stop orders actually come in two flavors: stop-limit and stop-market orders. A stop order has a certain stop price at which point it becomes active, and then is treated as a normal limit or market order (respectively). This differs from limit orders in that a stop order isn’t in the order book until its stop price is reached, while a limit order is in the order book, it just isn’t executed. Because the order isn’t in the order book until the stop price is reached, you can put in a stop-sell order at a price significantly below the current market price without actually selling anything.

Stop orders are often used by daytraders to contain their downside. They’ll figure out a price point below which they consider the market too hot, where they just want to get out entirely, and put a stop order there. They can then trade normally, confident that if the market really starts moving fast, they’ll get out automatically without losing too much.

For example, if the current price of Bitcoin is $8600, a trader might decide that anything under $8000 is more risk than he wants to take. He puts a stop-limit order with a stop price of $8010 and a limit price of $8000 to protect his downside. Then he buys in with a market order at $8600. If the price goes up, great, he’s made money. If the price drops below $8010, his stop order becomes active, and enters a limit order at $8000. Normally this will execute immediately, he’ll sell his position at the current market price (somewhere between $8000 and $8010), and he’s lost at most 7.5% on his investment.

There is a pitfall to be aware of, and it comes from slippage and the risks of market & limit orders. Say that the market is dropping really fast – maybe a whale dumped $500M worth of Bitcoin on the market all at once, and the price dropped from $8600 to $7900 immediately. At this point, the stop order activates, but the price is already under the limit price of $8000. Now your limit order goes on the order book as an $8000 ask, and it won’t be executed unless the price goes back over $8000, which may be never. If you’d placed a stop-market order instead of a stop-limit, you would guarantee that it executes, but the price you’d get is $7900 rather than $8000.

There’s no such thing as perfect protection. Stop orders can be valuable tools to get you out of a rapidly falling market, but there’s always a chance that your stop-limit won’t execute (leaving you exposed) or your stop-market will get a worse price than expected (losing you money).

Matching

The matching algorithm for order books is pretty simple: the book is kept sorted, and as new market orders come in, the best bid/ask is matched with them, the filled quantity is deducted from that price level, and a trade is recorded and settled.

For example, assume that the order book contains these entries:

PriceQuantitySide
2.288342.74Sell
0.718342.38Sell
2.008342.36Sell
1.038342.26Sell
7.858342.01Sell
0.098342.00Buy
0.118340.97Buy
0.108340.96Buy
0.208340.71Buy

Now imagine that a market buy for 2 BTC comes into the exchange. It’s matched up with the lowest asking price, so the new order book looks like:

PriceQuantitySide
2.288342.74Sell
0.718342.38Sell
2.008342.36Sell
1.038342.26Sell
5.858342.01Sell
0.098342.00Buy
0.118340.97Buy
0.108340.96Buy
0.208340.71Buy

The buyer’s quantity has been deducted from the entry for $8342.01, a trade occurs between them @ $8342.01, and the new quantity available is 5.85 BTC.

Now imagine that a limit sell for 1.5 BTC @ 8342.30 comes in. This is higher than the current best asking price, so it goes into the order book, in sorted order, and waits for another order to match with it:

PriceQuantitySide
2.288342.74Sell
0.718342.38Sell
2.008342.36Sell
1.508342.30Sell
1.038342.26Sell
5.858342.01Sell
0.098342.00Buy
0.118340.97Buy
0.108340.96Buy
0.208340.71Buy

Now suppose that a market sell for 0.25 BTC comes in. Because it’s a market order, it eats up the best bid. However, the order is for more BTC than is available in the first bid, so it also eats up the second, and half of the third. The order book now looks like this:

PriceQuantitySide
2.288342.74Sell
0.718342.38Sell
2.008342.36Sell
1.508342.30Sell
1.038342.26Sell
5.858342.01Sell
0.058340.96Buy
0.208340.71Buy

This takes the price down from $8342.01 to $8340.96, and the spread is now $2.05. After this, a limit buy comes in for 2 BTC @ $8341.95. This is inside the spread, lower than the best ask, and so it goes into the order book:

PriceQuantitySide
2.288342.74Sell
0.718342.38Sell
2.008342.36Sell
1.508342.30Sell
1.038342.26Sell
5.858342.01Sell
2.008341.95Buy
0.058340.96Buy
0.208340.71Buy

Note that no trade happens, and the price doesn’t change. However, if we were now to receive a limit sell for 2.5 BTC @ $8340.80, it’d be below the best bid. Despite being a limit order, part of it is executed immediately, eating up the 2 BTC @ $8341.95 and the 0.05 BTC @ $8340.96. However, since the limit price is higher than the next highest bid @ $8340.71, the remaining 0.45 BTC goes into the order book and isn’t executed immediately:

PriceQuantitySide
2.288342.74Sell
0.718342.38Sell
2.008342.36Sell
1.508342.30Sell
1.038342.26Sell
5.858342.01Sell
0.458340.80Sell
0.208340.71Buy

The last price is now $8340.96, the best ask is the new order @ $8340.80, and the best bid is @ $8340.71, for a spread of $0.09.

We hope this has given you a good picture of how the matching algorithm works for order book exchanges. Understanding how orders are matched up is critical to understanding how price movements happen and how certain strange situation may occur with markets.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s