Guide
BrokerMarket dataForexCFDsIndicesStocksFuturesCrypto API key

MetaTrader 5 API guide

If your forex broker gives you an MT5 login, Python can trade it. The catch is that the Python package drives a terminal on Windows rather than calling a server API.

Last verified · Also known as MT5, MetaTrader5 Python, MetaTrader 4, MT4, MQL5, MQL4

Quick answer

MetaTrader 5 is not a broker but the terminal hundreds of forex and CFD brokers provide. You automate it two ways: MQL5 Expert Advisors that run inside the terminal, or the official MetaTrader5 Python package, which talks to a running terminal on the same Windows machine to read rates and send orders. There is no cloud REST API; MT4 supports only MQL4 Expert Advisors.

MetaTrader 5 API at a glance

Pricing
Free terminal and Python package; broker spreads apply
Availability
Worldwide through MT5 brokers; Python package is Windows-only
Paper trading
Demo accounts at almost every MT5 broker
Integration type
Local gateway
In AlgoGP
Design and backtest in AlgoGP, connect the exported code
Python
pip install MetaTrader5 (Windows, terminal running)
In-terminal
MQL5 Expert Advisors and scripts
Data
copy_rates_range, copy_ticks_range from the broker
Orders
order_send with a request dict
MT4
MQL4 only; no official Python package

Trading MT5 from Python

import os
import MetaTrader5 as mt5
mt5.initialize(login=int(os.environ["MT5_LOGIN"]), password=os.environ["MT5_PASSWORD"],
               server=os.environ["MT5_SERVER"])
rates = mt5.copy_rates_from_pos("EURUSD", mt5.TIMEFRAME_H1, 0, 100)
tick = mt5.symbol_info_tick("EURUSD")
result = mt5.order_send({"action": mt5.TRADE_ACTION_DEAL, "symbol": "EURUSD", "volume": 0.01,
    "type": mt5.ORDER_TYPE_BUY, "price": tick.ask, "deviation": 20, "type_filling": mt5.ORDER_FILLING_IOC})
print(result.retcode, result.comment)
mt5.shutdown()
Bars and a market order on a demo account.

MT4 vs MT5 for automation

MT4MT5
LanguageMQL4MQL5
Official Python packageNoYes
Strategy testerSingle-threadedMulti-threaded, real ticks
AssetsMostly FX/CFDsFX, CFDs, exchange stocks and futures

Build a MetaTrader 5 strategy with AlgoGP

AlgoGP does not connect to MetaTrader 5 natively yet. MetaTrader 5 connects through a local gateway, socket, FIX or Protobuf stack rather than plain HTTPS, so it runs as a separate process next to your strategy.

  1. 1

    Design and backtest in AlgoGP

    Describe the strategy to the copilot and backtest it on years of normalized history. The result does not depend on any vendor.

  2. 2

    Export the Python project

    Every project exports strategy.py, market_data.py, brokers.py and live.py. The strategy only sees bars, cash, positions and orders, never a vendor’s wire format.

  3. 3

    Plug in MetaTrader 5

    Implement the four adapter methods (account, positions, submit_market_order and a candle fetcher) with MetaTrader 5’s SDK, or keep a built-in platform for paper trading and add MetaTrader 5 when you go live.

Built-in brokers for the same markets: Alpaca, Binance, Kraken, Bybit.

MetaTrader 5 API: frequently asked questions

Does MetaTrader 5 have an API?

Yes: MQL5 inside the terminal and the official MetaTrader5 Python package that controls a local terminal.

Can I use the MT5 Python package on a Mac?

Not natively; it requires the Windows terminal (Wine or a Windows VPS work).

Is there an MT4 Python API?

No official one; MT4 automation is done with MQL4 Expert Advisors or third-party bridges.

Design and backtest your MetaTrader 5 strategy first

AlgoGP turns an idea into Python and tests it on years of history before you connect it to MetaTrader 5. The exported code keeps the broker behind four small adapter methods.

  • AI copilot writes and revises the rules
  • Backtests with realistic costs
  • Exported Python ready to wire to MetaTrader 5

Free plan, no card required.

MetaTrader 5 alternatives

Independent guide. AlgoGP is not affiliated with MetaTrader 5. Limits, prices and availability change; confirm them in the official documentation. Nothing here is investment advice.

Build a strategy