We will be understanding CLOBs with the help of a story featuring KB, Molandak and Clobby.
KB wants to buy some apples and goes to the market.
There are a lot of shops that are selling apples. She decides to ask a few shops the price of apples, here is what she finds out:
Looks like everyone doesn’t agree on the same price for the apples. KB doesn’t want to get a bad deal, but she’ll have to ask all the shops to find out the best price. This is going to take a long time and is exhausting.
Now, let’s talk about Molandak.
He wants to sell some apples, and he's doing the same thing as KB-- asking the shops what price they can buy his apples at. This is what Molandak notes down:
Like KB, he’s trying to find the best deal, but it’s not easy.
KB and Molandak have another problem. Some stalls can't buy all of Molandak's apples, and some can't sell KB the exact number of apples she needs.
This is starting to get a bit tricky and time-consuming. KB and Molandak spend a lot of time just to make sure they're not getting a bad deal. There must be a simpler way, right?
Clobby to the rescue
Clobby has been watching this from the side and understands this problem. He comes up with an idea. He decides to visit all the shops and note down the price that each shop is offering, for both buying and selling.
This is what the book looks like:
It turns out that some shops are willing to buy and sell more apples than others, so he notes them down with an additional column called “Quantity”.
This seems helpful. Clobby shows this to KB and Molandak, and they love the idea.
It would be easier if the prices were arranged from best to worst. They want the best deal possible. For KB that means buying apples from the shop that offers the lowest price. For Molandak it means selling his apples to the shop that offers the highest price.
Clobby understands and changes this on his book.
If anyone comes to the market in the future, they can look at Clobby’s book and find the shops that trade at the best price quickly.
Looks like a very clean solution, right?
So, what is a CLOB?
This is exactly what a Central Limit Order Book, or CLOB is. Exchanges maintain a CLOB for each market. In our story it was apples and USD, but you would’ve seen BTC/USDT or ETH/USDC on other exchanges.
Users are free to come and place limit orders onto the book. A limit order contains the following information:
Type: Buy or Sell (is the shop willing to buy or sell apples?)
Price: What price do they want to buy/sell at (prices that the shops are willing to trade at)
Quantity: How much of the asset do they want to buy/sell (in this story apples, but could be any asset like BTC or ETH)
Time: At what time was the order placed? This is something that the exchange keeps track of, the user doesn’t have to specify this.
Here are two examples of orders from Shop Z and Shop A.
An order book has two sides, Bids and Asks
Bids are a list of all the buy orders on the book
Asks are a list of all the sell orders on the book
This is a screenshot of an order book from a crypto exchange.
It looks slightly different than our order book.
Order books can be visualised in multiple ways, but common ones are vertical and side-by-side. In our story asks and bids are being shown next to each other. This is the side-by-side view. Most exchanges however stack asks and bids vertically (ladder). In a vertical view, the prices are arranged in a descending order from highest on the top to lowest at the bottom.
Price Time Priority
An order book allows traders to find the best deal on the market. To make this easier, orders in the book are sorted according to a concept called Price Time Priority.
You’ll notice bids (buy orders) are sorted with the highest price on top (the highest price someone is willing to buy at) and the asks (sell orders) are sorted with the lowest price on top (the lowest price someone is willing to sell at). This is called Price Priority.
The highest priced buy order is called the best bid and the lowest priced sell order is called best ask. In our story, best bid was at $7 and the best ask was at $8.
What happens if there are two orders at the same price?
We give higher priority to orders that came in earlier, like a queue. This is Time Priority.
Here are the asks and the corresponding time that each order was written on the book. There are two orders at $8, one came at 1:45 pm and the other at 2:15 pm. The earlier one is placed on top of the later one.
An interesting thing to note: the $9 order came at 1:15 pm, earlier than the best ask (i.e $8 order at 1:45 pm). But we don’t place it on top. We give higher priority to better price (in this case $8) than time. Only if there are two similarly priced orders, we look at the time to decide which order goes in front of the queue.
This is how we maintain Price and Time Priority
Market orders
In the apples story above, KB and Molandak had no particular price in mind. They were willing to trade at the best price that the market had to offer. These are known as market orders.
Market orders contain the same information as limit orders, but usually contain no price or time.
When a market order comes in, they get matched against the orders sitting on the book.
KB wanted to buy 20 apples.
Step 1 - The best price she can get is $8. But that shop is only willing to sell 10 apples. So she buys 10 apples from there. She still needs to buy 10 more apples
Step 2 - Now the best price is $9. That shop can sell 20 apples, but KB needs only 10 more apples (she got 10 from the previous shop in Step 1). She can buy 10 more apples from here at $9.
Step 3 - KB now has the 20 apples she wanted
KB is taking 10 apples for $8 and 10 apples for $9. Notice that the best selling price is now $9.
Similarly let us look at Molandak trying to sell 15 apples.
Step 1 - The highest price he can sell is $7. But that shop is only willing to buy 10 apples. So he sells 10 apples. He’s left with 5 more apples to sell.
Step 2 - Next highest price is $6. That shop can buy 20 apples, but Molandak has only 5 more apples to sell (remember he sold 10 apples in Step 1 to another shop). He can sell his remaining 5 apples at $6.
Step 3 - Molandak has now sold the 15 apples.
Molandak is taking dollars from the shops in return for his apples. Notice that the best buying price is $6.
The user placing the market order is known as a taker, since they are taking assets (apples) from the orderbook. Conversely, a user placing resting limit orders is know as a maker, since they are making the market. Without them the book would just be empty.
Remember the shops in our story? They were willing to buy and sell apples. These shops were allowing traders like KB and Molandak to come and trade freely at any point. In crypto as well, there are market makers who are willing to both buy and sell the asset.
Why do they do this? What does the market maker get in return? We’ll talk more about this in the next blog.
s/o to Tequila, Reinn and JoNADthan, the amazing artists from our community for bringing our blog to life.