Skip to main content

REST and WebSocket API project for MetaTrader 5

Project description

open-api-mt5

MetaTrader 5 API service with FastAPI.

Important limitation

This API controls a single MT5 terminal/session instance per running service process.

  • A single API instance can be connected to only one account at a time.
  • /account/connect switches that single active session.
  • If you run multiple API services, use separate MT5 terminal instances/data folders for reliable isolation.

Setup

  1. Create a virtual environment:
    • Windows PowerShell: python -m venv .venv
  2. Activate it:
    • .\.venv\Scripts\Activate.ps1
  3. Install dependencies:
    • python -m pip install -U pip
    • pip install -e .

MT5 startup config

The API initializes MetaTrader 5 when FastAPI starts and closes it when FastAPI stops. It also checks MT5 connection every 5 seconds and tries to reconnect automatically if disconnected.

Set these environment variables in PowerShell before running:

$env:MT5_PATH = "C:\Program Files\MetaTrader 5\terminal64.exe"
$env:MT5_LOGIN = "12345678"
$env:MT5_PASSWORD = "your-password"
$env:MT5_SERVER = "YourBroker-Server"

MT5_PATH is optional if MT5 is already discoverable, but setting it is recommended.

Run

open-api-mt5

Optional flags:

open-api-mt5 --port 9000
open-api-mt5 --host 0.0.0.0 --port 8000
open-api-mt5 --reload

Default port is 8000.

API docs:

  • Swagger UI: http://127.0.0.1:8000/docs
  • WebSocket docs in Swagger:
    • GET /ws/positions/open/docs

Health endpoint:

  • GET http://127.0.0.1:8000/health

Account connection endpoints:

  • POST http://127.0.0.1:8000/account/connect
    • Body: username, password, server, optional path
    • Example body:
      {
        "username": "12345678",
        "password": "your-password",
        "server": "YourBroker-Server",
        "path": "C:\\Program Files\\MetaTrader 5\\terminal64.exe"
      }
      
  • POST http://127.0.0.1:8000/account/disconnect

Trade history endpoint:

  • GET http://127.0.0.1:8000/trades/history
  • Optional query params: fromDate, toDate (ISO datetime, UTC recommended)
  • If omitted, it returns the last 7 days by default

Calendar events endpoint:

  • GET http://127.0.0.1:8000/calendar/events
  • Optional query params: fromDate, toDate (ISO datetime), country (example: US), currency (example: USD)
  • Default range when omitted: last 7 days to next 7 days

WebSocket streams

Open positions stream:

  • ws://127.0.0.1:8000/ws/positions/open
  • Optional query param: intervalSeconds (poll interval, bounded to 0.2..60)
  • Events:
    • subscribed
    • positionsSnapshot
    • error
  • positionsSnapshot includes:
    • positions[].pnl (position PnL, sourced from MT5 profit)
    • totalPnl (sum of all open positions PnL)

Example JavaScript client:

const ws = new WebSocket("ws://127.0.0.1:8000/ws/positions/open?intervalSeconds=1");
ws.onmessage = (event) => {
  const payload = JSON.parse(event.data);
  console.log(payload.event, payload);
};

Build and publish package

  1. Build distribution files:
python -m pip install --upgrade build twine
python -m build
  1. Upload to PyPI:
python -m twine upload dist/*
  1. Install from PyPI and run:
pip install open-api-mt5
open-api-mt5 --port 8000

Optional: standalone executable (no Python required on target machine)

If you want users to run it without installing Python, build an executable:

python -m pip install pyinstaller
pyinstaller --onefile --name open-api-mt5 app/cli.py

The executable will be in dist/open-api-mt5.exe.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

open_api_mt5-0.3.0.tar.gz (14.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

open_api_mt5-0.3.0-py3-none-any.whl (17.3 kB view details)

Uploaded Python 3

File details

Details for the file open_api_mt5-0.3.0.tar.gz.

File metadata

  • Download URL: open_api_mt5-0.3.0.tar.gz
  • Upload date:
  • Size: 14.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for open_api_mt5-0.3.0.tar.gz
Algorithm Hash digest
SHA256 866f7599e02fb34b83a075036b029c75d5111a66c10f233148f7d14320845d10
MD5 f1d6361f81909db80f9bf1e7dc4bd761
BLAKE2b-256 40f53970305b55f663c3c5f182129e99f3b420bc00b0643394fffb2ad7366495

See more details on using hashes here.

File details

Details for the file open_api_mt5-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: open_api_mt5-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 17.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for open_api_mt5-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f57e6bdcaadbd32c14c5be7c70f5530680e9ebb92a31c16d343fd13a5d488eb3
MD5 fa650942fb5eaa757874702b29ae90ec
BLAKE2b-256 875b0c304b0db9b1ae3258fb56ef144617ada59b39cfb63dc56de57b09c9951b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page