DEV Community

EmilyL
EmilyL

Posted on

How to Detect Anomalies in Stock Data API Feeds (With Python Code)

When I first started building real-time market data pipelines, I focused almost entirely on connection stability and update speed. But after running a complete data flow, I realized that data accuracy matters just as much. Sometimes the feed looks fine, the process keeps running, and yet when you inspect historical records, you find small deviations in prices, timestamps, or volumes.

These anomalies aren’t caused by the market. They come from the processing layer. Real-time quotes keep flowing into your system, and without validation, a few bad records can corrupt your K-lines, indicators, and strategy results. That’s why I now put data validation directly in the quote processing pipeline.

Why You Need Validation for Real-Time Quotes

Stock market data is a continuously changing stream. Every second produces new prices and trade information. During reception and processing, several types of anomalies can appear:

  • Timestamp order issues: new data arrives with a time earlier than the previous record.
  • Price changes that clearly exceed the normal range.
  • Volume fields missing or formatted incorrectly.
  • Critical fields like symbol or trading status being empty.

If these records go straight into your database, later K-line generation can drift. For example, a wrong price inside a one-minute candle can distort the high, low, and close, which then affects every technical indicator built on top.

Check the Timestamp Order First

When processing real-time quotes, I check the timestamp field first. Normally, push messages for the same symbol should be increasing over time. If the program receives a record with an earlier time, it should be temporarily filtered out to avoid breaking the downstream ordering. A simple Python check:

def check_time(last_time, current_time):
    if current_time < last_time:
        return False
    return True
Enter fullscreen mode Exit fullscreen mode

In practice, you also need to account for exchange time zones, server time, and data source time. U.S. market data is especially tricky during daylight saving time transitions.

Detect Anomalous Price Moves

Price validation is another important step. A stock price won’t change without limit in an extremely short time, so you can set a threshold based on historical volatility. For example:

def check_price(old_price, new_price):
    change = abs(new_price - old_price) / old_price

    if change > 0.1:
        return False

    return True
Enter fullscreen mode Exit fullscreen mode

The threshold needs to be adjusted per stock type. Large-cap stocks and high-volatility stocks shouldn’t use the same criteria.

Validate Fields After Receiving a Push

Beyond price and time, I check whether the quote data structure is complete. For a real-time push, I don’t send the record to the calculation module immediately. Instead, I confirm the following first:

  • symbol exists.
  • price is not empty.
  • volume is correctly formatted.
  • timestamp is valid.

For example, when using AllTick API’s WebSocket to receive stock quotes, I run field checks before handing the data to the next stage.

import websocket
import json


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

    if not data.get("symbol"):
        return

    if not data.get("price"):
        return

    print(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

This keeps invalid records from reaching your core calculation logic.

Log Anomalies Instead of Silently Dropping Them

Many developers prefer to filter out bad data and move on. I think it’s better to keep a record. For each blocked item, store:

  • Symbol.
  • Data timestamp.
  • Original price.
  • Reason for rejection.

Later, reviewing these logs helps you determine whether the problem comes from the data source or from your own processing logic.

My Experience After Building Several Market Data Systems

The longer I work on stock quote systems, the more I care about data quality. A stock data API provides the source of truth, but validation is what makes that source usable. For real-time quotes, quantitative analysis, or automated trading, adding basic data checks reduces a lot of hidden risk.

A stable market data system isn’t only about speed. It’s about making sure every record that flows through it is reliable.

Top comments (0)