What it means
A plain number is passed where the function expects a value of an enumeration, such as ENUM_TIMEFRAMES for a timeframe. MQL5 doesn't convert an int variable to an enumeration on its own, so MetaEditor stops with error 262 and shows the function's expected signature underneath.
Usual causes
- A timeframe stored as an
intinput, as in MQL4 code where timeframes were minutes:input int InpTf = 15;. - Moving-average methods or applied prices as numbers where
ENUM_MA_METHODorENUM_APPLIED_PRICEis expected. - Your own enumerations passed from an
intvariable.
How to detect it
- MetaEditor's Errors tab shows
cannot convert enumon the call, then an information line with the built-in signature, for examplebuilt-in: int iMA(const string,ENUM_TIMEFRAMES,int,int,ENUM_MA_METHOD,int). Compare each argument with that list.
Broken code
MetaEditor reports cannot convert enum on the iMA() call (line 7, the timeframe argument).
input int InpTf = 15; // Timeframe, in minutes (MQL4 habit)
int maHandle = INVALID_HANDLE;
int OnInit()
{
maHandle = iMA(_Symbol, InpTf, 20, 0, MODE_EMA, PRICE_CLOSE);
return maHandle == INVALID_HANDLE ? INIT_FAILED : INIT_SUCCEEDED;
}
void OnTick()
{
}Fixed code
The same program, corrected. It compiles in MetaEditor with 0 errors and 0 warnings.
input ENUM_TIMEFRAMES InpTf = PERIOD_M15; // Timeframe
int maHandle = INVALID_HANDLE;
int OnInit()
{
maHandle = iMA(_Symbol, InpTf, 20, 0, MODE_EMA, PRICE_CLOSE);
if(maHandle == INVALID_HANDLE)
{
PrintFormat("iMA failed: error %d", GetLastError());
return INIT_FAILED;
}
return INIT_SUCCEEDED;
}
void OnTick()
{
}
void OnDeinit(const int reason)
{
if(maHandle != INVALID_HANDLE)
IndicatorRelease(maHandle);
}Why the fix works
The input is now declared as ENUM_TIMEFRAMES, so the value passed to iMA() already has the type it expects. As a bonus, an enumeration input shows as a drop-down list of timeframes in the EA's settings, so nobody can type a timeframe that doesn't exist.
Why not just cast the number?
(ENUM_TIMEFRAMES)InpTf compiles, but the numbers aren't what MQL4 code assumes. In MQL5, PERIOD_M15 is 15, but PERIOD_H1 is 16385, not 60. A cast of 60 doesn't give you H1. Use the enumeration names, or an ENUM_TIMEFRAMES input, and the problem doesn't arise.
Questions
Why is PERIOD_H1 not 60 in MQL5?
Because MQL5's ENUM_TIMEFRAMES values aren't all minutes. Minute timeframes match their minutes (PERIOD_M15 is 15), but hourly and longer ones use other codes: PERIOD_H1 is 16385. Always use the names.
How do I let the user choose a timeframe in an EA's inputs?
Declare the input as ENUM_TIMEFRAMES, for example input ENUM_TIMEFRAMES InpTf = PERIOD_M15;. The settings dialog shows a list of timeframes, and the value can go straight into iMA(), CopyRates() or iTime().
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.