DEV Community

EmilyL
EmilyL

Posted on

How We Engineered a Multi-Market Real-Time Quotes System for Stocks, Forex, Gold, and Crypto (API Architecture)

#ai

Hey folks! πŸ‘‹ Building data pipelines for FinTech is wildly unpredictable. As the CTO of a fast-moving quantitative tech startup, I want to share a recent architectural battle we won. If you've ever tried to shove Stocks, Forex, Gold, and Crypto into a single unified dashboard, you know the absolute headache of dealing with fragmented APIs. Here is how we bypassed the mess and shipped our system in record time.

The Startup Case: The MVP Dashboard
Our users wanted a god-view terminalβ€”one UI to track everything from AAPL to BTC, EUR/USD to Gold. In the startup world, you don’t have months to build custom adapters for every exchange on the planet. We needed an architecture that allowed for rapid prototyping without sacrificing scalability.

Data Pain Points: APIs Are Not Created Equal
We initially tried hooking up specialized providers for each asset class. It was a disaster.

  • Timing: Stocks sleep at night. Crypto never stops.
  • Pricing format: Forex relies heavily on dual bid/ask updates, while crypto folks usually just want the last matched trade. Gold has its own spot pricing quirks.
  • The Dev Experience: We were writing four different sets of WebSocket managers, four different heartbeat scripts, and parsing four different JSON structures. The codebase was bloating rapidly.

The Solution: One Schema, Fewer Endpoints
We hit the brakes and refactored. First, we implemented an absolute rule: No external API payload touches our internal services. We built an adapter pattern that morphs incoming streams into a strict interface: asset_type, symbol, timestamp (UTC or bust), bid, ask, and last.

Second, we fired redundant data providers. To streamline our connection logic, we shifted our primary streaming needs to the ALLTICK API, since it bundles traditional finance (fiat, metals, stocks) and digital assets into a single protocol. This drastically reduced our socket management overhead.

Check out our simplified Python WebSocket implementation:

import json
import websocket

API_KEY = "your_alltick_api_key"
WS_URL = f"wss://quote.alltick.co/quote-b-ws-apii?token={API_KEY}"

def on_open(ws):
    subscribe_msg = {
        "cmd_id": 22004,
        "seq_id": 1,
        "trace": "sub-us-stock",
        "data": {
            "symbol_list": [
                {"code": "EURUSD"},
                {"code": "GOLD"}
            ]
        }
    }
    ws.send(json.dumps(subscribe_msg))

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

def on_error(ws, error):
    print("WS Error:", error)

def on_close(ws, close_status_code, close_msg):
    print("WS Closed. Firing reconnect logic...")

if __name__ == "__main__":
    ws = websocket.WebSocketApp(
        WS_URL,
        on_open=on_open,
        on_message=on_message,
        on_error=on_error,
        on_close=on_close
    )
    ws.run_forever()

Enter fullscreen mode Exit fullscreen mode

Cost & Efficiency Optimization: Less Code, More Sleep
The results of this refactor were night and day for our engineering team.

  • Rock-Solid Stability: We implemented independent retry loops. If the 24/7 crypto stream fails, it doesn't crash the stock ticker process.
  • UTC Standardization: Handling time manipulation at the ingress layer saved our frontend devs from rendering bugs across different time zones.
  • Dev Hours Saved: We cut the time required to add a new asset class by 80%. We just push a new symbol to our subscription array.

Keep your boundaries strict and your vendors minimal. Happy coding! πŸš€

Top comments (0)