DEV Community

Master Wattson
Master Wattson

Posted on

I Built a DVR for Financial Markets Using Pyth Pro

Flash crashes happen in milliseconds. By the time you notice, it's over.
I wanted to build something that lets you rewind scrub back to the exact moment a market moved and watch it tick by tick, like rewinding a DVR. That's Market DVR.
The Problem
Most market data tools show you candles. The smallest is usually 1 minute. But real market microstructure bid/ask spread, confidence intervals, sub-second price movement lives and dies in milliseconds. When a spread spike or flash crash happens, candles hide it completely.
Pyth Pro changes that. With sub-50ms tick frequency, best_bid, best_ask, and a confidence interval on every tick, you suddenly have everything you need to record markets at a level of detail that was previously inaccessible.

How It Works
The backend runs a persistent WebSocket connection to the Pyth Pro Hermes endpoint and subscribes to two channels:
javascript// Crypto — maximum frequency
pythClient.subscribePriceFeedUpdates(CRYPTO_IDS, handler, 'real_time');

// Commodities + Forex — 200ms fixed rate
pythClient.subscribePriceFeedUpdates(OTHER_IDS, handler, 'fixed_rate@200ms', {
properties: ['price', 'confidence']
});
Every tick gets written to PostgreSQL with microsecond precision:
javascriptawait pool.query(
INSERT INTO ticks (asset, timestamp_us, price, best_bid, best_ask, confidence, exponent)
VALUES ($1, $2, $3, $4, $5, $6, $7)
,
[asset, timestamp_us, price, best_bid, best_ask, confidence, exponent]
);
Raw Pyth values are integers — you convert them using the exponent:
javascriptconst priceUSD = Number(raw.price) * Math.pow(10, exponent);
const bidUSD = Number(raw.best_bid) * Math.pow(10, exponent);
const askUSD = Number(raw.best_ask) * Math.pow(10, exponent);
const spreadUSD = askUSD - bidUSD;

// Confidence: how tight is the oracle's price band?
const confAbs = Number(raw.confidence) * Math.pow(10, exponent);
const confNorm = Math.max(0, Math.min(0.999, 1 - confAbs / priceUSD));
// Normal markets: confNorm ≈ 0.999 (99.9%) — this is correct and expected
Event Detection
Every 500ms, a loop scans recent ticks for notable events:
javascript// Crash or pump
if (Math.abs(pctChange) > 0.3) {
recordEvent(asset, pctChange > 0 ? 'pump' : 'crash', { magnitude: pctChange });
}

// Spread spike — compare to rolling 60-tick baseline
const baseline = rollingAvgSpread(asset);
if (spread > baseline * 2) {
recordEvent(asset, 'spread_spike', { spread, multiple: spread / baseline });
}

// Confidence drop
if (confNorm < 0.995) {
recordEvent(asset, 'confidence_drop', { confidence: confNorm });
}
After running for a few days we had 83,000+ events across 16 assets.

The Replay UI
The frontend fetches historical ticks from the DB and renders them into a scrubbable chart. Users pick a resolution (50ms, 200ms, 1s, 5s, 1m, 5m, 15m, 1h) and scrub frame by frame using keyboard shortcuts or the playback controls.
The Frame Inspector panel on the right shows the exact price, bid, ask, spread, confidence, and frame number at the current playback position — the equivalent of pausing a video on a single frame.

One Tricky Part: Commodities
Pyth Pro's fixed_rate@200ms channel for commodities and forex doesn't send best_bid/best_ask only price and confidence. So spread is effectively zero for XAU/USD, XAG/USD, and all forex pairs. The fix: display "—" instead of "$0.000000" in the UI, and fall back to price for both bid and ask in the DB.
javascriptconst bestBidPrice = update.bestBidPrice?.price ?? update.price?.price;
const bestAskPrice = update.bestAskPrice?.price ?? update.price?.price;

What I Learned
Pyth Pro's confidence interval is genuinely underused. It's not just a quality signal in calm markets it stays at 99.9%, and when it drops it's often the first signal that something is happening, before the price even moves. Building the stress gauge around confidence + spread + volatility gave a much more reliable market health indicator than any single metric alone.

Try It

Live demo: https://dvr.masterwattson.site
Source code: https://github.com/bshuaibu1/market-dvr

Top comments (0)