A sophisticated automated trading system that leverages MetaTrader 5 (MT5) as a high-performance trading server with Python as the intelligent client for strategy execution and analysis.
This repository is dedicated to automating a comprehensive forex trading strategy by establishing a robust communication bridge between MetaTrader 5 and Python. The system architecture utilizes a server-client model where MT5 handles order execution and market data, while Python notebooks manage strategy logic, analysis, and decision-making.
┌─────────────────────────────────────────┐
│ MetaTrader 5 (MT5) │
│ ├─ Server (via ZeroMQ) │
│ ├─ Order Execution │
│ └─ Real-time Market Data │
└────────────┬────────────────────────────┘
│
│ ZeroMQ (pyzmq)
│
┌────────────▼────────────────────────────┐
│ Python Environment │
│ ├─ Client (via pyzmq) │
│ ├─ Strategy Logic │
│ ├─ Data Analysis │
│ └─ Risk Management │
└─────────────────────────────────────────┘
- MetaTrader 5 (MT5): Trading platform and server, installed via Bottles (Flatpak) on Linux
- Python 3.10+: Core programming language for strategy implementation
- pyzmq: Python bindings for ZeroMQ messaging protocol
- ZeroMQ (0MQ): High-performance asynchronous messaging library for inter-process communication
- Jupyter Notebook: Interactive development and backtesting environment
- MetaTrader 5 installed via Bottles (Flatpak)
- Python 3.10+ with a virtual environment manager
- Linux system (tested on current setup)
-
Clone the repository (if applicable):
git clone git@github.com:Erickpython/forex.git cd forex -
Create and activate a virtual environment:
python3 -m venv forexvenv source forexvenv/bin/activate -
Install required Python packages:
pip install pyzmq jupyter pandas numpy
-
Configure MT5 Server:
- Launch MT5 through Bottles
- Set up ZeroMQ server connection on a designated port (default: 5555)
- Ensure MT5 expert advisor or script is listening for incoming connections
-
Run the Python client:
jupyter notebook
Open
forex.ipynbto start the trading client.
- MT5 Server: Initializes a ZeroMQ server socket on a specified port, ready to receive client requests
- Python Client: Connects to the MT5 server via pyzmq
- Strategy Execution: Python sends trading signals and receives market data in real-time
- Order Placement: MT5 executes orders and returns confirmation/status updates
The system uses ZeroMQ's REQ-REP (Request-Reply) pattern for synchronous communication:
- Python sends trading requests (buy/sell orders, data queries)
- MT5 processes requests and replies with results or confirmations
forex/
├── README.md # This file
├── forex.ipynb # Main trading strategy notebook
├── forexvenv/ # Python virtual environment
├── zeromq_training-examples/
│ ├── client.ipynb # ZeroMQ client examples
│ └── server.ipynb # ZeroMQ server examples
- ✅ Real-time market data streaming from MT5
- ✅ Automated order execution and management
- ✅ Python-based strategy development
- ✅ Asynchronous client-server communication
- ✅ Cross-platform compatibility (Linux, via MT5 on Bottles)
import zmq
# Connect to MT5 server
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:5555")
# Send a trading request
socket.send_string("BUY EURUSD 1.0")
# Receive response
response = socket.recv_string()
print(f"Server response: {response}")Update connection parameters in your Python notebooks:
- Server Host: Localhost (127.0.0.1) by default
- Server Port: 5555 (configurable)
- Timeout: Set appropriate timeouts for order execution
- Database integration for trade history logging
- Advanced backtesting framework
- Machine learning strategy optimization
- WebSocket support for additional data sources
- Docker containerization for deployment
- Verify MT5 server is running and accessible on the configured port
- Check firewall settings allowing ZeroMQ communication
- Confirm pyzmq is properly installed:
pip list | grep zmq
- Monitor CPU and memory usage during live trading
- Adjust message frequency and timeout settings
- Consider using asynchronous patterns (DEALER-ROUTER) for higher throughput
[OPEN SOURCE]
This project is provided for educational purposes. Trading forex involves substantial risk of loss. Past performance does not guarantee future results. Always conduct your own research and consult with financial advisors before engaging in live trading.
Last Updated: January 2026