Start here
Connecting MetaTrader 5
How your MetaTrader 5 terminal connects to the trading core: what the MQL5 gateway sends, what it accepts, and why it holds no trading logic at all.
Last reviewed: 25 August 2026
What the gateway is
An Expert Advisor running on a MetaTrader 5 terminal, and nothing more ambitious than that. It has one job in each direction: push what the market and the account are doing out to the trading core, and carry order commands back into MT5.
It holds no strategy. It calculates no indicators, evaluates no risk, and never decides to open, close or resize anything on its own. That is a deliberate constraint rather than an unfinished feature: MQL5 is hard to test and runs on a Windows terminal that is harder to control than a supervised service, so every decision worth auditing was moved into the C++ core where it can be tested and versioned.
What crosses the connection
| Direction | Message | When |
|---|---|---|
| Terminal → core | Tick — symbol, bid, ask, timestamp | On price movement |
| Terminal → core | Account state — equity, balance, margin used and free | On a timer, every few seconds |
| Terminal → core | Execution result — filled, rejected or partial, with the broker error code | Immediately on any trade transaction |
| Core → terminal | Order command — open, close or modify, with stop-loss and take-profit | Only after the risk engine has approved |
Every message carries both a broker identifier and a customer identifier. That is what lets the core resolve a broker's own symbol naming back to a canonical instrument, and what guarantees one customer's positions can never be attributed to another.
One terminal per customer, one gateway per broker
Terminals are never shared between customers. Each customer gets their own MT5 terminal and their own gateway instance, provisioned when they connect a broker. If you connect more than one broker, each gets its own gateway with its own broker identifier.
Stop-loss and take-profit always rest at the broker
When an order command is executed, the stop-loss and take-profit are set on the broker's own order — not merely tracked in memory somewhere upstream.
Why this detail matters more than it sounds
A resting stop is not a guaranteed fill. In a fast or gapping market it becomes an order at the next available price, which may be worse than the level set. That is a property of the market, not of this system, and no software removes it.
Terminal settings that matter
- Algorithmic trading must be enabled in the terminal. With it off, order commands arrive and cannot be executed.
- DLL imports must be permitted for the gateway, which uses a messaging library to reach the core. MQL5 has no usable native transport of its own.
- The terminal must stay running. Ticks are the heartbeat that position management runs on.
A terminal that is closed is not a paused system
How to tell it is healthy
The console reports connection state and the age of the last tick per symbol. Stale data is not treated as a cosmetic problem: the risk engine rejects proposals on any symbol whose last tick is older than the freshness limit, so a silent feed produces rejections rather than trades at prices that may no longer exist.
If something looks wrong, Troubleshooting lists the failures that actually occur and what each one means.