Skip to content

Order Flow Imbalance Example

File: python/workmanager/orderflow_imbalance_worker.py

Calculates weighted bid/ask imbalance from Binance CLOB depth data.

Pipeline

Binance → WebSocketManager → State Store → WorkManager Worker → Imbalance
  1. WebSocketManager connects to Binance depth stream
  2. Depth snapshots stored in state store (examples/btcusdt-orderbook)
  3. WorkManager worker polls state store for new updates
  4. Weighted bid/ask imbalance calculated and published

Imbalance Formula

For the top N price levels:

Weighted Bids  = Σ bid_price[i] × bid_qty[i]
Weighted Asks  = Σ ask_price[i] × ask_qty[i]
Imbalance      = Weighted Asks - Weighted Bids

Signal interpretation:

Condition Signal Meaning
Imbalance > 10% of asks SELLING Ask side has more liquidity — selling pressure
Imbalance < −10% of bids BUYING Bid side has more liquidity — buying pressure
Otherwise NEUTRAL Balanced order book

Running

# Terminal 1: Start the Binance CLOB feed
source .env
python python/websocketmanager/binance_clob_example.py

# Terminal 2: Start the imbalance worker
python python/workmanager/orderflow_imbalance_worker.py

Expected output:

============================================================
  Order Flow Imbalance Worker — BTCUSDT
============================================================

1/3  Starting Binance CLOB via WebSocketManager...
     ✓ WebSocket connected: id=def456
     ✓ Publishing depth to topic: btcusdt-depth

2/3  Waiting for initial order book snapshot in state store...
     ✓ Snapshot found (updateId=123456789)

3/3  Monitoring state changes. Publishing imbalance to: btcusdt-imbalance

     Time      Wtd Bids      Wtd Asks     Imbalance     Spread   Signal
   ────────  ────────────  ────────────  ──────────  ────────  ──────────
      10.0s   2,547,830,112 2,551,245,890   +3,415,778     1.50   NEUTRAL
      20.1s   2,512,450,100 2,530,891,234  +18,441,134     2.10  ⚠  SELLING
      30.1s   2,568,123,456 2,545,678,901  −22,444,555     1.80  ⚠  BUYING