MetaTrader 5 APIWebSocket streams

For AI agents: this page is also available as Markdown at /docs/mt5/websocket.md. The index of every docs page is /docs/llms.txt.

MetaTrader 5 WebSocket API

Stream live MetaTrader 5 data over a single WebSocket per account: ticks, OHLC bar closes, account updates, open positions, trade events, pending orders, and terminal status. Subscribe and unsubscribe with simple JSON control frames, no polling, and replay the trades you missed after a reconnect.

How streaming works on MT5

  • Trades as events. Trade events come from MT5's own trade-transaction events, one per deal as it happens, instead of from snapshots.
  • Pending orders as events. The orders topic reports each step of a pending order (placed, modified, deleted, expired, triggered), including stop-limit orders; triggered names the position the fill opened.
  • Nothing lost on a reconnect. Resubscribe to trades with since and the deals made while you were away are replayed before live events resume.
  • Exit deals and magic numbers. On MT5 the deal that closes a position usually carries magic 0 and an empty comment unless the closing request set them. Match exits to entries by position, which every trade event carries.
  • Degraded events. If the bridge cannot enrich an event in time, it is sent with degraded true: identifiers, symbol, type, volume and price are reliable, profit and the other details are zeroed. Reconcile with /OrderHistory.
WS wss://api.fxsocket.com/mt5/{account_id}/ws?api_key=fxs_live_...

One streaming socket per account. Authentication is via the api_key query parameter, the only method available on WebSocket connections. Send JSON control frames to subscribe and unsubscribe; on subscribe the server first sends one current snapshot (account / positions / orders / prices), marked "snapshot": true, before streaming changes. The snapshot can arrive just after the first live frame. Non-browser clients: the upgrade requires HTTP/1.1. The server pings every 5 seconds and closes a connection that hasn't answered for 30 seconds; every WebSocket library answers pings on its own, so this only affects a client that stops reading its socket. Seven topics are available:

  • prices: Per-symbol ticks (requires symbol).
  • bars: Closed bars per symbol + timeframe (requires symbol and timeframe). A bar frame goes out with the first tick of the next bar, so it always holds the bar that just closed, never the forming one. data.time is the bar's open time in broker server time. Bar boundaries follow the broker's tick timestamps, not your clock, so a frame can arrive slightly before the close by your clock when the broker's clock runs ahead. Without a new tick the frame waits, so the last bar before the weekend arrives when the market reopens. Each bar is sent once and never corrected. A reconnect can leave gaps; /PriceHistory fills them and always returns the current history.
  • account: Account snapshot, then changes within about 50 ms, and at least once a second.
  • positions: Open positions snapshot, then changes within about 50 ms, and at least once a second.
  • trades: A trade event per executed deal as it happens: deal, order, position, symbol, type, entry (In, Out, InOut, OutBy), volume, price, profit, commission, swap, magic, comment, time (with milliseconds) and timeUtc, sl and tp, positionVolume (still open on the position after this deal; -1 when unknown) and degraded. A modify event on the same topic reports an open position whose stop loss or take profit moved. Optional since (a broker server time, as the frames print it) replays the deals you missed first, see below.
  • orders: Pending orders: a snapshot with the /PendingOrders rows, then an order event per step of an order's life. event is placed, modified, deleted, expired or triggered; the rest is the order as of that step, plus position (for triggered, the position its fill opened or added to).
  • terminal: Terminal status, roughly once per second.
Client → server frames
{"action": "subscribe", "topic": "prices", "symbol": "EURUSD"}
{"action": "subscribe", "topic": "bars", "symbol": "EURUSD", "timeframe": "M5"}
{"action": "subscribe", "topic": "account"}
{"action": "subscribe", "topic": "orders"}
{"action": "subscribe", "topic": "trades", "since": "2026-11-03T09:58:12.481Z"}
{"action": "unsubscribe", "topic": "prices", "symbol": "EURUSD"}
WS server → client messages

Every server message is a JSON object with a type. Data messages mirror the REST shapes; control replies confirm your frames. Ignore types you don't know, so new ones can be added without breaking your client. Every live frame carries seq, numbered per topic, and ts, the moment it was sent (Unix milliseconds, real UTC). A gap in seq means frames were dropped for you; a slow consumer gets dropped messages plus a warning that names the topic (and symbol or timeframe) rather than a stalled socket. Snapshots and replayed frames have no seq. Trade and order events of a connection are sent ahead of its queued ticks.

Server → client messages
{"type": "tick", "symbol": "EURUSD", "seq": 1841, "ts": 1793696156731, "data": {"bid": 1.15325, "ask": 1.15333, "last": 0.0, "volume": 0, "time": "2026-11-03T10:55:56.728Z"}}
{"type": "bar", "symbol": "EURUSD", "timeframe": "M5", "seq": 12, "ts": 1793696100004, "data": {"time": "2026-11-03T10:50:00.000Z", "open": 1.15301, "high": 1.15348, "low": 1.15289, "close": 1.15330, "tickVolume": 412, "realVolume": 0, "spread": 8}}
{"type": "account", "seq": 300, "ts": 1793696157002, "data": { ... }}
{"type": "positions", "snapshot": true, "ts": 1793696150118, "data": [ ... ]}
{"type": "trade", "seq": 7, "ts": 1793696162513, "data": {"deal": 211438760, "order": 211438761, "position": 211438761, "symbol": "EURUSD", "type": "Buy", "entry": "In", "volume": 0.1, "price": 1.15333, "sl": 1.148, "tp": 1.162, "positionVolume": 0.1, "time": "2026-11-03T10:56:02.497Z", "timeUtc": "2026-11-03T08:56:02.497Z", ...}}
{"type": "modify", "seq": 8, "ts": 1793696170040, "data": {"position": 211438761, "symbol": "EURUSD", "type": "Buy", "volume": 0.1, "price": 1.15333, "sl": 1.15, "tp": 1.165, ...}}
{"type": "orders", "snapshot": true, "ts": 1793696150120, "data": [ ... ]}
{"type": "order", "seq": 3, "ts": 1793696175310, "data": {"event": "placed", "ticket": 211583734, "symbol": "GBPUSD", "type": "BuyLimit", "price": 1.33958, "position": 0, ...}}
{"type": "terminal", "seq": 52, "ts": 1793696157000, "data": {"connected": true, "tradeAllowed": true, "serverTime": "2026-11-03T10:55:57.000Z"}}
{"type": "warning", "topic": "prices", "symbol": "EURUSD", "dropped": 12, "ts": 1793696158000}
{"type": "subscribed" | "unsubscribed" | "error" | "replayed", ...}
WS trades replay (since)

Subscribing to trades with since first replays the deals at or after that moment, then switches to live events, so a client that was disconnected doesn't lose the deals in between. since is a broker server time as the frames print it, at most 24 hours back. The replay sends the newest 1000 deals at most, oldest first, as trade events marked "replay": true, then a replayed frame with their count (truncated is true when the limit cut it short), then live events. It waits for the live source before it ends, so no deal falls between the two; a deal right at the switch-over can arrive twice, so dedupe by deal. After a reconnect, subscribe again with the time of the last trade event you processed. The official SDKs do this for you.

Server → client messages
{"type": "trade", "replay": true, "ts": 1793696301002, "data": { ... }}
{"type": "trade", "replay": true, "ts": 1793696301002, "data": { ... }}
{"type": "replayed", "topic": "trades", "count": 2, "truncated": false}
{"type": "trade", "seq": 1, "ts": 1793696305117, "data": { ... }}
GET /ws-tester

A built-in browser tester is served per account at https://api.fxsocket.com/mt5/{account_id}/ws-tester?api_key=fxs_live_... (handy for poking at the stream before writing code). The page is API-key gated, and since browsers can't attach headers the key must ride in the URL (it will appear in your browser history). Open yours from the request builder on the overview page.