DEV Community

kalos
kalos

Posted on

Why certain stocks lose market data on free stock APIs during midday break


When building quantitative trading tools, market data collectors and trading strategies, free stock quote APIs are widely used for development and testing. Many developers have encountered a common issue: stock data works perfectly in the morning trading session, but some symbols suffer from stalled updates or missing data around the midday market break.

After repeated reproduction, cross-checking with original data and analyzing API mechanics, I confirmed this is not caused by local code bugs. It stems from market trading rules, API design, data delivery policies and server load. This article explains the root causes, shares a practical code example and provides actionable optimization tips.

A-share Market Trading Hours
First let’s clarify the trading schedule of China A-shares, which is essential to understand the anomaly:
Morning session: 9:30 — 11:30
Midday break: 11:30 — 13:00
Afternoon session: 13:00 — 15:00
Most free stock APIs pull data directly from official exchanges, but they do not forward full tick-by-tick raw data. This is the fundamental reason for midday data interruption.

Root Causes of Data Loss
1.Limited data update frequency
Paid commercial APIs support real-time updates for every transaction. To reduce bandwidth and maintenance costs, free APIs adopt batch scheduled updates, with an interval ranging from several seconds to tens of seconds.

For small-cap stocks and low-liquidity symbols with sparse trading activities, the refresh interval becomes even longer — sometimes new data only comes in every few minutes. When the midday break approaches, exchanges start aggregating and archiving trading data from the morning, which further slows down external data distribution.

In my tests, these low-activity stocks maintained stable data streams before noon, but began to disconnect intermittently near 11:30. Data delays would not be fully resolved right after the market reopened.

2.Performance difference between two mainstream connection types
Currently, stock APIs mainly use HTTP polling and WebSocket push. Their performance varies greatly under midday high concurrency.

HTTP Polling
The client continuously sends requests to fetch the latest data. Nearly all free APIs enforce rate limits and QPS restrictions. During the midday peak period, massive concurrent requests easily lead to timeouts and packet loss. The problem becomes more obvious when pulling data for a large number of stocks.

WebSocket Push
WebSocket long connection is a better choice for real-time data collection. Once connected, the server actively pushes data to clients with lower latency.
However, free WebSocket services still have limitations: maximum subscription quantity and data delivery priority. Platforms prioritize data transmission for high-volume blue chips and popular stocks, while small-cap and inactive symbols are delayed, which looks like a data outage on the client side.

Below is a runnable Python demo for AllTick API WebSocket subscription:

import websocket
import json

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

ws = websocket.WebSocketApp(
    "wss://api.alltick.co/stock/ws",
    on_message=on_message
)

payload = {
    "action": "subscribe",
    "symbols": ["000001", "600000"]
}

ws.on_open = lambda ws: ws.send(json.dumps(payload))
ws.run_forever()
Enter fullscreen mode Exit fullscreen mode

Even with WebSocket, intermittent delays for low-liquidity stocks are normal. This is reasonable resource scheduling on the API side, rather than service failure or code errors.

3.Data source and server load issues
During the midday break, exchanges perform data statistics and routine maintenance, which temporarily reduces data synchronization efficiency. Most free APIs also enable data caching to save bandwidth, so quotes will not refresh immediately.

Since free APIs are open to all developers, server load rises sharply at peak hours. Data resources for non-core stocks are squeezed, making missing data a frequent occurrence.

Practical Optimization Solutions

Based on real-world development experience, here are several universal solutions to improve data pipeline stability.

  1. Cross-verify with official data sources
    Compare data from third-party APIs with official exchange quotes. You can quickly tell whether the data gap is caused by zero trading volume of the stock, or actual API exceptions. This logic can be embedded into your data preprocessing module.

  2. Build tiered data collection rules
    Classify stocks by trading activity: keep high-frequency subscription for high-liquidity core symbols to guarantee timeliness; lower refresh frequency for inactive stocks to cut redundant requests and reduce overall pressure. This method works for both historical data crawling and real-time monitoring.

  3. Prefer WebSocket over HTTP Polling
    For long-running real-time collectors and quantitative trading services, replace HTTP polling with WebSocket. Long connections deliver better data continuity under high-concurrency scenarios.
    In production projects, I treat high-activity stocks as core data sources and use inactive symbols only for auxiliary reference. In this way, partial data interruption will not affect the whole application.

Wrap up
Midday data loss on free stock APIs is a combined result of trading schedules, API architecture, resource allocation and server pressure.

When you encounter similar problems, do not just troubleshoot your code blindly. Learning how public APIs work and adjusting your data collection strategy will greatly enhance the robustness of your applications.

Feel free to share your experience or other solutions in the comments.

Top comments (0)