How to Recover a Forex WebSocket Feed Without Showing Stale Quotes

TickDB Research ·

Illustrative EUR/USD stream interrupted between two connections

A WebSocket can reconnect while a dashboard still shows the last EUR/USD quote from the old connection. Treat those as two separate states. The socket is ready to send subscriptions when it opens. A currency pair is ready to display as live only after its first usable quote from that connection.

Recover each currency pair separately

On disconnect, mark the affected quotes stale. If keeping the last value on screen helps the user, label it as stale and show its timestamp. After reconnect, register listeners and resubscribe to the wanted symbols. Do not mark every pair live on a socket-open event or a subscription acknowledgment. A message for EUR/USD tells you nothing about whether USD/JPY has resumed.

For example, the TickDB public client documentation shows a ticker subscription that can include EURUSD. Send the documented subscription on each new connection. If the provider reports an authorization or subscription error, surface it rather than retrying indefinitely. For transient failures, use capped exponential backoff with jitter and allow only one active reconnect loop.

Reject messages from the old connection

Give each socket a locally assigned generation number. Capture it in the callbacks and check it before processing either quotes or disconnects. This also covers work that was queued by the old handler and finishes after the new socket opens.

An interval with no live quote separates connection n from n+1; the diagram is illustrative.

An interval with no live quote separates connection n from n+1; the diagram is illustrative.

The Python example below takes normalized, synthetic quotes, not raw provider frames. In one generation it rejects an earlier timestamp for the same symbol and invalid bid/ask values. It permits equal timestamps: millisecond timestamps alone do not establish a total order. Use a documented sequence number if your feed supplies one and its scope is known.

from dataclasses import dataclass

@dataclass(frozen=True)
class Quote:
    symbol: str
    time_ms: int
    bid: float
    ask: float

class QuoteGate:
    def __init__(self):
        self.epoch = 0
        self.live = False
        self.latest = {}

    def opened(self):
        self.epoch += 1
        self.live = True
        self.latest.clear()
        return self.epoch

    def disconnected(self, epoch):
        if epoch == self.epoch:
            self.live = False
            self.latest.clear()

    def accept(self, epoch, quote):
        if not self.live or epoch != self.epoch:
            return False
        if not (0 < quote.bid <= quote.ask < float("inf")):
            return False
        previous = self.latest.get(quote.symbol)
        if previous and quote.time_ms < previous.time_ms:
            return False
        self.latest[quote.symbol] = quote
        return True

if __name__ == "__main__":
    g = QuoteGate()
    a = g.opened()
    assert g.accept(a, Quote("EURUSD", 2000, 1.10, 1.11))
    g.disconnected(a)
    assert not g.latest
    assert not g.accept(a, Quote("EURUSD", 2200, 1.10, 1.11))
    b = g.opened()
    assert not g.accept(a, Quote("EURUSD", 2300, 1.10, 1.11))
    assert g.accept(b, Quote("EURUSD", 2100, 1.10, 1.11))
    assert not g.accept(b, Quote("EURUSD", 2090, 1.09, 1.11))
    assert not g.accept(b, Quote("USDJPY", 2100, 150, 149))
    g.disconnected(a)
    assert g.live and "USDJPY" not in g.latest
    print("8 synthetic assertions passed; no live WebSocket request")

The eight offline assertions include a late quote with a newer timestamp from the old socket and a late disconnect notification. Neither should change the new socket's state. The script does not connect to a live feed.

Do not turn a reconnect into a completeness claim

A fresh quote restores a display, not every update that arrived while the link was down. If you use a REST snapshot to fill the display, label its source and keep it from replacing a newer streaming update. For a trade recorder or replay pipeline, preserve the outage interval and only backfill from a documented, suitable source. A ticker snapshot or a candle is not a replay of all missed events.

A quiet symbol is not necessarily disconnected. Combine the provider's documented heartbeat behavior with a freshness policy for the market and the specific pair. You want to be able to say which connection supplied the quote, when you received it, and whether the UI is labeling it correctly.

Category coverage

Forex & metals
1,228
12/1/2026

Explore related data tools

Related articles

How to Check Forex API Quotes with a Cross Rate

EUR/JPY can differ from the rate implied by EUR/USD and USD/JPY without a broken feed. Check timestamp skew first, then compare bid/ask intervals using a reproducible Python example.

TickDB Research ·

What Does XAUUSD Mean? Read a Gold Quote Without Assuming a Contract

XAUUSD commonly denotes gold quoted in US dollars per troy ounce. It tells you how to read a price, not how much gold an order represents or which contract you are buying. Separating the quote unit from quantity and product terms makes a simple ounce-to-gram conversion useful without turning it into an invented lot calculator.

TickDB Research ·

Can You Use a Real-Time Forex API for Free? A Trial Has Limits

A free forex API can be useful for evaluating live quotes, but “free” does not necessarily mean ongoing access for a production application. Check the trial expiry, permitted currency pairs, request and subscription budgets, historical access, and usage rights separately. TickDB provides a concrete example: its documented Free plan is a seven-day trial, not an unlimited free production tier.

TickDB Research ·

XAUUSD Explained for Price Tracking and Alerts

XAUUSD quotes gold in US dollars per troy ounce. It is useful for following gold prices, building dashboards, and setting price alerts, but the symbol alone does not identify an executable price or tell you whether to buy or sell. For example, a synthetic bid of 2,400.00 and ask of 2,400.80 produce a midpoint of 2,400.40, but neither that calculation nor a threshold alert guarantees an execution price.

TickDB Research ·