MetaTrader & MQL5: the lay of the land
What MetaTrader and an Expert Advisor actually are, MT4 vs MT5, where an EA runs inside the terminal, and the event-driven model that everything in this part is built on.
For seven parts you have thought like a Python programmer: pull data into a DataFrame, loop over it, call a broker API. MetaTrader flips that model inside out. Here you write a program that *lives inside a trading terminal* and gets woken up by the market. That program is called an Expert Advisor (EA), and the language you write it in is MQL5. This part builds a real one from an empty file.
MetaTrader is the most widely deployed retail trading platform on earth β the default front end for thousands of forex and CFD brokers. If you want your strategy to run against a retail broker with no custom infrastructure, an EA is very often the shortest path from idea to live.
MT4 vs MT5, and why we use MT5
There are two generations. MetaTrader 4 (MT4) speaks the older MQL4 language; it is forex-centric and still everywhere. MetaTrader 5 (MT5) speaks MQL5 β a modern, near-C++ language with real classes, a proper multi-asset model (netting *and* hedging accounts), a far better Strategy Tester, and support for stocks, futures and options alongside forex.
- MQL5 is a strict, compiled, statically-typed language. It looks like C++ β semicolons, braces,
int/double/booltypes. If you know any C-family language it will feel familiar. - It runs sandboxed inside the terminal. No
pip install, no external processes. The terminal feeds it data and executes its orders. - MT5's Strategy Tester models real tick data and multi-symbol portfolios β the backtesting engine alone is a reason to prefer it. We lean on it hard in the final lesson.
What an Expert Advisor is β and is not
An Expert Advisor is an MQL5 program attached to *one chart* (one symbol, one timeframe) that can read market data and place, modify and close orders automatically. It is the MetaTrader equivalent of the trading bot you built in Part 8 β the same five-component blueprint from Part 1, just implemented as an EA instead of a Python script.
MetaTrader has two other program types you will meet but which cannot trade on their own: an indicator draws values on the chart (a moving average line, an oscillator) and a script runs once and exits. Only an EA has the event handlers and the trading permissions to run a strategy continuously. This part is about EAs.
Where an EA lives in the terminal
The event-driven model: everything is a tick
This is the single most important idea in MQL5, and it is exactly the mental shift from Python. You do not write a loop. You do not call while market_is_open(). Instead you write *functions that the terminal calls for you* when something happens. The main one is OnTick() β the terminal calls it every time a new price quote (a tick) arrives for your symbol.
A tick is not a fixed heartbeat β it fires whenever the price changes, which might be dozens of times a second in a fast market or once every few seconds in a quiet one. A huge number of EA bugs come from forgetting this: OnTick() can run hundreds of times inside a single candle. In the strategy we build, we will deliberately act only once per *bar*, not once per *tick*.
MetaEditor: where you write it
MQL5 is written and compiled in MetaEditor, the IDE bundled with every MT5 install (press F4 in the terminal, or open it from the toolbar). You write .mq5 source, press F7 to compile it into a .ex5 binary, and the compiled EA appears in the terminal's Navigator under *Expert Advisors*, ready to drag onto a chart. MetaEditor also holds the built-in Strategy Tester hooks, the debugger and the full MQL5 reference documentation offline.
Demo first β always
Next: the MQL5 language itself β types, functions, input parameters, and the three lifecycle handlers OnInit, OnTick and OnDeinit β with a minimal EA you can compile and attach today.