DEV Community

Emily
Emily

Posted on

How to Handle Weekend Data Gaps in XAUUSD Backtesting Without Losing Your Mind

The bug that wasn’t a bug

Confession time: I spent a whole week debugging a gold strategy that kept showing a mysterious profit spike every Monday at 00:00. The logic was fine. The data seemed fine. Then I realized my realtime feed simply didn’t send any XAUUSD ticks over the weekend, and my backtester was treating 48 hours of no-data as “price stayed flat.” That flat stretch was making the Monday gap look like a tradeable trend, and my metrics were lying to me.

If you’re building a gold trading system, this is almost certainly affecting your backtests too. Let’s walk through why, and how to code around it.

The mechanics of backtest distortion

Most backtesting loops work like this: for each time step, if there’s no new price, keep the old one. Over a weekend, that gives you a long flat line connecting Friday’s closing price to Monday’s opening price. When the new tick finally arrives, the instantaneous jump is fed directly to your indicators.

This artificially inflates trend-following signals and deflates volatility estimates. The model sees a smooth entry into a gap, but the real market offers only a discontinuous fill. The result? Beautiful backtests that dissolve in live trading.

Which fix is worth the effort

Here’s a quick comparison based on what I’ve tried:

Method Backtest Fidelity Implementation Effort Recommendation
Ignore weekends completely Poor Trivial Not recommended
Fill with Friday’s closing price Moderate Easy Barely acceptable
Replace with Monday’s opening price Moderate Easy Average
Mark gap intervals as events Good Medium Recommended
Incorporate external reference data High High Use as needed

I strongly suggest going with explicit gap marking. It involves a bit more code, but it preserves the discontinuity and lets you analyze how the strategy behaves around gaps — essential intel for gold.

Sample code: separating weekend ticks from strategy signals

I’ll show you a minimal example using a WebSocket connection to the AllTick API. The key is a date check that routes weekend data away from the strategy.

import websocket
import json
from datetime import datetime

def on_message(ws, message):
    data = json.loads(message)
    timestamp = datetime.fromtimestamp(data['time'])
    # Weekend tick: store for gap analysis only, don't feed to strategy
    if timestamp.weekday() >= 5:
        print(f"Weekend data {data['price']} written to gap buffer")
        # Save to gap storage
        store_gap_data(data)
    else:
        # Regular weekday tick
        process_tick(data['symbol'], data['price'])

ws = websocket.WebSocketApp(
    "wss://apis.alltick.co/websocket-api/stock-websocket-interface-api/transaction-quote-subscription",
    on_message=on_message
)
ws.run_forever()
Enter fullscreen mode Exit fullscreen mode

Write a small gap injection routine for your backtester that applies the Friday-close to Monday-open jump before the first tick of the week. Now your simulation matches reality.

Tips for honest gold backtests

  • Time axis should be partitioned by trading sessions, not calendar days.
  • Analyze gap PnL separately — if your edge comes purely from weekend jumps, you don’t have an edge.
  • Keep the exact same data processing path in production and research.

The fact that a gold API doesn’t push data on weekends is a guarantee of its accuracy. By explicitly modeling those gaps, you turn a hidden pitfall into a well-understood component of your system.

Top comments (0)