How do I go through an enumeration consistently?

 

Let's say we have an enumeration. How can we get all possible values of this enumeration one by one, e.g. in a loop?

ENUM_TIMEFRAMES tf1;

I.e. tf1++ must sequentially return PERIOD_M1, PERIOD_M2, PERIOD_M5... I think this cannot be done with the language tools.

Документация по MQL5: Стандартные константы, перечисления и структуры / Константы графиков / Периоды графиков
Документация по MQL5: Стандартные константы, перечисления и структуры / Константы графиков / Периоды графиков
  • www.mql5.com
Стандартные константы, перечисления и структуры / Константы графиков / Периоды графиков - Документация по MQL5
 
you cannot do this because the value of the periods does not increase by one, you have to organise your own enumeration index, like this
int TF(int i)
{
 switch(i)
 {
  case 1: return(PERIOD_M1);
  case 2: return(PERIOD_M5);
  case 3: return(PERIOD_M15);
  case 4: return(PERIOD_M30);
  case 5: return(PERIOD_H1);
  case 6: return(PERIOD_H4);
  case 7: return(PERIOD_D1);
  case 8: return(PERIOD_W1);
  case 9: return(PERIOD_MN1);
 }
}
 
sergey1294:
This is not possible because the period value is not incremented by one, you have to organise your own enumeration index, like this

If only it were that simple. The values are not evenly distributed. Suppose PERIOD_H1 is 60, and the next period PERIOD_H2 is 120.
Документация по MQL5: Стандартные константы, перечисления и структуры / Константы графиков / Периоды графиков
Документация по MQL5: Стандартные константы, перечисления и структуры / Константы графиков / Периоды графиков
  • www.mql5.com
Стандартные константы, перечисления и структуры / Константы графиков / Периоды графиков - Документация по MQL5
 
C-4:
If only it were that simple. The values are not evenly distributed. Let's say PERIOD_H1 is 60 and the next period PERIOD_H2 is 120.

There are probably two functions that need to be entered: 1 - returns the number of elements in the enumeration, 2 - returns the element by number...

 
Interesting:

There are probably two functions that need to be entered: 1 - returns the number of elements in the enumeration, 2 - returns the element by number...


That's right, that's what I did in MT4
 
C-4:
If only it were that simple. The values are not evenly distributed. Let's say PERIOD_H1 is 60 and the next period PERIOD_H2 is 120.

The point is that enumeration is a predefined thing and all its values can be viewed,

That's why respected sergey1294 advised you to describe the table of conversion into a serial call,

where if value increases by 1, the next enum value in the list will be called.

 
In general, there is one solution. Write a special Enumerator function for each enumeration. For example for Timeframes:
ENUM_TIMEFRAMES GetPeriodEnumerator(uchar i);
Now let's estimate how many enumerations there are and how many Enumerator functions there should be (one for each enumeration).
 
C-4:
In general, there is one solution. For each enumeration, write a special Enumerator function. For example, for Timeframes: Now let's estimate how many enumerations there are in total and then how many Enumerator functions there should be (one for each enumeration).
There are 21 timeframes in total in MT5. The final version will look like this
ENUM_TIMEFRAMES GetPeriodEnumerator(uchar i)
  {
   switch(i)
     {
      case 0  return(PERIOD_M1);
      case 1  return(PERIOD_M2);
      case 2  return(PERIOD_M3);
      case 3  return(PERIOD_M4);
      case 4  return(PERIOD_M5);
      case 5  return(PERIOD_M6);
      case 6  return(PERIOD_M10);
      case 7  return(PERIOD_M12);
      case 8  return(PERIOD_M15);
      case 9  return(PERIOD_M20);
      case 10 return(PERIOD_M30);
      case 11 return(PERIOD_H1);
      case 12 return(PERIOD_H2);
      case 13 return(PERIOD_H3);
      case 14 return(PERIOD_H4);
      case 15 return(PERIOD_H6);
      case 16 return(PERIOD_H8);
      case 17 return(PERIOD_H12);
      case 18 return(PERIOD_D1);
      case 19 return(PERIOD_W1);
      case 20 return(PERIOD_MN1);
     }
   return(-1);
  }
 
sergey1294:
There are a total of 21 timeframes in MT5. The final variant will look like this
Now we have to do the same for other standard numerators (identifiers) + if there are some custom things not to forget...
 
Another option is to write the timeframe values into an array and go through the array, obtaining the required value.
Документация по MQL5: Предопределенные переменные / _Period
Документация по MQL5: Предопределенные переменные / _Period
  • www.mql5.com
Предопределенные переменные / _Period - Документация по MQL5
 

In general, there are two options for enumeration

First using a function, I forgot to put a colon in my last post

ENUM_TIMEFRAMES GetPeriodEnumerator(uchar i)
  {
   switch(i)
     {
      case 0:  return(PERIOD_M1);
      case 1:  return(PERIOD_M2);
      case 2:  return(PERIOD_M3);
      case 3:  return(PERIOD_M4);
      case 4:  return(PERIOD_M5);
      case 5:  return(PERIOD_M6);
      case 6:  return(PERIOD_M10);
      case 7:  return(PERIOD_M12);
      case 8:  return(PERIOD_M15);
      case 9:  return(PERIOD_M20);
      case 10: return(PERIOD_M30);
      case 11: return(PERIOD_H1);
      case 12: return(PERIOD_H2);
      case 13: return(PERIOD_H3);
      case 14: return(PERIOD_H4);
      case 15: return(PERIOD_H6);
      case 16: return(PERIOD_H8);
      case 17: return(PERIOD_H12);
      case 18: return(PERIOD_D1);
      case 19: return(PERIOD_W1);
      case 20: return(PERIOD_MN1);
     }
   return(-1);
  }
The second way is using an array
ENUM_TIMEFRAMES GetPeriodEnumerator[21]=
  {
   PERIOD_M1,
   PERIOD_M2,
   PERIOD_M3,
   PERIOD_M4,
   PERIOD_M5,
   PERIOD_M6,
   PERIOD_M10,
   PERIOD_M12,
   PERIOD_M15,
   PERIOD_M20,
   PERIOD_M30,
   PERIOD_H1,
   PERIOD_H2,
   PERIOD_H3,
   PERIOD_H4,
   PERIOD_H6,
   PERIOD_H8,
   PERIOD_H12,
   PERIOD_D1,
   PERIOD_W1,
   PERIOD_MN1
  };
Reason: