open-api-mt5 0.3.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_api_mt5-0.3.0/PKG-INFO +151 -0
- open_api_mt5-0.3.0/README.md +141 -0
- open_api_mt5-0.3.0/app/__init__.py +1 -0
- open_api_mt5-0.3.0/app/cli.py +14 -0
- open_api_mt5-0.3.0/app/main.py +560 -0
- open_api_mt5-0.3.0/app/services/__init__.py +1 -0
- open_api_mt5-0.3.0/app/services/account_service.py +19 -0
- open_api_mt5-0.3.0/app/services/bar_service.py +79 -0
- open_api_mt5-0.3.0/app/services/calendar_service.py +127 -0
- open_api_mt5-0.3.0/app/services/connection_service.py +37 -0
- open_api_mt5-0.3.0/app/services/serialization.py +23 -0
- open_api_mt5-0.3.0/app/services/stream_service.py +35 -0
- open_api_mt5-0.3.0/app/services/symbol_service.py +44 -0
- open_api_mt5-0.3.0/app/services/ticket_service.py +29 -0
- open_api_mt5-0.3.0/app/services/trade_service.py +301 -0
- open_api_mt5-0.3.0/open_api_mt5.egg-info/PKG-INFO +151 -0
- open_api_mt5-0.3.0/open_api_mt5.egg-info/SOURCES.txt +21 -0
- open_api_mt5-0.3.0/open_api_mt5.egg-info/dependency_links.txt +1 -0
- open_api_mt5-0.3.0/open_api_mt5.egg-info/entry_points.txt +2 -0
- open_api_mt5-0.3.0/open_api_mt5.egg-info/requires.txt +3 -0
- open_api_mt5-0.3.0/open_api_mt5.egg-info/top_level.txt +1 -0
- open_api_mt5-0.3.0/pyproject.toml +18 -0
- open_api_mt5-0.3.0/setup.cfg +4 -0
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: open-api-mt5
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: REST and WebSocket API project for MetaTrader 5
|
|
5
|
+
Requires-Python: >=3.11
|
|
6
|
+
Description-Content-Type: text/markdown
|
|
7
|
+
Requires-Dist: fastapi<1.0.0,>=0.116.0
|
|
8
|
+
Requires-Dist: MetaTrader5<6.0.0,>=5.0.0
|
|
9
|
+
Requires-Dist: uvicorn[standard]<1.0.0,>=0.35.0
|
|
10
|
+
|
|
11
|
+
# open-api-mt5
|
|
12
|
+
|
|
13
|
+
MetaTrader 5 API service with FastAPI.
|
|
14
|
+
|
|
15
|
+
## Important limitation
|
|
16
|
+
|
|
17
|
+
This API controls a **single MT5 terminal/session instance** per running service process.
|
|
18
|
+
|
|
19
|
+
- A single API instance can be connected to only one account at a time.
|
|
20
|
+
- `/account/connect` switches that single active session.
|
|
21
|
+
- If you run multiple API services, use separate MT5 terminal instances/data folders for reliable isolation.
|
|
22
|
+
|
|
23
|
+
## Setup
|
|
24
|
+
|
|
25
|
+
1. Create a virtual environment:
|
|
26
|
+
- Windows PowerShell: `python -m venv .venv`
|
|
27
|
+
2. Activate it:
|
|
28
|
+
- `.\.venv\Scripts\Activate.ps1`
|
|
29
|
+
3. Install dependencies:
|
|
30
|
+
- `python -m pip install -U pip`
|
|
31
|
+
- `pip install -e .`
|
|
32
|
+
|
|
33
|
+
## MT5 startup config
|
|
34
|
+
|
|
35
|
+
The API initializes MetaTrader 5 when FastAPI starts and closes it when FastAPI stops.
|
|
36
|
+
It also checks MT5 connection every 5 seconds and tries to reconnect automatically if disconnected.
|
|
37
|
+
|
|
38
|
+
Set these environment variables in PowerShell before running:
|
|
39
|
+
|
|
40
|
+
```powershell
|
|
41
|
+
$env:MT5_PATH = "C:\Program Files\MetaTrader 5\terminal64.exe"
|
|
42
|
+
$env:MT5_LOGIN = "12345678"
|
|
43
|
+
$env:MT5_PASSWORD = "your-password"
|
|
44
|
+
$env:MT5_SERVER = "YourBroker-Server"
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
`MT5_PATH` is optional if MT5 is already discoverable, but setting it is recommended.
|
|
48
|
+
|
|
49
|
+
## Run
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
open-api-mt5
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Optional flags:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
open-api-mt5 --port 9000
|
|
59
|
+
open-api-mt5 --host 0.0.0.0 --port 8000
|
|
60
|
+
open-api-mt5 --reload
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Default port is `8000`.
|
|
64
|
+
|
|
65
|
+
API docs:
|
|
66
|
+
- Swagger UI: `http://127.0.0.1:8000/docs`
|
|
67
|
+
- WebSocket docs in Swagger:
|
|
68
|
+
- `GET /ws/positions/open/docs`
|
|
69
|
+
|
|
70
|
+
Health endpoint:
|
|
71
|
+
- `GET http://127.0.0.1:8000/health`
|
|
72
|
+
|
|
73
|
+
Account connection endpoints:
|
|
74
|
+
- `POST http://127.0.0.1:8000/account/connect`
|
|
75
|
+
- Body: `username`, `password`, `server`, optional `path`
|
|
76
|
+
- Example body:
|
|
77
|
+
```json
|
|
78
|
+
{
|
|
79
|
+
"username": "12345678",
|
|
80
|
+
"password": "your-password",
|
|
81
|
+
"server": "YourBroker-Server",
|
|
82
|
+
"path": "C:\\Program Files\\MetaTrader 5\\terminal64.exe"
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
- `POST http://127.0.0.1:8000/account/disconnect`
|
|
86
|
+
|
|
87
|
+
Trade history endpoint:
|
|
88
|
+
- `GET http://127.0.0.1:8000/trades/history`
|
|
89
|
+
- Optional query params: `fromDate`, `toDate` (ISO datetime, UTC recommended)
|
|
90
|
+
- If omitted, it returns the last 7 days by default
|
|
91
|
+
|
|
92
|
+
Calendar events endpoint:
|
|
93
|
+
- `GET http://127.0.0.1:8000/calendar/events`
|
|
94
|
+
- Optional query params: `fromDate`, `toDate` (ISO datetime), `country` (example: `US`), `currency` (example: `USD`)
|
|
95
|
+
- Default range when omitted: last 7 days to next 7 days
|
|
96
|
+
|
|
97
|
+
## WebSocket streams
|
|
98
|
+
|
|
99
|
+
Open positions stream:
|
|
100
|
+
- `ws://127.0.0.1:8000/ws/positions/open`
|
|
101
|
+
- Optional query param: `intervalSeconds` (poll interval, bounded to 0.2..60)
|
|
102
|
+
- Events:
|
|
103
|
+
- `subscribed`
|
|
104
|
+
- `positionsSnapshot`
|
|
105
|
+
- `error`
|
|
106
|
+
- `positionsSnapshot` includes:
|
|
107
|
+
- `positions[].pnl` (position PnL, sourced from MT5 `profit`)
|
|
108
|
+
- `totalPnl` (sum of all open positions PnL)
|
|
109
|
+
|
|
110
|
+
Example JavaScript client:
|
|
111
|
+
|
|
112
|
+
```javascript
|
|
113
|
+
const ws = new WebSocket("ws://127.0.0.1:8000/ws/positions/open?intervalSeconds=1");
|
|
114
|
+
ws.onmessage = (event) => {
|
|
115
|
+
const payload = JSON.parse(event.data);
|
|
116
|
+
console.log(payload.event, payload);
|
|
117
|
+
};
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Build and publish package
|
|
121
|
+
|
|
122
|
+
1. Build distribution files:
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
python -m pip install --upgrade build twine
|
|
126
|
+
python -m build
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
2. Upload to PyPI:
|
|
130
|
+
|
|
131
|
+
```bash
|
|
132
|
+
python -m twine upload dist/*
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
3. Install from PyPI and run:
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
pip install open-api-mt5
|
|
139
|
+
open-api-mt5 --port 8000
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Optional: standalone executable (no Python required on target machine)
|
|
143
|
+
|
|
144
|
+
If you want users to run it without installing Python, build an executable:
|
|
145
|
+
|
|
146
|
+
```bash
|
|
147
|
+
python -m pip install pyinstaller
|
|
148
|
+
pyinstaller --onefile --name open-api-mt5 app/cli.py
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
The executable will be in `dist/open-api-mt5.exe`.
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# open-api-mt5
|
|
2
|
+
|
|
3
|
+
MetaTrader 5 API service with FastAPI.
|
|
4
|
+
|
|
5
|
+
## Important limitation
|
|
6
|
+
|
|
7
|
+
This API controls a **single MT5 terminal/session instance** per running service process.
|
|
8
|
+
|
|
9
|
+
- A single API instance can be connected to only one account at a time.
|
|
10
|
+
- `/account/connect` switches that single active session.
|
|
11
|
+
- If you run multiple API services, use separate MT5 terminal instances/data folders for reliable isolation.
|
|
12
|
+
|
|
13
|
+
## Setup
|
|
14
|
+
|
|
15
|
+
1. Create a virtual environment:
|
|
16
|
+
- Windows PowerShell: `python -m venv .venv`
|
|
17
|
+
2. Activate it:
|
|
18
|
+
- `.\.venv\Scripts\Activate.ps1`
|
|
19
|
+
3. Install dependencies:
|
|
20
|
+
- `python -m pip install -U pip`
|
|
21
|
+
- `pip install -e .`
|
|
22
|
+
|
|
23
|
+
## MT5 startup config
|
|
24
|
+
|
|
25
|
+
The API initializes MetaTrader 5 when FastAPI starts and closes it when FastAPI stops.
|
|
26
|
+
It also checks MT5 connection every 5 seconds and tries to reconnect automatically if disconnected.
|
|
27
|
+
|
|
28
|
+
Set these environment variables in PowerShell before running:
|
|
29
|
+
|
|
30
|
+
```powershell
|
|
31
|
+
$env:MT5_PATH = "C:\Program Files\MetaTrader 5\terminal64.exe"
|
|
32
|
+
$env:MT5_LOGIN = "12345678"
|
|
33
|
+
$env:MT5_PASSWORD = "your-password"
|
|
34
|
+
$env:MT5_SERVER = "YourBroker-Server"
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
`MT5_PATH` is optional if MT5 is already discoverable, but setting it is recommended.
|
|
38
|
+
|
|
39
|
+
## Run
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
open-api-mt5
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Optional flags:
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
open-api-mt5 --port 9000
|
|
49
|
+
open-api-mt5 --host 0.0.0.0 --port 8000
|
|
50
|
+
open-api-mt5 --reload
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Default port is `8000`.
|
|
54
|
+
|
|
55
|
+
API docs:
|
|
56
|
+
- Swagger UI: `http://127.0.0.1:8000/docs`
|
|
57
|
+
- WebSocket docs in Swagger:
|
|
58
|
+
- `GET /ws/positions/open/docs`
|
|
59
|
+
|
|
60
|
+
Health endpoint:
|
|
61
|
+
- `GET http://127.0.0.1:8000/health`
|
|
62
|
+
|
|
63
|
+
Account connection endpoints:
|
|
64
|
+
- `POST http://127.0.0.1:8000/account/connect`
|
|
65
|
+
- Body: `username`, `password`, `server`, optional `path`
|
|
66
|
+
- Example body:
|
|
67
|
+
```json
|
|
68
|
+
{
|
|
69
|
+
"username": "12345678",
|
|
70
|
+
"password": "your-password",
|
|
71
|
+
"server": "YourBroker-Server",
|
|
72
|
+
"path": "C:\\Program Files\\MetaTrader 5\\terminal64.exe"
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
- `POST http://127.0.0.1:8000/account/disconnect`
|
|
76
|
+
|
|
77
|
+
Trade history endpoint:
|
|
78
|
+
- `GET http://127.0.0.1:8000/trades/history`
|
|
79
|
+
- Optional query params: `fromDate`, `toDate` (ISO datetime, UTC recommended)
|
|
80
|
+
- If omitted, it returns the last 7 days by default
|
|
81
|
+
|
|
82
|
+
Calendar events endpoint:
|
|
83
|
+
- `GET http://127.0.0.1:8000/calendar/events`
|
|
84
|
+
- Optional query params: `fromDate`, `toDate` (ISO datetime), `country` (example: `US`), `currency` (example: `USD`)
|
|
85
|
+
- Default range when omitted: last 7 days to next 7 days
|
|
86
|
+
|
|
87
|
+
## WebSocket streams
|
|
88
|
+
|
|
89
|
+
Open positions stream:
|
|
90
|
+
- `ws://127.0.0.1:8000/ws/positions/open`
|
|
91
|
+
- Optional query param: `intervalSeconds` (poll interval, bounded to 0.2..60)
|
|
92
|
+
- Events:
|
|
93
|
+
- `subscribed`
|
|
94
|
+
- `positionsSnapshot`
|
|
95
|
+
- `error`
|
|
96
|
+
- `positionsSnapshot` includes:
|
|
97
|
+
- `positions[].pnl` (position PnL, sourced from MT5 `profit`)
|
|
98
|
+
- `totalPnl` (sum of all open positions PnL)
|
|
99
|
+
|
|
100
|
+
Example JavaScript client:
|
|
101
|
+
|
|
102
|
+
```javascript
|
|
103
|
+
const ws = new WebSocket("ws://127.0.0.1:8000/ws/positions/open?intervalSeconds=1");
|
|
104
|
+
ws.onmessage = (event) => {
|
|
105
|
+
const payload = JSON.parse(event.data);
|
|
106
|
+
console.log(payload.event, payload);
|
|
107
|
+
};
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Build and publish package
|
|
111
|
+
|
|
112
|
+
1. Build distribution files:
|
|
113
|
+
|
|
114
|
+
```bash
|
|
115
|
+
python -m pip install --upgrade build twine
|
|
116
|
+
python -m build
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
2. Upload to PyPI:
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
python -m twine upload dist/*
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
3. Install from PyPI and run:
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
pip install open-api-mt5
|
|
129
|
+
open-api-mt5 --port 8000
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## Optional: standalone executable (no Python required on target machine)
|
|
133
|
+
|
|
134
|
+
If you want users to run it without installing Python, build an executable:
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
python -m pip install pyinstaller
|
|
138
|
+
pyinstaller --onefile --name open-api-mt5 app/cli.py
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
The executable will be in `dist/open-api-mt5.exe`.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Open MT5 API package."""
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import os
|
|
3
|
+
|
|
4
|
+
import uvicorn
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def main() -> None:
|
|
8
|
+
parser = argparse.ArgumentParser(description="Run the Open MT5 API server.")
|
|
9
|
+
parser.add_argument("--host", default="127.0.0.1", help="Bind host. Default: 127.0.0.1")
|
|
10
|
+
parser.add_argument("--port", type=int, default=int(os.getenv("PORT", "8000")), help="Bind port. Default: 8000")
|
|
11
|
+
parser.add_argument("--reload", action="store_true", help="Enable auto-reload for development")
|
|
12
|
+
args = parser.parse_args()
|
|
13
|
+
|
|
14
|
+
uvicorn.run("app.main:app", host=args.host, port=args.port, reload=args.reload)
|