DEV Community

kalos
kalos

Posted on • Edited on

How I Fixed Stale Exchange Rate Data on Weekends With a Simple Python Filter


If you’ve ever built a forex monitor, trading alert system, or real‑time currency dashboard, you’ve almost certainly run into this annoying problem:

Your real‑time rate API keeps sending data on weekends… but it’s just the same stale closing price from Friday.

The WebSocket stays alive, timestamps keep updating, and your system thinks it’s getting fresh data.

But the market is closed. This “fake data” triggers false alerts, pollutes your logic, wastes resources, and causes unnecessary headaches.

In this post, I’ll show you a lightweight, production‑ready filtering layer you can drop directly into your Python project to fix this for good.

The Problem We’re Solving
Many free and even commercial exchange rate APIs don’t check trading days.
They just keep sending the last available price during weekends and holidays.

This causes real pain:
-Weekend alerts blowing up your notifications
-Trading strategies misinterpreting stale prices
-Logs and databases flooded with useless duplicates
-Hours wasted debugging “ghost price movements”
The API isn’t broken — it’s just sending the latest value, not the latest valid trading value.
We have to validate data ourselves.

My 3‑Layer Filtering Logic
I built a simple but strong filter that runs before your business logic to reject bad data early.
-Trading day check — skip data when markets are closed
-Price change check — reject unchanged stale data
-Timestamp check — block frozen or backward timestamps

Core Validation Function

def is_valid_trading_data(price, timestamp, last_price, last_timestamp):
    # Price unchanged = stale data
    if price == last_price:
        return False

    # Timestamp not moving = invalid update
    if timestamp <= last_timestamp:
        return False

    # Not a trading day = skip entirely
    if not is_trading_day():
        return False

    return True
Enter fullscreen mode Exit fullscreen mode

Full WebSocket Implementation (using AllTick API as an example)
This example works with real‑time forex WebSocket streams and is ready to deploy.

import websocket
import json
from datetime import datetime

last_price = None
last_ts = None

def on_message(ws, message):
    global last_price, last_ts
    data = json.loads(message)

    current_price = data.get('price')
    current_ts = data.get('timestamp')

    # Skip on non-trading days
    if not is_trading_day():
        print("Non-trading day — skipped")
        return

    # Skip stale, unchanged prices
    if current_price == last_price:
        print("Price unchanged — filtering stale data")
        return

    # Only process valid data here
    print(f"Valid exchange rate: {current_price}")
    last_price = current_price
    last_ts = current_ts

def is_trading_day():
    # Monday–Friday are trading days
    return datetime.now().weekday() < 5

# Split WebSocket URL for safety
WS_DOMAIN = "wss://apis.alltick.co"
WS_PATH = "/websocket-api/stock-websocket-interface-api/transaction-quote-subscription"
ws_url = WS_DOMAIN + WS_PATH

# Start WebSocket connection
ws = websocket.WebSocketApp(ws_url, on_message=on_message)
ws.run_forever()
Enter fullscreen mode Exit fullscreen mode

Production Improvements (For Devs Going Live)
To make this even more reliable:

  1. Use a full‑year trading calendar instead of just weekday check
  2. Add a data age threshold to reject delayed quotes
  3. Log filtering rates to monitor API quality
  4. Add error handling for WebSocket disconnects

These small upgrades make your system quiet on weekends and rock‑solid during market hours.

Key Takeaway
APIs don’t know your use case.
You must build a validation layer between raw data and your system.

This tiny filter completely eliminated false alerts, cleaned my data pipeline, and saved me hours of debugging.

If you’re working with real‑time APIs, WebSocket feeds, or financial data — this pattern will save you too.

Top comments (0)