# MetaTrader 4 REST & 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/mt4

FxSocket exposes each connected MetaTrader 4 account through a REST API and a WebSocket stream. Every account gets its own dedicated MT4 terminal in our cloud. Requests to your account hit your own terminal, nobody shares it. All endpoints return JSON.

Per-account base URL: `https://api.fxsocket.com/mt4/{account_id}`

`{account_id}` is the UUID of your connected MT4 account. Copy it from the [Accounts page](https://app.fxsocket.com/dashboard/accounts) or read it from `GET https://api.fxsocket.com/v1/accounts`. Your API key lives on the [API Key page](https://app.fxsocket.com/dashboard/api-key).

You can link, list, and remove accounts from the dashboard, or programmatically through the account-management API at `https://api.fxsocket.com/v1`. See [Managing Accounts](https://fxsocket.com/docs/mt4/accounts.md).

## How MT4 Differs From MT5 in This API

The routes and JSON shapes are the same on both platforms, so one client serves MT4 and MT5 accounts alike. What differs is what MT4 itself can do, and the bridge in your terminal fills those gaps in a documented way:

|  | MetaTrader 4 | MetaTrader 5 |
| --- | --- | --- |
| Order types | 6: market, limit, stop | 8: adds BuyStopLimit and SellStopLimit |
| Account model | Always hedging | Netting or hedging (see marginMode) |
| Trade records | One ticket per order; deal is always 0 | Separate orders, deals and positions |
| Partial close | Remainder continues under a new ticket | Position keeps its ticket |
| Trade history | One row per closed order | One row per deal (In, Out, InOut, OutBy) |
| Error codes | MT4 errors mapped to MT5 retcodes | MT5's native retcodes |
| Request handling | One at a time, reads included | Reads answered while orders run |
| Live streams | Polled: trades every 250 ms | Trade events as they happen |
| Margin and profit calculators | Computed by the bridge | MT5's built-in functions |
| Volume data | Tick volume only | Real volume where the broker provides it |

Each topic page has a section on what this means in practice, for example the [partial-close renumbering](https://fxsocket.com/docs/mt4/trading.md) and the [MT4 error to retcode table](https://fxsocket.com/docs/mt4/errors.md).

Looking for MetaQuotes' own MT4 Server API or Manager API? Those are toolkits for brokers that run their own MetaTrader 4 server, not something you can call with a trading account. Our guide [MetaTrader Server, Manager and Web API explained](https://fxsocket.com/blog/metatrader-server-manager-api) covers what they do and who can get them.

## Explore the MT4 API

- [Authentication](https://fxsocket.com/docs/mt4/authentication.md): API keys: header or query parameter.
- [Managing Accounts](https://fxsocket.com/docs/mt4/accounts.md): Link, list, and remove accounts via /v1.
- [Account Data](https://fxsocket.com/docs/mt4/account-data.md): Balances, positions, history, and connection status.
- [Market Data](https://fxsocket.com/docs/mt4/market-data.md): Symbols, quotes, price history, and contract specs.
- [Trading](https://fxsocket.com/docs/mt4/trading.md): Place, modify, and close orders.
- [WebSocket Streams](https://fxsocket.com/docs/mt4/websocket.md): Live ticks, bars, positions, and trades.
- [Errors & Rate Limits](https://fxsocket.com/docs/mt4/errors.md): Error envelope, status codes, and rate limits.

## SDKs & Generated Clients

The official FxSocket SDKs wrap this API (account management, trading, market data, and live WebSocket streams) with typed models for MT4 and MT5:

- Python: `pip install fxsocket`, sync and async clients. [PyPI](https://pypi.org/project/fxsocket/) · [GitHub](https://github.com/fxsocket-com/FxSocket-Python-SDK)
- Node.js / TypeScript: `npm install @fxsocket/sdk`, ESM and CommonJS. [npm](https://www.npmjs.com/package/@fxsocket/sdk) · [GitHub](https://github.com/fxsocket-com/FxSocket-Node-SDK)

Using another language? The API is plain HTTP and JSON, and every account serves its own live OpenAPI 3 spec, so you can generate a typed client for any language instead:

- Swagger UI: `https://api.fxsocket.com/mt4/{account_id}/swagger-ui/`: browsable, per-account interactive docs (keep the trailing slash). No API key needed.
- Raw spec: `https://api.fxsocket.com/mt4/{account_id}/api-doc/openapi.json`: point any OpenAPI generator (openapi-generator, openapi-typescript, oapi-codegen, ...) at this URL.

These two routes (plus `/`) are the only ones served without an API key.
