Edgoro
Compile error 262 · MQL5

MQL5 error: cannot convert enum (timeframes in iMA)

Fix MetaEditor's "cannot convert enum" error: an int passed where MQL5 expects ENUM_TIMEFRAMES or another enumeration, and why PERIOD_H1 isn't 60.

MetaEditor · Errors tab · error 262 · line 7
cannot convert enum
built-in: int iMA(const string,ENUM_TIMEFRAMES,int,int,ENUM_MA_METHOD,int)

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 int input, as in MQL4 code where timeframes were minutes: input int InpTf = 15;.
  • Moving-average methods or applied prices as numbers where ENUM_MA_METHOD or ENUM_APPLIED_PRICE is expected.
  • Your own enumerations passed from an int variable.

How to detect it

  • MetaEditor's Errors tab shows cannot convert enum on the call, then an information line with the built-in signature, for example built-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).

MQL5
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.

MQL5
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.

Working code that avoids it

All MQL5 examples

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.