mt5linux

repository·master·Indexed 41 days ago

https://github.com/lucas-campagna/mt5linux

A Python package that enables running MetaTrader 5 on Linux by leveraging Wine, RPyC, and a standalone mt5server.exe binary. It acts as a bridge allowing native Linux Python environments to communicate with a Windows-based MetaTrader 5 instance, supporting deployment via Docker containers, udocker, or standalone Wine processes. Version 1.1.1.

Tokens
14.2K
Snippets
49
Records
54
Agent score
53%

What's inside mt5linux

  1. Understand when to use mt5linux vs. a manual Wine setup

    master

    Deciding whether to use mt5linux depends on your existing Python environment and trading architecture:

    When you DO NOT need mt5linux

    If your goal is simply to run Python and MetaTrader 5 on a Linux machine to execute trades, you can use a manual Wine setup. In this scenario, you install both Python for Windows and MetaTrader 5 on the same virtual drive in Wine, and install the Metatrader5 library directly into that Python for Windows instance. This allows the Windows-based Python to control MT5 directly.

    When you SHOULD use mt5linux

    Use mt5linux if you have a native Python for Linux environment (e.g., containing heavy machine learning libraries like TensorFlow or Anaconda) that needs to interact with MetaTrader 5.

    Because the Metatrader5 library is only compatible with Python for Windows, it cannot be used directly by your native Linux Python. mt5linux acts as a bridge, allowing your native Linux Python instance to communicate with a Python for Windows instance running in Wine that controls MT5.

  2. Install udocker as a fallback for mt5linux

    master

    If Docker is not available, you can install udocker to allow mt5linux to run containers without Docker daemon privileges.

    curl https://raw.githubusercontent.com/indigo-dc/udocker/master/udocker.py -o udocker
    chmod +x udocker
    sudo mv udocker /usr/local/bin/
  3. Install mt5linux on Linux

    master

    To use mt5linux, you must first install Wine on your Linux system. Then, install the Python package via pip and download the required mt5server.exe binary from the official releases page.

    # 1. Install Wine (system dependent)
    # 2. Install the package
    pip install mt5linux
    # 3. Download mt5server.exe from https://github.com/lucas-campagna/mt5linux/releases
  4. Use Library-Managed Docker Containers

    master

    The recommended way to run MetaTrader 5 via Docker is to let the library manage the container lifecycle. When you initialize the MetaTrader5 object, the library automatically starts the container. The container is automatically stopped when the mt5 object is deleted.

    from mt5linux import MetaTrader5
    
    mt5 = MetaTrader5(
        host="localhost",
        mt5_login=12345678,
        mt5_password="your_password",
        mt5_server="Broker-Server",
        engine="auto",        # 'auto', 'docker', or 'udocker'
        image_tag="latest",   # Docker image tag
        ui_port=8080,         # noVNC http port (auto-selected if None)
        ui_host="localhost",  # noVNC host for the UI URL
        ui_password=None,     # noVNC password
        vnc_port=5901,        # VNC port
    )
    mt5.initialize()
    mt5.terminal_info()
    mt5.shutdown()
  5. Use Manual Docker Setup

    master

    If you prefer to manage your own Docker environment, run your containers manually using docker compose up -d. Once the containers are running, connect to them by specifying the host and port in the MetaTrader5 constructor.

    docker compose up -d
    from mt5linux import MetaTrader5
    
    # Connect to the manually started container
    mt5 = MetaTrader5(host="localhost", port=18812)
    mt5.initialize()
    mt5.terminal_info()
    mt5.shutdown()
  6. Use Standalone Server (Linux with Wine)

    master

    You can run the mt5server.exe binary directly using Wine. This method requires you to manually start the server before connecting via Python.

    # Start the server (default port is 18812)
    wine mt5server.exe [-p/--port <port>]
    
    # View help
    wine mt5server.exe --help
    from mt5linux import MetaTrader5
    
    # Option A: Explicit connection
    mt5 = MetaTrader5()
    mt5.initialize(server="<server_ip>", login=<login_id>, password="<password>")
    
    # Option B: Auto-search for the server
    mt5 = MetaTrader5()
    mt5.initialize()
    
    mt5.terminal_info()
    mt5.shutdown()
  7. Run mt5linux using Docker

    master

    You can run mt5linux in an isolated environment with Wine and all dependencies pre-configured using Docker. There are two main approaches: using a pre-built image or building it locally.

    Copy the docker-compose.yml file locally and run:

    docker compose up -d

    Alternatively, run the image directly with docker run. Note that the latest tag is pre-MT5 installation; use the mt5-installed tag to use an image that already contains the MT5 installation.

    Option 2: Build locally

    Build the image from the docker/ directory and run it:

    docker build -t mt5linux docker/
    docker run -d --name mt5linux -p 8080:8080 -p 18812:18812 ... mt5linux
  8. Enable MT5 Autologin in Docker

    master

    To automatically log in to an MT5 account when the container starts, provide the following credentials as environment variables:

    • MT5_LOGIN (Account number)
    • MT5_PASSWORD (Account password)
    • MT5_SERVER (Broker server name)

    If these are not provided, the MT5 terminal will start without logging in, and you will need to connect manually via the Python API.

    MT5_HOST=0.0.0.0
    VNC_PASSWORD=your_secure_password
    MT5_LOGIN=12345678
    MT5_PASSWORD=your_password
    MT5_SERVER=Broker-Server
    NOVNC_HOST=localhost
  9. Connect to mt5linux from Linux Python

    master

    Once the Docker container is running, you can interact with the MT5 instance from a Linux Python environment using the mt5linux library. You must specify the host and the MT5 server port (default 18812).

    from mt5linux import MetaTrader5
    
    mt5 = MetaTrader5(host="localhost", port=18812)
    mt5.initialize()
    mt5.terminal_info()
    mt5.shutdown()
  10. Configure the container engine (Docker vs udocker)

    master

    The mt5linux Python library can use different engines to manage containers. By default, it uses auto mode, which attempts to use Docker and falls back to udocker if Docker is unavailable.

    You can explicitly specify the engine when initializing the MetaTrader5 object:

    • engine="docker": Force the use of Docker.
    • engine="udocker": Force the use of udocker.
    • engine="auto": Automatically detect (default).
    from mt5linux import MetaTrader5
    
    mt5 = MetaTrader5(engine="docker")   # Force Docker
    mt5 = MetaTrader5(engine="udocker")  # Force udocker
    mt5 = MetaTrader5(engine="auto")     # Auto-detect (default)
  11. Access Container Properties and Management

    master

    When using library-managed containers, the MetaTrader5 instance provides access to a container object. This object allows you to inspect the container's state and manually control its lifecycle.

    mt5 = MetaTrader5(mt5_login=12345678, mt5_password="password")
    
    print(mt5.container.name)        # Container name
    print(mt5.container.status())    # 'running', 'exited', etc.
    print(mt5.container.ui_port)     # noVNC port
    print(mt5.container.is_running()) # True/False
    
    mt5.container.stop()             # Stop container
    mt5.container.start()           # Start container
    mt5.container.remove()          # Remove container
  12. Configure mt5linux via Docker Compose

    master

    You can deploy the mt5linux service using Docker Compose. The configuration uses the lprett/mt5linux:latest image and exposes three specific ports to allow access via noVNC and the mt5linux web interface.

    By default, the following port mappings are used:

    • 8080: noVNC page port
    • 5901: noVNC port
    • 18812: mt5linux page port
    services:
      mt5linux:
        image: lprett/mt5linux:latest
        container_name: mt5linux
        restart: unless-stopped
        ports:
          - 8080:8080   # noVNC page port
          - 5901:5901   # noVNC port
          - 18812:18812 # mt5linux page port