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.
- 2020.08.24
- www.mql5.com
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.
int getPeriodAsInt(ENUM_TIMEFRAMES TF) { return PeriodSeconds(TF) / 60; }And don't type your comments in your code blocks, put it outside.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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.