ForexCopilotEA

What is MQL5? How MetaTrader Expert Advisors are built, explained

By Benjamin Lee · Published 2026-10-11 · 3 min read

Every MetaTrader Expert Advisor is a program, and the language it is written in is MQL5 for MetaTrader 5 or MQL4 for MetaTrader 4. You do not need to code to use an EA, but understanding what MQL5 is and how an EA is put together makes you a more informed buyer and user. This article explains the language, the structure of an EA, and what happens between the source code and the file you attach to a chart.

What MQL5 is

MQL5 stands for MetaQuotes Language 5. It is a C++-like programming language created by MetaQuotes for writing trading programs that run inside the MetaTrader 5 platform. It has built-in functions for reading prices, placing and managing orders, calculating indicators and working with account information.

MQL4 is the predecessor used by MetaTrader 4. The two languages look similar but differ in how orders, positions and history are handled, which is why an MT4 program cannot simply be run on MT5.

Three kinds of MQL5 program

Three program types cover most use cases, and they are easy to confuse.

  • Expert Advisors run continuously on a chart and can open, modify and close trades automatically.
  • Custom indicators calculate and draw information on a chart but do not trade.
  • Scripts run once, perform a single task such as closing all orders, and stop.

The structure of an Expert Advisor

An EA is built around event handlers that the platform calls. OnInit runs once when the EA is attached and prepares its settings. OnTick runs on every new price tick and holds the main trading logic. OnDeinit runs when the EA is removed, so it can clean up. There are other handlers for timers, trade events and chart events.

Inside OnTick the typical flow is to read the market, check whether conditions for an entry or exit are met, check risk rules such as spread, time filters or maximum open trades, and then send a trade request. The quality of an EA lies in what it does at each of those steps, not in the existence of the loop.

Inputs and setfiles

Parameters declared as inputs appear in the Inputs tab when the EA is attached. These are the settings you can change: lot size, stop loss, trading hours and so on. A setfile is a saved list of input values that can be loaded in one step, which is how developers ship recommended configurations.

From source code to .ex5

Code is written in MetaEditor, the editor that comes with MetaTrader. The source file has the extension .mq5. When it is compiled, MetaEditor produces a binary file with the extension .ex5, which is what the terminal actually runs. The .ex5 does not reveal the source code, which is why developers can distribute their work without sharing the logic.

The compiled file is what you place in the MQL5/Experts folder or receive from a developer. If a seller supplies only the .ex5, you are trusting the documented behaviour, which is a reason to test thoroughly on demo before using it live.

Testing inside the platform

MetaTrader 5 includes the Strategy Tester, which runs an EA over historical data in a simulated environment. It supports real-tick testing, optimisation across many input combinations and visual playback. It is the main way developers and users evaluate how a program behaves before using it live.

What this means for you as a user

Knowing how an EA is built helps you read its documentation critically. Ask what it does on each tick, what filters it uses, how it manages risk and what happens if a trade fails or the connection drops. A clear manual and a transparent description of the logic are good signs. Vague claims with no explanation of how decisions are made are not.

Frequently asked questions

Do I need to learn MQL5 to use an Expert Advisor?

No. You only need to install the file and set its inputs. MQL5 is needed to write or modify a program.

Is MQL5 hard to learn?

If you know C++ or a similar language it is approachable. Beginners can learn it with the official documentation and practice.

What is the difference between .mq5 and .ex5?

.mq5 is the human-readable source code. .ex5 is the compiled file the terminal runs.

Can I convert an MQL4 EA to MQL5?

It must be rewritten and compiled for MQL5, because the trading functions and structures are different.

Trading foreign exchange, gold and CFDs carries a high level of risk and may not be suitable for all investors. Past performance, backtests and live results do not guarantee future returns. This guide is general education, not financial advice.

More guides

Browse Expert Advisors