Have you ever had your live trading bot kicked off the data server just when the market started moving? I certainly have. The culprit wasn't my trading strategy, but how I was shouting subscribe and unsubscribe commands at my WebSocket connection. Let me share how I built a polite, stateful client that keeps the data flowing cleanly.
When you're developing a high-frequency forex bot as a solo dev, you quickly realize that a real-time feed isn't a static resource. Your strategy constantly shifts focus, and your code must tell the server to add new currency pairs and drop old ones without reconnecting. If you get this wrong, you'll either flood yourself with useless ticks or get rate-limited into oblivion. Let's fix that.
The Problem: Stateless Requests to a Stateful Connection
We often treat a WebSocket like a REST endpoint, firing off messages whenever we feel like it. Imagine a breakout strategy that monitors EURUSD. The moment volatility spikes, it wants to also watch GBPUSD and USDJPY. A naive implementation does this:
# Don't do this at home!
if volatility_spikes:
ws.send(json.dumps({"action": "subscribe", "symbols": ["GBPUSD"]}))
ws.send(json.dumps({"action": "subscribe", "symbols": ["USDJPY"]}))
If that condition oscillates during a choppy market, you'll hammer the server with rapid, repeated subscription attempts. The platform's firewall will likely classify your script as misbehaving and drop the connection. Now you're disconnected during the exact volatility you wanted to trade.
The Solution: A Local Subscription Mirror
The trick is to keep a client-side record of exactly what's currently active. Let this local object be the gatekeeper. Any subscription request gets checked against it; any cancellation is only sent if the pair is truly active.
I use this pattern with the AllTick real-time API, but it works with any provider that supports dynamic subscription messages. Here’s a battle-tested code snippet:
import websocket
import json
# Message handler for incoming ticks
def on_message(ws, message):
data = json.loads(message)
print("Tick:", data)
ws = websocket.WebSocketApp("wss://api.alltick.co/realtime",
on_message=on_message)
# Our local state mirror
subscribed = {}
def safe_subscribe(symbols):
# Filter out already active symbols
new_list = [s for s in symbols if s not in subscribed]
if new_list:
ws.send(json.dumps({"action": "subscribe", "symbols": new_list}))
for s in new_list:
subscribed[s] = True
def safe_unsubscribe(symbols):
# Filter out symbols we aren't watching
active_list = [s for s in symbols if s in subscribed]
if active_list:
ws.send(json.dumps({"action": "unsubscribe", "symbols": active_list}))
for s in active_list:
subscribed.pop(s)
ws.run_forever()
With safe_subscribe and safe_unsubscribe, your strategy can be as noisy as it wants. The gateway absorbs the duplicates and only sends clean, minimal diffs to the server. No more disconnection panic.
Pro Tips: Batching and Data Handling
-
Batch your commands: If you need to add five pairs and remove three, compute the entire diff and send a single
subscribeand a singleunsubscribemessage. One round trip, not eight. -
Cache the latest price: Don't process ticks directly in the
on_messagecallback. Update a globallatest_pricedictionary and have your strategy loop read it asynchronously. - Use a separate thread for I/O: Let a writer thread accumulate ticks and flush them to your database every 500ms. This prevents the GIL or I/O waits from slowing down your message parsing.
Managing WebSocket subscriptions feels like a tiny plumbing task, but doing it right is what separates a weekend prototype from a robust 24/7 trading system. How do you handle dynamic subscriptions in your projects? Let me know in the comments!

Top comments (0)