keepcalculator

06 Numbers and Powers

Gold Websocket Price Gaps: How to Stay Consistent

Card No. 06·77520 · Updated · Standard formula, shown below

Price gap 1.60 USD/oz
Corrected price
2,352.10 USD/oz
Result
Time since last tick
30 s
You entered
Last known price
2350.5 USD/oz
You entered
You enter
Time since last tick, Last known price, Reference price (REST)
You get
Price gap, Corrected price
Rounding
2 dp

When a gold price websocket misses ticks, your displayed price drifts from the exchange price, and the fix is to reconcile against a reliable reference source using a gap-detection and correction routine. You keep prices consistent by detecting the missed tick (via sequence numbers or timestamps), fetching the latest authoritative price from a REST fallback, and then either replaying missed ticks or applying a correction that your UI and downstream systems can understand.

Formula

The correction is simple: Corrected Price = Last Known Price + (Reference Price – Last Known Price). In symbols:

C = Plast + (Pref – Plast)

  • C – the price you should display after a gap (USD/oz)
  • Plast – the last tick you received before the gap (USD/oz)
  • Pref – the authoritative price from a REST API or another source at the moment you detect the gap (USD/oz)

The gap itself is simply Pref – Plast. If you have a sequence number on each tick, you can also compute how many ticks you missed: missed = current_seq – last_seq – 1.

Worked Example

Using the default values from the calculator:

  1. Your websocket last delivered a gold price of 2350.5 USD/oz at 10:00:00.
  2. No ticks arrive for 30 seconds. You query the REST reference endpoint and get 2352.1 USD/oz.
  3. Gap = 2352.1 – 2350.5 = 1.6 USD/oz.
  4. Corrected price = 2350.5 + 1.6 = 2352.1 USD/oz.

So you display 2352.1, not the stale 2350.5. The calculator shows the same result.

Why Websockets Miss Ticks in the First Place

Websocket connections are not guaranteed to deliver every message. Common causes include:

  • Network congestion – a temporary spike in latency or packet loss on your ISP or the exchange’s infrastructure.
  • Client-side processing delay – your app’s main thread is busy, and the websocket library drops messages because the buffer overflows.
  • Server-side throttling – the exchange may intentionally skip ticks if you are on a lower-tier data feed (e.g., delayed vs. real-time).
  • Reconnection gaps – when the websocket disconnects and reconnects, you miss everything in between unless you request a snapshot.

Gold prices are particularly sensitive because they trade nearly 24/5, and spreads can be wide during low-liquidity hours. A missed tick during a fast move (e.g., a non-farm payroll release) can leave your displayed price several dollars off.

The Core Strategy: Detect, Fetch, Correct

Here is the standard three-step approach used by trading dashboards and data aggregators:

  1. Detect the gap. Most websocket feeds include a sequence number or a timestamp on each message. If you see a jump in the sequence (e.g., from 100 to 105), you know you missed 4 ticks. If your feed has no sequence, use a timeout: if no message arrives within a threshold (e.g., 5 seconds during active trading), assume a gap.
  2. Fetch the authoritative price. Call a REST endpoint (the exchange’s REST API or a trusted market data provider) to get the current bid/ask or last trade. This is your ground truth.
  3. Correct your state. Set your internal price to the fetched value. If you need historical consistency (e.g., for charting), you can also request a short history from the REST API to fill the missing period, or simply mark the gap with a flag so downstream consumers know the data is interpolated.

For most applications, simply overwriting the last price with the REST value is enough. For charting, you may want to insert a synthetic tick with a flag like "gap": true so that indicators don’t draw misleading lines.

Handling Reconnection and Snapshot Resync

When the websocket reconnects, you should not assume the next tick is the current price. The standard pattern is:

  1. On reconnect, immediately call the REST endpoint to get the current price.
  2. Set your last price to that value.
  3. Then resume processing websocket ticks. If the feed sends a snapshot (a full order book or a recent trade history), use that instead of the REST call.

Many exchanges provide a snapshot message on subscribe. If you receive one, apply it before any subsequent incremental ticks. If not, the REST fallback is your best bet.

Choosing the Right Reference Source

Not all reference prices are equal. Consider:

  • Same exchange – the most consistent, because the websocket and REST come from the same matching engine. Use this when possible.
  • Aggregator (e.g., a gold price index) – useful if you are showing a composite price, but it may lag or use different weighting.
  • Your own cache – if you run multiple websocket connections, you can cross-check between them. But this only helps if at least one is healthy.

For a single exchange feed, the exchange’s own REST endpoint is the safest. For a multi-feed aggregator, you need to define a priority order and a fallback chain.

Common Mistakes and Misconceptions

One frequent mistake is to simply ignore missed ticks and wait for the next one. This works only if the market is flat; during a fast move, your price will be stale for seconds, which can trigger bad trades or user complaints.

Another mistake is to apply the correction only to the display but not to your internal state. If your trading logic uses the internal price, it will still act on the stale value. Always update the internal state first.

A third misconception is that you need to replay every missed tick. In practice, for most dashboard and alerting purposes, the latest price is all you need. Replay is only necessary for historical analysis or regulatory logging, and even then, a REST history call is simpler than trying to reconstruct tick-by-tick data.

Frequently Asked Questions

What if the REST API is also down?

If both websocket and REST fail, you have no authoritative source. The safest approach is to display the last known price with a “stale” indicator, and stop executing trades until connectivity is restored. Never invent a price.

Should I use a heartbeat message to detect gaps?

Yes, if the exchange supports it. Some websockets send a ping or heartbeat even when there is no trade. If you stop receiving heartbeats, you know the connection is dead. But heartbeats don’t tell you about missed ticks during an active connection – only sequence numbers do.

How often should I check for gaps?

Depends on your use case. For a retail chart, checking every 5 seconds is fine. For an algorithmic trader, you might check on every tick or use a sub-second timer. The trade-off is load on the REST endpoint – don’t hammer it more than necessary.

Can I use the previous tick’s timestamp to detect gaps?

Yes, if the feed includes timestamps. If the time since the last tick exceeds a threshold (e.g., 10 seconds during regular trading hours), assume a gap. This is less reliable than sequence numbers because a slow market can legitimately have long pauses.

What about delayed vs. real-time feeds?

If you are using a delayed feed, the reference price must also be delayed, or you will see a constant offset. Always match the delay of your websocket and REST sources.

Do I need to correct historical data too?

Only if you are storing ticks for later analysis. In that case, you can backfill from a REST history endpoint after the gap. For live display, correcting the current price is sufficient.

Related

Formula used
in t | Time since last tick | s | 30 | 0 | 3600
in p | Last known price | USD/oz | 2350.5 | 0 | 100000
in r | Reference price (REST) | USD/oz | 2352.1 | 0 | 100000
out diff | Price gap | USD/oz | r - p | 2
out corr | Corrected price | USD/oz | p + (r - p) | 2

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →