A lightweight REST API wrapper for MetaTrader 5 built with FastAPI.
This project exposes the official MetaTrader5 Python API as a set of well-documented, structured REST endpoints — making it easier to integrate trading, market data, and account management into your own systems.
- Connection Management – Initialize, reconnect, and shutdown MT5 terminal sessions.
- Account Information – Retrieve balance, equity, margin, and login details.
- Market Data – List available symbols, get symbol info, and fetch real-time ticks or OHLC data.
- Positions & Orders – Inspect open positions, active pending orders, and historical orders/deals.
- History – Query trade history by date range and filters.
- OpenAPI Documentation – Interactive
/docswith request/response examples.
- Python: 3.10+
- MetaTrader 5 terminal installed and configured on the same machine.
- MetaTrader5 Python package (
pip install MetaTrader5) - FastAPI + Uvicorn for serving the API.
-
Clone the repo
git clone https://github.com/eddipa/fastapi-mt5-wrapper.git cd fastapi-mt5-wrapper -
Install dependencies
pip install -r requirements.txt
-
Configure environment Create a
.envfile (see.env.exampleif available):MT5_PATH="C:/Path/To/terminal64.exe" MT5_LOGIN=123456 MT5_PASSWORD="your-password" MT5_SERVER="Broker-ServerName"
-
Run the API
uvicorn app.main:app --reload
API will be available at:
- Docs: http://127.0.0.1:8000/docs
- OpenAPI JSON: http://127.0.0.1:8000/openapi.json
| Tag | Description |
|---|---|
| system | Health & readiness checks, service metadata |
| connection | Initialize, shutdown, and manage MT5 terminal connection |
| account | Retrieve account login, balance, equity, margin |
| market | Symbol list, symbol info, tick data, OHLC candles |
| positions | View and manage open positions |
| orders | Active and historical pending orders |
| history | Trade and deal history queries |
Fetch OHLC data
curl "http://127.0.0.1:8000/market/ohlc?symbol=EURUSD&timeframe=M1&count=10"Run the app in development mode:
uvicorn app.main:app --reloadRun tests:
pytest -vFormat & lint:
black .MIT License — see LICENSE for details.
- This API is a thin wrapper around MetaTrader5’s Python API. You must have the MT5 terminal running for endpoints to work.
- Be cautious with trading endpoints — use a demo account for development/testing.
This software is provided as-is without any warranties.
Trading foreign exchange (Forex), CFDs, or other financial instruments carries a high level of risk and may not be suitable for all investors.
You could lose some or all of your invested capital. Always trade responsibly and test thoroughly on a demo account before using this API in live markets.
The author(s) and contributors of this project are not responsible for any losses incurred through the use of this software.