open-mt5-gateway 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- open_mt5_gateway-0.1.0/LICENSE +21 -0
- open_mt5_gateway-0.1.0/PKG-INFO +128 -0
- open_mt5_gateway-0.1.0/README.md +97 -0
- open_mt5_gateway-0.1.0/pyproject.toml +42 -0
- open_mt5_gateway-0.1.0/setup.cfg +4 -0
- open_mt5_gateway-0.1.0/src/open_mt5_gateway/__init__.py +26 -0
- open_mt5_gateway-0.1.0/src/open_mt5_gateway/cli.py +180 -0
- open_mt5_gateway-0.1.0/src/open_mt5_gateway/gateway.py +569 -0
- open_mt5_gateway-0.1.0/src/open_mt5_gateway.egg-info/PKG-INFO +128 -0
- open_mt5_gateway-0.1.0/src/open_mt5_gateway.egg-info/SOURCES.txt +13 -0
- open_mt5_gateway-0.1.0/src/open_mt5_gateway.egg-info/dependency_links.txt +1 -0
- open_mt5_gateway-0.1.0/src/open_mt5_gateway.egg-info/entry_points.txt +2 -0
- open_mt5_gateway-0.1.0/src/open_mt5_gateway.egg-info/requires.txt +8 -0
- open_mt5_gateway-0.1.0/src/open_mt5_gateway.egg-info/top_level.txt +1 -0
- open_mt5_gateway-0.1.0/tests/test_gateway.py +240 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 OpenMT5Gateway Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: open-mt5-gateway
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Open-source, zero-DLL cross-platform MetaTrader 5 bridge for macOS, Linux, and Windows.
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Project-URL: Homepage, https://github.com/basharat78/open-mt5-gateway
|
|
7
|
+
Project-URL: Repository, https://github.com/basharat78/open-mt5-gateway
|
|
8
|
+
Project-URL: Issues, https://github.com/basharat78/open-mt5-gateway/issues
|
|
9
|
+
Keywords: metatrader5,mt5,macos,algorithmic-trading,forex,mql5
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Financial and Insurance Industry
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Operating System :: MacOS
|
|
14
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
15
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Topic :: Office/Business :: Financial :: Investment
|
|
21
|
+
Requires-Python: >=3.10
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Provides-Extra: dataframe
|
|
25
|
+
Requires-Dist: pandas>=2.0; extra == "dataframe"
|
|
26
|
+
Provides-Extra: dev
|
|
27
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
28
|
+
Requires-Dist: build>=1.0; extra == "dev"
|
|
29
|
+
Requires-Dist: twine>=5.0; extra == "dev"
|
|
30
|
+
Dynamic: license-file
|
|
31
|
+
|
|
32
|
+
# OpenMT5Gateway
|
|
33
|
+
|
|
34
|
+
**Open-source, zero-DLL MetaTrader 5 bridge for macOS, Linux, and Windows.**
|
|
35
|
+
|
|
36
|
+
> 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++ `.dll` injection, and without paid cloud proxies.
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## Why This Exists
|
|
41
|
+
|
|
42
|
+
- MetaQuotes publishes `pip install MetaTrader5` **exclusively for Windows** — installing it on macOS or Linux fails (`No matching distribution found for MetaTrader5`).
|
|
43
|
+
- C++ DLL socket bridges break inside macOS/Linux Wine wrappers.
|
|
44
|
+
- 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.
|
|
45
|
+
|
|
46
|
+
```mermaid
|
|
47
|
+
flowchart LR
|
|
48
|
+
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)"]
|
|
49
|
+
EA <-->|"Native OrderSend & CopyRates"| MT5["Broker MT5 Server"]
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
## Features
|
|
55
|
+
|
|
56
|
+
- **Zero External Runtime Dependencies:** Uses Python stdlib only (`socket`, `threading`, `json`, `hmac`, `decimal`).
|
|
57
|
+
- **Token Authentication:** Constant-time `hmac.compare_digest` verification (`AUTH` / `AUTH_ACK`).
|
|
58
|
+
- **Correlated RPC Multiplexing:** Concurrent commands safely matched by `request_id`.
|
|
59
|
+
- **Idempotent Execution:** Duplicate `external_id` orders are blocked automatically.
|
|
60
|
+
- **Automatic Broker Suffix Resolution:** Resolves symbols like `EURUSD`, `EURUSD.a`, `EURUSDm`, `EURUSD.raw`, `EURUSD_pro`.
|
|
61
|
+
- **Filling-Mode Fallback in MQL5:** Automatically retries `IOC` -> `FOK` -> `RETURN`.
|
|
62
|
+
- **Live Telemetry & Reconciliation:** Streams balance/equity snapshots and pulls historical closed deals.
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
## Installation & Quickstart
|
|
67
|
+
|
|
68
|
+
### 1. Install Python Package
|
|
69
|
+
```bash
|
|
70
|
+
pip install -e .
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### 2. Test Locally Right Now (Simulated Demo EA)
|
|
74
|
+
```bash
|
|
75
|
+
python -m open_mt5_gateway.cli \
|
|
76
|
+
--port 18812 \
|
|
77
|
+
--token "local-dev-secret" \
|
|
78
|
+
--simulate-demo \
|
|
79
|
+
--symbols EURUSD GBPUSD XAUUSD
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### 3. Connect Real MetaTrader 5 (macOS / Windows)
|
|
83
|
+
1. Copy [`mql5/OpenMT5Gateway.mq5`](./mql5/OpenMT5Gateway.mq5) into your MT5 Data Folder under `MQL5/Experts/`.
|
|
84
|
+
2. Compile in MetaEditor (`0 errors`).
|
|
85
|
+
3. Enable **Allow Algorithmic Trading** in MT5 (`Tools -> Options -> Expert Advisors`).
|
|
86
|
+
4. Start your Python gateway, then attach `OpenMT5Gateway` onto any MT5 chart with matching `InpPort` and `InpAuthToken`.
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
from decimal import Decimal
|
|
90
|
+
from open_mt5_gateway import GatewayConfig, OpenMT5Client, OpenMT5GatewayServer, OrderRequest
|
|
91
|
+
|
|
92
|
+
with OpenMT5GatewayServer(GatewayConfig(port=18812, auth_token="my-secret")) as gw:
|
|
93
|
+
gw.wait_for_connection(timeout=30.0)
|
|
94
|
+
client = OpenMT5Client(gw)
|
|
95
|
+
|
|
96
|
+
acct = client.get_account()
|
|
97
|
+
print(acct.login, acct.balance)
|
|
98
|
+
|
|
99
|
+
res = client.submit_order(
|
|
100
|
+
OrderRequest(
|
|
101
|
+
external_id="trade-001",
|
|
102
|
+
symbol="EURUSD",
|
|
103
|
+
side="buy",
|
|
104
|
+
volume=Decimal("0.10"),
|
|
105
|
+
stop_loss=Decimal("1.08200"),
|
|
106
|
+
take_profit=Decimal("1.08900"),
|
|
107
|
+
)
|
|
108
|
+
)
|
|
109
|
+
print(res)
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
---
|
|
113
|
+
|
|
114
|
+
## Publishing to GitHub & PyPI
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
# Push to GitHub
|
|
118
|
+
gh repo create open-mt5-gateway --public --source=. --push
|
|
119
|
+
|
|
120
|
+
# Build & publish to PyPI
|
|
121
|
+
pip install build twine
|
|
122
|
+
python -m build
|
|
123
|
+
twine upload dist/*
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## License
|
|
127
|
+
|
|
128
|
+
MIT License — see [LICENSE](./LICENSE).
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# OpenMT5Gateway
|
|
2
|
+
|
|
3
|
+
**Open-source, zero-DLL MetaTrader 5 bridge for macOS, Linux, and Windows.**
|
|
4
|
+
|
|
5
|
+
> 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++ `.dll` injection, and without paid cloud proxies.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Why This Exists
|
|
10
|
+
|
|
11
|
+
- MetaQuotes publishes `pip install MetaTrader5` **exclusively for Windows** — installing it on macOS or Linux fails (`No matching distribution found for MetaTrader5`).
|
|
12
|
+
- C++ DLL socket bridges break inside macOS/Linux Wine wrappers.
|
|
13
|
+
- 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.
|
|
14
|
+
|
|
15
|
+
```mermaid
|
|
16
|
+
flowchart LR
|
|
17
|
+
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)"]
|
|
18
|
+
EA <-->|"Native OrderSend & CopyRates"| MT5["Broker MT5 Server"]
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Features
|
|
24
|
+
|
|
25
|
+
- **Zero External Runtime Dependencies:** Uses Python stdlib only (`socket`, `threading`, `json`, `hmac`, `decimal`).
|
|
26
|
+
- **Token Authentication:** Constant-time `hmac.compare_digest` verification (`AUTH` / `AUTH_ACK`).
|
|
27
|
+
- **Correlated RPC Multiplexing:** Concurrent commands safely matched by `request_id`.
|
|
28
|
+
- **Idempotent Execution:** Duplicate `external_id` orders are blocked automatically.
|
|
29
|
+
- **Automatic Broker Suffix Resolution:** Resolves symbols like `EURUSD`, `EURUSD.a`, `EURUSDm`, `EURUSD.raw`, `EURUSD_pro`.
|
|
30
|
+
- **Filling-Mode Fallback in MQL5:** Automatically retries `IOC` -> `FOK` -> `RETURN`.
|
|
31
|
+
- **Live Telemetry & Reconciliation:** Streams balance/equity snapshots and pulls historical closed deals.
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## Installation & Quickstart
|
|
36
|
+
|
|
37
|
+
### 1. Install Python Package
|
|
38
|
+
```bash
|
|
39
|
+
pip install -e .
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### 2. Test Locally Right Now (Simulated Demo EA)
|
|
43
|
+
```bash
|
|
44
|
+
python -m open_mt5_gateway.cli \
|
|
45
|
+
--port 18812 \
|
|
46
|
+
--token "local-dev-secret" \
|
|
47
|
+
--simulate-demo \
|
|
48
|
+
--symbols EURUSD GBPUSD XAUUSD
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### 3. Connect Real MetaTrader 5 (macOS / Windows)
|
|
52
|
+
1. Copy [`mql5/OpenMT5Gateway.mq5`](./mql5/OpenMT5Gateway.mq5) into your MT5 Data Folder under `MQL5/Experts/`.
|
|
53
|
+
2. Compile in MetaEditor (`0 errors`).
|
|
54
|
+
3. Enable **Allow Algorithmic Trading** in MT5 (`Tools -> Options -> Expert Advisors`).
|
|
55
|
+
4. Start your Python gateway, then attach `OpenMT5Gateway` onto any MT5 chart with matching `InpPort` and `InpAuthToken`.
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
from decimal import Decimal
|
|
59
|
+
from open_mt5_gateway import GatewayConfig, OpenMT5Client, OpenMT5GatewayServer, OrderRequest
|
|
60
|
+
|
|
61
|
+
with OpenMT5GatewayServer(GatewayConfig(port=18812, auth_token="my-secret")) as gw:
|
|
62
|
+
gw.wait_for_connection(timeout=30.0)
|
|
63
|
+
client = OpenMT5Client(gw)
|
|
64
|
+
|
|
65
|
+
acct = client.get_account()
|
|
66
|
+
print(acct.login, acct.balance)
|
|
67
|
+
|
|
68
|
+
res = client.submit_order(
|
|
69
|
+
OrderRequest(
|
|
70
|
+
external_id="trade-001",
|
|
71
|
+
symbol="EURUSD",
|
|
72
|
+
side="buy",
|
|
73
|
+
volume=Decimal("0.10"),
|
|
74
|
+
stop_loss=Decimal("1.08200"),
|
|
75
|
+
take_profit=Decimal("1.08900"),
|
|
76
|
+
)
|
|
77
|
+
)
|
|
78
|
+
print(res)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
---
|
|
82
|
+
|
|
83
|
+
## Publishing to GitHub & PyPI
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
# Push to GitHub
|
|
87
|
+
gh repo create open-mt5-gateway --public --source=. --push
|
|
88
|
+
|
|
89
|
+
# Build & publish to PyPI
|
|
90
|
+
pip install build twine
|
|
91
|
+
python -m build
|
|
92
|
+
twine upload dist/*
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## License
|
|
96
|
+
|
|
97
|
+
MIT License — see [LICENSE](./LICENSE).
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "open-mt5-gateway"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Open-source, zero-DLL cross-platform MetaTrader 5 bridge for macOS, Linux, and Windows."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
requires-python = ">=3.10"
|
|
13
|
+
keywords = ["metatrader5", "mt5", "macos", "algorithmic-trading", "forex", "mql5"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 4 - Beta",
|
|
16
|
+
"Intended Audience :: Financial and Insurance Industry",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Operating System :: MacOS",
|
|
19
|
+
"Operating System :: POSIX :: Linux",
|
|
20
|
+
"Operating System :: Microsoft :: Windows",
|
|
21
|
+
"Programming Language :: Python :: 3",
|
|
22
|
+
"Programming Language :: Python :: 3.10",
|
|
23
|
+
"Programming Language :: Python :: 3.11",
|
|
24
|
+
"Programming Language :: Python :: 3.12",
|
|
25
|
+
"Topic :: Office/Business :: Financial :: Investment",
|
|
26
|
+
]
|
|
27
|
+
dependencies = []
|
|
28
|
+
|
|
29
|
+
[project.optional-dependencies]
|
|
30
|
+
dataframe = ["pandas>=2.0"]
|
|
31
|
+
dev = ["pytest>=8.0", "build>=1.0", "twine>=5.0"]
|
|
32
|
+
|
|
33
|
+
[project.scripts]
|
|
34
|
+
open-mt5-preflight = "open_mt5_gateway.cli:main"
|
|
35
|
+
|
|
36
|
+
[project.urls]
|
|
37
|
+
Homepage = "https://github.com/basharat78/open-mt5-gateway"
|
|
38
|
+
Repository = "https://github.com/basharat78/open-mt5-gateway"
|
|
39
|
+
Issues = "https://github.com/basharat78/open-mt5-gateway/issues"
|
|
40
|
+
|
|
41
|
+
[tool.setuptools.packages.find]
|
|
42
|
+
where = ["src"]
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"""OpenMT5Gateway — Open-Source Cross-Platform MetaTrader 5 Bridge."""
|
|
2
|
+
|
|
3
|
+
from open_mt5_gateway.gateway import (
|
|
4
|
+
AccountInfo,
|
|
5
|
+
ClosedDeal,
|
|
6
|
+
GatewayConfig,
|
|
7
|
+
OpenMT5Client,
|
|
8
|
+
OpenMT5GatewayServer,
|
|
9
|
+
OrderRequest,
|
|
10
|
+
OrderResult,
|
|
11
|
+
SymbolSpec,
|
|
12
|
+
TelemetrySnapshot,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
__all__ = [
|
|
16
|
+
"AccountInfo",
|
|
17
|
+
"ClosedDeal",
|
|
18
|
+
"GatewayConfig",
|
|
19
|
+
"OpenMT5Client",
|
|
20
|
+
"OpenMT5GatewayServer",
|
|
21
|
+
"OrderRequest",
|
|
22
|
+
"OrderResult",
|
|
23
|
+
"SymbolSpec",
|
|
24
|
+
"TelemetrySnapshot",
|
|
25
|
+
]
|
|
26
|
+
__version__ = "0.1.0"
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
"""Pre-flight diagnostic CLI & local simulator for OpenMT5Gateway (`open-mt5-preflight`)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import json
|
|
7
|
+
import socket
|
|
8
|
+
import threading
|
|
9
|
+
from datetime import datetime, timezone
|
|
10
|
+
from typing import Any, Sequence
|
|
11
|
+
|
|
12
|
+
from open_mt5_gateway.gateway import GatewayConfig, OpenMT5Client, OpenMT5GatewayServer
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _start_demo_simulator(
|
|
16
|
+
port: int,
|
|
17
|
+
token: str,
|
|
18
|
+
account_login: str,
|
|
19
|
+
) -> tuple[threading.Event, threading.Thread]:
|
|
20
|
+
stop_event = threading.Event()
|
|
21
|
+
quotes: dict[str, tuple[str, str, str, str, int]] = {
|
|
22
|
+
"EURUSD": ("EURUSD.a", "1.08500", "1.08510", "0.00001", 5),
|
|
23
|
+
"GBPUSD": ("GBPUSD.a", "1.26800", "1.26814", "0.00001", 5),
|
|
24
|
+
"USDJPY": ("USDJPY.a", "154.200", "154.215", "0.001", 3),
|
|
25
|
+
"XAUUSD": ("XAUUSD.a", "2650.10", "2650.35", "0.01", 2),
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
def _run() -> None:
|
|
29
|
+
sock = socket.create_connection(("127.0.0.1", port), timeout=1.0)
|
|
30
|
+
try:
|
|
31
|
+
reader = sock.makefile("r", encoding="utf-8")
|
|
32
|
+
if token:
|
|
33
|
+
sock.sendall((json.dumps({"type": "AUTH", "token": token}) + "\n").encode("utf-8"))
|
|
34
|
+
reader.readline()
|
|
35
|
+
while not stop_event.is_set():
|
|
36
|
+
try:
|
|
37
|
+
line = reader.readline()
|
|
38
|
+
except TimeoutError:
|
|
39
|
+
continue
|
|
40
|
+
if not line:
|
|
41
|
+
break
|
|
42
|
+
msg = json.loads(line.strip())
|
|
43
|
+
if msg.get("type") != "COMMAND":
|
|
44
|
+
continue
|
|
45
|
+
req_id = msg["request_id"]
|
|
46
|
+
action = msg["action"]
|
|
47
|
+
payload = msg.get("payload", {})
|
|
48
|
+
if action == "GET_ACCOUNT":
|
|
49
|
+
resp: dict[str, Any] = {
|
|
50
|
+
"type": "RESPONSE",
|
|
51
|
+
"request_id": req_id,
|
|
52
|
+
"ok": True,
|
|
53
|
+
"login": account_login,
|
|
54
|
+
"server": "Demo-Server (Simulated)",
|
|
55
|
+
"balance": "50000.00000",
|
|
56
|
+
"equity": "50000.00000",
|
|
57
|
+
"server_time": int(datetime.now(timezone.utc).timestamp()),
|
|
58
|
+
}
|
|
59
|
+
elif action == "GET_SYMBOL_SPEC":
|
|
60
|
+
req_sym = str(payload.get("symbol", "")).upper()
|
|
61
|
+
resolved, bid, ask, pt, digits = quotes.get(
|
|
62
|
+
req_sym, (f"{req_sym}.a", "1.00000", "1.00012", "0.00001", 5)
|
|
63
|
+
)
|
|
64
|
+
resp = {
|
|
65
|
+
"type": "RESPONSE",
|
|
66
|
+
"request_id": req_id,
|
|
67
|
+
"ok": True,
|
|
68
|
+
"resolved_symbol": resolved,
|
|
69
|
+
"bid": bid,
|
|
70
|
+
"ask": ask,
|
|
71
|
+
"point": pt,
|
|
72
|
+
"digits": digits,
|
|
73
|
+
"volume_min": "0.01",
|
|
74
|
+
"volume_max": "100.00",
|
|
75
|
+
"volume_step": "0.01",
|
|
76
|
+
"trade_allowed": True,
|
|
77
|
+
}
|
|
78
|
+
else:
|
|
79
|
+
resp = {"type": "RESPONSE", "request_id": req_id, "ok": True}
|
|
80
|
+
sock.sendall((json.dumps(resp) + "\n").encode("utf-8"))
|
|
81
|
+
finally:
|
|
82
|
+
try:
|
|
83
|
+
sock.close()
|
|
84
|
+
except OSError:
|
|
85
|
+
pass
|
|
86
|
+
|
|
87
|
+
thread = threading.Thread(target=_run, name="open-mt5-demo-sim", daemon=True)
|
|
88
|
+
thread.start()
|
|
89
|
+
return stop_event, thread
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def main(argv: Sequence[str] | None = None) -> int:
|
|
93
|
+
parser = argparse.ArgumentParser(description="Verify MetaTrader 5 terminal connectivity.")
|
|
94
|
+
parser.add_argument("--host", default="127.0.0.1")
|
|
95
|
+
parser.add_argument("--port", type=int, default=18812)
|
|
96
|
+
parser.add_argument("--token", default="")
|
|
97
|
+
parser.add_argument("--wait-seconds", type=float, default=15.0)
|
|
98
|
+
parser.add_argument("--symbols", nargs="+", default=["EURUSD", "GBPUSD", "XAUUSD"])
|
|
99
|
+
parser.add_argument("--max-spread-pips", type=float, default=3.0)
|
|
100
|
+
parser.add_argument("--simulate-demo", action="store_true")
|
|
101
|
+
parser.add_argument("--account-login", default="DEMO-50K")
|
|
102
|
+
args = parser.parse_args(argv)
|
|
103
|
+
|
|
104
|
+
with OpenMT5GatewayServer(
|
|
105
|
+
GatewayConfig(host=args.host, port=args.port, auth_token=args.token)
|
|
106
|
+
) as gw:
|
|
107
|
+
sim_stop: threading.Event | None = None
|
|
108
|
+
sim_thread: threading.Thread | None = None
|
|
109
|
+
try:
|
|
110
|
+
if args.simulate_demo:
|
|
111
|
+
sim_stop, sim_thread = _start_demo_simulator(
|
|
112
|
+
gw.bound_port,
|
|
113
|
+
args.token,
|
|
114
|
+
args.account_login,
|
|
115
|
+
)
|
|
116
|
+
connected = gw.wait_for_connection(timeout=args.wait_seconds)
|
|
117
|
+
if not connected:
|
|
118
|
+
print(
|
|
119
|
+
json.dumps(
|
|
120
|
+
{
|
|
121
|
+
"passed": False,
|
|
122
|
+
"connected": False,
|
|
123
|
+
"error": "No OpenMT5Gateway MQL5 EA client connected.",
|
|
124
|
+
},
|
|
125
|
+
indent=2,
|
|
126
|
+
)
|
|
127
|
+
)
|
|
128
|
+
return 1
|
|
129
|
+
|
|
130
|
+
client = OpenMT5Client(gw)
|
|
131
|
+
acct = client.get_account()
|
|
132
|
+
skew = abs((acct.server_time_utc - datetime.now(timezone.utc)).total_seconds())
|
|
133
|
+
sym_results = []
|
|
134
|
+
passed = True
|
|
135
|
+
for sym in args.symbols:
|
|
136
|
+
try:
|
|
137
|
+
spec = client.get_symbol_spec(sym)
|
|
138
|
+
spread = spec.spread_pips
|
|
139
|
+
ok = spec.trade_allowed and spread <= args.max_spread_pips
|
|
140
|
+
if not ok:
|
|
141
|
+
passed = False
|
|
142
|
+
sym_results.append(
|
|
143
|
+
{
|
|
144
|
+
"requested": sym,
|
|
145
|
+
"resolved": spec.resolved_symbol,
|
|
146
|
+
"ok": ok,
|
|
147
|
+
"spread_pips": spread,
|
|
148
|
+
}
|
|
149
|
+
)
|
|
150
|
+
except RuntimeError as exc:
|
|
151
|
+
passed = False
|
|
152
|
+
sym_results.append(
|
|
153
|
+
{"requested": sym, "resolved": None, "ok": False, "error": str(exc)}
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
print(
|
|
157
|
+
json.dumps(
|
|
158
|
+
{
|
|
159
|
+
"passed": passed,
|
|
160
|
+
"connected": True,
|
|
161
|
+
"login": acct.login,
|
|
162
|
+
"server": acct.server,
|
|
163
|
+
"balance": str(acct.balance),
|
|
164
|
+
"equity": str(acct.equity),
|
|
165
|
+
"clock_skew_seconds": skew,
|
|
166
|
+
"symbols": sym_results,
|
|
167
|
+
},
|
|
168
|
+
indent=2,
|
|
169
|
+
)
|
|
170
|
+
)
|
|
171
|
+
return 0 if passed else 1
|
|
172
|
+
finally:
|
|
173
|
+
if sim_stop is not None:
|
|
174
|
+
sim_stop.set()
|
|
175
|
+
if sim_thread is not None:
|
|
176
|
+
sim_thread.join(timeout=1.0)
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
if __name__ == "__main__":
|
|
180
|
+
raise SystemExit(main())
|