Run MT5 from Python.
Download. Configure. Connect. Read. Stream. Disconnect.
Download the Python example · ZIP ↓
Step-by-step transcript
- 0:00
Run MT5 from Python.
This tutorial shows how to connect MT5 from Python, read your demo account, receive live quotes, and disconnect cleanly.
- 0:11
Start with the download.
Open M R P C dot pro slash A P I, sign in, and select Python. Click Download example. The website download creates a fresh demo account and runs a complete trading demonstration. It requires your personal API key. The downloaded file can contain that key, so keep the file private.
- 0:34
Choose the right example.
For this tutorial we use the separate Python quotes companion. It accepts an existing demo login and makes no trade requests. TRIAL can be used with an existing demo account when trial capacity is available. Creating a new demo account requires a personal key. These are two different starting points.
- 0:57
Prepare Python.
Use Python three point ten or newer. Create a virtual environment in your project folder. Install requests and websocket client using that environment’s Python executable. Requests sends the REST calls; websocket client receives the live stream. The original website demo uses the standard library; these two packages belong to our streaming companion.
- 1:23
Configure your credentials.
Set the four environment variables shown here. Replace each placeholder with your own value. Use the exact broker demo server name and the trading password for that account. Environment variables keep credentials out of the source file. On macOS or Linux, use export instead of the PowerShell environment syntax.
- 1:46
Connect once. Reuse the ID.
The companion calls Connect E X with the demo login, password, and server. It saves terminal instance GUID in memory. Later REST requests send that value in the id header and send your API key in the APIKey header. Your API key identifies your MetaRPC access; the terminal ID selects this connected account.
- 2:11
Check each response.
First check the HTTP Status Code. For HTTP Status Code 200, parse the JSON and inspect error. If error is not null, stop this step and inspect the error code and message privately. Otherwise read data. This companion logs concise failures without printing connection URLs, because those URLs contain the trading password.
- 2:37
Read the account.
Here is output from the actual Python run. The summary uses account Currency, account Balance, and account Equity from data. These are the broker account’s values, not your MetaRPC service-credit balance. The three following lines are actual quote messages received by the companion, rather than generated example values.
- 3:01
Subscribe to live quotes.
The WebSocket address subscribes to E U R U S D. Python can send APIKey and id as handshake headers. For each JSON message, check error first, then read data dot symbol Tick. Bid is the selling quote and ask is the buying quote. Use time U T C for a timestamp on the UTC timeline.
- 3:25
Stop and disconnect.
The full example stops after three quote messages. Its cleanup first closes the WebSocket, then calls Disconnect with the same terminal ID, even if the stream fails. Closing the socket stops the subscription; disconnecting releases the hosted terminal. The requested five-minute expiry is a fallback, not a replacement for cleanup.
- 3:49
Your first local integration.
Run the companion with your virtual environment’s Python executable. A successful run prints the account summary, three ticks, WebSocket closed, and Terminal disconnected. Keep the error checks and cleanup when adapting it. You can now replace the print statements with your application logic and use your own API key when you are ready.