immahdi/mt5-python

By immahdi

Updated 10 months ago

portable MetaTrader 5 in Docker with a secure RPC API Gateway and real-time WebSocket data streaming

Image
API management
Developer tools
3

9.9K

immahdi/mt5-python repository overview

Dockerized MetaTrader 5 with Python Data Bridge

This Docker image provides a ready-to-use environment to run a portable MetaTrader 5 (MT5) terminal inside a Windows Server Core container, combined with Python services for real-time account streaming and a secure, general-purpose RPC API Gateway.

It is designed for developers, data scientists, and algorithmic traders who want full programmatic access to all MT5 functions—from data retrieval to trade execution—without installing the terminal locally.

📌 For full deployment instructions, detailed usage examples, and source code, please see the complete GitHub repository: Dockerized-MetaTrader5-with-Python-DataBridge


Key Features

  • Portable MT5: A full MetaTrader 5 terminal runs inside the container.
  • Real-time Data Stream: streamer.py continuously streams live account data (balance, equity, open trades, etc.) to a WebSocket hub.
  • Secure RPC API Gateway: api_gateway.py provides a powerful and secure endpoint to execute almost any function from the MetaTrader5 library remotely, protected by an API key.
  • Automated & Headless: No GUI required; runs in the background as a container.
  • Lightweight Hardware Requirements: Runs with as little as 2GB RAM and 1 CPU core. Recommended: 3GB RAM and 2 cores for best performance.

How to Run

Pull and start the container with your MT5 credentials, WebSocket hub address, and a secret API key for the RPC Gateway:

docker 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://your-websocket-server-ip:8765" \
  -e API_KEY="YOUR_SUPER_SECRET_KEY" \
  immahdi/mt5-python:latest

Services Available

1. Full MT5 Control (RPC API)

Access the full power of the MetaTrader5 library through a secure RPC endpoint on port 8080. You can call functions by sending a JSON payload to the /rpc endpoint.


🔹 Example: Get Account Info
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"
}'

🔹 Example: Place a Trade (Order Send)
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
        }
    }
}'

The API intelligently formats the response into a clean, human-readable JSON object.

2. Real-time Account Stream (WebSocket)

The container streams live account and open trade data to the WebSocket server you specify via WEBSOCKET_URI.

Example Client:

import asyncio, websockets, json

WEBSOCKET_URI = "ws://<hub-server-ip>:8765"

async def view_stream():
    async with websockets.connect(WEBSOCKET_URI) as websocket:
        await websocket.send(json.dumps({"type": "viewer_hello"}))
        print("Connected as viewer. Receiving live data...")
        async for message in websocket:
            print(json.dumps(json.loads(message), indent=2))

asyncio.run(view_stream())

More Information

  • 📖 Full documentation and deployment guide are available on GitHub.
  • 📩 For questions, contributions, or issues, feel free to reach out via email, Telegram @immahdi74, or LinkedIn.

Tag summary

Content type

Image

Digest

sha256:d47927174

Size

2.4 GB

Last updated

10 months ago

docker pull immahdi/mt5-python