Blog / Fix MT5 errors · MQL4 · MQL5 · Expert Advisors
MQL4 Compile Errors in MT5: “Undeclared Identifier Ask/Bid” and Friends
Learn why MQL4 variables like Ask, Bid, Point and Digits cause compile errors in MT5, and discover their correct MQL5 replacements.
· 5 min read
One of the first problems developers encounter when moving an Expert Advisor from MetaTrader 4 to MetaTrader 5 is the compiler rejecting familiar MQL4 identifiers.
A common message is:
'Ask' - undeclared identifierThe same problem can occur with Bid, Point, Digits and price-series arrays such as Open[] and Close[].
These errors occur because MQL5 handles some market data differently. Copying an MQL4 source file into MetaEditor 5 does not automatically make it compatible with MQL5.
The fix is not simply to rename every identifier. You need to use the correct MQL5 replacement while preserving what the original code was intended to do.
1. Fixing Undeclared Ask and Bid Errors
In MQL4, Ask and Bid are predefined variables containing the latest known Ask and Bid prices for the current chart symbol.
An MQL4 EA might use:
double buyPrice = Ask;
double sellPrice = Bid;These predefined variables are not available in MQL5. Instead, retrieve the latest quote using SymbolInfoTick() and an MqlTick structure.
MQL5 replacement:
MqlTick tick;
if(SymbolInfoTick(_Symbol, tick))
{
double buyPrice = tick.ask;
double sellPrice = tick.bid;
Print("Ask: ", buyPrice);
Print("Bid: ", sellPrice);
}
else
{
Print("Unable to retrieve current prices. Error: ",
GetLastError());
}Here:
_Symbolidentifies the current chart symbol.tick.askcontains the Ask price.tick.bidcontains the Bid price.SymbolInfoTick()retrieves the latest available tick information.
This is generally the most convenient approach when you need both prices because they are retrieved together.
If you only need one price, you can also use SymbolInfoDouble():
double ask = 0.0;
double bid = 0.0;
if(!SymbolInfoDouble(_Symbol, SYMBOL_ASK, ask) ||
!SymbolInfoDouble(_Symbol, SYMBOL_BID, bid))
{
Print("Unable to retrieve Bid/Ask prices. Error: ",
GetLastError());
return;
}Important: Do not assume that a successfully retrieved quote remains current indefinitely. For time-sensitive trade requests, obtain a fresh tick immediately before using the price.
2. Replacing Point and Digits
Point and Digits are also common sources of MQL4-to-MQL5 compilation errors.
In MQL4, developers use them to calculate price distances and round prices to the symbol's displayed decimal precision.
For example:
double stopLoss = Ask - 100 * Point;
double takeProfit = Ask + 200 * Point;
double roundedPrice = NormalizeDouble(Ask, Digits);In MQL5, the corresponding predefined variables are _Point and _Digits. You must also replace Ask with a valid price source.
MQL5 replacement:
MqlTick tick;
if(SymbolInfoTick(_Symbol, tick))
{
double stopLoss = tick.ask - 100 * _Point;
double takeProfit = tick.ask + 200 * _Point;
double roundedPrice = NormalizeDouble(
tick.ask,
_Digits
);
}
else
{
Print("Unable to retrieve current prices. Error: ",
GetLastError());
}The common replacements are:
| MQL4 | MQL5 |
|---|---|
Ask |
tick.ask |
Bid |
tick.bid |
Point |
_Point |
Digits |
_Digits |
_Point represents the point size of the current chart symbol, while _Digits represents its displayed decimal precision.
If your EA calculates prices for a different symbol, retrieve that symbol's properties instead of using the current chart's _Point and _Digits.
For example:
double point = 0.0;
long digits = 0;
if(!SymbolInfoDouble("EURUSD", SYMBOL_POINT, point) ||
!SymbolInfoInteger("EURUSD", SYMBOL_DIGITS, digits))
{
Print("Unable to retrieve symbol properties.");
return;
}There is another important distinction: NormalizeDouble() controls decimal precision, but it does not necessarily align a price with the instrument's minimum trade tick size. Some instruments have a tick size different from their point size.
For trading prices, check SYMBOL_TRADE_TICK_SIZE when the calculation needs to comply with the instrument's actual price increments.
3. Replacing Open, High, Low and Close Arrays
MQL4 developers often access candle data through predefined price-series arrays:
double currentOpen = Open[0];
double currentHigh = High[0];
double currentLow = Low[0];
double currentClose = Close[0];Index zero generally represents the current, incomplete candle, while index one represents the previous candle.
An MQL5 Expert Advisor does not automatically have these same global arrays. Instead, use CopyRates() to retrieve price data into an MqlRates array.
MQL5 replacement:
MqlRates rates[];
ArraySetAsSeries(rates, true);
int copied = CopyRates(
_Symbol,
_Period,
0,
3,
rates
);
if(copied >= 3)
{
double currentOpen = rates[0].open;
double currentHigh = rates[0].high;
double currentLow = rates[0].low;
double currentClose = rates[0].close;
double previousClose = rates[1].close;
}
else
{
Print("Unable to retrieve sufficient price data. Copied: ",
copied);
}The MqlRates structure contains each bar's open, high, low, close, tick volume, spread and real volume.
ArraySetAsSeries(rates, true) makes the array use series-style indexing, so the most recent bar is at index zero.
Check which candle your original EA uses. If the strategy is designed to evaluate completed candles, use index one rather than index zero. Otherwise, the converted EA may start making decisions using an unfinished candle.
4. Replacing MarketInfo() With MQL5 Symbol Information Functions
MQL4 developers frequently use MarketInfo() to retrieve instrument specifications.
For example:
double minLot = MarketInfo(
Symbol(),
MODE_MINLOT
);
double maxLot = MarketInfo(
Symbol(),
MODE_MAXLOT
);MQL5 uses SymbolInfoDouble() and SymbolInfoInteger() instead. The correct function depends on the type of property being retrieved.
For example, to retrieve volume specifications:
double minLot = 0.0;
double maxLot = 0.0;
double lotStep = 0.0;
if(!SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN, minLot) ||
!SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX, maxLot) ||
!SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP, lotStep))
{
Print("Unable to retrieve volume specifications. Error: ",
GetLastError());
return;
}
Print("Minimum lot: ", minLot);
Print("Maximum lot: ", maxLot);
Print("Lot step: ", lotStep);Common MQL4-to-MQL5 property replacements include:
| MQL4 property | MQL5 property | Retrieval function |
|---|---|---|
MODE_MINLOT |
SYMBOL_VOLUME_MIN |
SymbolInfoDouble() |
MODE_MAXLOT |
SYMBOL_VOLUME_MAX |
SymbolInfoDouble() |
MODE_LOTSTEP |
SYMBOL_VOLUME_STEP |
SymbolInfoDouble() |
MODE_POINT |
SYMBOL_POINT |
SymbolInfoDouble() |
MODE_DIGITS |
SYMBOL_DIGITS |
SymbolInfoInteger() |
MODE_SPREAD |
SYMBOL_SPREAD |
SymbolInfoInteger() |
For example, to retrieve the current spread:
long spread = 0;
if(SymbolInfoInteger(_Symbol, SYMBOL_SPREAD, spread))
{
Print("Current spread: ", spread, " points");
}
else
{
Print("Unable to retrieve spread. Error: ",
GetLastError());
}Do not assume that replacing MarketInfo() with a similarly named MQL5 function is sufficient. You must also select the correct MQL5 property and handle its return value.
5. Why Fixing These Errors Does Not Automatically Convert an EA
Replacing Ask, Bid, Point and Digits may resolve several compilation errors, but it does not make the entire EA compatible with MQL5.
One of the biggest differences is how the two platforms handle trading operations.
MQL4 commonly uses functions such as:
OrderSend();
OrderClose();
OrderModify();
OrderSelect();MQL5 has a different trading architecture involving three distinct concepts:
- Orders: instructions submitted to the trading system.
- Deals: executed transactions resulting from orders.
- Positions: the resulting open market exposure.
MQL4 and MQL5 also have different function interfaces. For example, MQL4's OrderSend() uses positional parameters, while MQL5's OrderSend() accepts an MqlTradeRequest structure and an MqlTradeResult structure (how to fix the "wrong parameters count" error).
Simply replacing one function with another can therefore introduce incorrect behaviour, even if the code compiles.
A proper conversion must review the EA's complete trading logic, including:
- Order submission and execution.
- Position selection and identification.
- Stop-loss and take-profit management.
- Trade modification and closing.
- Hedging and netting account compatibility.
Treat compilation as the first checkpoint, not proof that the conversion is complete.
6. A Practical Checklist for Fixing MQL4-to-MQL5 Compile Errors
When an old MQL4 EA produces undeclared identifier errors in MetaEditor 5, work through these steps.
Step 1: Identify the exact identifier.
Read the compiler message and locate the affected line. Do not assume every error has the same cause.
Step 2: Check whether it is an MQL4 predefined variable.
Common examples include Ask, Bid, Point and Digits. Also check for price-series arrays such as Open[] and Close[].
Step 3: Find the correct MQL5 replacement.
Use the appropriate MQL5 variable, structure or symbol-information function. For example, use SymbolInfoTick() for current Bid and Ask prices.
Step 4: Preserve the original logic.
Check the surrounding calculations and trading conditions. A replacement that compiles may still change the EA's behaviour if it uses a different price, candle or symbol.
Step 5: Compile again and review the remaining errors.
Fix each incompatibility individually. Resolving one undeclared identifier may reveal additional differences between MQL4 and MQL5.
Step 6: Test the converted EA.
Run it in the MT5 Strategy Tester and verify its trading logic, order handling and position management. Compilation success alone does not establish that the conversion is correct.
Conclusion
Undeclared identifier errors involving Ask, Bid, Point and Digits are common when moving an Expert Advisor from MQL4 to MetaTrader 5.
Use SymbolInfoTick() and MqlTick to retrieve current prices, _Point and _Digits for the current chart symbol, CopyRates() for candle data, and the appropriate SymbolInfo functions for instrument specifications.
However, converting an entire EA requires more than replacing a few identifiers. Its trading functions, order-management logic and account compatibility must also be reviewed.
Fix the compiler errors first, then test the converted EA to confirm that it behaves as intended.
To convert a whole file at once, PositionAlpha's MQL4 to MQL5 converter returns the EA as an .mq5 with a report of what converted exactly, what had to change for MT5 and what couldn't be converted. Compile it in MetaEditor and test it in the Strategy Tester and on a demo account before you rely on it. For the bigger picture, see MQL4 vs MQL5: the differences that matter.
Build a bot from your idea
Describe your trading idea and the AI writes MT4 or MT5 code for you to review and test, with a plain-English explanation and setup steps. Your first bot is free.