# MetaTrader WebSocket API. Every tick, as it lands.

Source: https://pipsocket.com/metatrader-websocket-api

One socket per account pushes prices, fills, deals and account state the moment the terminal sees them. Send orders down the same connection.

$15 per account, per month. MT5 today, MT4 next.

- [Get an API key](https://app.pipsocket.com)
- [Read the stream docs](https://docs.pipsocket.com/websocket)

## Live stream

Node:

```js
import WebSocket from "ws"

const socket = new WebSocket(`wss://wss.pipsocket.com/v1/accounts/${process.env.ACCOUNT}/stream`, {
  headers: { Authorization: `Bearer ${process.env.PIPSOCKET_KEY}` },
})

socket.on("open", () =>
  socket.send(
    JSON.stringify({
      id: "t1",
      method: "PUT",
      path: "/ticks",
      body: { symbols: ["EURUSD", "XAUUSD"] },
    }),
  ),
)
socket.on("message", (data) => console.log(JSON.parse(data.toString())))
```

Python:

```python
import asyncio, json, os, websockets

async def main():
    url = f"wss://wss.pipsocket.com/v1/accounts/{os.environ['ACCOUNT']}/stream"
    headers = {"Authorization": f"Bearer {os.environ['PIPSOCKET_KEY']}"}
    async with websockets.connect(url, additional_headers=headers) as socket:
        await socket.send(json.dumps({"id": "t1", "method": "PUT", "path": "/ticks",
                                     "body": {"symbols": ["EURUSD", "XAUUSD"]}}))
        async for message in socket:
            print(json.loads(message))

asyncio.run(main())
```

Receiving (shortened):

```
{"type":"tick","symbol":"EURUSD","bid":1.08412,"ask":1.08414}
{"type":"tick","symbol":"XAUUSD","bid":2391.80,"ask":2392.10}
{"type":"request","state":"done","result":{"retcode":10009}}
{"type":"tick","symbol":"EURUSD","bid":1.08415,"ask":1.08417}
{"type":"account","equity":10482.16,"margin":108.41}
```

## How the stream behaves

- **Prices as the broker sends them.** Bid, ask and last on every change, for up to 100 symbols per account.
- **Your fills, however you sent them.** A REST call, a TradingView alert or the kill switch: each result lands on the stream.
- **Calls on the same socket.** Send any API call as a message and get the REST answer back, matched by your id.
- **No silent gaps.** Fall behind and you get a lagged event with the count, so you know to re-read state.

## Everything on the account, in one socket.

Every message is a JSON object with a type. Fields follow MT5's own: MqlTick for prices, OnTradeTransaction for changes. Connect to `wss://wss.pipsocket.com/v1/accounts/{account}/stream` with `Authorization: Bearer <your key>` on the upgrade request.

### Events pushed to you

- `tick`: bid, ask and last for the symbols you picked
- `request`: a trade settled: filled, rejected or refused
- `deal`: a fill, close, stop loss or take profit
- `account`: balance, equity and margin, every second

### Status, and calls you send

- `transaction` (pushed): orders and positions changing, from MT5
- `status` (pushed): the terminal connecting or disconnecting
- `lagged` (pushed): the stream fell behind, with how many missed
- `PUT /ticks` (sent): pick the symbols to stream, up to 100
- `POST /orders` (sent): any REST call, answered on the socket

Full stream reference: https://docs.pipsocket.com/websocket (every docs page as Markdown: https://docs.pipsocket.com/llms.txt).

## From MetaTrader login to live prices. Three steps, no polling.

### Connect your MT5 login

Server, login and password, from any MT5 broker. Your terminal starts next to your broker's trade server and stays on.

### Make your API key

One key for your workspace, shown once. Keep it on your server and roll it whenever you like.

### Open the stream

Connect with your key in the header, send the symbols you want, and events start arriving.

## Why not poll, or run a socket EA?

|                | Polling, or a socket EA on a VPS             | Pipsocket WebSocket                                           |
| -------------- | -------------------------------------------- | ------------------------------------------------------------- |
| Prices         | A request a second, and you still miss moves | Each price change pushed as the broker sends it               |
| Your fills     | Poll positions and diff them yourself        | A request event the moment MT5 answers                        |
| Falling behind | Gaps you find out about later                | A lagged event with how many you missed                       |
| Security       | A port open on the VPS for the EA's socket   | No open ports. Terminals dial out; your key opens the stream. |
| Trading        | A second channel just for orders             | Orders on the same socket, idempotent like REST               |

## MetaTrader WebSocket questions

### Can I stream MT5 prices without running a terminal?

Yes. Pipsocket runs the MT5 terminal on its servers and streams it to you. All you need is an MT5 login from your broker.

### How many symbols can I stream?

Up to 100 per account, shared by that account's streams. Change the set any time with one call.

### Can I connect from a browser?

Not directly. The key goes in the Authorization header of the upgrade, which browsers can't set, so connect from your server.

### What happens if my connection drops?

Reconnect and send your symbols again. Events from the gap aren't replayed, so read positions and history over REST.

### What does it cost?

$15 per account, per month, with REST, TradingView alerts, MCP and the kill switch included. No per-message fees.

## The same account, other ways in

- [MetaTrader REST API](https://pipsocket.com/metatrader-rest-api.md) (`api.pipsocket.com`): orders, positions and history over HTTPS.
- [TradingView to MetaTrader](https://pipsocket.com/tradingview-to-metatrader.md) (`hooks.pipsocket.com`): turn alerts into orders on every account you pick.
- [MetaTrader MCP server](https://pipsocket.com/metatrader-mcp-server.md) (`mcp.pipsocket.com`): let Claude or any agent read and trade your account.
