CLIENT API

MT4 Client API

Automate MetaTrader 4 terminals with low-latency order routing, account monitoring, and real-time streaming.

Auth model Bearer + session token
Primary use Retail account automation
Latency profile Optimized for cloud execution

Bridge Session

Open a single MT4 bridge session, then reuse it for account reads, trade tasks, and market subscriptions.

Trading Surface

Cover market orders, pending orders, close operations, and on-the-fly SL/TP adjustments from one API surface.

Monitoring Layer

Pair REST reads with WebSocket updates for equity, margin, open orders, and quote telemetry.

Quick Start

Create an API key in the client area, open a terminal bridge, and exchange a short-lived session token before placing any request.

The MT4 client flow is designed around predictable task-based execution so your integrations can queue and reconcile orders deterministically.

  • Create project and environment credentials.
  • Open broker session with account number, password, and server.
  • Persist the returned session id for subsequent trading calls.
  • Subscribe to quotes and account telemetry over WebSocket.
BASH
curl -X POST https://api.metatraderapi.xyz/mt4/client/session/open \
  -H "Authorization: Bearer <api-key>" \
  -H "Content-Type: application/json" \
  -d '{
    "accountId": "demo-mt4-01",
    "server": "Broker-Demo",
    "login": 120045,
    "password": "<terminal-password>"
  }'

Session Lifecycle

Every MT4 bridge session exposes broker state, margin data, positions, open orders, and trade history in a single channel.

For resilient infrastructure, pair periodic REST polling with WebSocket subscriptions for balance, equity, and order status updates.

  • POST /session/open
  • GET /session/{sessionId}
  • POST /session/{sessionId}/heartbeat
  • DELETE /session/{sessionId}

Trading Operations

The MT4 Client API supports market execution, pending orders, position closure, stop-loss and take-profit updates, and task cancellation.

Each call returns a task identifier so you can reconcile asynchronous broker execution in your own control plane.

JSON
{
  "symbol": "EURUSD",
  "type": "BUY",
  "volume": 0.2,
  "sl": 1.0735,
  "tp": 1.0790,
  "comment": "alpha-engine"
}

Streaming & Monitoring

Use quote, depth, account, order, and on-profit channels to drive dashboards, risk monitors, and alerting pipelines.

For high-throughput strategies, batch your subscription sets by symbol universe and only retain the channels you actually consume.

  • Quotes and spreads
  • Open trade P/L snapshots
  • Equity and margin updates
  • Order execution acknowledgements

Authentication & Headers

Use your workspace API key to open a bridge session, then propagate the returned session id on every trading or read request.

For retry-safe workflows, send your own request id so order tasks and reconciliation jobs stay idempotent under network failures.

HeaderValueUseDescription
AuthorizationBearer <api-key>RequiredWorkspace credential created in the client area.
x-session-idsess_mt4_demo_01RequiredBridge session returned by the session bootstrap flow.
x-request-idreq_order_0192RecommendedStable id used for retries and external reconciliation.
Content-Typeapplication/jsonWrite callsJSON payload content type for create / update requests.

Endpoint Matrix

Connection & Sessions

MethodPathPurpose
POST/session/openStart a broker-connected MT4 bridge session.
GET/session/{sessionId}Inspect session health, account state, and broker metadata.
POST/session/{sessionId}/heartbeatKeep long-running bridge sessions alive.
DELETE/session/{sessionId}Release server resources and revoke the session.

Account & Market Data

MethodPathPurpose
GET/accountRead balance, equity, free margin, and currency data.
GET/positionsList open positions and pending orders.
GET/symbolsReturn enabled trading symbols for the account.
GET/quotes/historyFetch OHLC bars for analytics or charting.

Trading & Events

MethodPathPurpose
POST/ordersCreate market or pending trade tasks.
PATCH/orders/{taskId}Modify open task parameters before execution.
POST/positions/{ticket}/closeClose an open position or reduce exposure.
WS/eventsReceive quotes, profit, and order execution events in real time.

Errors & Limits

Write-side requests should be treated as task submissions. A queued response does not guarantee broker execution, so always reconcile by task id or execution event.

Production workloads should combine request ids, adaptive retry backoff, and circuit breaking around broker connectivity incidents.

CodeNameMeaning
400ValidationErrorPayload, symbol, or trade parameters failed validation.
401UnauthorizedAPI key or session token is missing, expired, or revoked.
409TaskConflictThe same external request id was replayed with different payload data.
429RateLimitedWorkspace or account throughput limits were exceeded.
502BridgeUnavailableThe bridge lost connectivity to the terminal or broker server.

Deployment & Runtime

Run the bridge close to your trading infrastructure to reduce round-trip time between order creation and broker execution.

Split quote streaming, trading, and reporting jobs into separate workers so a burst in one pipeline does not delay another.

  • Keep a single session per account and environment whenever possible.
  • Persist task ids together with your strategy event ids.
  • Alert on stale quote streams, heartbeat failures, and margin drawdown thresholds.
  • Terminate idle sessions to recover bridge capacity.