mt5direct
Standalone MetaTrader 5 client in pure Python. It speaks the terminal's native WebSocket protocol directly to the broker's trade server, so there is no MT5 desktop, no Wine, no bridge DLL, and it runs on Linux and macOS.
- Login, account info and live equity/margin summary
- Symbol list and trading specifications (contract size, volumes, trade mode)
- Candles (optionally as a pandas DataFrame), live ticks, trade and position events
- Market, pending, modify, cancel and close orders; open positions and pending orders
- Deal and order history with order and position ids, prices, volumes and comments
- Heartbeat and automatic reconnect that re-logs in and re-subscribes
- Sync and asyncio clients, typed results (frozen dataclasses), offline-testable parsers
Contents
- Install
- Choosing a server
- Quick start
- Usage
- API overview
- Return values and errors
- Documentation
- Development
- Releasing
- Security
- Disclaimer
Install
pip install mt5direct
Requires Python 3.10+. Dependencies (pycryptodome, websocket-client) are
installed automatically.
From source:
git clone https://github.com/rhamaxv/mt5direct
cd mt5direct
pip install -e '.[dev]'
Choosing a server
Same flow as the MT5 app: search the company, pick a server from the list, connect.
from mt5direct import MT5Client, list_servers
list_servers("finex")
# ['FinexBisnisSolusi-Demo', 'FinexBisnisSolusi-Real']
client = MT5Client.from_server("FinexBisnisSolusi-Real")
client.connect()
client.login(login, password)
list_servers queries MetaQuotes' public directory (about 2,800 MT5 servers).
from_server looks the host up in the web terminal registry, verifies it, and
connects. MT5Client(server="...") does the same.
How a name becomes a host
Every broker runs its own MT5 Web Terminal, and each terminal host serves exactly
one trade server, which it announces in window.__terminal_params on its
/terminal page. MetaQuotes publishes server names but never these hosts, so
the library carries a registry and verifies every candidate before using it:
- Registry lookup: a JSON file bundled with the package
(
src/mt5direct/data/webterminals.json), refreshed from GitHub once a day and cached under~/.cache/mt5direct/. Exact entries (FinexBisnisSolusi-Real→real-terminal.finex.co.id,HFMarketsGlobal-Live3→live3.webterminal-hfm.com:1951) and broker patterns (Exness→{code}.exwebterm.com). - Guesses for brokers not in the registry: a few host patterns that brokers commonly use.
- Verification: each candidate's
/terminalpage must announce exactly the requested server. A verified guess is remembered in~/.cache/mt5direct/learned.jsonso the next call is instant.
If nothing verifies, ServerNotFound tells you what was tried and how to fix it.
What does not work, and why
Verified against the live services on 2026-11-23, so nobody has to re-explore:
web.metatrader.appserves onlyMetaQuotes-Demo. Its?servers=parameter is ignored; it is not a universal proxy.- MetaQuotes' hosted web terminal (
metatraderweb.app/trade) uses an older, different protocol throughgwt*.mql5.com, and only opted-in brokers are enabled there. Exness is not. - The public directory lists names only, never hosts. The new web terminal's
JavaScript makes no HTTP calls at all; it only opens
wss://<its own host>/terminal. - A broker that does not offer an MT5 Web Terminal cannot be reached with this protocol by any means.
Quick start
import os
from mt5direct import MT5Client
with MT5Client.from_server("Exness-MT5Trial6") as client:
client.login(int(os.environ["MT5_LOGIN"]), os.environ["MT5_PASSWORD"])
client.get_symbols() # download the symbol table (required once)
client.init_trading() # open the trading session
acct = client.get_account_info()
print(acct.balance, acct.currency)
candles = client.get_candles("EURUSD", "H1", count=100)
print(candles[-1].close)
result = client.buy("EURUSD", 0.01)
print(result.retcode_str, result.deal, result.price)
with calls connect() on entry and close() on exit. Without with, call
them yourself.
Usage
Session setup
The three calls after connect() are always the same:
client.login(login_id, password) # raises LoginFailed on rejection
client.get_symbols() # fills client.symbol_list / symbol_map / ta_to_sym
client.init_trading() # server starts sending trade events
Skipping get_symbols() leaves ticks unnamed (symbol="#<id>") and
subscribe_by_name with nothing to look up.
Account
acct = client.get_account_info() # balance, credit, currency, leverage, group, server, company, stopout_level
summary = client.get_account_summary() # + equity, margin, free_margin, margin_level, profit from open positions
deals, orders = client.get_history() # list[Deal], list[HistoryOrder]
deals, _ = client.get_history(from_ts=1700000000, to_ts=1700600000)
Symbols
for s in client.get_symbols():
print(s.name, s.digits, s.path)
eurusd = client.symbol_map["EURUSD"] # Symbol
info = client.get_symbol_info("EURUSD", "XAUUSD")["XAUUSD"] # SymbolInfo
info.contract_size, info.volume_min, info.volume_step, info.trade_mode, info.tick_value
Candles
candles = client.get_candles("XAUUSD", "M15", count=500)
candles = client.get_candles("XAUUSD", "D1", from_ts=1690000000, to_ts=1700000000)
Timeframes: M1 M2 M3 M4 M5 M6 M10 M12 M15 M20 M30 H1 H2 H3 H4 H6 H8 H12 D1 W1 MN1
(see mt5direct.TIMEFRAMES). Up to count most recent candles are returned.
With pip install 'mt5direct[pandas]', client.get_candles_df(...) returns a
DataFrame indexed by UTC time.
Live ticks
client.subscribe_by_name("EURUSD", "XAUUSD")
# or everything: client.subscribe_all()
def on_tick(tick):
print(tick.symbol, tick.bid, tick.ask, tick.spread)
client.on_tick(on_tick)
thread = client.start_tick_listener() # daemon thread; reconnects on error
...
client.stop_tick_listener()
Other events from the same listener:
client.on_trade(lambda r: print(r.retcode_str, r.deal, r.price)) # every execution report
client.on_transaction(lambda tx: print(tx.kind, tx.deal, tx.position)) # every order/deal/position event
client.on_position_change(lambda positions: print(len(positions))) # open positions after each change
Keep the session alive and self-healing while the listener runs:
client.start_heartbeat(interval=30) # pings; on failure reconnects, re-logs in with the token, re-subscribes
Requests from your main thread (buy, ping, get_positions, ...) are safe while
the listener is running: replies are routed back to the caller through the session
queue.
If you prefer to drive the loop yourself, read frames directly:
from mt5direct.constants import CMD_TICK
frame = client.recv_any(timeout=1)
if frame and frame.cmd == CMD_TICK:
for tick in client.market.parse_ticks(frame.payload):
...
Orders
client.buy("EURUSD", 0.01, sl=1.0800, tp=1.1000, comment="bot")
client.sell("EURUSD", 0.01)
client.buy_limit("EURUSD", 0.01, price=1.0850)
client.sell_stop("EURUSD", 0.01, price=1.0800, sl=1.0850)
client.modify_sl_tp("EURUSD", position_ticket, sl=1.0820, tp=1.0950)
client.modify_pending("EURUSD", order_ticket, price=1.0860)
client.cancel_order("EURUSD", order_ticket)
Lots are plain floats (0.01). Prices are plain floats in the symbol's own
scale. 0.0 for sl/tp means "not set".
Positions
for p in client.get_positions():
print(p.ticket, p.type, p.symbol, p.volume, p.price_open, p.profit, p.margin)
for o in client.get_pending_orders():
print(o.ticket, o.type, o.symbol, o.price, o.volume, o.comment)
positions, pending = client.get_positions_and_orders() # one round-trip
client.close_position("EURUSD", ticket, lots=0.01, pos_type="BUY")
closed, total = client.close_all() # every position
closed, total = client.close_all("EURUSD") # one symbol
def on_pnl(rows):
for r in rows:
print(r.position.ticket, r.current_price, round(r.pnl, 2))
client.track_positions(duration=60, callback=on_pnl)
Low-level trade request
All order helpers call client.trade(...), which sends one raw request and
returns the server's acknowledgement. Use it when you need a combination the
helpers do not cover:
from mt5direct import ORDER_BUY, TRADE_ACTION_DEAL, FILLING_IOC
ack = client.trade(ORDER_BUY, "EURUSD", 0.05, exec_mode=TRADE_ACTION_DEAL,
filling_ea=FILLING_IOC, deviation=20)
if ack and ack.retcode == 0:
result = client.wait_trade_result(timeout=30)
Persistent sessions (no password on later runs)
client = MT5Client.from_server("Exness-MT5Trial6")
client.connect()
client.login(login, password)
client.save_session() # ~/.cache/mt5direct/sessions/<login>@Exness-MT5Trial6.json
# any later run, any process:
client = MT5Client.resume(login=login, server="Exness-MT5Trial6")
What is saved: the login token the server returns, its um companion, the
client's random device id and the server's wt_dtoken cookie. The password is
not saved. The server accepts the token only when the cookie and device id match
the ones used at login, and the cookie is issued for ten years. Automatic
reconnects use the same token path. If the server ever rejects the token,
resume raises LoginFailed: log in with the password once and save again.
asyncio
from mt5direct import AsyncMT5Client
async with AsyncMT5Client.from_server("Exness-MT5Trial6") as client:
await client.login(login, password)
await client.get_symbols()
acct = await client.get_account_summary()
result = await client.buy("EURUSD", 0.01)
Every blocking call runs in a worker thread; callbacks and start_*/stop_*
methods are plain functions.
API overview
Everything is reachable two ways: flat on the client (client.buy(...)) or
grouped by domain (client.trading.buy(...)). Both call the same code.
| Group | Methods |
|---|---|
client |
from_server, connect, close, recv_any, host, address, endpoint, server_id, symbol_list, symbol_map, ta_to_sym |
mt5direct.discovery |
list_servers, resolve_server, discover_from_url, inspect_host, load_registry, candidate_addresses |
client.auth |
login, login_with_token, logout, ping, reconnect, auto_reconnect |
client |
save_session, resume (persistent login) |
client.account |
get_account_info, get_account_summary, init_trading, get_history |
client.symbols |
get_symbol_types, get_symbols, get_symbol_info, resolve_symbols, subscribe, subscribe_by_name, subscribe_all |
client.market |
get_candles, get_candles_df, on_tick, on_trade, on_position_change, start_tick_listener, stop_tick_listener, start_heartbeat, stop_heartbeat |
client.trading |
trade, wait_trade_result, buy, sell, buy_limit, sell_limit, buy_stop, sell_stop, modify_sl_tp, modify_pending, cancel_order, close_position, close_by_order |
client.positions |
get_positions, get_pending_orders, get_positions_and_orders, close_all, track_positions |
Full signatures and field lists: docs/api.md.
Return values and errors
Results are frozen dataclasses from mt5direct.models:
| Model | Returned by |
|---|---|
AccountInfo |
get_account_info |
AccountSummary |
get_account_summary |
SymbolInfo |
get_symbol_info |
Symbol |
get_symbols, symbol_map, ta_to_sym |
Tick |
tick callbacks, parse_ticks |
Candle |
get_candles |
Position |
get_positions, on_position_change |
PendingOrder |
get_pending_orders |
PositionPnL |
track_positions callback |
Deal, HistoryOrder |
get_history |
TradeAck |
trade, and order helpers when rejected up front |
TradeResult |
wait_trade_result, order helpers on execution, on_trade |
Frame |
recv_any, init_trading, ping, resolve_symbols |
Order helpers (buy, sell, close_position, ...) return:
TradeResultwhen the server reports execution (retcode10009 or 10011) or a final rejection (any other non-transient code);TradeAckwhen the request was refused before reaching the dealer (ack.retcode != 0);Nonewhen nothing came back within the timeout.
Exceptions, all subclasses of mt5direct.MT5Error:
| Exception | Raised by | Meaning |
|---|---|---|
ServerNotFound |
MT5Client(server=...), resolve_server |
No web terminal host announces that server name |
ConnectionFailed |
connect |
WebSocket opened but the hello handshake failed |
LoginFailed |
login |
Server rejected the credentials |
Both also subclass ConnectionError. Read-style calls (get_candles,
get_positions, ...) never raise on timeout; they return None or an empty
list. Return codes are in mt5direct.RETCODES; see
docs/errors.md.
Documentation
| Document | Contents |
|---|---|
| docs/api.md | Every public method with signature, parameters and return type; every model with its fields |
| docs/protocol.md | Wire format: framing, encryption, handshake, command ids, record layouts |
| docs/errors.md | Exceptions, None/empty returns, full return-code table |
| docs/architecture.md | Package layers, dependency rules, how a call travels through the stack |
| docs/contributing.md | Dev setup, tests, commit format, adding a parser or command |
| examples/ | demo.py (full walkthrough), find_server.py (search → pick → connect), stream_ticks.py, place_order.py |
Development
pip install -e '.[dev]'
pytest
Tests cover protocol and parsers with synthetic bytes and need no network.
transport and services need a live server; run examples/demo.py with
MT5_LOGIN and MT5_PASSWORD set.
Releasing
- Bump
versioninpyproject.toml. - Commit, tag and push:
git tag v0.3.1 && git push origin main --tags
The Publish to PyPI workflow runs the tests, checks that the tag matches the
version, builds, and uploads through PyPI Trusted Publishing.
Security
- TLS certificates are verified on every connection (WebSocket and discovery).
MT5Client(..., verify_ssl=False)exists for brokers with self-signed certificates; it exposes your password and orders to anyone on the path, so avoid it. - The password is never written to disk.
save_sessionstores only the server's login token, cookie and the client's device id (mode 0600); anyone who copies that file can use the account until the token is revoked by a password change, so treat it like a password. Examples read credentials from environment variables. - The server registry is fetched from this repository over HTTPS and cached under
~/.cache/mt5direct/;MT5DIRECT_OFFLINE=1disables the fetch. Every host from the registry is still verified against its own/terminalannouncement before use. - Nothing is sent anywhere except the broker's web terminal host and, for
list_servers, MetaQuotes' public directory.
Disclaimer
This library implements a reverse-engineered protocol and is not affiliated with or endorsed by MetaQuotes Ltd. Use it with demo accounts first, read your broker's terms, and trade at your own risk.
Metadata
Release files for mt5direct 0.3.0
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 |
|---|---|---|---|---|
| mt5direct-0.3.0.tar.gz | 54.2 kB | With provenance | Details for mt5direct-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mt5direct-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for mt5direct-0.3.0-py3-none-any.whl |
Total release size: 113.2 kB
Release files / mt5direct-0.3.0.tar.gz
| Download URL | mt5direct-0.3.0.tar.gz |
|---|---|
| Size | 54.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
617fa67b42e0d30c5084c90bdd9e1ed1e335360c08e054e07782c4ba44a333cc
|
|
BLAKE2b-256 checksum How to use checksums |
ea88d1cd1c3ed071f943c97a99080d0d60e4ab918d57359e336c5819b50f672c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logRelease files / mt5direct-0.3.0-py3-none-any.whl
| Download URL | mt5direct-0.3.0-py3-none-any.whl |
|---|---|
| Size | 59.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
93240736294cac36ef1180c5429d895ed0233f07ecb5ef4183a6ff779b81a9e7
|
|
BLAKE2b-256 checksum How to use checksums |
a97945892d97e0cbc8a064a0f6bfbe4428c9f0930d21543b9e56e79c780587f4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logSecurity for mt5direct 0.3.0
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.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for mt5direct-0.3.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logA 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.