In this post, we will cover the basics of the Limit Order Book (LOB) for use in algorithmic trading. The mechanics of the order book are relatively simple, but getting an intuitive understanding of how it works can take a while. Familiarity with the order book is crucial to understanding more advanced concepts such as market making, liquidity, and various quantitative trading strategies.

The Limit Order Book

The basic function of the Limit Order Book, also called just LOB or order book, is to match buyers and sellers in the market. The order book is the mechanism used by the majority of electronic exchanges today, both in the financial and the cryptocurrency markets. We will look at example data from the cryptocurrency markets because such data is free and easy to obtain. Obtaining order book data in the financial markets often requires paid subscriptions.

Let's look at the order book for BTC/USDT , trading Bitcoin for USD, on the Binance exchange. The snapshot was taken from querying the Binance API at https://api.binance.com/api/v3/depth?symbol=BTCUSDT . In this post, we limit the visualization to the first 10 levels of the book.

Each level in the order book consists of a price and a quantity.

The book has two sides, asks (red) and bids (blue). Asks are sometimes called offers. Asks consists of orders from other traders offering to sell an asset - BTC in this case. Bids are orders from traders offering to buy an asset.

The best ask (8711.93) is the lowest price at which someone is willing to sell. That is, the lowest price at which you can buy BTC.

The best bid (8711.46) is the highest price at which someone is willing to buy. That is, the highest price at which you can sell BTC.

These two quantities are also called the top of the book since they are the best prices available. The best ask is always larger than the best bid. If this was not the case, you could make a quick profit by buying at the best ask and immediately selling at the best bid.

The mid price is the average of the best bid and best ask: (8711.93 + 8711.46) / 2 = 8711.695

The difference of the best bid and ask is called the spread: 8711.93 - 8711.46 = 0.47 . The spread is proportional to what would pay if you were to buy a small quantity of BTC and sell it again immediately. You can think of it as a fee you are paying for transacting in the market. The spread is one of the most important quantities of a market and is typically used as a measure of liquidity. But more on that later.

Each bid and ask level has a quantity associated with it. This quantity tells you how much is available at that price. For example, you can buy at most 0.12 BTC at the best ask of 8711.93 . If you want to buy more, you would need to go to the next higher ask of 8711.94 . The more you buy (sell), the higher (lower) the average price you are paying will be. The 0.12 BTC the make up the best ask could be a single order from one trader, or the sum of many orders from multiple traders. We typically don't get such fine-grained information (also called L3 data) from the exchanges - we only know the cumulative volume at each of the bid and ask levels (L2 data). Only some exchanges provide L3 data via public data feeds.

The tick size defines the precision of the bid and ask levels. It's set by the exchange. For the Binance BTC/USDT market the tick size is 0.01 . This means you can place orders at quantities such as 8711.93 and 8711.94 but not at 8711.935 . Tick size can have a large effect on market behavior. With a small tick size you can place fine-grained orders, which may result many tiny quantities at different levels of the book. For example, there is only a quantity of 0.01 available at 8713.10 - this quantity is so small that the whole level mostly noise, but it makes processing the order book more challenging and compute-intensive. It's not uncommon to post-process the order book to remove such noise or aggregate adjacent levels.

Now that we've defined the basic properties of the order book, let's see how we can interact with it.

Market Orders

The simplest way to interact with the market is to submit a market order for specific quantity. Let's say you submit a market buy order for 0.2 BTC . When this order hits the exchange, the exchange's matching engine will match your order against the current order book and execute it at the best price available. Assuming the book above, you would pay (0.12 * 8711.93) + (0.08 * 8711.94) = 1742.3868 USD , or an average price of 8711.934 USD per BTC . Once your order is executed, the order book is updated and would look like this:

Note that the best ask at 8711.93 is gone because you've bought all of it. The quantity at the next best ask has been reduced by 0.08 and only 0.01 is left . The spread has widened as a result of your order.

Submitting large market orders can be dangerous because you don't have any guarantee on the price you are paying. By the time your order arrives at the exchange, the order book will already be different from what you saw in the GUI or API when you submitted the order. This difference between expected and executed price is called slippage. However, it is guaranteed that your order is executed immediately. That is the tradeoff you are making with a market order: A guarantee on immediate execution, but no guarantee on the price.

Another way to look at market orders it that they take liquidity from the market. By matching against others orders in the book, you reduce the total available quantity in the market. That's why market orders are sometimes called taker orders.

Limit Orders

We've seen how you can match with other orders in the book, but how do you put orders into the book yourself? That's where limit orders come in. Limit orders are the opposite of market orders. A limit order guarantees you a price, but makes no guarantee on when a trade may happen. Limit orders are also called passive orders because they can sit in the book passively without ever getting matched. Limit orders provide liquidity - they are giving other traders to option to trade with you.

When submitting a limit order you specify a price and a quantity. Let's say you submit a limit order for quantity 0.50 BTC at price 8712.50 . That is, you are willing to sell 0.50 BTC at a price of 8712.50 USD . Once your order is processed by the exchange, the book would look as follows. Note how the quantity at 8712.50 has increased by 0.50 .

When somebody else buys BTC, they would first be matched against orders that offer a better price than you, then against orders that offer the same price as you but were submitted earlier, and then against your order. This is also called price-time priority. Your limit order doesn't necessarily need to be matched completed - it may be matched partially with multiple trades at different times.

To sum it up, the high-level differences between market and limit orders are these:

Market Order Limit Order Parameters Quantity Price + Quantity Price Guarantee No Yes Immediate Execution (Time Guarantee) Yes No Liquidity Taking Providing

Complex order types

Every exchange supports market and limit orders. They are the basic building blocks of a market. Depending on the exchange, there may be additional order types, such as iceberg limit orders (briefly described below) or stop loss market orders. These additional order types are simple extensions to make basic market and limit orders smarter, but they don't fundamentally change the building blocks of the order book.

The cumulative book

Another way to visualize the order book is by summing up the quantities available at each level with the quantities below. This is called the cumulative book and looks as follows in our example:

Here, a total quantity of 0.88 BTC is available to buy a price of at most 8713.0 . The cumulative books makes it easy to see the worst price you would pay for a certain market order. The total available liquidity is also easier to see.

The deceptive mid price

Remember that the mid price is the average of the best bid and ask. When asking "what is the price of X?" it's not clear which price you are referring to. As we've seen above, the price will be different depending on the trade direction (buy or sell) and quantity. When you see exchanges displaying a single price for an asset it's often the mid price because it's somewhat of a neutral average.

Note that you can never trade at the mid price. When you buy, you pay more than the mid price. When you sell, you get less than the mid price. In other words, the mid price is a theoretical quantity. And relying on it can be deceptive. For example, consider an order book such as the following.

The mid price is (100 + 99) / 2 = 99.5 . However, the order book is thin, meaning you cannot trade significant quantities are the best levels. If you were to buy a quantity of 1.0 you would pay 0.01 * 100 + 0.01 * 101 + 0.01 * 102 + 0.97 * 103 = 102.94 - almost 3% more than the mid price!

Let's look at another example.

Here, the mid price is (150 + 50) / 2 = 100.0 . When you see this price, you may think you buy or sell the asset at around 100.0 . But note that the spread ( 150 - 50 = 100 ) is huge! If you were to buy even just a small quantity with a market order you would be paying 150.0 - the best ask. That's 50% more than the mid price tells you.

In both of the above examples, the market is illiquid. Buyers and sellers have not agreed on a price, or there are simply not enough people who want to trade the asset. A thin order book, or a large spread, are typical signs of an illiquid market. When trading in such illiquid markets, it is crucial to look at the order book instead of relying on macro quantities such as the mid price. Liquid markets usually have the opposite properties - small spreads and thick order books. In such cases, relying on the mid price can be a good enough estimate of what transaction prices are. In a future post, we will learn more about how market makers can provide liquidity and what their incentive is for doing so.

Hidden liquidity and information

Another way to look at market and limit orders is in terms of the information they broadcast. With a limit order that goes into the book, you are broadcasting your belief of what an asset is worth to everyone. Such information can impact the market. Let's say a big institution has insider information about Intel earnings, and as a result of that, wants to sell a large quantity of Intel stock.

They would likely go to a middleman to execute this order, but let's assume for a moment they were trading on an exchange. They would not submit a market order because the price would be too bad for a large quantity. They could put a large limit sell order into the book, but that order would be visible to everyone else, signaling that the market may go down soon. Other trades may think: Why would someone try to get rid of a large quantity of Intel stock? There could be several reasons. Maybe the seller just needs cash. But it's not unlikely that the seller has information about Intel that others don't. This would result in other traders not wanting to buy, and with that, the large order would never get filled, and the market is more likely to go down.

In other words, it's in your best interest to not signal your intention with orders in the book. Information comes with a price. If you want to sell a large quantity, you don't want to make that large order visible to everyone. Instead, you'd be better off splitting your larger order into multiple smaller orders over time, hoping to make it less obvious what you're up to. Such approaches are loosely referred to as "order execution" - trying to execute an order while minimizing market impact and getting the best price.

That's what OTC (over the counter) services are for. They will handle execution for you while minimizing market impact. Some exchanges also offer special order types for such cases. One such example is the iceberg order, which only displays a fraction of the full order quantity in the order book:

The visible quantity of the Iceberg order is published in the order book. After the visible quantity of the Iceberg order has been executed, a further portion of the total quantity shall be published in the order book as a new visible quantity, and so on until the total quantity is executed, deleted or expires.

There are many other order types covering a wide range of scenarios. Even without using a special order type, which may not be supported by all exchanges, you can implement the same strategy in a manual fashion: Submit smaller orders and wait until they get executed to hide your true intentions.

This is just one example. The point is that the order book does not always paint the full picture of a market. When you see a small quantity in the order book, you don't know if the trader behind that order actually wants to buy or sell more than is being advertised.

Conclusion

The order book is the mechanism by which buyers and traders in a market are matched. It is also a mechanism by which market information is shared and prices are discovered. Looking at the order book is crucial when trading at high frequencies, where small movements make a big difference, or trading in illiquid markets.

In the next post, we will explore how market makers provide liquidity in a market by quoting spreads and interacting with the order book.