Why Compilation Matters in MetaTrader
Every Expert Advisor (EA), custom indicator, or script you run on MetaTrader 4 or 5 starts as plain text in the MQL4 or MQL5 language. Before it can execute a single trade or draw a line on your chart, the MetaEditor must translate that human-readable code into machine-readable executable files (EX4 for MT4, EX5 for MT5). This process is called compilation.
A failed compilation means your EA won't appear in the Navigator panel, and your indicator won't attach to a chart. Understanding exactly what happens during compilation—and how to fix the errors the compiler throws—is a fundamental skill every automated trader must master. This guide walks you through the entire compile workflow, from opening MetaEditor to resolving the most common build errors you'll encounter in real-world MQL development.
Prerequisites
- Platform version: Any MetaTrader 4 (build 1400+) or MetaTrader 5 (build 4000+). Both include MetaEditor.
- Access rights: You must have a live or demo trading account logged in to the platform. Without an active account, MetaEditor will not allow you to compile EA files.
- Source files: An MQL4 (
.mq4) or MQL5 (.mq5) source file. You can write your own or download one from a reputable source. Ensure the file is placed in the correct folder:MQL4/Experts/for EAs,MQL4/Indicators/for indicators, orMQL4/Scripts/for scripts. The same structure applies for MT5 underMQL5/. - MetaEditor knowledge: Basic familiarity with the MetaEditor interface—opening files, the project panel, and the output window.
Step-by-Step Compilation Workflow
Step 1: Open MetaEditor
From the MT4/MT5 terminal, press F4 on your keyboard, or navigate to Tools → MetaQuotes Language Editor from the main menu. MetaEditor launches as a separate window.
Step 2: Load Your Source File
In MetaEditor, go to File → Open or press Ctrl+O. Navigate to the MQL4 (or MQL5) folder inside your platform's data directory. The default path on Windows is C:\Users\[YourUserName]\AppData\Roaming\MetaQuotes\Terminal\[TerminalID]\. Select your .mq4 or .mq5 file and click Open. The source code appears in the editor pane.
Step 3: Compile the Code
Press F7 on your keyboard, or click the Compile button (a small gear icon) in the toolbar. Alternatively, go to File → Compile or use the hotkey Ctrl+F7.
Watch the Output panel at the bottom of MetaEditor. A successful compilation shows a green checkmark and a message like:
0 error(s), 0 warning(s) File compiled successfully
The compiled executable (.ex4 for MT4, .ex5 for MT5) is automatically placed in the corresponding Experts, Indicators, or Scripts folder. You must restart the terminal or refresh the Navigator panel (right-click in Navigator and select Refresh) to see the new file.
Step 4: Read and Interpret Errors
If compilation fails, the Output panel displays error messages. Each error line includes:
- Line number where the error occurred
- Error code (e.g.,
'(396, 18)') - Error description (e.g.,
unresolved external function)
Double-click any error line to jump directly to the offending line in the source code. This is your fastest debugging tool.
Common Compilation Errors and How to Fix Them
Below are the most frequent build errors you'll encounter, with real fixes from experience.
"unresolved external function"
This means your code calls a function that the compiler cannot find. Causes include:
- Missing include file: Your
#includedirective points to a file that doesn't exist in theIncludefolder. Verify the path is correct and the file is present. - Typo in function name: Check spelling. MQL is case-sensitive.
OrderSendis correct;Ordersendis not. - Library not linked: If using external DLLs or custom libraries, ensure the
.dllfile is in theLibrariesfolder and the#importdeclaration is correct.
Fix: Add the missing include file, correct the function name, or link the library properly.
"type mismatch"
MQL is strongly typed. You cannot assign a double to an int variable without explicit conversion. Common scenarios:
- Passing a
stringargument to a function expectingint. - Using
Bid(which isdouble) where anintis required. - Mixing
datetimeandintin arithmetic.
Fix: Use explicit typecasting: (int)MyDouble or (double)MyInt. Ensure function signatures match exactly.
"'xxx' - undeclared identifier"
The compiler cannot find a variable or constant you're referencing. This usually happens when:
- You forgot to declare the variable before using it.
- You misspelled the variable name.
- The variable is declared in a different scope (e.g., inside a block you're outside of).
Fix: Declare the variable at the top of the function or globally. Check spelling and scope.
"expression not boolean"
This error occurs in conditionals like if, while, or for where the condition is not a boolean expression. For example:
int a = 5;
if(a) { ... } // ERROR: 'a' is int, not bool
Fix: Change to if(a != 0) or if(a > 0).
"array out of range" (runtime error, not compile-time)
This is a runtime error that crashes your EA during backtesting or live trading. The compiler won't catch it. It occurs when you access an array index that doesn't exist, like arr[100] when the array has only 10 elements.
Fix: Always check array bounds before accessing: if(ArraySize(arr) > index). Use ArrayResize() to dynamically adjust array sizes.
Tips and Best Practices from Experience
- Always compile after every edit. Even a single character change can introduce errors. Get into the habit of pressing F7 after each modification.
- Read the first error first. Many subsequent errors are cascading effects of the first one. Fix the top error, recompile, and see if the rest vanish.
- Use the "Find" feature (Ctrl+F) to locate all occurrences of a problematic function or variable.
- Keep your include files organized. Use a consistent naming convention like
MyLib_Utilities.mqhand store them in a subfolder withinInclude. - Enable warnings. In MetaEditor, go to Tools → Options → Compiler and check "Enable warnings". Warnings don't stop compilation but flag potential issues like unused variables or implicit conversions.
- Use version control. Even a simple backup of your
.mq4files in a Git repository saves hours when you break something. - Test on a demo account first. After successful compilation, run your EA in the Strategy Tester before deploying live. Compilation success doesn't guarantee logical correctness.
Common Mistakes and Troubleshooting
Mistake: Compiling in the wrong MetaEditor
If you have both MT4 and MT5 installed, each has its own MetaEditor. Compiling an MQL4 file in the MT5 MetaEditor will fail because the MQL5 compiler does not understand MQL4 syntax (and vice versa). Always use the correct MetaEditor for your platform.
Fix: Launch MetaEditor from the specific terminal you're targeting. Check the title bar—it says "MetaEditor 4" or "MetaEditor 5".
Mistake: Forgetting to refresh after compilation
You compile successfully, but the EA doesn't show in the Navigator. You haven't refreshed the list.
Fix: Right-click inside the Navigator panel and select Refresh, or restart the terminal.
Mistake: Using MT4 functions in MT5
MT5 uses a completely different trading API (OrderSend is replaced by PositionOpen and OrderSend for pending orders). Compiling an MT4 EA in MT5 will produce dozens of "unresolved external function" errors.
Fix: Convert your code to MQL5 syntax, or use a compatibility layer like the Trade.mqh library included with MT5.
Mistake: Ignoring warning messages
Warnings like "implicit conversion" or "unused variable" are often ignored, but they can indicate subtle bugs that manifest later.
Fix: Treat warnings as potential errors. Fix them by adding explicit casts or removing unused variables.
Summary and Next Steps
Compilation is the gatekeeper between your MQL code and a working trading tool. You now know how to open MetaEditor, load source files, compile them, and interpret the most common error messages. Mastering this process eliminates the frustration of "my EA won't show up" and gives you the confidence to debug your own code.
Next steps:
- Practice by compiling a simple EA from the MQL4 documentation (like the
Moving Averagesample). - Deliberately introduce a typo and observe the error message—this builds pattern recognition.
- Learn to use the Debugger in MetaEditor (F5) to step through your code line by line during backtesting.
- Explore the MQL4 Reference (Help → MQL4 Reference) for function signatures and examples.
With solid compilation skills, you're ready to move on to more advanced topics like multi-file projects, custom libraries, and performance optimization.
Frequently Asked Questions
Why does my compiled EA not appear in the Navigator panel?
The most common cause is a compilation error that you missed. Check the Output panel in MetaEditor for red error messages. If you see "0 error(s), 0 warning(s)", then refresh the Navigator by right-clicking inside it and selecting "Refresh". If it still doesn't appear, verify the file was saved in the correct folder (e.g., MQL4/Experts/ for EAs) and that the terminal is logged into an account.
Can I compile MQL4 code in MetaTrader 5's MetaEditor?
No. MetaEditor 5 compiles only MQL5 code. Attempting to compile an .mq4 file in MetaEditor 5 will produce many errors. Always launch MetaEditor from the specific terminal version (MT4 or MT5) you intend to use.
What does "unresolved external function" mean and how do I fix it?
It means the compiler cannot find a function that your code calls. Check for missing #include files, typos in function names, or missing library imports. Double-click the error line to jump to the offending call, then verify the function exists and is correctly spelled.

