Inspecting Order Book Depth, Imbalance, Trades, and VWAP
Summary
This tutorial demonstrates how to inspect market depth and trade flow in an event-driven backtest. It first reads the nearest visible bid and ask levels, then shows a region-of-interest vector representation that limits depth access to a configured price range. The examples calculate order book imbalance at the best quotes and across bands extending to 0.5% and 1% from the midpoint, recording the values over time for plotting. The tutorial also retrieves recent market trades and separates buyer-initiated from seller-initiated volume to calculate short rolling VWAP measures, including total, buy-side, and sell-side values.
The code illustrates practical details such as price ticks, capacity settings for retaining trades, and clearing retrieved trades to avoid accumulation. It configures a simulated BTCUSDT perpetual-style market with latency, fees, and queue assumptions, but does not present a trading signal, profitability test, or interpretation of the plotted results. The depth region must cover relevant prices, and the shown backtest assumptions and sample data constrain how the outputs should be generalized.
Key ideas
- Market depth can be queried by price tick or through a bounded vector region of interest.
- Order book imbalance can compare bid and ask quantities at the top of book and across midpoint bands.
- Recent trades can be grouped by aggressor side to compute buy-side and sell-side VWAP.
- Retrieved trade data should be cleared after processing to prevent unnecessary accumulation.
- The examples demonstrate data access and measurement rather than a validated trading strategy.
Tags
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.