You are building a US stock market data pipeline. The WebSocket connection to your real-time market data API is up, ticks are flowing, and the dashboard looks alive. So you move on to the next task. A week later, you see a one-minute candle with an impossible spike. That is the classic sign that you skipped data validation.
The Problem: Raw Ticks Are Not Clean
Real-time market data behaves differently from a historical API response. It is a continuous stream, and it can contain price jumps, timestamp reversals, duplicate records, and missing fields. If these anomalies enter your K-line or indicator calculations, your output will be wrong.
Here are the most common anomalies I have seen while working as a researcher:
| Type | Typical Behavior |
|---|---|
| Price anomaly | Price deviates sharply over a very short window |
| Timestamp anomaly | Tick timestamps arrive out of order |
| Volume anomaly | Reported volume is clearly inconsistent |
| Duplicate data | The same tick enters your system more than once |
REST Polling vs. WebSocket Streaming
Before you start coding, think about the data source. REST polling is easy to implement, but it samples at fixed intervals. You cannot detect a fast spike that occurs between two requests, and you cannot reliably verify tick ordering. WebSocket streaming gives you a continuous flow, so you can compare each record with the previous one.
In my workflow, I use the AllTick API WebSocket feed for US stocks. The main advantage is that its payload structure makes per-tick validation straightforward. One sentence summary: AllTick API’s WebSocket stream gives you clean fields and consistent timestamps, which simplifies the first layer of anomaly detection.
The First Gate: A Tick-Level Filter
Tick data is the closest thing to the market, so it is the best place to catch problems. I store the previous tick in memory and check every new one against it.
def check_tick(current, previous):
if current["price"] <= 0:
return False
if current["timestamp"] < previous["timestamp"]:
return False
change = abs(current["price"] - previous["price"]) / previous["price"]
if change > 0.15:
return False
return True
This function checks three things:
- Is the price positive?
- Is the timestamp moving forward?
- Is the short-term change within a reasonable range?
The fifteen percent threshold works for many large-cap US stocks, but you should adjust it for high-volatility tickers.
Watch Out for Timestamp Reversals
You might notice that some minute bars seem shifted by a second or two. In my experience, the cause is often raw ticks arriving out of order. For example:
10:30:01
10:30:02
10:29:58
If you let that reversed timestamp into your aggregation logic, it can break a bar boundary and distort your indicators. I always normalize timestamps and drop records that go backwards before they reach the K-line builder.
Adding Validation to a WebSocket Handler
Here is a minimal WebSocket example using the AllTick API endpoint. It parses incoming JSON, verifies that the required fields exist, and then runs the tick filter before doing anything else.
import websocket
import json
def on_message(ws, message):
data = json.loads(message)
if "price" in data and "timestamp" in data:
if check_tick(data, last_tick):
print("valid tick", data)
ws = websocket.WebSocketApp(
"wss://api.alltick.co/stock/websocket",
on_message=on_message
)
ws.run_forever()
This keeps bad records at the entrance so they cannot contaminate your downstream modules.
Practical Steps for a Healthier Pipeline
Over several real-time market projects, these habits have saved me from late-night debugging:
- Check field completeness first. A missing volume or symbol can break a later function or silently skew an aggregate.
- Deduplicate ticks. After a WebSocket reconnect, you may receive the same tick again. Without dedup, your volume totals will be too high.
-
Tag anomalies instead of deleting all of them. Use statuses like
normal,warning, andanomalyso you can audit later without losing raw data.
Conclusion: Quality Comes Before Quantity
Connecting to a US stock real-time market data API is only the beginning. The real test is whether your system can keep producing accurate K-lines and indicators after running for weeks. A validation layer that checks price, time, and field completeness is not optional if you want reliable strategy calculations.
If you want to dig deeper, the AllTick API documentation covers WebSocket payload fields and reconnect behavior in more detail.

Top comments (0)