Blog / Fix MT5 errors · MQL5 Market · MQL5 · Expert Advisors
Part 3: Handling Every Symbol and Timeframe the Validator Throws at You
Learn how to prepare your MQL5 Expert Advisor for different symbols and timeframes by handling market data, indicator handles, and trading specifications.
· 4 min read
An Expert Advisor can work perfectly on EURUSD M15 and still fail when tested on another symbol or timeframe.
The reason is often simple: the code assumes that every instrument behaves like the one used during development.
MQL5 Market validation can expose these assumptions. A symbol may have a different point size, tick size, volume step, trading session, or available history. A different timeframe can also expose missing bars, incorrect indicator handles, and assumptions about how frequently signals should appear.
The solution is not to make every EA trade every instrument. It is to make sure the EA handles its supported trading conditions correctly and fails gracefully when those conditions are not met.
This is Part 3. Part 1 covers runtime errors, closed sessions and missing history, and Part 2 the automatic tester's no-trades, invalid volume and margin failures.
1. Stop hardcoding the chart symbol and timeframe
One common mistake is mixing the current chart's properties with data requested for another symbol or timeframe.
For example:
double point = _Point;
int digits = _Digits;These values belong to the chart symbol. If the EA is processing another instrument, using them for that instrument can produce incorrect price calculations.
Retrieve the properties of the symbol being processed instead:
double point = SymbolInfoDouble(symbol, SYMBOL_POINT);
double tick_size = SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_SIZE);
int digits = (int)SymbolInfoInteger(symbol, SYMBOL_DIGITS);The same principle applies to timeframes. If the EA uses an input timeframe, indicator handles and historical data requests must consistently use that timeframe rather than silently falling back to _Period.
For example:
input ENUM_TIMEFRAMES SignalTimeframe = PERIOD_CURRENT;
ENUM_TIMEFRAMES GetSignalTimeframe()
{
if(SignalTimeframe == PERIOD_CURRENT)
return (ENUM_TIMEFRAMES)_Period;
return SignalTimeframe;
}This makes the intended timeframe explicit and avoids accidentally mixing chart data with signal data.
2. Select the symbol and verify its history
A symbol name that works with one broker may not exist under exactly the same name with another broker. You might encounter suffixes such as EURUSD.a or XAUUSDm.
Do not assume that every requested symbol is available.
Check that it exists, select it in Market Watch, and verify that the required historical data can be retrieved.
bool LoadRates(const string symbol,
const ENUM_TIMEFRAMES timeframe,
const int required_bars,
MqlRates &rates[])
{
if(!SymbolSelect(symbol, true))
{
PrintFormat("Cannot select symbol %s. Error: %d",
symbol, GetLastError());
return false;
}
int available_bars = Bars(symbol, timeframe);
if(available_bars < required_bars)
{
PrintFormat("Insufficient history for %s on %s",
symbol, EnumToString(timeframe));
return false;
}
ArraySetAsSeries(rates, true);
int copied = CopyRates(symbol, timeframe, 0,
required_bars, rates);
if(copied != required_bars)
{
PrintFormat("Could not copy enough rates for %s on %s",
symbol, EnumToString(timeframe));
return false;
}
return true;
}The function returns false when the symbol cannot be selected or the required bars are unavailable. The calling code must then skip that calculation or handle the failure appropriately. It should never continue using incomplete data.
Also remember that requesting history can initiate downloading or series construction. A first request may not immediately return all the bars you need. Your EA should be able to retry safely rather than assuming the data will always be ready on the first call.
3. Create indicator handles for the correct symbol and timeframe
Another frequent problem is creating an indicator handle for the current chart and then using it while processing a different symbol or timeframe.
For example:
int ma_handle = iMA(_Symbol, PERIOD_CURRENT,
50, 0, MODE_EMA, PRICE_CLOSE);This handle belongs to the current chart symbol and timeframe.
If your strategy needs an EMA for another instrument or period, create the handle using those exact parameters:
int ma_handle = iMA(symbol, timeframe,
50, 0, MODE_EMA, PRICE_CLOSE);
if(ma_handle == INVALID_HANDLE)
{
PrintFormat("Failed to create EMA handle for %s",
symbol);
return false;
}Create handles once, usually in OnInit(), and reuse them. Creating a new handle on every tick wastes resources and gives the indicator no time to calculate.
Before reading its buffer, check that the indicator has calculated enough values:
if(BarsCalculated(ma_handle) < 50)
{
Print("EMA data is not ready yet.");
return false;
}Then check the return value from CopyBuffer() before accessing the array. Indicator Handles and CopyBuffer Explained covers this step in detail.
If an EA processes multiple symbols and timeframes, maintain separate handles for each symbol-timeframe combination. Do not reuse a handle created for one instrument as though it belongs to another.
Release handles with IndicatorRelease() when they are no longer needed.
4. Check instrument-specific trading properties
Correct historical data does not automatically mean that an order will be accepted.
Symbols can have different:
- Minimum and maximum trading volumes.
- Volume steps and aggregate volume limits.
- Tick sizes and decimal precision.
- Stop and freeze levels.
- Filling modes and trading permissions.
- Trading sessions.
For example, a volume calculation that works for a forex pair with a 0.01 lot step may fail on an instrument that uses a 0.001 step. Lot Size Normalization in MT5 shows how to round to the symbol's own step.
Likewise, rounding a price to _Digits does not guarantee that it aligns with the instrument's actual tick size.
Retrieve the relevant properties for each symbol and validate the order before sending it. Avoid assuming that one set of trading specifications applies to every instrument.
5. Test the EA beyond its development setup
Before submitting an EA to the Market, test the combinations that are relevant to its intended use.
At a minimum, check:
- The primary symbol and timeframe.
- Other supported symbols with different price precision and volume steps.
- Higher and lower timeframes that the EA claims to support.
- Symbols with different trading sessions.
- Cases where historical data or indicator calculations are temporarily unavailable.
Watch the Experts and Journal tabs for runtime errors, failed indicator requests, invalid prices, rejected volumes, and repeated trade requests. How to read the Journal and Experts logs explains what to look for.
If the strategy is designed exclusively for a particular instrument or timeframe, state that limitation clearly in the product description and enforce it safely in the code. Do not pretend that a strategy is universal when it is not.
Final check
A reliable EA does not assume that every symbol has the same trading specifications or that every timeframe has immediately available history.
Use the correct symbol and timeframe in every data request, retrieve instrument-specific properties, validate indicator data before reading it, and handle unavailable conditions without crashing.
Part 4 moves beyond the code to the product page itself: the name, description, screenshots, logo and price.
If your EA still contains hardcoded symbol assumptions, inconsistent indicator handles, or fragile data requests, PositionAlpha's Fix an EA can help before you submit it. Paste or upload the EA and describe the problem; the AI returns the complete updated .mq4 or .mq5 file and explains the problems it found and each change it made. Compile the result in MetaEditor and test it in the Strategy Tester and on a demo account before you rely on it.
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.