Skip to content

Business Logic Market Data and Pricing

tien.nguyen edited this page Jul 30, 2026 · 1 revision

Business Logic: Market Data, Pricing & Order Book Aggregation

This document details market data aggregation, Level-1 / Level-2 order book depth structure, Price-Time priority matching, and micro-price / VWAP calculation formulas in Emporia Trading Platform's market-data-service.


๐Ÿ“Š Market Data Feed Hierarchy

market-data-service aggregates prices from multiple venue sources (FIX protocol simulators, Alpaca IEX WebSocket, external price feeds) to produce composite order books.

flowchart TD
    FixFeed["FIX Protocol Simulator"] -->|FIX Incremental Refresh| Aggregator["QuoteAggregator Engine"]
    AlpacaFeed["Alpaca IEX WebSocket"] -->|JSON Depth Snapshots| Aggregator
    SimFeed["Synthetic Quote Generator"] -->|Tick Stream| Aggregator

    Aggregator -->|Consolidate Depth| L1Book["Level-1 Top of Book (BBO)"]
    Aggregator -->|Consolidate Depth| L2Book["Level-2 Depth of Book (5-10 Levels)"]

    L1Book -->|HTTP SSE Stream| WebUI["React 19 Trading Dashboard"]
    L2Book -->|gRPC Proto Stream| Services["Execution & Portfolio Services"]
Loading

๐Ÿ“– Order Book Structure & Price-Time Priority

An order book organizes active resting limit orders into two sorted sides:

  • Bids (Buy Orders): Sorted in descending order by price ($\max P_{\text{bid}}$ first). Ties sorted by earliest timestamp (FIFO).
  • Asks (Sell Orders): Sorted in ascending order by price ($\min P_{\text{ask}}$ first). Ties sorted by earliest timestamp (FIFO).

Order Book Depth Table Example

Bid Quantity Bid Price Ask Price Ask Quantity
โ€” โ€” $150.05 (Ask 3) 500
โ€” โ€” $150.02 (Ask 2) 300
โ€” โ€” $150.00 (Best Ask) 100
$149.98 (Best Bid) 250 โ€” โ€”
$149.95 (Bid 2) 400 โ€” โ€”
$149.90 (Bid 3) 1,000 โ€” โ€”

๐Ÿงฎ Pricing Formulas & Indicator Calculations

1. Best Bid and Offer (BBO) & Spread

  • Best Bid ($P_{\text{bid}}$): Highest resting buy price.
  • Best Ask ($P_{\text{ask}}$): Lowest resting sell price.
  • Bid-Ask Spread: $$\text{Spread} = P_{\text{ask}} - P_{\text{bid}}$$

2. Mid-Price Formula

The simple mid-point price between top-of-book bid and ask:

$$P_{\text{mid}} = \frac{P_{\text{bid}} + P_{\text{ask}}}{2}$$

3. Volume-Weighted Micro-Price Formula

The micro-price adjusts the mid-price based on order book depth imbalance, providing a superior predictive indicator for short-term price direction:

$$P_{\text{micro}} = \frac{(Q_{\text{bid}} \times P_{\text{ask}}) + (Q_{\text{ask}} \times P_{\text{bid}})}{Q_{\text{bid}} + Q_{\text{ask}}}$$

Properties:

  • If bid volume $Q_{\text{bid}} \gg Q_{\text{ask}}$ (heavy buying pressure), $P_{\text{micro}}$ shifts upward toward $P_{\text{ask}}$.
  • If ask volume $Q_{\text{ask}} \gg Q_{\text{bid}}$ (heavy selling pressure), $P_{\text{micro}}$ shifts downward toward $P_{\text{bid}}$.

๐Ÿ“ก Real-time Streaming Distribution

  • Server-Sent Events (SSE): Pushes JSON tick objects ({ symbol, bid, ask, last, volume, timestamp }) to web browsers over persistent HTTP connections (/api/market-data/stream).
  • gRPC Streaming: Low-latency binary Protobuf streaming (MarketDataService/SubscribeQuotes) for inter-service communication with execution-service and portfolio-service.

Clone this wiki locally