I'm trying to implement a code to build limit order books. While doing some research I have seen two methods mainly, order-based for equities and level-based for futures.
I could not understand why there are two methods. Why doesn't level-based methods fit for equities? or vice versa...
Thanks
1 Answer
The only true order book is the order book which consists of orders, i.e. order-by-order or market-by-order (MBO) full-depth order book. It's the order book managed internally by absolutely all exchanges (stocks, futures, crypto/digital, etc). The market-by-price (MBP) is an aggregated view of the order book. You manage the type of order book, corresponding to the type of market data provided by the exchange, and depending on type of data delivered to you by your data vendor. For instance, CME futures exchange does provide MBO data and it can be used undamaged on retail trading platforms, for instance, with Bookmap
Managing a market-by-price order book is trivial:
public class OrderBookMarketByPrice {
public final TreeMap<Integer, Integer> asks = new TreeMap<>((a, b) -> a - b);
public final TreeMap<Integer, Integer> bids = new TreeMap<>((a, b) -> b - a);
@Override
public void quote(long t, boolean isBuy, int price, int size) {
TreeMap<Integer, Integer> queue = (isBuy ? bids : asks);
@SuppressWarnings("unused")
Integer prevSize = size == 0 ? queue.remove(price) : queue.put(price, size);
}
}
This can easily process 10+ million updates per second on a regular laptop. Here, you delete the price level if its size is zero. The price is an integer because it represents the number of minimum price increments. The two TreeMap objects allow navigating from the best bid or best ask and deeper into each side. Managing an order-by-order order book is just slightly more complex