Quick Answer
undeclared identifier is an MQL5 compile error. The compiler found a name it does not know: a variable, function, class or constant that was never declared, is out of scope, is spelled differently, or needs an #include you did not add.
Read the first error in the MetaEditor Errors tab, check the spelling and case of that name, check where it was declared, and check whether it comes from a library or from another language such as MQL4 or Pine Script.
Key Facts
| Item | Detail |
|---|---|
| Error type | Compile error, the program is not built |
| Error text | 'name' - undeclared identifier |
| Where to read it | The Errors tab in MetaEditor, with the file, line and column |
| Case sensitivity | MQL5 is case sensitive, so trade and Trade are different names |
| Most common causes | Typo, wrong case, missing include, variable out of scope |
| Common for converters | MQL4 names such as Bid and Ask, and Pine names such as close |
What the Compiler Is Telling You
When you press F7 in MetaEditor, the compiler reads your file from top to bottom. When it meets a name that it has not seen before, it stops with a message like this:
'trade' - undeclared identifier MyEA.mq5 42 8
The numbers are the line and the column. Press Ctrl+G, enter the line, and look at that name first.
One missing declaration often causes many errors that follow. Fix the first error in the list, compile again, and many of the others disappear.
7 Common Causes
1. Missing include
You use a class from the standard library but never included its file.
// Error: 'CTrade' - undeclared identifier
CTrade trade;
// Fixed
#include <Trade\Trade.mqh>
CTrade trade;
2. A typo
SymbolInfoDoubel instead of SymbolInfoDouble, or lotSize in one place and lotsize in another. The compiler sees two different names.
3. Wrong case
MQL5 distinguishes upper and lower case. If you declare CTrade trade; and later write Trade.Buy(...), the compiler reports Trade as undeclared.
4. Variable out of scope
A variable declared inside a block exists only inside that block.
void OnTick()
{
if(IsNewBar())
{
double ma = 1.0; // exists only inside this if block
}
Print(ma); // Error: 'ma' - undeclared identifier
}
// Fixed: declare it where you need to read it
void OnTick()
{
double ma = 0.0;
if(IsNewBar())
{
ma = 1.0;
}
Print(ma);
}
5. Missing input or global declaration
A value used in several functions must be a global variable or an input declared outside any function.
input int MaPeriod = 20; // visible in every function in this file
6. MQL4 names that do not exist in MQL5
Code copied from an MQL4 Expert Advisor often uses Bid and Ask as predefined variables. They do not exist in MQL5.
// Error: 'Bid' - undeclared identifier
double price = Bid;
// Fixed
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
7. Pine Script names used as if they were MQL5
Pine Script defines close, open, ta.ema() and strategy.entry() for you. MQL5 does not. These names must be replaced with explicit calls. Our Pine Script to MQL5 guide covers the full conversion process. The table below covers the names that cause this error most often.
| Pine Script or MQL4 habit | MQL5 replacement |
|---|---|
| Bid (MQL4) | SymbolInfoDouble(_Symbol, SYMBOL_BID) |
| Ask (MQL4) | SymbolInfoDouble(_Symbol, SYMBOL_ASK) |
| close[1] (Pine) | iClose(_Symbol, _Period, 1), mind the shift, see close[1] in MQL5 |
| ta.ema(close, 20) (Pine) | iMA(...) handle, then CopyBuffer(...) |
| strategy.entry(...) (Pine) | trade.Buy(...) or trade.Sell(...) with CTrade |
| input.int(14) (Pine) | input int Period = 14; |
How to Debug It
- Compile with F7 and open the Errors tab.
- Double-click the first error, and MetaEditor jumps to the line.
- Check the spelling of the name against where you declared it.
- Check the case of every letter.
- Check whether the declaration is inside a block that has already closed.
- If the name belongs to a class, check the
#includeat the top of the file. - Compile again, and repeat with the next first error.
Compile Error vs Runtime Error
An undeclared identifier stops the build, so you see it before you run anything. Errors such as array out of range are different, because the program compiles and fails later. Knowing which kind you have tells you where to look: the Errors tab for the first, and the Experts tab for the second.
Related Guides
- cTrader C# error CS0103: fix "name does not exist" in a cBot
- MQL5 CopyBuffer guide: read indicator values safely
- Pine Script input.* in MQL5: inputs, groups and dropdowns
- Pine Script strategy.entry to MQL5 CTrade: entry mapping guide
- MQL5 type conversion warning: the common warning that often comes next
- MQL5 Wrong Parameters Count Error: MT4 names like OP_BUY and Ask in MT5 code
- cTrader C# Error CS1061: the cTrader version of a name the compiler cannot match.
- cTrader C# Error CS0246: the cTrader version when a type name is unknown.
- Pine Script Error: Undeclared Identifier: the Pine Script version of the same error.
- MQL5 OnCalculate not found: an indicator whose
OnCalculateis not one of the two valid forms. - MQL5 variable already defined: a name declared twice in the same scope.
- MQL5 PositionSelectByIndex undeclared identifier: a position function that does not exist, and the loop to use instead.
- MQL5 unexpected end of program, unbalanced parentheses: find the missing brace.
FAQ
Convert Without Chasing Compile Errors
When you move a Pine Script strategy into MQL5 by hand, every Pine built-in has to be rewritten, and each one you miss becomes a compile error. CodeFlowOS generates MQL5 code you can review before you compile and test it in MetaEditor and the MT5 Strategy Tester. You can try it on the converter page.
This article is educational. It does not describe trading performance and is not financial advice. Always compile and test code in MetaEditor and the Strategy Tester before using it on a live account.