OpenMT5Gateway
Open-source, zero-DLL MetaTrader 5 bridge for macOS, Linux, and Windows.
Run Python trading bots against MetaTrader 5 natively on macOS (Apple Silicon / Intel), Linux, Docker, or Windows — without MetaQuotes' Windows-only Python wheel, without C++
.dllinjection, and without paid cloud proxies.
Why This Exists
- MetaQuotes publishes
pip install MetaTrader5exclusively for Windows — installing it on macOS or Linux fails (No matching distribution found for MetaTrader5). - C++ DLL socket bridges break inside macOS/Linux Wine wrappers.
- MQL5 natively supports outbound client TCP sockets (
SocketCreate,SocketConnect,SocketSend,SocketRead). OpenMT5Gateway flips the direction: Python runs a lightweight, token-authenticated TCP server, and a pure-MQL5 Expert Advisor connects outward from inside MT5.
flowchart LR
PY["Python Trading Bot<br/>(macOS / Linux / Windows)"] <-->|"NDJSON RPC over TCP<br/>127.0.0.1:18812"| EA["OpenMT5Gateway.mq5<br/>(Pure MQL5, Zero DLLs)"]
EA <-->|"Native OrderSend & CopyRates"| MT5["Broker MT5 Server"]
Features
- Zero External Runtime Dependencies: Uses Python stdlib only (
socket,threading,json,hmac,decimal). - Token Authentication: Constant-time
hmac.compare_digestverification (AUTH/AUTH_ACK). - Correlated RPC Multiplexing: Concurrent commands safely matched by
request_id. - Idempotent Execution: Duplicate
external_idorders are blocked automatically. - Automatic Broker Suffix Resolution: Resolves symbols like
EURUSD,EURUSD.a,EURUSDm,EURUSD.raw,EURUSD_pro. - Filling-Mode Fallback in MQL5: Automatically retries
IOC->FOK->RETURN. - Live Telemetry & Reconciliation: Streams balance/equity snapshots and pulls historical closed deals.
Installation & Quickstart
1. Install Python Package
Install directly from PyPI:
pip install open-mt5-gateway
With optional pandas DataFrame support:
pip install "open-mt5-gateway[dataframe]"
Or install editable from a local source checkout:
pip install -e .
2. Test Locally Right Now (Simulated Demo EA)
Run the installed pre-flight diagnostic CLI with the built-in simulated demo EA:
open-mt5-preflight \
--port 18812 \
--token "local-dev-secret" \
--simulate-demo \
--symbols EURUSD GBPUSD XAUUSD
(Or run via module: python -m open_mt5_gateway.cli --port 18812 --token "local-dev-secret" --simulate-demo --symbols EURUSD GBPUSD XAUUSD)
3. Connect Real MetaTrader 5 (macOS / Windows)
- Copy
mql5/OpenMT5Gateway.mq5into your MT5 Data Folder underMQL5/Experts/. - Compile in MetaEditor (
0 errors). - Enable Allow Algorithmic Trading in MT5 (
Tools -> Options -> Expert Advisors). - Start your Python gateway, then attach
OpenMT5Gatewayonto any MT5 chart with matchingInpPortandInpAuthToken.
from decimal import Decimal
from open_mt5_gateway import GatewayConfig, OpenMT5Client, OpenMT5GatewayServer, OrderRequest
with OpenMT5GatewayServer(GatewayConfig(port=18812, auth_token="my-secret")) as gw:
gw.wait_for_connection(timeout=30.0)
client = OpenMT5Client(gw)
acct = client.get_account()
print(acct.login, acct.balance)
res = client.submit_order(
OrderRequest(
external_id="trade-001",
symbol="EURUSD",
side="buy",
volume=Decimal("0.10"),
stop_loss=Decimal("1.08200"),
take_profit=Decimal("1.08900"),
)
)
print(res)
Supported NDJSON RPC Actions
All frames are newline-delimited UTF-8 JSON objects (AUTH, AUTH_ACK, TELEMETRY, COMMAND, RESPONSE).
| Action | Python Method (OpenMT5Client) |
Description |
|---|---|---|
PING |
ping() |
Connectivity heartbeat check between Python and the MQL5 EA. |
GET_ACCOUNT |
get_account() |
Fetches account login, broker server name, balance, equity, and UTC server timestamp. |
GET_SYMBOL_SPEC |
get_symbol_spec(symbol) |
Resolves broker symbol suffixes and returns bid/ask, point, digits, volume limits, and trade_allowed. |
GET_RATES |
get_rates(symbol, timeframe, count) |
Fetches OHLCV bar dicts (time, open, high, low, close, volume). |
ORDER_SEND |
submit_order(request) |
Submits a market order with external_id deduplication and automatic IOC -> FOK -> RETURN fallback. |
ORDER_MODIFY |
modify_position(...) |
Updates stop-loss and take-profit levels on an open broker ticket. |
POSITION_CLOSE_PARTIAL |
close_partial(broker_ticket, volume) |
Partially closes an open position by lot volume. |
CLOSE_ALL |
close_all(reason) |
Closes all open positions matching the configured magic_number. |
GET_CLOSED_DEALS |
get_closed_deals(since_timestamp) |
Fetches closed deal history (profit, commission, swap, prices, timestamps) for reconciliation. |
Development & Testing
git clone https://github.com/basharat78/open-mt5-gateway.git
cd open-mt5-gateway
pip install -e ".[dev]"
pytest -v
License
MIT License — see LICENSE.
Metadata
Release files for open-mt5-gateway 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| open_mt5_gateway-0.1.1.tar.gz | 16.3 kB | Without provenance | Details for open_mt5_gateway-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| open_mt5_gateway-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for open_mt5_gateway-0.1.1-py3-none-any.whl |
Total release size: 29.3 kB
Release files / open_mt5_gateway-0.1.1.tar.gz
| Download URL | open_mt5_gateway-0.1.1.tar.gz |
|---|---|
| Size | 16.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8f3e4d94eea4cfc38f471d7bbd4fd459c096768b90e9ec56cea42756643afaa9
|
|
BLAKE2b-256 checksum How to use checksums |
90adb5a0dadd38756486f01ff414987d3b84860eb3bbec50b088ddced84ac07a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.15
|
Release files / open_mt5_gateway-0.1.1-py3-none-any.whl
| Download URL | open_mt5_gateway-0.1.1-py3-none-any.whl |
|---|---|
| Size | 12.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
afe9731bedc802d88b6a5296b4edaae379fb504a90d5b77f7e3c0b1475307a9d
|
|
BLAKE2b-256 checksum How to use checksums |
f6c65f8b02840dd0d8cdde387760c80ac79c41c5717dacec75ebfc66a6d5c6fd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.15
|
Security for open-mt5-gateway 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.