Broker APITrading InfrastructureOrder ManagementReliabilityIntegration

    Broker API Integration: The Things That Break in Production (and How to Prevent Them)

    Why broker API integrations fail in live trading — order state, duplicates, partial fills, reconnects, rate limits, tokens and time — and the design patterns that prevent each problem.

    By ThinkStak Engineering 7 min read

    A broker API integration rarely fails on the happy path. It fails when an order is partially filled, a connection drops mid-request, a message arrives twice or out of order, a token expires or a rate limit is hit. Production-grade integrations treat the broker as an unreliable remote system: they keep their own record of every order, make every action safe to repeat, and reconcile with the broker continuously.

    Whether you are integrating MetaTrader, Interactive Brokers, Tradovate, DXTrade, Match-Trader, a crypto exchange or an Indian broker, these are the problems we see most often — and how to design around them.

    1. You do not know an order's real state

    The classic bug: your code sends an order, the request times out, and you do not know whether the broker received it. Retrying blindly can double the position; giving up can leave an order you never track.

    Prevent it:

    • Give every order your own client order ID before sending it.
    • Store the order in your database as "pending" first, then send.
    • On a timeout, query the broker by your client ID before deciding to resend.
    • Model the full lifecycle: pending, accepted, partially filled, filled, modified, cancel-requested, cancelled, rejected, expired.

    2. Duplicate orders

    Duplicates come from retries, from a signal delivered twice, or from a process restart that replays a queue.

    Prevent it:

    • Make order placement idempotent: the same signal ID can create at most one order.
    • Use the broker's own client-order-ID field where it exists, so the broker rejects a duplicate too.
    • After a restart, load open orders and positions from the broker before acting on new signals.

    3. Partial fills and modifications

    A 10-lot order may fill 3 lots, then 4, then the rest is cancelled. A stop-loss modification may arrive while a partial fill is in flight.

    Prevent it:

    • Track filled quantity and average price per order from execution reports or fills, not from the original request.
    • Size follow-up actions (stops, take-profits, copies to other accounts) from what actually filled.
    • Serialise actions per order so a modification and a fill for the same order are processed in sequence.

    4. Disconnects, reconnects and stale data

    WebSockets drop. Brokers restart servers at weekends and during maintenance windows. Your server loses network for ten seconds.

    Prevent it:

    • Detect silence with heartbeats or timeouts, not only with explicit errors.
    • On every reconnect, re-subscribe and then reconcile: fetch open orders, positions and recent fills, and compare with your records.
    • Mark prices as stale after a set time and block trading on stale data.
    • For FIX, handle sequence gaps and resend requests properly — see what is FIX protocol.

    5. Events arrive out of order

    A fill notification can arrive before the order acknowledgement, or a cancel confirmation after a fill.

    Prevent it: treat events as facts to merge, not commands to replay. Use broker timestamps and sequence numbers where available, never move an order backwards in its lifecycle, and let reconciliation be the final judge.

    6. Rate limits

    Every broker limits requests — per second, per minute, per day, sometimes per endpoint. Hitting them during a fast market is the worst time to find out.

    Prevent it:

    • Put a rate limiter in front of the broker client, with separate budgets for orders and data calls.
    • Prefer streaming over polling for prices and order updates.
    • Batch where the API allows it, and queue non-urgent calls.

    7. Authentication and tokens

    Tokens expire daily on some brokers, hourly on others; refresh tokens get revoked; passwords change; IP whitelists (such as India's static IP rule for API orders) reject calls from a new server.

    Prevent it: automate the login and refresh flow, check session health before market open, alert a person on failure, and keep credentials in a secrets store, never in code or logs.

    8. Symbols, contract specs and time

    The same instrument has different names, contract sizes and lot steps across brokers. Trading sessions, holidays and daylight-saving changes differ by venue.

    Prevent it: keep an instrument master per broker (symbol, size, tick, lot step, sessions), refresh it daily, store all times in UTC and convert only for display.

    9. No visibility when something goes wrong

    Many incidents are found by the client, not the system.

    Prevent it: log every request, response and event with IDs; alert on rejects, repeated errors, reconnect loops and position mismatches; and keep a dashboard showing each account's connection, last update and open exposure.

    A production readiness checklist

    • Client order IDs on every order, stored before sending
    • Idempotent signal handling; duplicates ignored
    • Full order lifecycle with partial fills
    • Reconciliation on startup, on reconnect and on a timer
    • Stale-data detection that blocks trading
    • Rate limiter with separate order and data budgets
    • Automated login and token refresh with alerts
    • Instrument master per broker, refreshed daily
    • Structured logs and alerts to a named person
    • Tested against the broker's demo or UAT environment, including forced disconnects

    ThinkStak builds broker integrations for brokers, prop firms and trading products in the UK, UAE, Australia, US and India — see broker API integration, OMS and risk engine development, our multi-broker platform case study, or book a free audit.

    ThinkStak builds trading software only. We do not provide trading signals or investment advice.

    Questions about this topic

    What is the most common broker API integration bug?

    Losing track of an order's real state after a timeout or disconnect, which leads to duplicate orders or untracked positions. Client order IDs and reconciliation with the broker prevent it.

    How do you prevent duplicate orders in an automated trading system?

    Give each signal and order a unique ID, store it before sending, use the broker's client-order-ID field where available, and check the broker before retrying after a timeout.

    What should happen when a broker connection drops?

    The system should stop trading on stale data, reconnect, re-subscribe, then reconcile open orders, positions and recent fills with the broker before acting on new signals.

    Should I poll a broker API or use WebSockets?

    Use streaming (WebSockets or FIX) for prices and order updates where the broker offers it, and keep polling for periodic reconciliation. Polling for everything wastes rate limits and adds delay.

    Want this built for your trading business?

    ThinkStak designs and builds trade copiers, broker integrations and risk systems. Book a free audit to discuss your setup.