MetaTrader 4 APIWebSocket streams
For AI agents: this page is also available as Markdown at /docs/mt4/websocket.md. The index of every docs page is /docs/llms.txt.
MetaTrader 4 WebSocket API
Stream live MetaTrader 4 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 MT4
- Polled, not event-driven. MT4 offers no event hooks a bridge can attach to, so every topic is produced by polling inside the terminal's main loop, after any queued requests have been handled.
- Prices on change. A tick is sent whenever bid or ask changes. last is always 0 and volume is the tick count of the current M1 bar.
- Account and positions on change. Both are re-read every second and within about 50 ms of a detected change, and sent when something changed.
- Trades within 100 ms. Trade and pending-order events are built by comparing snapshots of open orders and recent history, at least every 100 ms and right after each order the API sends. An order that opens and closes between two checks shows only its close; reconcile with /OrderHistory when you need every step.
- MT4 event fields. deal is always 0, position is the order ticket, and magic and comment are present on both the opening and the closing event, because MT4 orders keep them for their whole life. A partial close links its halves: remainderTicket on the Out, remainderOf on the remainder's In.
- Nothing lost on a reconnect. Resubscribe to trades with since and the trades made while you were away are replayed before live events resume. Dedupe the switch-over by order plus entry.
wss://api.fxsocket.com/mt4/{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. On MT4 every topic is produced by polling inside the terminal (MT4 has no event hooks for this), at the rates listed below. Seven topics are available:
prices: Per-symbol ticks (requires symbol), sent whenever bid or ask changes; volume is the tick count of the current M1 bar.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. On MT4, spread is the spread at that moment and realVolume is always 0.account: Account snapshot, then changes, checked once a second and within about 50 ms of a detected change.positions: Open positions snapshot, then changes, checked once a second and within about 50 ms of a detected change.trades: Trade events, built by comparing snapshots of open orders and recent history at least every 100 ms and right after each order the API sends: a ticket that appears among the open orders is an In, one that appears in history an Out. deal is 0, position is the order ticket, and magic and comment are present on both the opening and the closing event. Each event also carries sl and tp, positionVolume (still open after this event; -1 when unknown) and timeUtc, and a partial close links its two halves: remainderTicket on the Out, remainderOf on the remainder's In. A modify event on the same topic reports an open order whose stop loss or take profit moved. Optional since (a broker server time, as the frames print it) replays the trades 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, found the same way as trades. event is placed, modified, deleted, expired or triggered; the rest is the order as of that step, plus position (for triggered, the order's own ticket, which MT4 keeps for the position).terminal: Terminal status, roughly once per second.
{"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.000Z"}
{"action": "unsubscribe", "topic": "prices", "symbol": "EURUSD"}server → client messagesEvery 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.
{"type": "tick", "symbol": "EURUSD", "seq": 1841, "ts": 1793696156731, "data": {"bid": 1.15325, "ask": 1.15333, "last": 0.0, "volume": 37, "time": "2026-11-03T10:55:56.000Z"}}
{"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": 0, "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, "remainderTicket": 0, "remainderOf": 0, "time": "2026-11-03T10:56:02.000Z", "timeUtc": "2026-11-03T08:56:02.000Z", ...}}
{"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", ...}trades replay (since)Subscribing to trades with since first replays the trades at or after that moment, then switches to live events, so a client that was disconnected doesn't lose what happened in between. since is a broker server time as the frames print it, at most 24 hours back. The replay sends the newest 1000 events 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 nothing falls between the two; an event right at the switch-over can arrive twice, so dedupe by order plus entry (deal is always 0 on MT4). The replay rebuilds In and Out events from the terminal's order lists, so a replayed In carries the order's current stops, comment and commission rather than those it opened with. After a reconnect, subscribe again with the time of the last trade event you processed. The official SDKs do this for you.
{"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": { ... }}/ws-testerA built-in browser tester is served per account at https://api.fxsocket.com/mt4/{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.