This repository provides everything needed to run a portable MetaTrader 5 (MT5) inside a Docker container and expose its full functionality to your apps via a WebSocket stream and a secure, general-purpose RPC API Gateway.
- Runs a portable MT5 terminal inside a Windows container.
- Streams live account information (balance, equity, open trades, etc.) from the MT5 instance using
streamer.pyto a central WebSocket Hub. - Exposes a secure RPC (Remote Procedure Call) API Gateway (
api_gateway.py) that allows you to execute almost any function from the PythonMetaTrader5library remotely, protected by an API key. - Intended audience: Developers and algorithmic traders who want full, programmatic access to MT5 data and trading functions without running the terminal on their local machine.
- Build the image locally:
docker build -t immahdi/mt5-python:latest . - Or pull a prebuilt image from Docker Hub:
docker pull immahdi/mt5-python:latest - Run a central WebSocket Hub on a server (see
websocket_hub.py). - Run one or more MT5 containers that connect to the Hub and the API Gateway.
- Use a client to connect to the WebSocket Hub for live data, and send authenticated requests to the
/rpcendpoint on the MT5 container to execute any command.
Docker Hub: DockerHub/mt5-python
- Dockerfile — builds the Windows container image with portable MT5 and the Python services.
- src/streamer.py — The inside-container service that reads MT5 account state and open trades and forwards JSON messages to the WebSocket Hub.
- src/api_gateway.py — Exposes a secure, general-purpose RPC API on port
8080. It listens for requests at the/rpcendpoint and executesMetaTrader5functions dynamically. - src/start.ps1 — The PowerShell startup script used inside the container to launch both the
streamerandapi_gatewayservices. - websocket_hub/websocket_hub.py — The central WebSocket Hub/Router. It accepts connections from multiple streamers and viewers and broadcasts data. Run this on the machine you want to host the hub.
- tests/test_api_connection.py — An integration test script to verify that the API Gateway is running correctly and responding to requests.
- meta.zip — (large) The portable MetaTrader 5 files. Not checked in by default. You must download this file and place it in the repo root before building the image locally.
- python-3.11.4-amd64.exe — The Python installer used to set up the environment inside the container.
Note:
meta.zipyou should download the file and place it at the project root before building locally.
Download meta.zip (place in repo root):
git clone https://github.com/im-mahdi-74/Dockerized-MetaTrader5-with-Python.git
cd Dockerized-MetaTrader5-with-Python
python -m pip install --user websockets
python websocket_hub/websocket_hub.pyThe Hub listens on ws://0.0.0.0:8765 by default.
If you prefer to build the image yourself (you must have meta.zip and the python installer at the repository root):
# from repository root where Dockerfile is located
docker build -t immahdi/mt5-python:latest .Or pull the prebuilt image from Docker Hub:
docker pull immahdi/mt5-python:latestdocker run -d --name my-mt5-bot \
-p 8080:8080 \
-e MT5_ACCOUNT="YOUR_ACCOUNT_NUMBER" \
-e MT5_PASSWORD="YOUR_ACCOUNT_PASSWORD" \
-e MT5_SERVER="YOUR_MT5_SERVER_NAME" \
-e WEBSOCKET_URI="ws://<hub-server-ip>:8765" \
-e API_KEY="YOUR_SUPER_SECRET_KEY" \
immahdi/mt5-python:latest- Set
WEBSOCKET_URIto the Hub address. - Set a unique and secret
API_KEYwhich will be used to authenticate your RPC requests.
Run the Python snippet provided in the original README.md to connect to the WebSocket Hub and see the live data stream.
You can execute almost any MT5 function by sending a POST request to the /rpc endpoint.
docker run -d -p 8080:8080 your-dockerhub-username/mt5-python-bridge
curl -X POST http://<docker-host-ip>:8080/rpc \
-H "Content-Type: application/json" \
-H "X-API-KEY: YOUR_SUPER_SECRET_KEY" \
-d '{
"function_name": "account_info"
}'curl -X POST http://<docker-host-ip>:8080/rpc \
-H "Content-Type: application/json" \
-H "X-API-KEY: YOUR_SUPER_SECRET_KEY" \
-d '{
"function_name": "order_send",
"kwargs": {
"request": {
"action": 1,
"symbol": "EURUSD",
"volume": 0.01,
"type": 0,
"price": 0,
"magic": 123456,
"comment": "Sent via API Gateway",
"type_filling": 1,
"type_time": 0
}
}
}'curl -X POST http://<docker-host-ip>:8080/rpc \
-H "Content-Type: application/json" \
-H "X-API-KEY: YOUR_SUPER_SECRET_KEY" \
-d '{
"function_name": "history_deals_get",
"args": [1757000000, 1759600000]
}'The response is a JSON object with status, function_name, and a data field containing the formatted result from MetaTrader 5.
- WebSocket Hub:
8765(TCP) - RPC API Gateway:
8080(HTTP)
Ensure firewall rules allow traffic on these ports between your components.
- Minimum: 2 GB RAM, 1 vCPU
- Recommended: 3 GB RAM, 2 vCPU
If you have issues or want to contribute, please reach out:
- Telegram:
@immahdi74 - LinkedIn: https://www.linkedin.com/in/immahdi74
- Email: mahdi.mosavi.nsa@gmail.com
Contributions are welcome — open a PR or an issue.
This project is released under the MIT License.