DEV Community

James Tao
James Tao

Posted on

My Production Pipeline for 24/7 Level 2 Market Data Collection

Intro

If you’re a quant dev, algorithmic trader, or fund research engineer, you’ve almost certainly run into this frustrating issue: models trained only on tick trades and Level 1 top-of-book data perform great in backtesting, yet fall apart the second you run live simulation. Critical order flow signals disappear entirely.
I’ve spent years building market data pipelines for institutional quantitative teams. When we built an intraday volatility forecasting system for an equity fund, our initial data stack only pulled trade prints and single-tier quotes. It worked fine during quiet sideways markets, but signals lost all accuracy around opening auctions, closing volume spikes, and large block order events.

After deep debugging, we found the core limitation: tick data only records completed trades, and Level 1 data hides layered limit orders stacked far from the current price. Without full market depth, you can’t map supply and demand shifts ahead of price moves.
This guide walks through a production-ready WebSocket Level 2 data pipeline, including data comparison, connection architecture, local order book logic, production stability fixes, and minimal Python code you can drop into your project.

1. Tick vs Level 1 vs Level 2: When to Use Each Feed

Before building any trading model, clarify the purpose of each market data source — picking the wrong feed ruins your analytical depth.
Tick-by-Tick Trade Data
Logs every matched order’s price and volume. Great for candlestick generation and historical backtesting. The downside: it only shows past trades, no visibility into pending limit orders that will drive future price action.
Level 1 Top-of-Book Data
Only returns the best bid, best ask, and their respective volumes. Perfect for simple market dashboards, but unable to detect hidden buy/sell walls across distant price levels. You’ll consistently underestimate support and resistance strength.
Level 2 Full Depth Data
Stores aggregated resting order volume for every price tier on both sides of the market. This is the foundational feed for measuring real-time liquidity and spotting early order imbalance signals before prices shift.
Real-world example: Large buy orders stacking at key support, or mass sell order cancellations at resistance — these predictive patterns only appear in Level 2 streams, never in finished tick data.

2. Why WebSockets Beat HTTP Polling for Level 2 Data

Traditional polling has fatal flaws for high-frequency order book data: fixed-interval requests skip millisecond book updates, and repeated API calls waste server bandwidth and inflate long-term cloud costs.
Level 2 order books refresh constantly, so bidirectional persistent WebSocket connections are the industry standard. The core workflow has four simple steps:
Establish a permanent WebSocket tunnel between your client and market data gateway
Send a subscription request with your target tickers and data type (Level 2)
Receive continuous incremental delta updates whenever the order book changes
Refresh your local order book snapshot using cached historical state
A common beginner mistake: most data APIs send only delta changes, not full resets every message. If you don’t cache the latest full book locally, delta-only messages will create missing price tiers and miscalculated total order volume, breaking all your liquidity metrics.

3. Core Level 2 Fields & Local Order Book Storage Logic

Every incremental Level 2 message relies on five mandatory fields we standardize across all production pipelines:
symbol: Ticker identifier to separate data for multiple instruments
side: Marks bid (buy) or ask (sell) order layers
price: The price tier being updated
size: Remaining resting order quantity at this price
timestamp: Event time for cross-data time alignment and outage validation
Our production design splits buy and sell layers into two independent arrays.
When we receive an update:
Adjust the volume value for the matching price tier based on side
Delete the tier entry if size drops to zero
This setup removes full array scans when calculating top bid/ask or total market depth, cutting down compute latency and supporting parallel monitoring of dozens of tickers at once.

4. Minimal Working Python WebSocket Snippet

We separate network transport and data parsing logic for stable cloud deployments. Our dev environments use AllTick API as the market data provider. This stripped-down example works out of the box; you just add tier parsing logic for full production order book maintenance:

import websocket

def receive_data(ws, msg):
    data = eval(msg)
    code = data.get("symbol")
    price = data.get("price")
    vol = data.get("volume")
    print(f"Ticker: {code}, Price: {price}, Order Volume: {vol}")

def connect_init(ws):
    sub_info = {"action": "subscribe", "symbol": "MSFT", "type": "level2"}
    ws.send(str(sub_info))

if __name__ == "__main__":
    ws_client = websocket.WebSocketApp("wss://api.alltick.co/stock/websocket",
                                       on_open=connect_init,
                                       on_message=receive_data)
    ws_client.run_forever()
Enter fullscreen mode Exit fullscreen mode

5. Four Production Stability Improvements for 24/7 Pipelines

Level 2 streams generate thousands of delta messages per second. Network drops and timezone mismatches easily break data continuity. After deploying dozens of institutional pipelines, we standardized four fail-safe workflows to eliminate manual data repair work:
Scheduled full order book snapshot caching
Save a complete copy of all bid/ask tiers on a fixed interval. After disconnection, restore the full book instantly from the snapshot instead of requesting full historical resyncs.
Timestamp sequence validation filter
Compare incoming message timestamps against local cache records. Auto-discard out-of-order and duplicate messages to prevent duplicate volume counting errors.
Auto resync after WebSocket disconnect
Once the tunnel reopens, trigger supplementary data requests to fill missing delta records from the outage window.
Abnormal price/volume threshold filtering
Set reasonable min/max bounds for price swings and order sizes. Auto-filter noise like flash price jumps and spoof oversized orders to avoid skewing your quant indicators.
Pro tip: Most market gateways output UTC timestamps. If you mix UTC with local time during backtesting or time-window aggregation, your entire timeline will misalign. Normalize all timestamps during initial parsing to avoid this common bug.

Wrap Up

A lot of new quant engineers treat WebSocket connection setup as the entire Level 2 integration process. In reality, data reliability hinges on underrated engineering details: persistent snapshot storage, automatic outage recovery, and unified time synchronization.

Tick data only shows completed trades, while full Level 2 depth exposes pre-trade order flow that drives price moves. Building a standardized Level 2 pipeline adds multi-layer analysis for intraday forecasting and liquidity risk tracking. For teams monitoring short-term capital shifts, this stack drastically reduces performance gaps between backtesting and live trading, delivering far more reliable trading signals.

Top comments (0)