# MetaTrader 5 WebSocket API

> Index of every docs page as Markdown: [llms.txt](https://fxsocket.com/docs/llms.txt). All pages in one file: [llms-full.txt](https://fxsocket.com/llms-full.txt).

Source: https://fxsocket.com/docs/mt5/websocket

Stream live MetaTrader 5 data over a single WebSocket per account: ticks, OHLC bar closes, account updates, open positions, trade events, and terminal status. Subscribe and unsubscribe with simple JSON control frames, no polling.

## 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.
- **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 / prices) before streaming deltas. Non-browser clients: the upgrade requires HTTP/1.1. Six topics are available:

- `prices`: Per-symbol ticks (requires symbol).
- `bars`: OHLC bar closes per symbol + timeframe (requires symbol and timeframe).
- `account`: Account snapshot, then changes.
- `positions`: Open positions snapshot, then changes.
- `trades`: Trade events as they happen.
- `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": "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. Slow consumers get dropped messages plus a warning message rather than a stalled socket.

Server → client messages:

```
{"type": "tick", "symbol": "EURUSD", "data": {"bid": 1.15325, "ask": 1.15333, "last": 0.0, "volume": 0, "time": "2026-06-11T08:55:56.728Z"}}
{"type": "bar", "symbol": "EURUSD", "timeframe": "M5", "data": {"open": 1.15301, "high": 1.15348, "low": 1.15289, "close": 1.15330}}
{"type": "account", "data": { ... }}
{"type": "positions", "data": [ ... ]}
{"type": "trade", "data": { ... }}
{"type": "terminal", "data": {"connected": true, "tradeAllowed": true, "serverTime": "2026-06-11T08:55:57.000Z"}}
{"type": "subscribed" | "unsubscribed" | "error" | "warning", ...}
```

### 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.

## Related

- [Market Data](https://fxsocket.com/docs/mt5/market-data.md)
- [Account Data](https://fxsocket.com/docs/mt5/account-data.md)
- [Trading](https://fxsocket.com/docs/mt5/trading.md)
