DEV Community

Cover image for Building a Real-Time Market Microstructure Analyzer for Indian Equities
Aarya Parekh
Aarya Parekh

Posted on

Building a Real-Time Market Microstructure Analyzer for Indian Equities

Financial markets produce enormous amounts of data—but converting raw order-book updates into meaningful liquidity and order-flow signals requires more than displaying prices on a chart.

I built the Real-Time Market Microstructure Analyzer, an open-source research platform that processes five-level order-book snapshots, calculates market microstructure indicators, and displays the results through an interactive dashboard.

🔗 Live demo: https://aariiparekh3012-collab.github.io/quantproject2/
💻 GitHub: https://github.com/aariiparekh3012-collab/quantproject2

What is market microstructure?

Market microstructure studies how orders, trades, liquidity, and market participants interact to form prices.

Rather than looking only at daily closing prices, microstructure analysis examines information such as:

  • Bid and ask prices
  • Order-book depth
  • Bid–ask spreads
  • Order-flow imbalance
  • Trading volume
  • Price impact
  • Execution costs

These measurements can help researchers understand short-term liquidity and trading behaviour.

What the platform does

The system accepts five-level order-book snapshots and processes them through a streaming analytics engine.

It currently provides:

  • Bid–ask and depth-weighted spread calculations
  • Order-flow imbalance across multiple rolling windows
  • Session VWAP and deviation bands
  • Cumulative signed volume
  • Kyle’s lambda price-impact estimates
  • Amihud illiquidity estimates
  • Roll spread estimates
  • Rolling anomaly detection
  • OFI strategy backtesting
  • TWAP and replay-VWAP execution simulation
  • FastAPI REST and WebSocket endpoints
  • A React-based analytics dashboard

The repository also includes an offline demonstration using embedded synthetic data for five NSE-listed equities:

  • RELIANCE
  • TCS
  • HDFCBANK
  • INFY
  • ICICIBANK

System architecture

The project has four main layers:

  1. Data ingestion
    Generates deterministic synthetic order-book data or accepts data through an experimental brokerage adapter.

  2. Analytics engine
    Calculates spread, liquidity, order-flow, VWAP, volume, price-impact, and anomaly signals.

  3. API layer
    Publishes market data, analytics, and alerts using FastAPI and WebSockets.

  4. Frontend dashboard
    Displays the order book, price behaviour, VWAP, spread, OFI, cumulative delta, and statistical alerts.

Each incoming snapshot is normalised and passed through the analytics engine. Stateful modules retain rolling windows and online aggregates between updates.

Core analytics

Order-flow imbalance

Order-flow imbalance, or OFI, measures changes in supply and demand at the best bid and ask.
The platform calculates event-level OFI and aggregates it over rolling 60-second, 300-second, and 900-second windows.
This helps identify whether recent order-book activity has been dominated by buying or selling pressure.

Spread and liquidity

The spread module calculates:

  • Quoted spread
  • Relative spread
  • Depth-weighted spread

The system also includes Kyle’s lambda, Amihud illiquidity, and Roll spread estimators to examine different aspects of liquidity and price impact.

VWAP and volume

Session VWAP is calculated through online running sums and resets at the trading-day boundary.
The volume module uses tick-rule classification to estimate signed volume, create price-level volume profiles, and maintain cumulative delta.

Anomaly detection

The platform monitors spread, volume, and OFI using rolling z-scores.
When a metric crosses the configured threshold—three standard deviations by default—the engine generates an alert.

Building for reproducibility

One of my priorities was ensuring that another developer could reproduce the same research workflow.

The synthetic market-data generator therefore uses:

  • A local seeded random-number generator
  • A fixed clock
  • A configurable number of ticks
  • Deterministic symbol-level output

For example:

python scripts/generate_sample_data.py \
  --ticks-per-symbol 1000 \
  --seed 42
Enter fullscreen mode Exit fullscreen mode

Using the same configuration and seed produces the same snapshots and analytics records.

The project also includes automated tests, linting, coverage checks, frontend build validation, and reproducibility smoke tests through GitHub Actions.

Backtesting and execution simulation

The repository includes an OFI-based directional backtester with configurable:

  • Entry and exit thresholds
  • Lookback period
  • Transaction costs
  • Position size
  • Initial capital

It reports trade-level profit and loss, maximum drawdown, profit factor, win rate, and an unannualised completed-trade return statistic.

The execution simulator compares TWAP with an ex-post replay-VWAP schedule. It walks the available order-book levels and reports fill quantities, slippage, and implementation shortfall.
These components test the research pipeline—not the existence of a profitable trading strategy.

Engineering challenges

Developing the project involved fixing several issues that commonly arise in real-time analytical systems:

  • Inconsistent timestamp fields between the backend and frontend
  • Incorrect data types in tick classification
  • Expensive repeated scans of rolling windows
  • Missing-data failures in statistical estimators
  • Transaction-cost reconciliation in backtesting
  • WebSocket protocol differences between HTTP and HTTPS
  • Reproducibility problems caused by global randomness and wall-clock timestamps

The rolling OFI and anomaly modules were redesigned using queues and running aggregates so that processing work remains bounded as the session history grows.

Running the project locally

Clone the repository and start the backend:

git clone https://github.com/aariiparekh3012-collab/quantproject2.git
cd quantproject2

python -m venv .venv
source .venv/bin/activate

pip install -r requirements.txt
cp .env.example .env

uvicorn backend.api.main:app --reload
Enter fullscreen mode Exit fullscreen mode

Then start the React dashboard:

cd frontend
npm install
npm run dev
Enter fullscreen mode Exit fullscreen mode

The backend will be available at http://localhost:8000, while the dashboard will run at http://localhost:5173.

Alternatively, the self-contained offline demo can be opened directly from demo/index.html.

Current limitations

The current version uses deterministic synthetic data for its validated workflow.
An Angel One SmartAPI adapter exists in the repository, but it remains experimental and is not a completed production data connector.

Similarly, results produced from synthetic or replayed data validate the software pipeline—not predictive power, profitability, or exchange-feed correctness. Those claims would require licensed historical data and proper out-of-sample testing.

What I learned

This project helped me connect quantitative-finance theory with practical software engineering.
The most important lesson was that implementing a formula is only one part of building a research platform. Reproducibility, testing, data consistency, computational efficiency, and honest documentation are equally important.

Next steps

Future improvements may include:

  • Completing and validating live-data integration
  • Adding reconnect and subscription-recovery logic
  • Testing with licensed historical order-book data
  • Expanding the backtesting framework
  • Introducing additional execution models
  • Adding more dashboard controls and research visualisations

If you are interested in quantitative finance, market microstructure, Python, or streaming analytics, feel free to explore the repository and share your feedback.

This project is intended for research and educational purposes and does not constitute investment advice.

Top comments (0)