Edgoro
Compile error 256 · MQL5

MQL5 error: undeclared identifier (causes and fix)

Fix the MetaEditor error "undeclared identifier" in MQL5: typos, scope, and MQL4 names such as Ask, Bid, Close[] and OP_BUY that don't exist in MQL5.

MetaEditor · Errors tab · error 256 · line 5
undeclared identifier 'Ask'

What it means

The compiler met a name it doesn't know at that point of the code. Nothing called Ask (or whatever is quoted) has been declared where the line can see it, so MetaEditor stops with error 256 and doesn't build the program. Older builds print the same error as 'Ask' - undeclared identifier.

Usual causes

  • MQL4 names. MetaTrader 4's built-in variables and constants don't exist in MQL5: Ask, Bid, Close[], Time[], OP_BUY, MODE_TRADES. Code converted from MT4 is the most common source of this error.
  • A typo or different capitalisation. InpStopPoint instead of InpStopPoints; MQL5 names are case-sensitive.
  • Declared out of reach. A variable declared inside an if, a loop or another function, or further down than the line that uses it.
  • A missing #include. Classes like CTrade come from #include <Trade/Trade.mqh>. Without it, MetaEditor reports 'CTrade' - unexpected token, probably type is missing? rather than an undeclared identifier.

How to detect it

  • MetaEditor's Errors tab, after you compile (F7), shows undeclared identifier 'Name' with the file, the line and the column. Double-click the message to jump to it.
  • Fix the first error first: one missing name can cause several follow-up errors.

Broken code

MetaEditor reports two errors on line 5: undeclared identifier 'Ask' (an MQL4 variable) and undeclared identifier 'InpStopPoint' (a typo).

MQL5
input int InpStopPoints = 200; // Stop distance, points

void OnStart()
  {
   double sl = Ask - InpStopPoint * _Point;
   Print("Stop-loss would be ", DoubleToString(sl, _Digits));
  }

Fixed code

The same program, corrected. It compiles in MetaEditor with 0 errors and 0 warnings.

MQL5
input int InpStopPoints = 200; // Stop distance, points

void OnStart()
  {
   double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
   double sl  = ask - InpStopPoints * _Point;
   Print("Stop-loss would be ", DoubleToString(sl, _Digits));
  }

Why the fix works

MQL5 has no Ask variable: the current price comes from SymbolInfoDouble(_Symbol, SYMBOL_ASK) (or SymbolInfoTick(), which returns the Bid and the Ask together). The input's name now matches its declaration exactly. Both names exist where line 5 uses them, so the script compiles.

MQL4 names and their MQL5 replacements

MQL4MQL5
Ask, BidSymbolInfoDouble(_Symbol, SYMBOL_ASK), SYMBOL_BID, or SymbolInfoTick()
Point, Digits_Point, _Digits (or Point(), Digits())
Close[i], Open[i], Time[i]iClose(_Symbol, PERIOD_CURRENT, i), iOpen(), iTime(), or CopyRates()
OP_BUY, OP_SELLORDER_TYPE_BUY, ORDER_TYPE_SELL
OrderSend() with 11 argumentsCTrade's Buy() and Sell(): see wrong parameters count

Without parentheses, Point and Digits give a different error in MQL5, "open parenthesis expected", because they're functions there. The rest of a port is in how to convert MQL4 to MQL5.

Questions

Why is 'Ask' an undeclared identifier in MQL5?

Because Ask and Bid were built-in variables in MQL4 only. In MQL5, read prices with SymbolInfoDouble(_Symbol, SYMBOL_ASK) and SYMBOL_BID, or get both with SymbolInfoTick().

Why is OP_BUY undeclared in MQL5?

OP_BUY and OP_SELL are MQL4 constants. MQL5 uses ORDER_TYPE_BUY and ORDER_TYPE_SELL, and the CTrade class's Buy() and Sell() methods for market orders.

Can I fix undeclared identifiers by defining the MQL4 names myself?

You can, with #define or helper functions, but it usually hides the real differences between MQL4 and MQL5, such as order handling and indicator handles. Converting the code properly is safer.

The programs on this page are minimal examples written to show the error and its fix, not trading systems. Some of them send orders when they run: try them on a demo account. Trading involves substantial risk of loss and is not suitable for every investor. Edgoro is a software tool, not a broker or financial adviser: nothing it produces is investment advice, and past or backtested performance does not guarantee future results.

Rather not write the error handling yourself?

Edgoro's MT5 EA generator writes complete MQL5 Expert Advisors from plain English and compiles each one in MetaEditor before you get it. A clean build proves the code compiles, not that a strategy works: test it on a demo account.