DEV Community

James Tao
James Tao

Posted on

Build a Stable Pre/Post Market US Stock Ranking Dashboard

**Intro
When building market data pipelines for US stock quantitative research, most developers start by only checking if an API can return real-time tick data. After weeks of unattended runtime testing, we quickly learn the real pain point: inconsistent data handling during pre-market and after-hours extended trading sessions.

If your gain/loss ranking dashboard shifts stock positions randomly every few seconds, all your backtesting datasets and real-time strategy signals become unreliable. This ranking system isn’t just a simple fetch-and-render frontend project — the core engineering work lies in normalizing timestamp logic, baseline pricing rules, and sorting logic for low-liquidity extended hours market data.

Better Baseline Logic for Extended Hours Pct Change
The most common rookie mistake is using the previous day’s closing price as the single baseline for all trading windows. This artificially inflates volatility readings in pre and post market, skewing your entire leaderboard.

I use segmented baseline pricing to eliminate this bias:
Pre-market: Set baseline as the first stable matched trade price before market open
After-hours: Set baseline as the first valid trade price once regular hours close
Never reuse the single previous close price across all sessions
For ranking pipelines, consistent calculation rules beat ultra-fast recalculation every time. If your baseline logic shifts between ticks, stock positions will flicker endlessly on the dashboard.

3-Tier Decoupled Data Architecture (Avoid Frontend Heavy Computation)
I split the data flow into three isolated layers to insulate the UI from network lag, bad ticks, and calculation spikes:

  1. Raw Tick Layer: Persist all original market ticks for backtesting replay and error tracing
  2. Central Calculation Layer: Handle unified pct change math and global sorting logic
  3. Presentation Layer: Render pre-computed ranking results with zero live arithmetic Add a short sliding tick aggregation window as an extra stabilization step. Group all ticks within a 2–5 second window before sending batches to the calculation layer — this smooths out constant micro price jumps that cause leaderboard chaos.

Minimal Python Implementation: Baseline Tracking & Pct Calculation

import websocket
import json
from collections import defaultdict

# Store session baseline price per ticker
base_price_map = defaultdict(float)

def on_message(ws, raw_message):
    data = json.loads(raw_message)
    symbol = data.get("symbol")
    price = float(data.get("price", 0))
    timestamp = data.get("timestamp")

    # Lock initial baseline price on first tick of the session
    if base_price_map[symbol] == 0:
        base_price_map[symbol] = price
    base_price = base_price_map[symbol]
    change_pct = (price - base_price) / base_price * 100 if base_price else 0
    print(f"Ticker: {symbol} | Price: {price} | 24h Change: {round(change_pct, 2)}%")

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

Fix Stale Data Bias (A Frequently Overlooked Edge Case)
Ticker update frequency varies wildly during extended hours: high-volume large-cap stocks stream ticks nonstop, while low-liquidity small caps may sit dormant for minutes at a time. Without filtering, outdated old prices will falsely push inactive stocks to the top of your leaderboard.

A simple lightweight filter fixes this: if a ticker has no new tick data within your defined freshness threshold, lower its sort priority or temporarily remove it from the ranking list entirely. This small tweak drastically improves the realism and trustworthiness of your real-time dashboard.

Final Engineering Takeaways
When building US stock market data infrastructure, raw network latency of your API feed is not the biggest factor impacting dashboard quality. The single most impactful variable is consistent, session-separated data processing for pre-market and after-hours ticks.

Nearly all flickering, unreliable leaderboards stem from mixing tick data from disjoint trading windows under one universal calculation formula. Three simple adjustments resolve almost all ranking instability: separate session classification logic, segmented baseline pricing rules, and throttled refresh cadences.


Once these standards are built into your pipeline, your ranking dashboard will reflect genuine market movement instead of a chaotic, constantly shifting list of numbers.

Top comments (0)