mt5api
MetaTrader 5 REST API
mt5api exposes MT5 market data, account info, trading history, trading state,
order validation, and operational terminal endpoints over HTTP. It is a
FastAPI/HTTP adapter over pdmt5, which
provides the core MT5 client, dataframe/order primitives, and canonical MT5
constant parsing. mt5api adds API-key auth and JSON/Parquet response
formatting.
The API supports all MetaTrader5 Python functions through REST operations or
application-managed connection lifecycle. Live order submission
(POST /order/send) is disabled by default and requires both
MT5API_ENABLE_ORDER_SEND=true and MT5API_SECRET_KEY. The server does
not implement strategy orchestration.
The API server must run on Windows. pdmt5 connects through the MetaTrader 5
Python API, which is supported only on Windows, so you must host mt5api on a
Windows machine with a logged-in MetaTrader 5 terminal. HTTP clients can connect
from any operating system.
Architecture
graph TB
Client["HTTP Client<br/>(Any OS)"]
subgraph "Windows Host"
subgraph "FastAPI Application"
Middleware["Middleware Stack<br/>Logging · Error Handler"]
Routers["Routers<br/>health · symbols · market · account · history · calc · trading"]
Auth["API Key Security Dependency<br/>Security(api_key_header) · verify_api_key"]
Deps["FastAPI Dependencies<br/>MT5 Client Singleton · Format Negotiation"]
Formatters["Response Formatters<br/>JSON · Parquet"]
Middleware --> Routers --> Deps --> Formatters
Auth -.-> Routers
Formatters --> Middleware
end
Deps --> pdmt5["pdmt5<br/>MT5 Client · Constants · Dataframes"]
pdmt5 --> MT5["MetaTrader 5<br/>Terminal"]
end
Client -- "HTTP/REST" --> Middleware
Middleware -- "JSON / Parquet" --> Client
Features
- REST endpoints for symbols, market data, account info, orders, positions, history, margin/profit calculations, order validation, and terminal operations
- JSON and Apache Parquet responses (content negotiation)
- Required API key authentication
- Structured JSON logging
- OpenAPI/Swagger docs built into the API
Requirements
- Windows host with MetaTrader 5 terminal installed and logged in
- Linux and macOS are not supported for the API server runtime, but they work for HTTP clients
Installation
Install and run the API on the Windows machine where MetaTrader 5 is installed.
Install the latest release from PyPI:
pip install mt5api
Or, when managing the project with uv, add it as a dependency:
uv add mt5api
Alternatively, install from source:
git clone https://github.com/dceoy/mt5api.git
cd mt5api
uv sync
Running the API on Windows
After installing from PyPI:
$env:MT5API_SECRET_KEY = "your-secret-api-key" # Required
$env:MT5API_ROUTER_PREFIX = "/api/v1" # Optional: omit for root-level routes
python -m mt5api
python -m mt5api reads MT5API_HOST, MT5API_PORT, and MT5API_LOG_LEVEL
from the environment. You can also invoke uvicorn directly:
uvicorn mt5api.main:app --host 127.0.0.1 --port 8000
If you cloned the source tree, prepend uv run to either command (for example,
uv run uvicorn mt5api.main:app --host 127.0.0.1 --port 8000).
Docs:
- Swagger UI:
http://localhost:8000/docs - OpenAPI JSON:
http://localhost:8000/openapi.json
Set MT5API_MT5_PATH to the full path of the default MT5 terminal64.exe;
unset or empty lets MT5 choose. Optionally configure named terminals in the
single instance using MT5API_MT5_TERMINALS (comma-separated name=path pairs):
$env:MT5API_MT5_TERMINALS = 'index=C:\Program Files\OANDA MetaTrader 5\index\terminal64.exe,commodity=C:\Program Files\OANDA MetaTrader 5\commodity\terminal64.exe'
curl.exe -H 'X-MT5-Terminal: index' http://localhost:8000/terminal
curl.exe -H 'X-MT5-Terminal: commodity' http://localhost:8000/terminal
Use X-MT5-Terminal on authenticated MT5-client endpoints; unknown names
return HTTP 400. Public /health ignores the header, including unknown names,
and never switches terminals. Without the header, requests use the current active terminal.
Login retains the active terminal path; if a login request includes the header,
it must name the configured active terminal or login returns HTTP 400 without reconnecting.
Switching releases market-book subscriptions
before the switch; if it fails, subscriptions are not restored and clients must resubscribe.
The active terminal is instance-global: last switch wins. Replacement is
serialized, but ordinary endpoint calls do not hold the lock throughout their MT5
calls; concurrent requests can observe another request's switch. Serialize requests
externally when terminal/account isolation matters.
Set MT5API_ROUTER_PREFIX to mount the API endpoints under a shared path such
as /api/v1. The default is "", which keeps routes like /health and
/symbols at the root. "/api/v1", "api/v1", and "/api/v1/" are treated
the same.
Set MT5API_MAX_MARKET_BOOK_SUBSCRIPTIONS to cap active market-book
subscriptions. The default limit is 100.
To explicitly allow live order submission on a trusted Windows host, set
MT5API_ENABLE_ORDER_SEND=true and MT5API_SECRET_KEY before launching
the server. Never expose a trading-enabled instance directly to the public
Internet; use HTTPS, network access controls, and a dedicated least-privilege
trading account. An authenticated request to POST /order/send returns 403
when order sending is disabled. The server fails to start if the key is missing.
The endpoint does not automatically call order_check or retry requests;
clients must examine the MT5 retcode and avoid ambiguous duplicate orders.
Example Requests with curl
Run these requests on the Windows host with the configuration above. In
PowerShell, use curl.exe if curl resolves to
Invoke-WebRequest.
curl "http://localhost:8000/api/v1/health"
# Include X-API-Key for protected endpoints.
curl -H "X-API-Key: your-secret-api-key" "http://localhost:8000/api/v1/symbols?group=*USD*"
curl -H "X-API-Key: your-secret-api-key" -H "Accept: application/parquet" "http://localhost:8000/api/v1/rates/from?symbol=EURUSD&timeframe=TIMEFRAME_M1&date_from=2024-01-01T00:00:00Z&count=100"
Market-data and calculation endpoints accept MetaTrader 5 constants by official
name (TIMEFRAME_M1, COPY_TICKS_ALL, ORDER_TYPE_BUY), short alias (M1,
ALL, BUY), or integer value.
Endpoints
If MT5API_ROUTER_PREFIX is set, prepend that value to every API route below.
Read-Only Endpoints
- Health:
GET /health,GET /version,GET /last-error - Symbols:
GET /symbols,GET /symbols/total,GET /symbols/{symbol},GET /symbols/{symbol}/tick - Market data:
GET /rates/from,GET /rates/from-pos,GET /rates/range,GET /ticks/from,GET /ticks/range,GET /market-book/{symbol} - Calculations:
GET /calc/margin,GET /calc/profit - Account:
GET /account,GET /terminal - Trading state:
GET /positions,GET /positions/total,GET /orders,GET /orders/total - History:
GET /history/orders,GET /history/orders/total,GET /history/deals,GET /history/deals/total
Operational Endpoints
POST /symbols/{symbol}/select— Show or hide symbol in MarketWatchPOST /market-book/{symbol}/subscribe— Subscribe to DOM eventsPOST /market-book/{symbol}/unsubscribe— Unsubscribe from DOM eventsPOST /order/check— Validate a trade request without execution
License
MIT License - see LICENSE.
The server requires a nonempty MT5API_SECRET_KEY and binds to 127.0.0.1
by default. For remote access, explicitly set MT5API_HOST (or the uvicorn
--host option) and set MT5API_TRUSTED_HOSTS to a comma-separated list of
allowed Host names or IP addresses, such as windows-host,localhost,127.0.0.1.
The default allowlist is localhost,127.0.0.1,[::1].
Metadata
Release files for mt5api 2.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| mt5api-2.1.0.tar.gz | 29.2 kB | With provenance | Details for mt5api-2.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mt5api-2.1.0-py3-none-any.whl | Python 3 | none | any | Details for mt5api-2.1.0-py3-none-any.whl |
Total release size: 65.2 kB
Release files / mt5api-2.1.0.tar.gz
| Download URL | mt5api-2.1.0.tar.gz |
|---|---|
| Size | 29.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fed6a9c182aca039a40268860c85294815292a586c61f86f814eeab88366e1b2
|
|
BLAKE2b-256 checksum How to use checksums |
6a8fddfa7430b7d20e2d40b94df9689e2b3010a0090947ed49f5e0e4fbe025c4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.
Transparency logRelease files / mt5api-2.1.0-py3-none-any.whl
| Download URL | mt5api-2.1.0-py3-none-any.whl |
|---|---|
| Size | 35.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a5a53c6d333fcaf7d8f2248f1f6184fc311a40150eb86d8b5ad09c557befb0ac
|
|
BLAKE2b-256 checksum How to use checksums |
8578b8add6d548199a8ecb1d51d6ba500140305e262498d39017187d73e38ece
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.
Transparency logSecurity for mt5api 2.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for mt5api-2.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.