Learning Center

How to diagnose common MQL4 and MQL5 compilation errors

Read MetaEditor messages, fix the first error first, and resolve undeclared identifiers, wrong parameter counts and type conversion warnings.

Fix from the top

MetaEditor lists errors in order. One missing brace or semicolon can produce dozens of later errors, so fix the first one, recompile, and repeat. Double-click a message to jump to the line; the real mistake is often on the line above.

'X' - undeclared identifier

Usually a Pine name left in the code (close, ta.sma, na) or an MQL4 function used in MQL5. MQL5 has no built-in Close[] or iMA value call; you create a handle and copy data. Variables declared inside an if block are also not visible outside it.

MQL5

// MQL4 style, fails in MQL5:
// double ma = iMA(NULL, 0, 20, 0, MODE_SMA, PRICE_CLOSE, 1);

// MQL5:
int hMa = iMA(_Symbol, _Period, 20, 0, MODE_SMA, PRICE_CLOSE); // in OnInit
double v[1];
if(CopyBuffer(hMa, 0, 1, 1, v) == 1){ double ma = v[0]; }

Wrong parameters count / cannot convert

"wrong parameters count" means the function signature differs between MQL4 and MQL5 or between overloads; check the reference page for the exact arguments. "cannot convert enum" appears when you pass an int where ENUM_TIMEFRAMES or ENUM_MA_METHOD is expected; use the named constants.

"possible loss of data due to type conversion" is a warning, not an error, but read it: assigning a double price to an int silently truncates it. Use explicit casts like (int) only when truncation is intended.

'Trade.mqh' - can't open include file

The include path is relative to MQL5/Include. Use #include <Trade/Trade.mqh> with angle brackets. If you copied code from MQL4 that includes stdlib.mqh, that file does not exist in MQL5.

When it compiles but does nothing

Check the Experts and Journal tabs. Error 4806 or a CopyBuffer return of -1 means data is not ready yet; return and wait for the next tick. Error 10027 means AutoTrading is disabled in the terminal. Our Compilation Error Fixer can help with the first pass, but always recompile and test yourself.

Related code snippets