DEV Community

Emily
Emily

Posted on

Building a Low-Cost US Stock Real-Time API Integration with WebSocket and REST

When I started building a personal US stock monitor, I assumed real-time data would require a paid terminal. The cheapest professional option I found was still over $200 per month, which is absurd for a side project. After two weeks of testing, I settled on a hybrid architecture using WebSocket for live quotes and REST for historical minute bars. The total running cost is under $5 per day, and the latency is well within my requirements. This post documents the technical choices so you can reproduce the setup without the trial-and-error.

Problem statement and data requirements

The core issue is not the lack of APIs but the way pricing is structured. Institutional terminals bundle thousands of symbols and features that an individual developer doesn’t need. Before integrating any vendor, define your actual data contract:

  • Number of concurrent symbols (mine was < 10)
  • Required update frequency (1-second snapshot was sufficient)
  • Historical data granularity (1-minute bars for charting)
  • Acceptable connection drop recovery time
  • Authentication and key management complexity

With these constraints, the solution becomes a simple two-component system.

WebSocket vs REST: a technical decision matrix

Factor WebSocket REST
Data flow Push-based, server-initiated Pull-based, client-initiated
Latency Milliseconds Depends on polling interval
State Stateful, requires heartbeats Stateless, no server-side session
Resource usage Low per update, but constant connection Higher per request, but only when needed
Ideal scenario Live quote streaming for few symbols Historical bulk download

The table above guided my implementation: WebSocket for intraday updates, REST for backfilling minute bars. This separation avoids polling overhead while ensuring historical completeness.

Implementation details

I tested a multi-asset ALLTICK API provider that handles US stocks, HK stocks, and forex with a single key. The onboarding was straightforward — API key delivered same day, and the WebSocket endpoint responded immediately. Using one key for multiple asset classes reduces integration overhead if you later expand beyond US equities.

Below is the minimal Python code for subscribing to real-time AAPL quotes.

import websocket
import json

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

def on_open(ws):
    sub_msg = {
        "cmd_id": 22002,
        "seq_id": 1,
        "trace": "sub-us-stock",
        "data": {
            "symbol_list": [{"code": "AAPL.US"}]
        }
    }
    ws.send(json.dumps(sub_msg))

ws = websocket.WebSocketApp(
    "wss://quote.alltick.co/quote-stock-b-ws-api",
    on_open=on_open,
    on_message=on_message
)
ws.run_forever()
Enter fullscreen mode Exit fullscreen mode

After running this script, you’ll see continuous AAPL quotes. The average round-trip latency is in the single-digit milliseconds, which is more than sufficient for a personal dashboard or a lightweight algorithmic strategy.

Handling historical K-lines and reliability

For 1-minute bars, I use the REST endpoint at startup to fetch the last few days of data. This fills the local database, and then WebSocket updates append new bars in real time. This prevents gaps in the chart when the market opens. I’ve been running this setup for about a month, with a total server cost below $150. The main operational issue was WebSocket disconnections, but a simple exponential backoff reconnection loop resolved it.

If you need a low-cost US stock real-time API integration, this hybrid pattern is robust and easy to maintain. I recommend starting with a minimal symbol list and adding more only after you’ve validated the reconnection and storage logic.

Top comments (0)