MT5 Python API

MetaTrader 5 in Python, on any computer.

The official MetaTrader5 package needs Windows and a terminal left open. Pipwire runs the terminal for you, so Python on Windows, a Mac, a Linux server or Docker reads and trades your MT5 account over HTTPS.

Balance and open positions, on any OS

import os, requests

API = "https://api.pipwire.io/v1"
s = requests.Session()
s.headers["Authorization"] = f"Bearer {os.environ['PIPWIRE_KEY']}"

acc = s.get(f"{API}/accounts").json()["data"][0]["id"]
info = s.get(f"{API}/accounts/{acc}/info").json()
print(info["balance"], info["equity"], info["currency"])

for p in s.get(f"{API}/accounts/{acc}/positions").json()["data"]:
    print(p["symbol"], p["side"], p["volume"], p["profit"])

Why pip install MetaTrader5 fails on a Mac.

macOS or Linux

$ pip install MetaTrader5
ERROR: Could not find a version that satisfies the requirement MetaTrader5
ERROR: No matching distribution found for MetaTrader5

Windows only

PyPI has the MetaTrader5 package only for 64-bit Windows, so pip on a Mac or Linux finds nothing to install.

It needs a terminal

The package doesn't talk to your broker. It drives a MetaTrader 5 terminal on the same machine, which has to stay open and signed in.

Workarounds are one more machine

Wine, a Windows VM or a Windows VPS can run both, but that's another machine to keep alive, update and restart.

Pipwire takes the terminal off your hands: one per account, in a data centre, kept signed in. Your code only makes HTTPS calls, so on Windows too, your PC no longer has to stay on.

Up and running in four steps.

  1. 1

    Connect your account

    Sign up and add your MT5 login. The investor password gives read-only access.

  2. 2

    Create an API key

    Read or trade, on the API keys page. Put it in PIPWIRE_KEY.

  3. 3

    Install requests

    Run pip install requests pandas on Windows, macOS or Linux. No MetaTrader to install.

  4. 4

    Call the API

    Positions, history, candles and orders, over HTTPS. Times come back in UTC.

Candles into pandas

import pandas as pd

r = s.get(f"{API}/accounts/{acc}/candles",
          params={"symbol": "EURUSD", "timeframe": "1h", "limit": 500})
df = pd.DataFrame(r.json()["data"]).set_index("time")  # times are UTC
print(df.tail())

Place an order (trade key)

r = s.post(f"{API}/accounts/{acc}/orders", json={
    "symbol": "EURUSD", "side": "buy", "volume": 0.01,
    "stopLoss": 1.0750, "takeProfit": 1.0950,
    "clientId": "my-bot-0001",  # a retry with the same id never trades twice
})
print(r.status_code, r.json())  # 201 when filled; 422 with the broker's reason if not

Stream position and balance changes

import asyncio, json, os, websockets

async def main():
    url = "wss://api.pipwire.io/v1/accounts/acc_…/stream"
    headers = {"Authorization": f"Bearer {os.environ['PIPWIRE_KEY']}"}
    async with websockets.connect(url, additional_headers=headers) as ws:
        await ws.send(json.dumps({"type": "subscribe", "topics": ["positions", "account"]}))
        async for raw in ws:
            msg = json.loads(raw)
            print(msg["type"], msg.get("data"))

asyncio.run(main())

From the MetaTrader5 package to Pipwire.

Each function you know has an HTTPS call that does the same job. Lots, prices and symbol names are the same; times come back in UTC instead of the broker's server time.

MetaTrader5 packagePipwire
mt5.initialize(), mt5.login()Connect the account once (dashboard or POST /v1/accounts); it stays signed in
mt5.account_info()GET /v1/accounts/{id}/info
mt5.positions_get()GET /v1/accounts/{id}/positions
mt5.orders_get()GET /v1/accounts/{id}/orders
mt5.history_deals_get()GET /v1/accounts/{id}/deals
mt5.history_orders_get()GET /v1/accounts/{id}/orders/history
mt5.copy_rates_from_pos(), copy_rates_range()GET /v1/accounts/{id}/candles
mt5.symbol_info_tick()GET /v1/accounts/{id}/price
mt5.symbol_info()GET /v1/accounts/{id}/symbols/{symbol}
mt5.order_send()POST /orders to open; PATCH or DELETE /positions/{id} to change or close
No equivalentA WebSocket stream of position, order, account and price changes

Python questions

Why can't I pip install MetaTrader5 on a Mac?
MetaQuotes publishes the MetaTrader5 package only for 64-bit Windows, and it works only alongside a MetaTrader 5 terminal on the same machine. On macOS or Linux, pip reports that no matching distribution exists.
Does it work on Windows?
Yes. The same code runs on Windows, macOS and Linux. On Windows the difference is that no MetaTrader terminal has to stay open: your script can run on any machine or server, around the clock, for as many accounts as you connect.
Does it work on Apple Silicon Macs?
Yes. Pipwire runs the MetaTrader terminal in a data centre; your Mac only makes HTTPS calls, so any Python on any processor works.
Can I run my bot in Docker, on a Linux VPS or in a serverless function?
Yes. Anything that can make HTTPS requests can read and trade the account. There's no Wine and no Windows anywhere in your stack.
Is there a Pipwire Python SDK?
Not yet. Any HTTP library works (requests, httpx, aiohttp), and the OpenAPI spec at api.pipwire.io/openapi.json can generate a typed client.
Can I backtest with it?
You can pull the history to backtest on: up to 50,000 candles a request, from 1 minute to monthly, from your broker's own terminal. Running the backtest is up to your code; Pipwire has no strategy tester.
When is the MetaTrader5 package the better choice?
If you're on Windows with the terminal open anyway, need one account, and your script runs only while your computer is on, the package is free and local.
What does it cost?
$5 per account a month to read, $10 to trade, billed only for the days the account is connected. Requests aren't metered.

Trade MT5 from Python, from anywhere.

$5 a month to read, $10 to trade. No terminal to keep open, no Wine, no VPS.