MetaTrader 5 and MQL5: a practical starting point
Three key takeaways
- MetaTrader 5 is the terminal; MQL5 is the language used to create programs that run inside it.
- Your first useful milestone is not a profitable robot. It is a rule you can compile, run, observe, and reproduce.
- Backtests and demo results are evidence about a specific setup, not promises about future returns or live execution.
Algorithmic trading is often marketed as if the difficult part were finding a clever indicator or adding AI. In practice, the foundation is less glamorous: define an unambiguous rule, encode it without changing its meaning, test it under recorded assumptions, and inspect every failure.
MetaTrader 5 (MT5) and MQL5 support that workflow in one ecosystem. The official MetaTrader 5 download page opens in a new tab provides desktop, web, and mobile access, but the desktop terminal is the relevant environment for writing an Expert Advisor and using Strategy Tester.
This guide explains the toolchain before you write your first trading program. It is broker-neutral: available markets, symbols, trading costs, account rules, and even some interface labels depend on the broker and jurisdiction you use.
MT5, MQL5, and an EA are different things
The names appear together so often that beginners understandably treat them as one product. Separating them makes the rest of the workflow easier to reason about.
| Term | What it is | What you use it for |
|---|---|---|
| MetaTrader 5 | A trading terminal and execution environment | View markets, manage charts and orders, run programs, and test strategies |
| MetaEditor | The development environment bundled with desktop MT5 | Write, compile, and debug MQL5 source code |
| MQL5 | A C++-style language designed for the MetaTrader ecosystem | Express signals, risk rules, order logic, indicators, and utilities |
| Expert Advisor (EA) | An MQL5 program attached to a chart or run in Strategy Tester | Evaluate rules repeatedly and, when permitted, send or manage orders |
| Strategy Tester | MT5's historical simulation and optimization tool | Replay an EA against historical data and inspect its behavior |
MetaQuotes describes the platform's algorithmic-trading stack as an integrated combination of MetaEditor, MQL5, Expert Advisors, and Strategy Tester. The official algorithmic trading overview opens in a new tab is a useful map of those components.
What can you build with MQL5?
The MQL5 language reference opens in a new tab documents several program types. They share a language but have different lifecycles and permissions.
| Program type | Typical job | Sensible first use |
|---|---|---|
| Expert Advisor | React to ticks or events and optionally place trades | Log one clearly defined signal on a demo chart |
| Custom Indicator | Calculate and draw analytical values | Visualize a signal before automating an order |
| Script | Run a task once and exit | Print symbol information or export a snapshot |
| Service | Run independently of a chart | Advanced data or integration tasks |
| Library / include file | Package reusable logic | Separate risk and order code as a project grows |
A beginner does not need machine learning, a paid signal, or a complex indicator stack. A deterministic rule such as “evaluate the previous completed candle once per new bar” is enough to learn program state, data access, compilation, logs, and test controls.
The workflow that makes a test reproducible
Treat an EA as an experiment rather than an answer. Each stage should produce something another person—or your future self—can inspect.
| Stage | Deliverable | Question to answer before moving on |
|---|---|---|
| 1. Specify | Plain-language entry, exit, sizing, and failure rules | Could two developers implement the same behavior from this specification? |
| 2. Implement | Versioned .mq5 source | Does the code match the rule, including edge cases? |
| 3. Compile | An .ex5 file with zero compiler errors | Did the intended source file compile in the recorded MT5 build? |
| 4. Backtest | Settings, report, logs, and test dates | Does order behavior make sense before looking at net profit? |
| 5. Forward-test | Demo logs across unseen market data | Do spread, session, and connection conditions expose new failures? |
| 6. Deploy cautiously | Monitoring and stop procedures | Can you disable the EA and reconcile its positions when something goes wrong? |
A compiler success only proves that the source is syntactically acceptable. A profitable backtest only describes one historical simulation. A clean demo run still cannot reproduce live liquidity, slippage, latency, or the psychological consequences of real losses.
Install the desktop toolchain
- Download desktop MT5 from MetaQuotes or from the regulated broker you have independently selected.
- Open a demo account appropriate for your location. Do not fund an account just to complete this tutorial.
- In MT5, use Help → About to record the terminal build and architecture.
- Press F4 to open MetaEditor.
- Press Ctrl+R in MT5 to confirm that Strategy Tester opens.
- In Market Watch, open the specification for the symbol you intend to test and record its contract size, volume step, stop level, trading sessions, and displayed spread.
The terminal may show broker-specific symbol suffixes such as .a, -pro, or another naming convention. Do not assume that a symbol called EURUSD, XAUUSD, or an index alias has identical contract specifications across accounts.
Learn the smallest code-to-log loop first
Before an EA can send an order, prove that you can create, compile, and observe a harmless program. In MetaEditor, create a new Script, replace the template with the code below, and compile it with F7.
void OnStart()
{
Print("Hello from MQL5 on ", _Symbol);
}Run the script from MT5's Navigator and look for the message in the Experts or Journal log. This establishes a compact feedback loop:
- edit source;
- compile;
- run in the terminal;
- inspect the log;
- change one thing and repeat.
The official MetaEditor and automated-trading help opens in a new tab explains how MQL5 programs are created, compiled, debugged, and run in the platform.
Turn trading language into testable rules
“Buy in an uptrend” is an idea, not a specification. A program needs definitions for the observation time, data source, order action, position sizing, and exceptional cases.
| Vague phrase | Questions that make it testable |
|---|---|
| Buy in an uptrend | Which timeframe? Which completed bar? Which indicator and exact threshold? |
| Use a small position | Fixed volume or percentage risk? What if the requested volume violates the symbol's minimum or step? |
| Add a stop loss | Points, pips, price, or account currency? What if the broker's minimum stop distance is larger? |
| Trade once | Once per tick, bar, day, signal transition, or open position? |
| Exit when conditions change | Evaluate intrabar or only at a completed-bar boundary? What happens if closing fails? |
A deliberately simple learning rule might be:
- evaluate only when a new bar begins;
- read the previous completed candle;
- request one direction based on that candle;
- allow no more than one position for the symbol;
- attach explicit stop-loss and take-profit requests;
- log every rejected trade request with its return code.
That is not a recommended strategy. It is a compact system for learning the mechanics in the next hands-on guide.
Record the environment, not just the result
An MT5 result without its inputs is difficult to reproduce. Keep this record beside each test:
| Field | Example of what to record |
|---|---|
| Terminal | MT5 build number, operating system, 32/64-bit architecture |
| Account | Demo/live status, account currency, netting or hedging mode, leverage shown by the account |
| Instrument | Exact broker symbol, contract specification, commission, swap, session, and volume rules |
| Data | Date range, timeframe, modeling mode, data quality notes |
| EA | Source version or checksum, input preset, magic number |
| Execution | Spread setting, latency assumptions, rejected-order logs |
| Evaluation | Net profit, drawdown, trade count, costs, and known anomalies |
Two tests that use the same strategy name but different symbol specifications or data ranges are different experiments.
What systematic execution improves—and what it does not
Code can apply a defined rule consistently, create an audit trail, and expose assumptions to testing. Those are valuable benefits. Code cannot make a weak premise profitable, prevent regime changes, guarantee fills, repair bad data, or remove operational risk.
Particular risks include:
- overfitting: tuning parameters until they explain historical noise;
- look-ahead bias: accidentally using information that was unavailable at decision time;
- cost blindness: omitting spread, commission, swap, and slippage;
- execution mismatch: assuming demo and live fills behave identically;
- automation risk: repeating a coding or configuration mistake faster than a human would;
- leverage risk: losing more quickly when a product amplifies price movements.
Before any live use, you should be able to explain the EA's position-sizing ceiling, stop procedure, reconnection behavior, duplicate-order protection, and ownership of existing positions. If you cannot, the system is not operationally ready.
A practical readiness checklist
You are ready for the first EA exercise when you can confirm all of the following:
- [ ] Desktop MT5 and MetaEditor open normally.
- [ ] You are signed in to a demo account.
- [ ] You know where Navigator, Market Watch, Toolbox, Experts, and Journal are.
- [ ] A harmless script compiles and produces a visible log message.
- [ ] Strategy Tester opens, even if you have not run a test yet.
- [ ] You have recorded the exact symbol specification and terminal build.
- [ ] You understand that enabling Algo Trading grants execution permission; it does not validate the EA.
Up next
Next, build and attach your first MQL5 Expert Advisor on an MT5 demo account. You will download the complete source, compile it, inspect its limitations, attach it to a chart, and use the Experts and Journal logs as the success criteria—not profit.