DEV Community

kalos
kalos

Posted on • Edited on

How to Auto-Detect Forex Market Holidays with API Data Streams


If you build forex data pipelines, trading bots, or market integrations, you’ve likely run into a silent failure mode: your code works perfectly on normal trading days but breaks unpredictably on holidays.

Thanksgiving, Christmas, bank holidays, and regional market closures create uneven liquidity, patchy data, and unstable tick delivery — without any obvious error from your API.

In this post, I share a production‑proven, data‑driven method to automatically detect market closures using only real‑time WebSocket ticks. No hardcoded calendars. No manual maintenance. Zero external dependencies.

This approach works with any reliable forex API and integrates cleanly into quant systems, data collectors, and risk engines.

Why Holidays Break Your Forex Integration
Forex is a global, decentralized market. When one region closes, another may still be open — leading to partial liquidity drops rather than full shutdowns.

Key changes you’ll see in holiday conditions:

  • Tick frequency: Multiple ticks/sec → minutes between updates
  • Liquidity: Full depth → sharply reduced
  • Spread: Tight and stable → significantly widened
  • Data consistency: Continuous → gappy or missing

Without detection logic, your system will:
Spam reconnection attempts
Generate invalid signals
Waste compute on empty data
Produce unclean backtest results

A Better Approach: Detect Holidays From Behavior
Instead of maintaining a global holiday calendar (fragile, high‑effort, error‑prone), we infer market state from the data itself.

We monitor:

  • Tick interval
  • Message frequency
  • Volume thresholds
  • Cross‑pair consistency

Below is a complete, copy‑pasteable WebSocket‑based detector.

import websocket
import json
import time

class HolidayDetector:
    def __init__(self):
        self.last_tick_time = None
        self.tick_count = 0

    def on_message(self, ws, message):
        data = json.loads(message)
        current_time = time.time()

        if self.last_tick_time:
            interval = current_time - self.last_tick_time
            # Flag abnormally long gaps between ticks
            if interval > 10:
                print(f"Abnormal tick interval: {interval:.1f}s → possible holiday")

        self.last_tick_time = current_time
        self.tick_count += 1
        print(f"{data.get('symbol')} price: {data.get('price')}")

# Initialize detector
detector = HolidayDetector()

# Real-time forex WebSocket endpoint (example)
url = "wss://apis.alltick.co/websocket-api/stock-websocket-interface-api/transaction-quote-subscription"

# Start listening
ws = websocket.WebSocketApp(url, on_message=detector.on_message)
ws.run_forever()
Enter fullscreen mode Exit fullscreen mode

This lightweight observer flags unusual gaps in real time.

3 Improvements for Production Accuracy
For robust deployment, add these three validation layers:

  1. Volume Thresholding
    Set a minimum volume baseline. Sub‑threshold activity = low liquidity or partial closure.

  2. Cross‑Currency Verification
    One pair quiet = local illiquidity
    EUR/USD, GBP/USD, USD/JPY all quiet = market‑wide closure

  3. Session Awareness
    Tokyo open: thin data is normal
    London–New York overlap: thin data = strong holiday signal
    Well‑behaved APIs like AllTick do not drop connections during holidays — they simply reduce tick rate, making pattern detection highly reliable.

My Production State Machine
I use a three‑state model to keep bots efficient and stable:

Normal – full processing, strategy execution

Monitoring – tick interval exceeded; observe for 30 seconds

Holiday – pause strategies, preserve heartbeat only; auto‑resume when normal flow returns

This reduces resource waste and eliminates holiday‑induced signal noise.

Final Takeaway
You don’t need holiday calendars to build resilient forex systems.

The data already contains all the signals you need.
By building a self‑aware data pipeline that observes tick frequency, liquidity, and cross‑asset behavior, you create a system that adapts automatically to global market conditions.

This small, clean pattern will make your data feeds, trading bots, and quant strategies significantly more robust.

Top comments (0)