Skip to main content

Command Palette

Search for a command to run...

Order Book Imbalance Predictor: Forecasting Short-Term Price Moves with Python and Streamlit

Published
4 min read
Order Book Imbalance Predictor: Forecasting Short-Term Price Moves with Python and Streamlit

Introduction

In modern electronic markets, price movement rarely starts with a candle.

It starts in the order book.

Before prices rise or fall, liquidity shifts. Bids stack. Asks thin out. Market pressure builds quietly—often visible only through order book imbalance.

For quantitative traders and researchers, this makes order book imbalance one of the most powerful signals for short-horizon price forecasting.

In this article, we’ll:

  • Explain what an order book imbalance is and why it matters

  • Show how imbalance can forecast short-term price moves

  • Build a reusable order book data module

  • Develop an Order Book Imbalance Predictor in Python

  • Visualize predictions using Streamlit


What Is an Order Book?

An order book is a real-time list of:

  • Bid orders (buyers)

  • Ask orders (sellers)

Each level shows:

  • Price

  • Quantity

  • Depth

Example (simplified):

PriceBid QtyAsk Qty
100.01120
100.00300
99.99250
99.98400

The imbalance between bid and ask liquidity often precedes price movement.

What Is Order Book Imbalance?

Order Book Imbalance (OBI) measures buying vs selling pressure.

A common definition:

$$\text{Imbalance} = \frac{\sum BidVolume - \sum AskVolume}{\sum BidVolume + \sum AskVolume}$$

  • +1 → strong buy pressure

  • 0 → balanced market

  • −1 → strong sell pressure

This signal is especially effective for:

  • Market making

  • Short-term alpha

  • Execution optimization

  • HFT and intraday strategies

Why Order Book Imbalance Predicts Price Moves

Order book imbalance works because:

  1. Liquidity precedes price

  2. Markets move to where liquidity is thinner

  3. Aggressive orders follow pressure

  4. Microstructure effects dominate short horizons

Unlike technical indicators, imbalance:

  • Uses raw market intent

  • Updates in real time

  • Captures hidden pressure


Hands-On Tutorial

System Architecture

We’ll build the project in three layers:

OrderBook Generator → Feature Engineering → Predictor → Streamlit UI

Step 1: Order Book Data Module (Reusable)

We’ll start by creating a module that generates realistic order book data for any stock.

orderbook.py

import numpy as np
import pandas as pd

class OrderBookSimulator:
    def __init__(self, mid_price=100.0, levels=5):
        self.mid_price = mid_price
        self.levels = levels

    def generate_snapshot(self):
        bids = []
        asks = []

        for i in range(1, self.levels + 1):
            bids.append({
                "price": round(self.mid_price - i * 0.01, 2),
                "volume": np.random.randint(50, 500)
            })
            asks.append({
                "price": round(self.mid_price + i * 0.01, 2),
                "volume": np.random.randint(50, 500)
            })

        return pd.DataFrame(bids), pd.DataFrame(asks)

    def stream(self, steps=100):
        data = []
        price = self.mid_price

        for _ in range(steps):
            bids, asks = self.generate_snapshot()
            imbalance = self.compute_imbalance(bids, asks)

            # Simulated price movement
            price += np.sign(imbalance) * np.random.uniform(0, 0.02)

            data.append({
                "imbalance": imbalance,
                "price": round(price, 2)
            })

        return pd.DataFrame(data)

    @staticmethod
    def compute_imbalance(bids, asks):
        bid_vol = bids["volume"].sum()
        ask_vol = asks["volume"].sum()
        return (bid_vol - ask_vol) / (bid_vol + ask_vol)

This module:

  • Generates multi-level order books

  • Computes imbalance

  • Simulates price response

  • Can be reused across projects


Step 2: Feature Engineering

Now we prepare features for prediction.

def create_features(df):
    df["future_price"] = df["price"].shift(-1)
    df["price_move"] = df["future_price"] - df["price"]
    df["direction"] = (df["price_move"] > 0).astype(int)
    return df.dropna()

We’ll predict directional movement (up/down).


Step 3: Train an Imbalance Predictor

We’ll use a simple but effective model: Logistic Regression.

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression

def train_model(data):
    X = data[["imbalance"]]
    y = data["direction"]

    X_train, X_test, y_train, y_test = train_test_split(
        X, y, test_size=0.2, shuffle=False
    )

    model = LogisticRegression()
    model.fit(X_train, y_train)

    return model, model.score(X_test, y_test)

Step 4: Streamlit App

Now let’s build an interactive dashboard.

app.py

import streamlit as st
from orderbook import OrderBookSimulator
from model import train_model, create_features

st.title("📊 Order Book Imbalance Predictor")

simulator = OrderBookSimulator(
    mid_price=st.slider("Mid Price", 50.0, 200.0, 100.0),
    levels=5
)

data = simulator.stream(200)
features = create_features(data)

model, accuracy = train_model(features)

st.metric("Model Accuracy", f"{accuracy:.2f}")

st.line_chart(data["price"], height=300)
st.line_chart(data["imbalance"], height=300)

latest_imbalance = data["imbalance"].iloc[-1]
prediction = model.predict([[latest_imbalance]])[0]

st.subheader("Next Price Move Prediction")
st.success("📈 UP" if prediction else "📉 DOWN")

What This Project Demonstrates

  • Market microstructure modeling

  • Feature engineering from order books

  • Alpha signal design

  • Real-time visualization

  • Modular system design

How This Scales to Real Markets

To move toward production:

  • Replace simulator with real order book feeds (WebSockets)

  • Add multi-level imbalance features

  • Include time decay and rolling windows

  • Use LSTM / XGBoost

  • Add transaction cost modeling

Why This Matters for Quants & Traders

Order book imbalance:

  • Operates before price changes

  • Works at the microsecond to second horizons

  • Complements traditional alpha signals

  • Improves execution quality

For quants, this project is a foundation for HFT, intraday, and market-making systems.

Final Thoughts

Most trading models look at prices after the fact. Order book imbalance looks at intent before execution. By combining market microstructure signals with lightweight ML and real-time visualization, you gain a sharper lens into short-term price dynamics.

The real edge isn’t predicting prices—it’s understanding pressure.