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

Published: (May 8, 2026 at 12:57 AM EDT)
2 min read
Source: Dev.to

Source: Dev.to

The Problem We’re Solving

My 3‑Layer Filtering Logic

Core Validation Function

def is_valid_trading_data(price, timestamp, last_price, last_timestamp):
    # 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

Full WebSocket Implementation (using AllTick API as an example)

import json
import websocket

WS_DOMAIN = "wss://apis.alltick.co"
ws_url = f"{WS_DOMAIN}/your_endpoint"

last_price = None
last_ts = None

def is_trading_day():
    # Implement your logic to determine if today is a trading day
    ...

def on_message(ws, message):
    data = json.loads(message)  # assuming JSON payload
    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}")
    global last_price, last_ts
    last_price = current_price
    last_ts = current_ts

ws = websocket.WebSocketApp(ws_url, on_message=on_message)

Production Improvements (For Devs Going Live)

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 the data pipeline, and saved hours of debugging.
  • If you’re working with real‑time APIs, WebSocket feeds, or financial data — this pattern will save you too.
0 views
Back to Blog

Related posts

Read more »

str() vs repr() vs print() in Python

Overview When learning Python you encounter three built‑in utilities that often look similar: - str - repr - print At first they may seem to do the same thing—...