MT5 Python API

Trade MT5 from Python on Mac, Linux or any server

The official MetaTrader5 Python package needs Windows and a terminal open on the same computer. Tickwire runs the MetaTrader 5 terminal for you, in the cloud, and gives your Python code a REST and WebSocket API. No Windows, no VPS, no terminal to keep open.

Example

Read the account, get a price and place an order

Plain Python with requests. It runs on macOS, Linux and Windows. Try it first in the free sandbox, which needs no broker account.

import os, uuid, requests

API = "https://api.tickwire.io"
KEY = os.environ["TW_KEY"]            # your Tickwire key
ACC = os.environ["TW_ACCOUNT"]        # your account id, e.g. acc_7Qx
h = {"auth-token": KEY}

base = f"{API}/users/current/accounts/{ACC}"

info = requests.get(f"{base}/account-information", headers=h).json()
print(info["balance"], info["equity"], info["currency"])

price = requests.get(f"{base}/symbols/EURUSD/current-price", headers=h).json()
print(price["bid"], price["ask"])

# Buy 0.01 lots. Sending the same Idempotency-Key again returns the first result.
order = requests.post(f"{base}/trade", json={
    "actionType": "ORDER_TYPE_BUY", "symbol": "EURUSD", "volume": 0.01,
}, headers={**h, "Idempotency-Key": str(uuid.uuid4()),
            "X-Actor": "my-script", "X-Reason": "first order from Python"}).json()
print(order["stringCode"], order.get("outcome"))

for p in requests.get(f"{base}/positions", headers=h).json():
    print(p["symbol"], p["type"], p["volume"], p["profit"])

From the MetaTrader5 package to a hosted API

The same jobs, without a Windows machine.

# The MetaTrader5 package: Windows only, with the terminal open on the same machine
import MetaTrader5 as mt5
mt5.initialize()
mt5.account_info()
mt5.symbol_info_tick("EURUSD")
mt5.order_send({...})
mt5.positions_get()
# Tickwire: any OS, the terminal runs in our cloud
GET  /users/current/accounts/{id}/account-information
GET  /users/current/accounts/{id}/symbols/EURUSD/current-price
POST /users/current/accounts/{id}/trade
GET  /users/current/accounts/{id}/positions

What you get

Any operating system

Your code makes HTTPS calls. Run it on a MacBook, a Linux server, a notebook, a cloud function or a Raspberry Pi.

A terminal that stays on

We run a real MetaTrader 5 terminal for each account and restart it if it stops. Health tells you when the broker link is up and prices are moving.

Orders that don't go out twice

Send an Idempotency-Key. If your script retries after a timeout, it gets the first result back, not a second order. If a result can't be confirmed, the answer is unknown.

Live prices and account updates

One WebSocket per account streams prices, positions and orders, with sequence numbers so you can resume after a disconnect. See streaming.

History and candles

Deals, history orders, candles on all 21 MT5 timeframes and historical ticks, for backtests and reports.

MT4 too

The same calls work for MetaTrader 4 accounts. See the MT4 API for what MT4 can't do.

Questions

Does the MetaTrader5 Python package work on a Mac or on Linux?

No. The official MetaTrader5 package for Python runs on Windows only, and it talks to a terminal that is open on the same computer. With Tickwire the terminal runs in our cloud, so your Python code calls a REST API and runs on macOS, Linux, Windows or a server.

Do I need a Windows VPS to trade MT5 from Python?

Not with Tickwire. We run the MetaTrader 5 terminal for each account and keep it connected. Your script only needs an internet connection and an HTTP client such as requests or httpx.

Is there a Python package?

Yes, a typed Python SDK called tickwire, with sync and async clients and resumable streams. It isn't on PyPI yet. Until it is, every call is a plain HTTP request, as in the example on this page.

Does the same code work for MT4?

Yes. MT4 accounts use the same API and the same fields. A few MT5 features, such as market depth and tick history, don't exist on MT4, and those calls answer not_supported_on_mt4.

Which brokers work?

Any broker that offers MetaTrader 5 or MetaTrader 4 accounts. You give the server name from the terminal's login dialog, your login and your password.

The MT5 API
The Python SDK
Connect Claude or ChatGPT to MT5

Run your first MT5 call from Python

7-day free trial. Then from $12 per account a month. See pricing.