Skip to content

Erickpython/forex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Automated Forex Trading Strategy

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.

Overview

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.

Architecture

┌─────────────────────────────────────────┐
│   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                    │
└─────────────────────────────────────────┘

Technology Stack

  • 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

Setup & Installation

Prerequisites

  • MetaTrader 5 installed via Bottles (Flatpak)
  • Python 3.10+ with a virtual environment manager
  • Linux system (tested on current setup)

Installation Steps

  1. Clone the repository (if applicable):

    git clone git@github.com:Erickpython/forex.git
    cd forex
  2. Create and activate a virtual environment:

    python3 -m venv forexvenv
    source forexvenv/bin/activate
  3. Install required Python packages:

    pip install pyzmq jupyter pandas numpy
  4. 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
  5. Run the Python client:

    jupyter notebook

    Open forex.ipynb to start the trading client.

How It Works

Communication Flow

  1. MT5 Server: Initializes a ZeroMQ server socket on a specified port, ready to receive client requests
  2. Python Client: Connects to the MT5 server via pyzmq
  3. Strategy Execution: Python sends trading signals and receives market data in real-time
  4. Order Placement: MT5 executes orders and returns confirmation/status updates

Request-Reply Pattern

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

Project Structure

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

Features

  • ✅ 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)

Usage

Basic Client Example

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}")

Configuration

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

Important Notes

⚠️ Risk Management: This is an automated trading system. Ensure proper risk management and stop-loss configurations are in place before running with real funds.

⚠️ Testing: Always thoroughly backtest and paper trade strategies before deploying with real capital.

⚠️ System Requirements: Ensure stable internet connection and sufficient system resources for continuous operation.

Future Enhancements

  • Database integration for trade history logging
  • Advanced backtesting framework
  • Machine learning strategy optimization
  • WebSocket support for additional data sources
  • Docker containerization for deployment

Troubleshooting

Connection Issues

  • 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

Performance Issues

  • Monitor CPU and memory usage during live trading
  • Adjust message frequency and timeout settings
  • Consider using asynchronous patterns (DEALER-ROUTER) for higher throughput

Resources

License

[OPEN SOURCE]

Disclaimer

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

About

MT5 {bottles} + Python

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors