MQL5 is the programming language used to build Expert Advisors, indicators and scripts for MetaTrader 5. Every automated forex strategy running on that platform is written in it. Plenty of guides explain the syntax; far fewer explain what actually happens when code written in a strategy tester meets a live market. This guide covers both, with the emphasis on the second.
What MQL5 actually is
MQL5 is a C++-like language compiled to bytecode and executed inside the MetaTrader 5 terminal. That single sentence carries three consequences most tutorials skip.
It is event-driven, not procedural. Your code does not run in a loop you control. The terminal calls specific functions when specific things happen: OnTick() when a price quote arrives, OnTimer() on a schedule you request, OnTradeTransaction() when an order changes state. Strategies written as though the program is in charge tend to behave unpredictably, because it isn’t.
It runs inside the terminal, not on the broker’s server. If MetaTrader closes, the internet drops or the machine sleeps, the Expert Advisor stops. This is why serious automated trading runs on a VPS rather than a laptop — the code is only as available as the machine hosting it.
It is single-threaded per chart. A slow calculation inside OnTick() blocks the next tick. Heavy work — optimisation loops, large file reads, network calls — has to be pushed to a timer or done sparingly, or the strategy quietly starts reacting late.
MQL5 against MQL4, and why it matters now
MQL4 targeted MetaTrader 4 and is effectively frozen. MQL5 differs in ways that change how strategies are structured, not merely how they are typed:
- Position model. MT4 treated every order as separate. MT5 uses netting or hedging depending on account type, so the same code can produce different results on two accounts. This catches out more developers than any syntax difference.
- Real object orientation. Classes, inheritance and the standard library make larger strategies maintainable rather than a single thousand-line file.
- Multi-currency testing. The MT5 strategy tester can run a strategy across several symbols simultaneously. MT4 could not, which meant portfolio behaviour was largely untestable.
- Real tick data. MT5 can test against actual tick history rather than interpolating inside a bar — the single largest improvement in backtest realism.
Porting MQL4 to MQL5 is rarely a translation exercise. The order-handling layer usually has to be rewritten around the position model.
The part that decides whether a strategy works
Most MQL5 guides finish at “compile and run the tester”. That is where the difficult part begins, because a backtest is a model of trading, not a record of it. Four things separate the two, and every one of them costs money in live trading.
Spread is variable, and testers often assume it is not
Backtests frequently run at a fixed spread. Real spreads widen around news, at the daily rollover and in thin liquidity. A strategy taking many small profits is disproportionately exposed: if average profit per trade is close to the spread, a modest widening removes the edge entirely. Test at realistic and at deliberately pessimistic spreads, and see which of your results survive.
Slippage does not appear in an ideal fill
The tester can fill you at the price you asked for. A live market fills you at the next available price, which in fast conditions is not the same thing. Strategies entering on breakouts and momentum are most affected, because they trade precisely when the book is thinnest. Slippage and spread are the two costs that quietly convert a profitable backtest into a flat live account.
Gaps ignore your stop price
A stop-loss is an instruction to sell at the next available price once a level trades, not a guarantee of that level. Over a weekend gap or a news jump, the next available price can be far away. Any risk calculation assuming stops fill exactly is understating the tail.
Overfitting is the default outcome, not an accident
The MT5 optimiser will happily search thousands of parameter combinations and return the best. On any finite history, the best combination is partly signal and largely coincidence. The defence is procedural rather than clever: optimise on one period, validate on a period the optimiser never saw, and be suspicious of any parameter set that collapses when nudged slightly. A robust strategy has a broad plateau of decent settings; an overfitted one has a single sharp peak.
A realistic first Expert Advisor
The instructive first project is not a profitable strategy. It is a deliberately simple one built end to end, so the plumbing becomes familiar before the logic gets hard:
- Open MetaEditor from MetaTrader 5 (Tools → MetaQuotes Language Editor, or F4) and create a new Expert Advisor from the wizard.
- Use
CTradefrom the standard library rather than rawOrderSendcalls. It handles retries and filling modes that are tedious to get right by hand. - Put every entry and exit decision behind a single function you can test independently of the terminal.
- Log every decision, including the ones that produced no trade. When live behaviour diverges from the backtest, that log is the only evidence of why.
- Run it on a demo account for longer than feels necessary before it touches real money.
The MetaEditor debugger runs strategies on historical data with breakpoints, which is far quicker than reasoning about why a backtest looks wrong.
Should you learn MQL5?
It depends on what you actually want.
If you want to test your own ideas precisely, then yes — there is no substitute for expressing a rule in code and seeing what it did across years of data. The discipline of making a strategy explicit enough to compile exposes vagueness that survives indefinitely in discretionary trading.
If you want to run automated strategies without becoming a developer, learning MQL5 is not the shortest path. Building, testing and maintaining a strategy that holds up in live conditions is a sustained engineering commitment, and the failure modes above are ones every developer meets personally. Using an existing Expert Advisor trades that work for a different one: evaluating whether someone else’s system is sound.
Either way, understanding what MQL5 can and cannot guarantee makes you a better judge of any automated system, including one you did not write. The official MQL5 documentation is the reference, and the code base is worth reading for structure rather than for strategies to copy.
The short version
MQL5 is a capable, well-documented language with an unusually good testing environment attached. The language is the easy part. The hard part is that a strategy tester models a market that does not charge you variable spreads, does not slip your fills, does not gap through your stops, and will not punish you for fitting to noise. Every one of those gaps is closable — but only if you know to look for it before you fund the account.

