← Back to Blog

Building a Real-Time Market Data Generator for Testing Algo Trading Systems

Published on October 17, 2025 · by Brijesh Yadav

When building algorithmic trading systems, the hardest part to test reliably is the real-time market feed. Live markets are unpredictable, noisy, and expensive to test against. A lightweight, deterministic market data generator that simulates realistic tick-level patterns lets you validate strategies, risk controls, and order routing without paying exchange fees or risking real money.

Why Build a Market Data Generator?

Core Design Requirements

  1. Generate tick-level events: price, size, symbol, timestamp.
  2. Support multiple instruments: equities, options, indices.
  3. Inject scenarios: volatility spikes, news shocks, gaps.
  4. Produce realistic microstructure: bid/ask, trade sizes, spreads.
  5. Adjustable time scale: real-time or accelerated playback.

System Architecture

The generator consists of five lightweight components:

Tick Generation Model

A simple yet realistic price evolution model is a stochastic random walk with drift and volatility. Each tick is computed as:

p_next = p_prev * (1 + drift*dt + sigma * sqrt(dt) * N(0,1) + jump)

You can further enhance realism by rounding prices to tick size, generating heavy-tailed trade volumes, and simulating spreads between bid and ask quotes.

Implementation Example (Python)


import asyncio, random, math, json, datetime

async def tick_generator(symbol, base_price=100.0, drift=0.0001, sigma=0.001):
    p = base_price
    while True:
        dt = 1/100  # seconds between ticks
        jump = 0
        if random.random() < 0.0005:  # rare news event
            jump = random.uniform(-0.03, 0.05)
        p *= (1 + drift*dt + sigma*math.sqrt(dt)*random.gauss(0,1) + jump)
        yield {
            "symbol": symbol,
            "price": round(p, 2),
            "volume": random.randint(10, 1000),
            "timestamp": datetime.datetime.utcnow().isoformat() + "Z"
        }
        await asyncio.sleep(dt)

This coroutine continuously yields ticks that you can broadcast via FastAPI using WebSockets or Server-Sent Events.

Publishing with FastAPI + SSE


from fastapi import FastAPI
from fastapi.responses import StreamingResponse
import asyncio, json

app = FastAPI()

async def event_stream():
    gen = tick_generator("RELIANCE")
    async for tick in gen:
        yield f"data: {json.dumps(tick)}\n\n"

@app.get("/stream")
async def stream():
    return StreamingResponse(event_stream(), media_type="text/event-stream")

Your frontend can connect using JavaScript’s EventSource:


const es = new EventSource("/stream");
es.onmessage = (e) => {
  const tick = JSON.parse(e.data);
  console.log("Tick:", tick.symbol, tick.price);
};

Scenario Engine

To make simulations meaningful, add a “scenario engine” that manipulates volatility and drift. For instance:

Integrating into CI/CD

You can embed the generator into automated test pipelines:

Possible Extensions

Example Architecture Overview

Here’s a simplified flow of the generator setup:


[Generator] → [FastAPI / Redis Stream] → [Trading Engine] → [Frontend Dashboard]

Each stage consumes real-time data just like a production setup, but under full developer control.

Closing Thoughts

A reliable market data generator is an indispensable tool for any algorithmic trading system. It bridges the gap between theory and production by providing controlled realism. Start with a single-symbol generator and incrementally add features — volatility modeling, bursts, option greeks, or multi-exchange feeds.

For Growth Quantix, my goal is to use this generator to validate latency, signal accuracy, and trade execution logic before connecting to the real Upstox live feed. The same system can even run on weekends for regression testing or strategy dry-runs.

— Brijesh Yadav