Skip to main content

MetaTrader 5 native Python library for macOS - control MT5 from Python scripts

Project description

PyPI License Python Platform

mt5_mac

MetaTrader 5 native Python library for macOS.

Control MetaTrader 5 installed on your Mac directly from Python — no Windows, no remote servers, no Docker. Uses the Wine runtime bundled inside MetaTrader 5.app to run the official MetaTrader5 Python package.

Features

  • Full API matching the official MetaTrader5 package
  • Account info, tick data, historical rates, symbols
  • Market orders, position management, pending orders
  • Deal history and order history
  • Auto-setup — downloads Python + dependencies on first use
  • No Windows, no Docker, no remote servers needed

Installation

pip install mt5_mac

Requirements

  • macOS (Intel or Apple Silicon)
  • MetaTrader 5 for macOS installed from metatrader5.com
  • Python 3.9+

On first use, the library automatically downloads Python 3.9 for Windows and installs the MetaTrader5 pip package inside MT5's bundled Wine environment. An internet connection is required for this one-time setup (~8 MB download).

Quick Start

import mt5_mac as mt5

# Initialize
if not mt5.initialize():
    print("initialize() failed - is MetaTrader 5 installed?")
    quit()

# Login
if not mt5.login(12345678, "your_password", "YourBroker-Server"):
    print("login() failed - check your credentials")
    mt5.shutdown()
    quit()

# Account info
info = mt5.account_info()
print(f"Balance: {info.balance:.2f} {info.currency}")
print(f"Equity: {info.equity:.2f}")
print(f"Leverage: 1:{info.leverage}")

# Live tick data
tick = mt5.symbol_info_tick("EURUSD")
print(f"EURUSD: bid={tick.bid} ask={tick.ask}")

# Historical rates
rates = mt5.copy_rates_from_pos("EURUSD", mt5.TIMEFRAME_M1, 0, 10)
for r in rates:
    print(f"[{r.time}] O={r.open} H={r.high} L={r.low} C={r.close}")

# Open positions
positions = mt5.positions_get()
print(f"Open positions: {len(positions) if positions else 0}")

# Place a market order
symbol = "EURUSD"
tick = mt5.symbol_info_tick(symbol)
request = {
    "action": mt5.TRADE_ACTION_DEAL,
    "symbol": symbol,
    "volume": 0.01,
    "type": mt5.ORDER_TYPE_BUY,
    "price": tick.ask,
    "sl": tick.ask - 0.0050,
    "tp": tick.ask + 0.0100,
    "deviation": 10,
    "magic": 123456,
    "comment": "mt5_mac",
    "type_time": mt5.ORDER_TIME_GTC,
    "type_filling": mt5.ORDER_FILLING_IOC,
}
result = mt5.order_send(request)
if result.retcode == mt5.RES_E_SUCCESS:
    print(f"Order placed: #{result.order} @ {result.price}")
else:
    print(f"Order failed: retcode={result.retcode}")

# Cleanup
mt5.shutdown()

Complete API Reference

Initialization & Connection

Function Description
initialize(path=None, login=None, password=None, server="", quiet=False) Initialize connection to MT5 terminal. Optionally log in directly.
shutdown() Disconnect from MT5 and release resources.
login(login_id, password, server="") Log in to a trading account.
last_error() Get the last error code (always 0 in current version).

Account & Market Data

Function Returns Description
account_info() MqlAccountInfo Balance, equity, margin, leverage, server, etc.
symbols_get(symbol=None) tuple[MqlSymbolInfo] All available symbols, or filter by name.
symbol_info(symbol) MqlSymbolInfo Detailed symbol specifications (spread, digits, etc.).
symbol_info_tick(symbol) MqlTick Latest tick: bid, ask, last, volume, time.
symbol_select(symbol, enable=True) bool Add/remove symbol from MarketWatch.

Historical Data

Function Returns Description
copy_rates_from_pos(symbol, tf, start, count) tuple[MqlRates] Rates starting from a position index.
copy_rates_from(symbol, tf, date_from, count) tuple[MqlRates] Rates starting from a date (Unix timestamp).
copy_rates_range(symbol, tf, date_from, date_to) tuple[MqlRates] Rates for a date range.

Trading

Function Returns Description
order_send(request) MqlTradeResult Place/modify/close orders.
positions_get(symbol="") tuple[MqlPosition] Get open positions, optionally filtered by symbol.
orders_get(symbol="") tuple[MqlOrder] Get pending orders, optionally filtered by symbol.
history_deals_get(from=0, to=0) tuple[MqlDeal] Get deal history.
history_orders_get(from=0, to=0) tuple[MqlOrder] Get order history.

Constants

Timeframes: TIMEFRAME_M1, M5, M15, M30, H1, H4, D1, W1, MN1

Order Types: ORDER_TYPE_BUY, ORDER_TYPE_SELL, ORDER_TYPE_BUY_LIMIT, ORDER_TYPE_SELL_LIMIT, ORDER_TYPE_BUY_STOP, ORDER_TYPE_SELL_STOP

Trade Actions: TRADE_ACTION_DEAL, TRADE_ACTION_PENDING, TRADE_ACTION_SLTP, TRADE_ACTION_MODIFY, TRADE_ACTION_REMOVE

Order Fillings: ORDER_FILLING_FOK, ORDER_FILLING_IOC, ORDER_FILLING_RETURN

Order Times: ORDER_TIME_GTC, ORDER_TIME_DAY, ORDER_TIME_SPECIFIED

Data Classes

MqlTick: time, bid, ask, last, volume, time_msc, flags, volume_real

MqlRates: time, open, high, low, close, tick_volume, spread, real_volume

MqlAccountInfo: login, trade_mode, leverage, balance, credit, profit, equity, margin, margin_free, margin_level, name, server, currency, company

MqlTradeResult: retcode, deal, order, volume, price, bid, ask, comment, request_id

MqlPosition: ticket, time, type, magic, identifier, symbol, volume, price_open, sl, tp, price_current, swap, profit

MqlOrder: ticket, time_setup, type, state, magic, symbol, volume_current, volume_initial, price_open, sl, tp, price_current, comment

MqlDeal: ticket, order, time, type, entry, magic, position, symbol, volume, price, commission, swap, profit, fee

MqlSymbolInfo: name, digits, spread, point, trade_mode, bid, ask, volume_min, volume_max, volume_step, contract_size, currency_base, currency_profit, description, path

How It Works

MetaTrader 5 for macOS is the Windows version of MT5 running inside a bundled Wine (CrossOver) wrapper.

┌─────────────────────────────────────────────────┐
│  Your Python Script (macOS)                      │
│  import mt5_mac as mt5                           │
│  mt5.initialize()                                │
│  mt5.login(123, "pwd", "server")                 │
│  info = mt5.account_info()                       │
│  mt5.shutdown()                                  │
└──────────────┬──────────────────────────────────┘
               │ JSON over stdin/stdout
               ▼
┌─────────────────────────────────────────────────┐
│  Wine Subprocess (inside MT5.app's bundled Wine) │
│  Python 3.9 for Windows                          │
│  import MetaTrader5 as mt5                       │
│  mt5.initialize(path=...)                        │
│  mt5.login(...)                                  │
│  mt5.account_info()                              │
└──────────────┬──────────────────────────────────┘
               │ MT5 API
               ▼
┌─────────────────────────────────────────────────┐
│  MetaTrader 5 Terminal (Windows binary in Wine)  │
│  terminal64.exe                                  │
│  Connected to broker                             │
└─────────────────────────────────────────────────┘

First-Run Setup

On the very first call to initialize(), the library:

  1. Locates MetaTrader 5.app in /Applications
  2. Finds the bundled Wine binaries
  3. Downloads Python 3.9 embeddable for Windows (~8 MB) from python.org
  4. Extracts it to the appropriate Wine prefix
  5. Installs pip and the MetaTrader5 package
  6. Launches the agent and connects to MT5

This setup happens once. Subsequent connections are instant.

Project Structure

mt5_mac/
├── mt5_mac/
│   ├── __init__.py       # Top-level API (initialize, login, order_send, etc.)
│   ├── agent.py          # Agent script that runs inside Wine
│   ├── backend.py        # Wine subprocess communication + auto-bootstrap
│   ├── bootstrap.py      # Auto-download Python & MetaTrader5 in Wine
│   └── types.py          # Data classes (MqlTick, MqlRates, etc.)
├── examples/
│   ├── basic.py           # Basic usage example
│   ├── streaming.py       # Real-time tick streaming
│   └── trading.py         # Market order placement
├── pyproject.toml         # Package configuration
├── setup.py               # Legacy setup for older pip
├── LICENSE                # MIT License
└── README.md              # This file

Example Scripts

Basic: examples/basic.py

import mt5_mac as mt5

mt5.initialize()
mt5.login(12345678, "password", "Broker-Server")

info = mt5.account_info()
print(f"{info.login} @ {info.server} | ${info.balance:.2f}")

tick = mt5.symbol_info_tick("EURUSD")
print(f"EURUSD: bid={tick.bid} ask={tick.ask}")

mt5.shutdown()

Streaming: examples/streaming.py

import time
import mt5_mac as mt5

mt5.initialize()
mt5.login(12345678, "password", "Broker-Server")

symbols = ["EURUSD", "GBPUSD", "USDJPY"]
for sym in symbols:
    mt5.symbol_select(sym, True)

try:
    while True:
        for sym in symbols:
            tick = mt5.symbol_info_tick(sym)
            if tick:
                print(f"{sym}: {tick.bid:.5f} / {tick.ask:.5f}")
        time.sleep(1)
except KeyboardInterrupt:
    pass
finally:
    mt5.shutdown()

Trading: examples/trading.py

import mt5_mac as mt5

mt5.initialize()
mt5.login(12345678, "password", "Broker-Server")

tick = mt5.symbol_info_tick("EURUSD")
if not tick:
    quit()

request = {
    "action": mt5.TRADE_ACTION_DEAL,
    "symbol": "EURUSD",
    "volume": 0.01,
    "type": mt5.ORDER_TYPE_BUY,
    "price": tick.ask,
    "sl": tick.ask - 0.0050,
    "tp": tick.ask + 0.0100,
    "deviation": 10,
    "magic": 123456,
    "comment": "mt5_mac",
    "type_time": mt5.ORDER_TIME_GTC,
    "type_filling": mt5.ORDER_FILLING_IOC,
}
result = mt5.order_send(request)
print(f"Order: #{result.order} retcode={result.retcode}")

mt5.shutdown()

Troubleshooting

"MetaTrader 5.app not found"

Download and install MetaTrader 5 from metatrader5.com. After installation, the app should be at /Applications/MetaTrader 5.app.

"Failed to download Python"

The first run downloads Python 3.9 embeddable for Windows (~8 MB). Check your internet connection. The download is cached after extraction.

"Login failed"

Verify your account credentials (login ID, password, server name). Some brokers require the full server name (e.g. Exness-MT5Trial15, not just Exness).

"Empty response from backend"

This usually means the MT5 terminal couldn't start inside Wine. Try launching MetaTrader 5.app manually once, let it fully load, then try again.

License

MIT License — see LICENSE for details.

Author

Abdul Rafay Jiwani


Built for macOS · No Windows required · No Docker · No remote servers

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mt5_mac-0.2.0.tar.gz (19.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mt5_mac-0.2.0-py3-none-any.whl (17.4 kB view details)

Uploaded Python 3

File details

Details for the file mt5_mac-0.2.0.tar.gz.

File metadata

  • Download URL: mt5_mac-0.2.0.tar.gz
  • Upload date:
  • Size: 19.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for mt5_mac-0.2.0.tar.gz
Algorithm Hash digest
SHA256 451f7afe14feb7e2cfc63cb125fe52cb58767b3726cc04eaa306695f50c1f476
MD5 2ac129cac569582354cccc3e71b9475e
BLAKE2b-256 657af09c4181210ccb406a1ebead9c5a3cc69a3c0161ffa8990951289e82166c

See more details on using hashes here.

File details

Details for the file mt5_mac-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: mt5_mac-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 17.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for mt5_mac-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3eb43c0e4e47d6d77c4e5debd31f946f12d471afab2968bee48fc15b1825774a
MD5 859344b24533f54f2e7f9d9f76d98376
BLAKE2b-256 9add76409ae011ee3d4338b77f925222d937058a07355af1acdc546ea8c33366

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page