Skip to main content

mt5direct

Standalone MetaTrader 5 client in pure Python. Talks the terminal's native WebSocket protocol directly to the broker, so no MT5 desktop, no Wine, no bridge DLL.

Install

pip install -e .

Requires Python 3.10+, pycryptodome and websocket-client.

Quick start

import os
from mt5direct import MT5Client

with MT5Client() as client:
    client.login(int(os.environ["MT5_LOGIN"]), os.environ["MT5_PASSWORD"])
    client.get_symbols()
    client.init_trading()

    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)

Live ticks

client.subscribe_by_name("EURUSD", "XAUUSD")
client.on_tick(lambda t: print(t.symbol, t.bid, t.ask))
client.start_tick_listener()

Positions

for p in client.get_positions():
    print(p.ticket, p.type, p.symbol, p.volume, p.price_open)

client.close_all("EURUSD")

More in examples/.

API layout

Every operation is available two ways: flat on the client (client.buy(...)) or grouped by domain (client.trading.buy(...)).

Group Methods
client.auth login, ping, reconnect, auto_reconnect
client.account get_account_info, init_trading, get_history
client.symbols get_symbols, resolve_symbols, subscribe, subscribe_by_name, subscribe_all
client.market get_candles, on_tick, start_tick_listener, stop_tick_listener
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
client.positions get_positions, close_all, track_positions

Results are frozen dataclasses from mt5direct.models: AccountInfo, Symbol, Tick, Candle, Position, Deal, TradeAck, TradeResult.

Order methods return a TradeResult when the server confirms execution, the raw TradeAck when the request was rejected up front, or None on timeout.

Package structure

src/mt5direct/
  client.py       facade
  services/       one module per domain: auth, account, symbols, market_data, trading, positions
  transport/      WebSocket connection, AES session, message queue
  protocol/       crypto, framing, field serializer, binary reader, record layouts
  parsers/        bytes -> models, pure functions
  models/         dataclasses
  constants/      command ids, order types, retcodes, timeframes, hosts

Dependencies point downward only: client -> services -> transport -> protocol, and services -> parsers -> protocol, models.

Tests

pip install -e '.[dev]'
pytest

Tests cover protocol and parsers with synthetic bytes. transport and services need a live server and are exercised by examples/demo.py.

Migrating from main.py

The single-file version lives in legacy/main.py. Differences:

  • Results are dataclasses, not dicts: tick['bid'] becomes tick.bid.
  • client.symbols is now the symbol service; the symbol list is client.symbol_list.
  • connect()/login() raise ConnectionFailed/LoginFailed (both subclass ConnectionError).
  • Credentials come from MT5_LOGIN / MT5_PASSWORD in the examples.

Metadata

Release files for mt5direct 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mt5direct 0.1.0
File Size Uploaded Provenance Actions
mt5direct-0.1.0.tar.gz 22.7 kB Without provenance Details for mt5direct-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mt5direct 0.1.0
File Interpreter ABI Platform Actions
mt5direct-0.1.0-py3-none-any.whl Python 3 none any Details for mt5direct-0.1.0-py3-none-any.whl

Total release size: 54.6 kB

Release files / mt5direct-0.1.0.tar.gz

Download URL mt5direct-0.1.0.tar.gz
Size 22.7 kB
Tags Source
SHA-256 checksum
How to use checksums
4a16faec68eb917c7d91aafb06824a05f4ae02b92a6af0e8ce4ae3be1a421178
BLAKE2b-256 checksum
How to use checksums
2a2e46e3d0e76a0f011c22321f5e7131b8f179963004c55271762dfb6207df6d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.0

Release files / mt5direct-0.1.0-py3-none-any.whl

Download URL mt5direct-0.1.0-py3-none-any.whl
Size 32.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3bf47465b57200ce5339739f0baffc10aad29bfbf91a426297a49cf084e2287e
BLAKE2b-256 checksum
How to use checksums
a85d156206528f569fba4c0e8299d3fd70cecd2c58f0152c4f1acbe1431f0d49
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.0

Security for mt5direct 0.1.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.

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page