Introduction: Why Compiling MQL5 Code Matters
Every Expert Advisor (EA), custom indicator, or script you write in MQL5 must be compiled before it can run inside MetaTrader 5. Compilation translates your human-readable source code (.mq5 files) into executable machine code (.ex5 files) that the MT5 terminal can load and execute. Without a successful compile, your trading robot is just text on a screen.
This guide walks you through the complete compilation workflow in MetaEditor 5 — from writing code, triggering a build, interpreting compiler messages, fixing common errors, to finally attaching and running your EA on a live chart. You’ll also learn the critical difference between compiling in the editor and enabling AutoTrading in the terminal, a step that trips up many new developers.
In MQL5, compilation is particularly important because the language supports object-oriented programming, complex data structures, and multi-threaded operations. The MQL5 compiler is stricter than MQL4’s, catching type mismatches and memory issues early. This guide covers both beginner and intermediate scenarios, including handling #include dependencies, using preprocessor directives, and optimizing compile times for large projects.
Prerequisites
- MetaTrader 5 installed and logged into a demo or live account (build 4000+ recommended).
- MetaEditor 5 (comes bundled with MT5; launch it from the MT5 terminal via Tools → MetaQuotes Language Editor or press F4).
- A valid MQL5 source file (e.g.,
MyExpertAdvisor.mq5) — you can create a new one via File → New → Expert Advisor. - Basic familiarity with opening files in MetaEditor and navigating the MT5 Navigator panel.
Additionally, ensure your MetaTrader 5 installation has sufficient disk space (at least 500 MB free) for compiled files and temporary build artifacts. If you’re working on a network drive, copy your .mq5 files to a local folder first — compilation over network shares can fail due to permission issues.
Step-by-Step: How to Compile MQL5 Code in MetaEditor
1. Open Your MQL5 Source File in MetaEditor
- Launch MetaEditor 5 from the MT5 terminal (F4 or Tools → MetaQuotes Language Editor).
- In MetaEditor, go to File → Open and navigate to your
.mq5file (default location:%APPDATA%\MetaQuotes\Terminal\[INSTANCE_ID]\MQL5\Experts\). - Alternatively, use the Navigator panel inside MetaEditor (View → Navigator) to browse and double-click the file.
The source code appears in the main editor window, with line numbers and syntax highlighting. For new projects, you can create a template EA via File → New → Expert Advisor (template), which includes basic structure like OnInit(), OnTick(), and OnDeinit().
Pro tip: Use the Project panel (View → Projects) to manage multi-file projects. This is essential when your EA uses multiple #include files or custom libraries. The Project panel shows all dependencies and lets you compile the entire project with one click.
2. Trigger the Compilation
You have three ways to compile:
- Press F7 (the standard compile hotkey).
- Click the Compile button (a blue gear icon) on the toolbar.
- Go to File → Compile from the menu.
MetaEditor immediately processes the entire file and all its #include dependencies. A small progress bar appears briefly in the status bar at the bottom. For large projects (e.g., 10,000+ lines of code), compilation may take 5–15 seconds. During this time, avoid switching windows or clicking in the editor, as this can cause the IDE to become unresponsive.
Note for MT4 users: In MetaEditor 4, the hotkey is also F7, but the compiler is less strict. MQL5 requires explicit type declarations and does not allow implicit conversions in many cases. If you’re migrating from MQL4, expect more errors initially — this is normal and indicates better code quality.
3. Read the Compiler Output
After compilation, the Toolbox panel (usually docked at the bottom) shows the Errors tab. This is your primary feedback mechanism. You will see one of three outcomes:
- 0 errors, 0 warnings — compilation succeeded. The
.ex5file is now ready in the same folder as your source. - Errors (red X icon) — the compiler found problems that prevent code generation. Each error shows the line number, error code, and description.
- Warnings (yellow triangle icon) — potential issues that don’t stop compilation but may cause runtime problems (e.g., unused variables, implicit conversions).
Double-click any error line to jump directly to the offending line in the editor. The error description often includes the exact token (e.g., variable name) that caused the issue. For example, ';' - semicolon expected means the compiler reached the end of a line expecting a semicolon but found something else.
Advanced tip: Use the Warnings tab to enable stricter checking. Go to Tools → Options → MQL5 Compiler and set Warning Level to 4 (maximum). This catches subtle issues like unused parameters or potential division by zero.
4. Fix Errors and Recompile
Common MQL5 syntax errors and their fixes:
| Error Code | Common Cause | Fix |
|---|---|---|
| 'xxx' - semicolon expected | Missing ; at end of a statement |
Add a semicolon at the indicated line. |
| 'xxx' - undeclared identifier | Variable or function not declared before use | Declare the identifier or check for typos (case-sensitive). |
| 'xxx' - type mismatch | Assigning incompatible types (e.g., int to string) |
Use explicit conversion or correct the variable type. |
| 'OnTick' - function already defined | Duplicate event handler definition | Remove or rename the duplicate function. |
| 'xxx' - cannot convert from 'int' to 'string' | Implicit conversion not allowed | Use IntegerToString() or DoubleToString() for explicit conversion. |
After fixing, press F7 again. Repeat until you see 0 errors, 0 warnings. For complex errors, check the Error List panel (View → Error List) which shows all errors and warnings in a sortable table. You can filter by severity or search for specific error codes.
Edge case: If you get an error like 'xxx' - unresolved external function, it means you’re calling a function declared in another file that isn’t included. Add #include "MyLibrary.mqh" at the top of your main file, or ensure the function is defined in the same file.
5. Enable AutoTrading in MT5 Terminal
Compilation alone does not make your EA trade. You must enable algorithmic trading in the terminal:
- In MT5, click the Algo Trading button on the toolbar (it looks like a green play icon). If it shows a red X, click it to enable.
- Go to Tools → Options → Expert Advisors and ensure Allow automated trading is checked. Also tick Allow DLL imports if your EA uses external libraries.
- On the Common tab, check Allow Algo Trading.
Important difference from MT4: In MT5, you must also enable Algo Trading on the Common tab of the EA’s properties dialog when attaching it to a chart. MT4 only requires the global setting. Additionally, MT5 has a per-symbol setting: go to View → Symbols → [Symbol] → Trade and ensure Allow automated trading is enabled for the symbol your EA trades.
If your EA uses OrderSend() or PositionOpen(), also check that the account has sufficient margin and that the broker allows algorithmic trading on your account type (some demo accounts restrict EAs).
6. Attach the Compiled EA to a Chart
- Open a chart in MT5 (e.g., EURUSD, M15).
- In the Navigator panel (Ctrl+N), expand Expert Advisors.
- Drag your compiled EA onto the chart, or double-click it and confirm the dialog.
- In the EA settings dialog, check Allow automated trading at the bottom, then click OK.
- Look at the top-right corner of the chart: a smiling face icon means the EA is running. A frowning face or red warning means it’s disabled.
Troubleshooting: If the EA icon shows a red warning, hover over it to see the reason (e.g., “AutoTrading disabled by server”). Common causes: the broker has disabled EAs on that symbol, or the EA requires DLL imports that are blocked. Check the Journal tab for detailed error messages like “OrderSend failed: 4109 — trade is disabled.”
Advanced Compilation Techniques
Using Preprocessor Directives
MQL5 supports preprocessor directives that control compilation behavior:
// Conditional compilation for debug mode
#ifdef DEBUG_MODE
#define LOG(msg) Print(__FUNCTION__ + ": " + msg)
#else
#define LOG(msg)
#endif
// Include guard to prevent multiple includes
#ifndef MY_LIBRARY_MQH
#define MY_LIBRARY_MQH
// Library code here
#endif
Use #ifdef to enable debug prints during development and disable them in release builds. Include guards (#ifndef) prevent duplicate definitions when the same header is included from multiple files.
Compiling for Different Builds
MetaEditor supports Release and Debug build configurations. To switch:
- Go to Build → Configuration Manager.
- Select Debug to include debug symbols and disable optimizations (slower execution but better for stepping through code).
- Select Release for optimized code that runs faster (default).
Debug builds produce larger .ex5 files and may cause slight runtime overhead. Always test with Release before deploying to a live account.
Tips and Best Practices from Experience
- Compile often. After every few lines of code, press F7. This catches errors early and makes debugging much faster.
- Use the Debugger. MetaEditor has a built-in debugger. Set breakpoints by clicking the left margin, then press F5 to run. This lets you step through
OnTick()and inspect variables in real time. - Clean build before final testing. Use File → Clean to delete all compiled files, then recompile. This ensures no stale
.ex5remains. - Read compiler warnings. W
Frequently Asked Questions
My EA compiles with 0 errors but still shows a red face on the chart. What's wrong?
A red face means the EA is disabled. Check that AutoTrading is enabled in MT5 (green play icon) and that the EA’s settings dialog has “Allow automated trading” checked. Also verify your account has sufficient margin and the market is open.
Can I compile MQL5 code without MetaEditor?
No. The MQL5 compiler is only available inside MetaEditor 5. You cannot compile with command-line tools or third-party IDEs. However, you can edit code in any text editor and then open the file in MetaEditor to compile.
How do I compile an indicator or script?
The process is identical. Open the
.mq5file (indicators are inMQL5






