mt5direct 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.
- mt5direct-0.1.0/LICENSE +21 -0
- mt5direct-0.1.0/PKG-INFO +122 -0
- mt5direct-0.1.0/README.md +108 -0
- mt5direct-0.1.0/pyproject.toml +25 -0
- mt5direct-0.1.0/setup.cfg +4 -0
- mt5direct-0.1.0/src/mt5direct/__init__.py +18 -0
- mt5direct-0.1.0/src/mt5direct/client.py +196 -0
- mt5direct-0.1.0/src/mt5direct/constants/__init__.py +5 -0
- mt5direct-0.1.0/src/mt5direct/constants/commands.py +16 -0
- mt5direct-0.1.0/src/mt5direct/constants/network.py +12 -0
- mt5direct-0.1.0/src/mt5direct/constants/orders.py +29 -0
- mt5direct-0.1.0/src/mt5direct/constants/retcodes.py +26 -0
- mt5direct-0.1.0/src/mt5direct/constants/timeframes.py +17 -0
- mt5direct-0.1.0/src/mt5direct/exceptions.py +13 -0
- mt5direct-0.1.0/src/mt5direct/models/__init__.py +13 -0
- mt5direct-0.1.0/src/mt5direct/models/account.py +11 -0
- mt5direct-0.1.0/src/mt5direct/models/candle.py +12 -0
- mt5direct-0.1.0/src/mt5direct/models/frame.py +10 -0
- mt5direct-0.1.0/src/mt5direct/models/history.py +20 -0
- mt5direct-0.1.0/src/mt5direct/models/position.py +24 -0
- mt5direct-0.1.0/src/mt5direct/models/symbol.py +12 -0
- mt5direct-0.1.0/src/mt5direct/models/tick.py +16 -0
- mt5direct-0.1.0/src/mt5direct/models/trade.py +29 -0
- mt5direct-0.1.0/src/mt5direct/parsers/__init__.py +10 -0
- mt5direct-0.1.0/src/mt5direct/parsers/account.py +18 -0
- mt5direct-0.1.0/src/mt5direct/parsers/candles.py +21 -0
- mt5direct-0.1.0/src/mt5direct/parsers/history.py +38 -0
- mt5direct-0.1.0/src/mt5direct/parsers/positions.py +35 -0
- mt5direct-0.1.0/src/mt5direct/parsers/resolve.py +89 -0
- mt5direct-0.1.0/src/mt5direct/parsers/symbols.py +32 -0
- mt5direct-0.1.0/src/mt5direct/parsers/ticks.py +44 -0
- mt5direct-0.1.0/src/mt5direct/parsers/trade.py +41 -0
- mt5direct-0.1.0/src/mt5direct/protocol/__init__.py +7 -0
- mt5direct-0.1.0/src/mt5direct/protocol/crypto.py +27 -0
- mt5direct-0.1.0/src/mt5direct/protocol/framing.py +29 -0
- mt5direct-0.1.0/src/mt5direct/protocol/layouts.py +22 -0
- mt5direct-0.1.0/src/mt5direct/protocol/reader.py +44 -0
- mt5direct-0.1.0/src/mt5direct/protocol/serializer.py +50 -0
- mt5direct-0.1.0/src/mt5direct/services/__init__.py +7 -0
- mt5direct-0.1.0/src/mt5direct/services/account.py +22 -0
- mt5direct-0.1.0/src/mt5direct/services/auth.py +78 -0
- mt5direct-0.1.0/src/mt5direct/services/base.py +32 -0
- mt5direct-0.1.0/src/mt5direct/services/market_data.py +91 -0
- mt5direct-0.1.0/src/mt5direct/services/positions.py +69 -0
- mt5direct-0.1.0/src/mt5direct/services/symbols.py +66 -0
- mt5direct-0.1.0/src/mt5direct/services/trading.py +116 -0
- mt5direct-0.1.0/src/mt5direct/transport/__init__.py +2 -0
- mt5direct-0.1.0/src/mt5direct/transport/session.py +86 -0
- mt5direct-0.1.0/src/mt5direct/transport/websocket.py +45 -0
- mt5direct-0.1.0/src/mt5direct.egg-info/PKG-INFO +122 -0
- mt5direct-0.1.0/src/mt5direct.egg-info/SOURCES.txt +52 -0
- mt5direct-0.1.0/src/mt5direct.egg-info/dependency_links.txt +1 -0
- mt5direct-0.1.0/src/mt5direct.egg-info/requires.txt +5 -0
- mt5direct-0.1.0/src/mt5direct.egg-info/top_level.txt +1 -0
mt5direct-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 rhamaxv
|
|
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.
|
mt5direct-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mt5direct
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Standalone MetaTrader 5 client over the native WebSocket protocol. No terminal required.
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Requires-Python: >=3.10
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: pycryptodome>=3.19
|
|
10
|
+
Requires-Dist: websocket-client>=1.6
|
|
11
|
+
Provides-Extra: dev
|
|
12
|
+
Requires-Dist: pytest>=7; extra == "dev"
|
|
13
|
+
Dynamic: license-file
|
|
14
|
+
|
|
15
|
+
# mt5direct
|
|
16
|
+
|
|
17
|
+
Standalone MetaTrader 5 client in pure Python. Talks the terminal's native
|
|
18
|
+
WebSocket protocol directly to the broker, so no MT5 desktop, no Wine, no
|
|
19
|
+
bridge DLL.
|
|
20
|
+
|
|
21
|
+
## Install
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
pip install -e .
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Requires Python 3.10+, `pycryptodome` and `websocket-client`.
|
|
28
|
+
|
|
29
|
+
## Quick start
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
import os
|
|
33
|
+
from mt5direct import MT5Client
|
|
34
|
+
|
|
35
|
+
with MT5Client() as client:
|
|
36
|
+
client.login(int(os.environ["MT5_LOGIN"]), os.environ["MT5_PASSWORD"])
|
|
37
|
+
client.get_symbols()
|
|
38
|
+
client.init_trading()
|
|
39
|
+
|
|
40
|
+
acct = client.get_account_info()
|
|
41
|
+
print(acct.balance, acct.currency)
|
|
42
|
+
|
|
43
|
+
candles = client.get_candles("EURUSD", "H1", count=100)
|
|
44
|
+
print(candles[-1].close)
|
|
45
|
+
|
|
46
|
+
result = client.buy("EURUSD", 0.01)
|
|
47
|
+
print(result.retcode_str, result.deal, result.price)
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Live ticks
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
client.subscribe_by_name("EURUSD", "XAUUSD")
|
|
54
|
+
client.on_tick(lambda t: print(t.symbol, t.bid, t.ask))
|
|
55
|
+
client.start_tick_listener()
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Positions
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
for p in client.get_positions():
|
|
62
|
+
print(p.ticket, p.type, p.symbol, p.volume, p.price_open)
|
|
63
|
+
|
|
64
|
+
client.close_all("EURUSD")
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
More in `examples/`.
|
|
68
|
+
|
|
69
|
+
## API layout
|
|
70
|
+
|
|
71
|
+
Every operation is available two ways: flat on the client
|
|
72
|
+
(`client.buy(...)`) or grouped by domain (`client.trading.buy(...)`).
|
|
73
|
+
|
|
74
|
+
| Group | Methods |
|
|
75
|
+
|--------------------|-------------------------------------------------------------------------|
|
|
76
|
+
| `client.auth` | `login`, `ping`, `reconnect`, `auto_reconnect` |
|
|
77
|
+
| `client.account` | `get_account_info`, `init_trading`, `get_history` |
|
|
78
|
+
| `client.symbols` | `get_symbols`, `resolve_symbols`, `subscribe`, `subscribe_by_name`, `subscribe_all` |
|
|
79
|
+
| `client.market` | `get_candles`, `on_tick`, `start_tick_listener`, `stop_tick_listener` |
|
|
80
|
+
| `client.trading` | `buy`, `sell`, `buy_limit`, `sell_limit`, `buy_stop`, `sell_stop`, `modify_sl_tp`, `modify_pending`, `cancel_order`, `close_position`, `trade`, `wait_trade_result` |
|
|
81
|
+
| `client.positions` | `get_positions`, `close_all`, `track_positions` |
|
|
82
|
+
|
|
83
|
+
Results are frozen dataclasses from `mt5direct.models`: `AccountInfo`,
|
|
84
|
+
`Symbol`, `Tick`, `Candle`, `Position`, `Deal`, `TradeAck`, `TradeResult`.
|
|
85
|
+
|
|
86
|
+
Order methods return a `TradeResult` when the server confirms execution, the
|
|
87
|
+
raw `TradeAck` when the request was rejected up front, or `None` on timeout.
|
|
88
|
+
|
|
89
|
+
## Package structure
|
|
90
|
+
|
|
91
|
+
```
|
|
92
|
+
src/mt5direct/
|
|
93
|
+
client.py facade
|
|
94
|
+
services/ one module per domain: auth, account, symbols, market_data, trading, positions
|
|
95
|
+
transport/ WebSocket connection, AES session, message queue
|
|
96
|
+
protocol/ crypto, framing, field serializer, binary reader, record layouts
|
|
97
|
+
parsers/ bytes -> models, pure functions
|
|
98
|
+
models/ dataclasses
|
|
99
|
+
constants/ command ids, order types, retcodes, timeframes, hosts
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Dependencies point downward only: `client -> services -> transport -> protocol`,
|
|
103
|
+
and `services -> parsers -> protocol, models`.
|
|
104
|
+
|
|
105
|
+
## Tests
|
|
106
|
+
|
|
107
|
+
```bash
|
|
108
|
+
pip install -e '.[dev]'
|
|
109
|
+
pytest
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Tests cover `protocol` and `parsers` with synthetic bytes. `transport` and
|
|
113
|
+
`services` need a live server and are exercised by `examples/demo.py`.
|
|
114
|
+
|
|
115
|
+
## Migrating from `main.py`
|
|
116
|
+
|
|
117
|
+
The single-file version lives in `legacy/main.py`. Differences:
|
|
118
|
+
|
|
119
|
+
- Results are dataclasses, not dicts: `tick['bid']` becomes `tick.bid`.
|
|
120
|
+
- `client.symbols` is now the symbol service; the symbol list is `client.symbol_list`.
|
|
121
|
+
- `connect()`/`login()` raise `ConnectionFailed`/`LoginFailed` (both subclass `ConnectionError`).
|
|
122
|
+
- Credentials come from `MT5_LOGIN` / `MT5_PASSWORD` in the examples.
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# mt5direct
|
|
2
|
+
|
|
3
|
+
Standalone MetaTrader 5 client in pure Python. Talks the terminal's native
|
|
4
|
+
WebSocket protocol directly to the broker, so no MT5 desktop, no Wine, no
|
|
5
|
+
bridge DLL.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install -e .
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Requires Python 3.10+, `pycryptodome` and `websocket-client`.
|
|
14
|
+
|
|
15
|
+
## Quick start
|
|
16
|
+
|
|
17
|
+
```python
|
|
18
|
+
import os
|
|
19
|
+
from mt5direct import MT5Client
|
|
20
|
+
|
|
21
|
+
with MT5Client() as client:
|
|
22
|
+
client.login(int(os.environ["MT5_LOGIN"]), os.environ["MT5_PASSWORD"])
|
|
23
|
+
client.get_symbols()
|
|
24
|
+
client.init_trading()
|
|
25
|
+
|
|
26
|
+
acct = client.get_account_info()
|
|
27
|
+
print(acct.balance, acct.currency)
|
|
28
|
+
|
|
29
|
+
candles = client.get_candles("EURUSD", "H1", count=100)
|
|
30
|
+
print(candles[-1].close)
|
|
31
|
+
|
|
32
|
+
result = client.buy("EURUSD", 0.01)
|
|
33
|
+
print(result.retcode_str, result.deal, result.price)
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### Live ticks
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
client.subscribe_by_name("EURUSD", "XAUUSD")
|
|
40
|
+
client.on_tick(lambda t: print(t.symbol, t.bid, t.ask))
|
|
41
|
+
client.start_tick_listener()
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Positions
|
|
45
|
+
|
|
46
|
+
```python
|
|
47
|
+
for p in client.get_positions():
|
|
48
|
+
print(p.ticket, p.type, p.symbol, p.volume, p.price_open)
|
|
49
|
+
|
|
50
|
+
client.close_all("EURUSD")
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
More in `examples/`.
|
|
54
|
+
|
|
55
|
+
## API layout
|
|
56
|
+
|
|
57
|
+
Every operation is available two ways: flat on the client
|
|
58
|
+
(`client.buy(...)`) or grouped by domain (`client.trading.buy(...)`).
|
|
59
|
+
|
|
60
|
+
| Group | Methods |
|
|
61
|
+
|--------------------|-------------------------------------------------------------------------|
|
|
62
|
+
| `client.auth` | `login`, `ping`, `reconnect`, `auto_reconnect` |
|
|
63
|
+
| `client.account` | `get_account_info`, `init_trading`, `get_history` |
|
|
64
|
+
| `client.symbols` | `get_symbols`, `resolve_symbols`, `subscribe`, `subscribe_by_name`, `subscribe_all` |
|
|
65
|
+
| `client.market` | `get_candles`, `on_tick`, `start_tick_listener`, `stop_tick_listener` |
|
|
66
|
+
| `client.trading` | `buy`, `sell`, `buy_limit`, `sell_limit`, `buy_stop`, `sell_stop`, `modify_sl_tp`, `modify_pending`, `cancel_order`, `close_position`, `trade`, `wait_trade_result` |
|
|
67
|
+
| `client.positions` | `get_positions`, `close_all`, `track_positions` |
|
|
68
|
+
|
|
69
|
+
Results are frozen dataclasses from `mt5direct.models`: `AccountInfo`,
|
|
70
|
+
`Symbol`, `Tick`, `Candle`, `Position`, `Deal`, `TradeAck`, `TradeResult`.
|
|
71
|
+
|
|
72
|
+
Order methods return a `TradeResult` when the server confirms execution, the
|
|
73
|
+
raw `TradeAck` when the request was rejected up front, or `None` on timeout.
|
|
74
|
+
|
|
75
|
+
## Package structure
|
|
76
|
+
|
|
77
|
+
```
|
|
78
|
+
src/mt5direct/
|
|
79
|
+
client.py facade
|
|
80
|
+
services/ one module per domain: auth, account, symbols, market_data, trading, positions
|
|
81
|
+
transport/ WebSocket connection, AES session, message queue
|
|
82
|
+
protocol/ crypto, framing, field serializer, binary reader, record layouts
|
|
83
|
+
parsers/ bytes -> models, pure functions
|
|
84
|
+
models/ dataclasses
|
|
85
|
+
constants/ command ids, order types, retcodes, timeframes, hosts
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Dependencies point downward only: `client -> services -> transport -> protocol`,
|
|
89
|
+
and `services -> parsers -> protocol, models`.
|
|
90
|
+
|
|
91
|
+
## Tests
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
pip install -e '.[dev]'
|
|
95
|
+
pytest
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Tests cover `protocol` and `parsers` with synthetic bytes. `transport` and
|
|
99
|
+
`services` need a live server and are exercised by `examples/demo.py`.
|
|
100
|
+
|
|
101
|
+
## Migrating from `main.py`
|
|
102
|
+
|
|
103
|
+
The single-file version lives in `legacy/main.py`. Differences:
|
|
104
|
+
|
|
105
|
+
- Results are dataclasses, not dicts: `tick['bid']` becomes `tick.bid`.
|
|
106
|
+
- `client.symbols` is now the symbol service; the symbol list is `client.symbol_list`.
|
|
107
|
+
- `connect()`/`login()` raise `ConnectionFailed`/`LoginFailed` (both subclass `ConnectionError`).
|
|
108
|
+
- Credentials come from `MT5_LOGIN` / `MT5_PASSWORD` in the examples.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "mt5direct"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Standalone MetaTrader 5 client over the native WebSocket protocol. No terminal required."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
dependencies = [
|
|
13
|
+
"pycryptodome>=3.19",
|
|
14
|
+
"websocket-client>=1.6",
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
[project.optional-dependencies]
|
|
18
|
+
dev = ["pytest>=7"]
|
|
19
|
+
|
|
20
|
+
[tool.setuptools.packages.find]
|
|
21
|
+
where = ["src"]
|
|
22
|
+
|
|
23
|
+
[tool.pytest.ini_options]
|
|
24
|
+
testpaths = ["tests"]
|
|
25
|
+
pythonpath = ["src"]
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"""mt5direct: standalone MetaTrader 5 client over the native WebSocket protocol."""
|
|
2
|
+
|
|
3
|
+
from .client import MT5Client
|
|
4
|
+
from .constants import * # noqa: F401,F403
|
|
5
|
+
from .exceptions import ConnectionFailed, LoginFailed, MT5Error
|
|
6
|
+
from .models import (
|
|
7
|
+
AccountInfo, Candle, Deal, Frame, HistoryOrder, Position, PositionPnL, Symbol, Tick,
|
|
8
|
+
TradeAck, TradeResult,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
__version__ = "0.1.0"
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
"MT5Client", "__version__",
|
|
15
|
+
"MT5Error", "ConnectionFailed", "LoginFailed",
|
|
16
|
+
"AccountInfo", "Candle", "Deal", "Frame", "HistoryOrder", "Position", "PositionPnL",
|
|
17
|
+
"Symbol", "Tick", "TradeAck", "TradeResult",
|
|
18
|
+
]
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
"""Facade that wires the session and services behind one object."""
|
|
2
|
+
|
|
3
|
+
from typing import Optional
|
|
4
|
+
|
|
5
|
+
from .constants.network import TRADE_HOSTS
|
|
6
|
+
from .models import Frame, Symbol
|
|
7
|
+
from .services import (
|
|
8
|
+
AccountService, AuthService, ClientState, MarketDataService, PositionService,
|
|
9
|
+
SymbolService, TradingService,
|
|
10
|
+
)
|
|
11
|
+
from .transport import Session
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class MT5Client:
|
|
15
|
+
"""Standalone MT5 client.
|
|
16
|
+
|
|
17
|
+
Domain operations live on ``auth``, ``account``, ``symbols``, ``market``, ``trading``
|
|
18
|
+
and ``positions``; the flat methods below delegate to them for convenience.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
def __init__(self, host: Optional[str] = None):
|
|
22
|
+
self.session = Session(host or TRADE_HOSTS[0])
|
|
23
|
+
self.state = ClientState()
|
|
24
|
+
self.account = AccountService(self.session, self.state)
|
|
25
|
+
self.symbols = SymbolService(self.session, self.state)
|
|
26
|
+
self.auth = AuthService(self.session, self.state, self.symbols, self.account)
|
|
27
|
+
self.market = MarketDataService(self.session, self.state, self.auth)
|
|
28
|
+
self.trading = TradingService(self.session, self.state)
|
|
29
|
+
self.positions = PositionService(self.session, self.state, self.trading)
|
|
30
|
+
|
|
31
|
+
# -- connection -------------------------------------------------------
|
|
32
|
+
|
|
33
|
+
@property
|
|
34
|
+
def host(self) -> str:
|
|
35
|
+
return self.session.host
|
|
36
|
+
|
|
37
|
+
@host.setter
|
|
38
|
+
def host(self, value: str) -> None:
|
|
39
|
+
self.session.host = value
|
|
40
|
+
|
|
41
|
+
@property
|
|
42
|
+
def server_id(self) -> int:
|
|
43
|
+
return self.session.server_id
|
|
44
|
+
|
|
45
|
+
def connect(self) -> None:
|
|
46
|
+
self.session.connect()
|
|
47
|
+
|
|
48
|
+
def close(self) -> None:
|
|
49
|
+
self.session.close()
|
|
50
|
+
|
|
51
|
+
def recv_any(self, timeout: float = 5) -> Optional[Frame]:
|
|
52
|
+
return self.session.recv_any(timeout=timeout)
|
|
53
|
+
|
|
54
|
+
def __enter__(self):
|
|
55
|
+
self.connect()
|
|
56
|
+
return self
|
|
57
|
+
|
|
58
|
+
def __exit__(self, *exc) -> None:
|
|
59
|
+
self.close()
|
|
60
|
+
|
|
61
|
+
# -- symbol tables ----------------------------------------------------
|
|
62
|
+
|
|
63
|
+
@property
|
|
64
|
+
def symbol_list(self) -> list[Symbol]:
|
|
65
|
+
return self.state.symbols
|
|
66
|
+
|
|
67
|
+
@property
|
|
68
|
+
def symbol_map(self) -> dict[str, Symbol]:
|
|
69
|
+
return self.state.symbol_map
|
|
70
|
+
|
|
71
|
+
@property
|
|
72
|
+
def ta_to_sym(self) -> dict[int, Symbol]:
|
|
73
|
+
return self.state.ta_to_sym
|
|
74
|
+
|
|
75
|
+
# -- auth -------------------------------------------------------------
|
|
76
|
+
|
|
77
|
+
def login(self, login_id: int, password: str):
|
|
78
|
+
return self.auth.login(login_id, password)
|
|
79
|
+
|
|
80
|
+
def ping(self):
|
|
81
|
+
return self.auth.ping()
|
|
82
|
+
|
|
83
|
+
def reconnect(self, login_id=None, password=None):
|
|
84
|
+
return self.auth.reconnect(login_id, password)
|
|
85
|
+
|
|
86
|
+
# -- account ----------------------------------------------------------
|
|
87
|
+
|
|
88
|
+
def get_account_info(self):
|
|
89
|
+
return self.account.get_account_info()
|
|
90
|
+
|
|
91
|
+
def init_trading(self):
|
|
92
|
+
return self.account.init_trading()
|
|
93
|
+
|
|
94
|
+
def get_history(self, from_ts=0, to_ts=0):
|
|
95
|
+
return self.account.get_history(from_ts, to_ts)
|
|
96
|
+
|
|
97
|
+
# -- symbols ----------------------------------------------------------
|
|
98
|
+
|
|
99
|
+
def get_symbol_types(self):
|
|
100
|
+
return self.symbols.get_symbol_types()
|
|
101
|
+
|
|
102
|
+
def get_symbols(self):
|
|
103
|
+
return self.symbols.get_symbols()
|
|
104
|
+
|
|
105
|
+
def resolve_symbols(self, ta_list):
|
|
106
|
+
return self.symbols.resolve_symbols(ta_list)
|
|
107
|
+
|
|
108
|
+
def subscribe(self, ta_values):
|
|
109
|
+
return self.symbols.subscribe(ta_values)
|
|
110
|
+
|
|
111
|
+
def subscribe_by_name(self, *names):
|
|
112
|
+
return self.symbols.subscribe_by_name(*names)
|
|
113
|
+
|
|
114
|
+
def subscribe_all(self):
|
|
115
|
+
return self.symbols.subscribe_all()
|
|
116
|
+
|
|
117
|
+
def parse_trade_sessions(self, resolve_data):
|
|
118
|
+
return self.symbols.parse_trade_sessions(resolve_data)
|
|
119
|
+
|
|
120
|
+
def parse_trade_modes(self, resolve_data):
|
|
121
|
+
return self.symbols.parse_trade_modes(resolve_data)
|
|
122
|
+
|
|
123
|
+
def find_resolve_bid_offset(self, resolve_data, sym_name, expected_price, digits, tolerance=0.02):
|
|
124
|
+
return self.symbols.find_resolve_bid_offset(resolve_data, sym_name, expected_price, digits, tolerance)
|
|
125
|
+
|
|
126
|
+
def extract_resolve_prices(self, resolve_data, bid_offset):
|
|
127
|
+
return self.symbols.extract_resolve_prices(resolve_data, bid_offset)
|
|
128
|
+
|
|
129
|
+
# -- market data ------------------------------------------------------
|
|
130
|
+
|
|
131
|
+
def get_candles(self, symbol, timeframe='H1', count=1000, from_ts=None, to_ts=None):
|
|
132
|
+
return self.market.get_candles(symbol, timeframe, count, from_ts, to_ts)
|
|
133
|
+
|
|
134
|
+
def parse_tick(self, data, offset=0):
|
|
135
|
+
return self.market.parse_tick(data, offset)
|
|
136
|
+
|
|
137
|
+
def on_tick(self, callback):
|
|
138
|
+
return self.market.on_tick(callback)
|
|
139
|
+
|
|
140
|
+
def start_tick_listener(self, symbols=None):
|
|
141
|
+
return self.market.start_tick_listener(symbols)
|
|
142
|
+
|
|
143
|
+
def stop_tick_listener(self):
|
|
144
|
+
return self.market.stop_tick_listener()
|
|
145
|
+
|
|
146
|
+
# -- trading ----------------------------------------------------------
|
|
147
|
+
|
|
148
|
+
def trade(self, *args, **kwargs):
|
|
149
|
+
return self.trading.trade(*args, **kwargs)
|
|
150
|
+
|
|
151
|
+
def wait_trade_result(self, timeout=30):
|
|
152
|
+
return self.trading.wait_trade_result(timeout)
|
|
153
|
+
|
|
154
|
+
def buy(self, symbol, lots, sl=0.0, tp=0.0, comment=""):
|
|
155
|
+
return self.trading.buy(symbol, lots, sl, tp, comment)
|
|
156
|
+
|
|
157
|
+
def sell(self, symbol, lots, sl=0.0, tp=0.0, comment=""):
|
|
158
|
+
return self.trading.sell(symbol, lots, sl, tp, comment)
|
|
159
|
+
|
|
160
|
+
def buy_limit(self, symbol, lots, price, sl=0.0, tp=0.0, comment=""):
|
|
161
|
+
return self.trading.buy_limit(symbol, lots, price, sl, tp, comment)
|
|
162
|
+
|
|
163
|
+
def sell_limit(self, symbol, lots, price, sl=0.0, tp=0.0, comment=""):
|
|
164
|
+
return self.trading.sell_limit(symbol, lots, price, sl, tp, comment)
|
|
165
|
+
|
|
166
|
+
def buy_stop(self, symbol, lots, price, sl=0.0, tp=0.0, comment=""):
|
|
167
|
+
return self.trading.buy_stop(symbol, lots, price, sl, tp, comment)
|
|
168
|
+
|
|
169
|
+
def sell_stop(self, symbol, lots, price, sl=0.0, tp=0.0, comment=""):
|
|
170
|
+
return self.trading.sell_stop(symbol, lots, price, sl, tp, comment)
|
|
171
|
+
|
|
172
|
+
def modify_sl_tp(self, symbol, position_ticket, sl=0.0, tp=0.0):
|
|
173
|
+
return self.trading.modify_sl_tp(symbol, position_ticket, sl, tp)
|
|
174
|
+
|
|
175
|
+
def modify_pending(self, symbol, order_ticket, price, sl=0.0, tp=0.0):
|
|
176
|
+
return self.trading.modify_pending(symbol, order_ticket, price, sl, tp)
|
|
177
|
+
|
|
178
|
+
def cancel_order(self, symbol, order_ticket):
|
|
179
|
+
return self.trading.cancel_order(symbol, order_ticket)
|
|
180
|
+
|
|
181
|
+
def close_position(self, symbol, ticket, lots=None, pos_type='BUY'):
|
|
182
|
+
return self.trading.close_position(symbol, ticket, lots, pos_type)
|
|
183
|
+
|
|
184
|
+
def close_by_order(self, symbol, order_ticket, lots, pos_type='BUY'):
|
|
185
|
+
return self.trading.close_by_order(symbol, order_ticket, lots, pos_type)
|
|
186
|
+
|
|
187
|
+
# -- positions --------------------------------------------------------
|
|
188
|
+
|
|
189
|
+
def get_positions(self):
|
|
190
|
+
return self.positions.get_positions()
|
|
191
|
+
|
|
192
|
+
def close_all(self, symbol=None):
|
|
193
|
+
return self.positions.close_all(symbol)
|
|
194
|
+
|
|
195
|
+
def track_positions(self, duration=60, callback=None):
|
|
196
|
+
return self.positions.track_positions(duration, callback)
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
from .commands import * # noqa: F401,F403
|
|
2
|
+
from .network import INITIAL_KEY, TRADE_HOSTS, WS_PATH, WS_PORT # noqa: F401
|
|
3
|
+
from .orders import * # noqa: F401,F403
|
|
4
|
+
from .retcodes import RETCODE_DONE, RETCODE_DONE_PARTIAL, RETCODES, retcode_name # noqa: F401
|
|
5
|
+
from .timeframes import TF_SECONDS, TIMEFRAMES # noqa: F401
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"""Protocol command identifiers (u16, little-endian)."""
|
|
2
|
+
|
|
3
|
+
CMD_HELLO = 0
|
|
4
|
+
CMD_ACCOUNT = 3
|
|
5
|
+
CMD_INIT_TRADING = 4
|
|
6
|
+
CMD_HISTORY = 5
|
|
7
|
+
CMD_SUBSCRIBE = 7
|
|
8
|
+
CMD_TICK = 8
|
|
9
|
+
CMD_SYMBOL_TYPES = 9
|
|
10
|
+
CMD_CANDLES = 11
|
|
11
|
+
CMD_TRADE = 12
|
|
12
|
+
CMD_RESOLVE = 18
|
|
13
|
+
CMD_TRADE_RESULT = 19
|
|
14
|
+
CMD_LOGIN = 28
|
|
15
|
+
CMD_SYMBOLS = 34
|
|
16
|
+
CMD_PING = 51
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"""Connection endpoints and the bootstrap AES key used before the hello handshake."""
|
|
2
|
+
|
|
3
|
+
INITIAL_KEY = bytes.fromhex('02de02a1a65cc794684fcbea1ecb0fd74ae657e43662c11eee885d2fd64f4964')
|
|
4
|
+
|
|
5
|
+
TRADE_HOSTS = [
|
|
6
|
+
"mt5trial6.exwebterm.com",
|
|
7
|
+
"8.219.172.6", "47.236.224.248", "16.79.3.122",
|
|
8
|
+
"47.130.41.116", "57.182.183.85", "18.61.99.175",
|
|
9
|
+
]
|
|
10
|
+
|
|
11
|
+
WS_PORT = 443
|
|
12
|
+
WS_PATH = "/terminal"
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"""Order, execution and filling constants used by trade requests."""
|
|
2
|
+
|
|
3
|
+
EXEC_INSTANT_NOPX = 0
|
|
4
|
+
EXEC_INSTANT_PX = 1
|
|
5
|
+
EXEC_MARKET = 3
|
|
6
|
+
EXEC_EXCHANGE = 4
|
|
7
|
+
|
|
8
|
+
ORDER_BUY = 0
|
|
9
|
+
ORDER_SELL = 1
|
|
10
|
+
ORDER_BUY_LIMIT = 2
|
|
11
|
+
ORDER_SELL_LIMIT = 3
|
|
12
|
+
ORDER_BUY_STOP = 4
|
|
13
|
+
ORDER_SELL_STOP = 5
|
|
14
|
+
ORDER_BUY_STOP_LIMIT = 6
|
|
15
|
+
ORDER_SELL_STOP_LIMIT = 7
|
|
16
|
+
ORDER_CLOSE_BY = 8
|
|
17
|
+
|
|
18
|
+
TRADE_ACTION_DEAL = 3
|
|
19
|
+
TRADE_ACTION_PENDING = 5
|
|
20
|
+
TRADE_ACTION_SLTP = 6
|
|
21
|
+
TRADE_ACTION_MODIFY = 7
|
|
22
|
+
TRADE_ACTION_REMOVE = 8
|
|
23
|
+
|
|
24
|
+
FILLING_FOK = 0
|
|
25
|
+
FILLING_IOC = 1
|
|
26
|
+
FILLING_RETURN = 2
|
|
27
|
+
|
|
28
|
+
FLAGS_NONE = 0
|
|
29
|
+
FLAGS_XA = 2
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"""Trade server return codes."""
|
|
2
|
+
|
|
3
|
+
RETCODE_DONE = 10009
|
|
4
|
+
RETCODE_DONE_PARTIAL = 10011
|
|
5
|
+
|
|
6
|
+
RETCODES = {
|
|
7
|
+
0: 'PENDING', 1: 'OK',
|
|
8
|
+
10001: 'ON_THE_WAY', 10002: 'ACCEPTED', 10003: 'PROCESSED',
|
|
9
|
+
10004: 'REQUOTE', 10005: 'PRICES', 10006: 'REJECTED', 10007: 'CANCELED',
|
|
10
|
+
10008: 'PLACED', 10009: 'DONE', 10011: 'DONE_PARTIAL',
|
|
11
|
+
10012: 'TIMEOUT', 10013: 'INVALID', 10014: 'INVALID_VOLUME',
|
|
12
|
+
10015: 'INVALID_PRICE', 10016: 'INVALID_SLTP',
|
|
13
|
+
10017: 'TRADE_DISABLED', 10018: 'MARKET_CLOSED', 10019: 'NO_MONEY',
|
|
14
|
+
10020: 'PRICE_CHANGED', 10021: 'OFF_QUOTES',
|
|
15
|
+
10026: 'ALGO_DISABLED_SERVER', 10027: 'ALGO_DISABLED_CLIENT',
|
|
16
|
+
10023: 'ORDER_LOCKED', 10024: 'LONG_ONLY',
|
|
17
|
+
10030: 'UNSUPPORTED_FILLING', 10033: 'ORDERS_LIMIT',
|
|
18
|
+
10034: 'VOLUME_LIMIT', 10035: 'INVALID_ORDER_TYPE',
|
|
19
|
+
10036: 'INVALID_ORDER', 10038: 'POSITION_NOT_FOUND',
|
|
20
|
+
10040: 'LIMIT_POSITIONS', 10041: 'NO_CHANGES',
|
|
21
|
+
10042: 'CONNECTION_LOST', 10043: 'FREQUENT_REQUESTS',
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def retcode_name(code: int) -> str:
|
|
26
|
+
return RETCODES.get(code, f'UNKNOWN_{code}')
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"""Timeframe identifiers and their length in seconds."""
|
|
2
|
+
|
|
3
|
+
TIMEFRAMES = {
|
|
4
|
+
'M1': 1, 'M2': 2, 'M3': 3, 'M4': 4, 'M5': 5, 'M6': 6,
|
|
5
|
+
'M10': 10, 'M12': 12, 'M15': 15, 'M20': 20, 'M30': 30,
|
|
6
|
+
'H1': 16385, 'H2': 16386, 'H3': 16387, 'H4': 16388,
|
|
7
|
+
'H6': 16390, 'H8': 16392, 'H12': 16396,
|
|
8
|
+
'D1': 16408, 'W1': 32769, 'MN1': 49153,
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
TF_SECONDS = {
|
|
12
|
+
1: 60, 2: 120, 3: 180, 4: 240, 5: 300, 6: 360,
|
|
13
|
+
10: 600, 12: 720, 15: 900, 20: 1200, 30: 1800,
|
|
14
|
+
16385: 3600, 16386: 7200, 16387: 10800, 16388: 14400,
|
|
15
|
+
16390: 21600, 16392: 28800, 16396: 43200,
|
|
16
|
+
16408: 86400, 32769: 604800, 49153: 2592000,
|
|
17
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"""Exception hierarchy for mt5direct."""
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class MT5Error(Exception):
|
|
5
|
+
"""Base class for all mt5direct errors."""
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class ConnectionFailed(MT5Error, ConnectionError):
|
|
9
|
+
"""Hello handshake with the trade server failed."""
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class LoginFailed(MT5Error, ConnectionError):
|
|
13
|
+
"""Server rejected the login request."""
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
from .account import AccountInfo
|
|
2
|
+
from .candle import Candle
|
|
3
|
+
from .frame import Frame
|
|
4
|
+
from .history import Deal, HistoryOrder
|
|
5
|
+
from .position import Position, PositionPnL
|
|
6
|
+
from .symbol import Symbol
|
|
7
|
+
from .tick import Tick
|
|
8
|
+
from .trade import TradeAck, TradeResult
|
|
9
|
+
|
|
10
|
+
__all__ = [
|
|
11
|
+
"AccountInfo", "Candle", "Deal", "Frame", "HistoryOrder",
|
|
12
|
+
"Position", "PositionPnL", "Symbol", "Tick", "TradeAck", "TradeResult",
|
|
13
|
+
]
|