Live MT4 quotes with WebSockets
Connect. Subscribe. Read ticks. Stop and disconnect.
Step-by-step transcript
- 0:00
Live MT4 quotes with WebSockets
This tutorial shows how to stream live prices from MetaTrader four using WebSockets. We will connect a demo account, subscribe to a symbol, read real quote messages, and finish by stopping the stream and disconnecting.
- 0:17
Open the API console
Open M T four dot M R P C dot pro. The console opens automatically. Choose Connect Ex from the connection methods. This walkthrough only reads quotes. It does not open or close any trades.
- 0:34
Connect your demo account
Use Trial or your own API key. Enter your dedicated demo account login, trading password, and exact broker server name. Leave the optional id field blank for this new connection, then click Execute Request. Keep your credentials private.
- 0:53
Keep the terminal id
For Connect Ex, check HTTP Status Code two hundred, then check the JSON response. If error is present, read its details before continuing. On success, data contains terminal Instance Guid. The console saves that terminal id for subsequent requests, and shows that the terminal is connected.
- 1:16
Choose one symbol
Under Subscription Service, select On Symbol Tick. Keep the session id from your connection. In symbol Names, enter the exact broker symbol, here Euro U S dollar. Symbol names can differ between brokers, including suffixes. Click Connect Stream to start receiving updates.
- 1:37
Watch live quotes arrive
The connected badge shows the WebSocket is open. The live quote card updates as ticks arrive. The received frames are the actual server messages. This is a continuous stream, not repeated HTTP requests. Inspect each message for error before reading data dot symbol Tick.
- 1:58
Read bid and ask
Inside symbol Tick, symbol identifies the instrument. Bid is the quoted sell price, and ask is the quoted buy price. Their difference is the spread. These are quotes, not confirmations of trade execution. Last and volume can be zero when the broker does not supply a last-deal value for this instrument.
- 2:20
Use the UTC timestamp
For consistent timestamps across systems, use time U T C, or time M S C U T C for milliseconds. In this capture, the broker-time field is two hours ahead of the explicit U T C field. Do not treat the broker-time field as U T C just because its serialized text ends with Z. The frame receipt time is separate.
- 2:44
The matching JavaScript
The matching JavaScript uses the secure WebSocket address and the same symbol. Browser WebSockets cannot set custom authorization headers, so this example passes the API key and terminal id in the query. Keep the complete URL private. Parse each message, handle error first, then read the tick and its U T C timestamp.
- 3:09
Stop the subscription
When finished, click Disconnect inside the stream panel. In code, close the WebSocket. This ends the subscription, but it is a separate action from disconnecting the terminal. A quiet stream alone does not mean failure: first check the connection state, the symbol, and whether the broker is publishing quotes.
- 3:31
Disconnect the terminal
Now select the Disconnect method and execute it with the same terminal id. Check HTTP Status Code two hundred and the response error field. Our successful response includes the terminal identifier and its lifetime, and the header changes to disconnected. Your quote subscription and terminal session are now both finished.