Level-3 Market-By-Order (MBO) data represents the highest fidelity market data available, containing every individual order in the book with unique order IDs. This enables the most accurate order fill simulation possible, as you can track the exact position of your orders in the queue and see when orders ahead of you are canceled or filled.
Level-3 backtesting is primarily available for CME futures markets through DataBento’s MBO feed. The complexity and data volume of Level-3 feeds means they are not commonly available for cryptocurrency exchanges.
HftBacktest provides built-in support for converting DataBento’s CME Market-By-Order data:
from hftbacktest.data.utils import databento# Convert DataBento MBO files to HftBacktest formatfor date in range(20240609, 20240615): databento.convert( f'data/db/glbx-mdp3-{date}.mbo.dbn.zst', 'BTCM4', # Symbol to extract output_filename=f'data/BTCM4_{date}_l3.npz' )
The conversion process includes:
Latency correction: Aligns exchange and local timestamps
Event ordering: Ensures proper chronological order
# FIFO queue model for Level-3 dataasset.l3_fifo_queue_model()# Your position in queue is tracked exactly based on:# - Order arrival time# - Cancellations ahead of you# - Fills ahead of you
Order Submission: When your order joins the queue, your position equals the current quantity at that price level
Order Ahead Filled: When a trade occurs, all orders at the front are filled before yours
Order Ahead Canceled: When an order ahead of you cancels, your position improves immediately
Your Turn: You get filled when:
You’re at the front of the queue AND
A trade occurs at your price level OR
The market crosses your price
# Example: Queue position tracking# Price level 69000 has orders:# Order A: 5 contracts (front)# Order B: 3 contracts# Your Order: 2 contracts# Order D: 4 contracts (back)# Initial queue position: 8 contracts aheadqueue_position = 5 + 3 # Orders A + B# If Order A cancels:queue_position = 3 # Only Order B ahead now# If trade occurs for 2 contracts:queue_position = 1 # 3 - 2 from Order B# If another trade for 1+ contracts:queue_position = 0 # Your order fills!
# 1. Use ROI (Region of Interest) to limit depth processingasset.roi_lb(0.0).roi_ub(100000.0) # Only track relevant price range# 2. Limit backtest period for initial testingasset.data(['data/BTCM4_20240609_l3.npz']) # Start with single day# 3. Increase running interval if sub-millisecond precision not neededwhile hbt.elapse(100_000_000) == 0: # 100ms vs 1ms intervals