Wrong value returned for Period() in mql5

 

Hello,

Recently I noticed that in mql5 the Period() function is returning wrong value for Time frame anything above 1 hour. It returns correct values for 1,2,3,4,5,610,12,15 and 30 minutes. But when I try on 1 hour chart and above the value always returned more than 16380. This can be tested easily with this line:

Comment(Period());

Try switching between time frames and you will notice it too. Is this a bug in mql5? I am using build 2593.

Documentation on MQL5: Checkup / Period
Documentation on MQL5: Checkup / Period
  • www.mql5.com
Period - Checkup - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Syed Oarasul Islam:

Hello,

Recently I noticed that in mql5 the Period() function is returning wrong value for Time frame anything above 1 hour. It returns correct values for 1,2,3,4,5,610,12,15 and 30 minutes. But when I try on 1 hour chart and above the value always returned more than 16380. This can be tested easily with this line:

Comment(Period());

Try switching between time frames and you will notice it too. Is this a bug in mql5? I am using build 2593.

It is not wrong, it is returning the enumeration as an int.

MQL5 is not the same as MQL4.

 
Chartperiod() or period() value greater than 1 hour quite divergent from minute values. Incl. request table enhancement (mql5 documentation)
Chartperiod() or period() value greater than 1 hour quite divergent from minute values. Incl. request table enhancement (mql5 documentation)
  • 2020.08.24
  • www.mql5.com
Can someone explain the logic/rationale behind the chosen values for the period (H1 - MN1) in MQL5 ( red values in table below...
 
Syed Oarasul Islam:   Recently I noticed that in mql5 the Period() function is returning wrong value for Time frame anything above 1 hour.
On MT5 ENUM_TIMEFRAMES values are not the period in minutes.
Use EnumToString(TF[i]) to see the string representation of the time frame.
Use PeriodSeconds(TF[i]) to see the duration of the time frame.
          Why are MT5 ENUM_TIMEFRAMES strange? - General - MQL5 programming forum #9 2020.06.10
 
 


Thank you Keith and William for clarifying this. Although it still feels weird to me how it works. However I figured a work around and below is my code for  those who atumble upon with this same problem: 

 //+------------------------------------------------------------------+
      //| Getting period in minutes                                         |
      //+------------------------------------------------------------------+  
      
        int getPeriodAsInt(ENUM_TIMEFRAMES TF) {

         int period = 0;
         switch (TF) {
         case PERIOD_M1:
            period = 1;
            break;
         case PERIOD_M2:
            period = 2;
            break;
         case PERIOD_M3:
            period = 3;
            break;
         case PERIOD_M4:
            period = 4;
            break;
         case PERIOD_M5:
            period = 5;
            break;
         case PERIOD_M6:
            period = 6;
            break;
         case PERIOD_M10:
            period = 10;
            break;
         case PERIOD_M12:
            period = 12;
            break;
         case PERIOD_M15:
            period = 15;
            break;
         case PERIOD_M20:
            period = 20;
            break;
         case PERIOD_M30:
            period = 30;
            break;
         case PERIOD_H1:
            period = 60;
            break;
         case PERIOD_H2:
            period = 120;
            break;
         case PERIOD_H3:
            period = 180;
            break;
         case PERIOD_H4:
            period = 240;
            break;
         case PERIOD_H6:
            period = 360;
            break;
         case PERIOD_H8:
            period = 480;
            break;
         case PERIOD_H12:
            period = 720;
            break;
         case PERIOD_D1:
            period = 1440;
            break;
         case PERIOD_W1:
            period = 10080;
            break;
         case PERIOD_MN1:
            period = 43200;
         }
         return (period);
      }

Comment(getPeriodAsInt(Period()));






This will return the values of each Time frame in minutes. Just like the way values returned for Period() function in MQL4.
 
Simplify
int getPeriodAsInt(ENUM_TIMEFRAMES TF) { return PeriodSeconds(TF) / 60; }
And don't type your comments in your code blocks, put it outside.
Reason: