Using PERIOD (....)

 

I am trying to migrate some of my work to MT5 (from MT4) and struggling with the 'Period Constant'. 

I want to pass the current chart information to a routine and return a string containing M1 or M5 or  H1 etc to match against other data.

   //int ipc = PERIOD_CURRENT;
   //double dpc = PERIOD_CURRENT;
   //ulong upc = PERIOD_CURRENT;
   int ipc = PERIOD_H1;
   double dpc = PERIOD_H1;
   ulong upc = PERIOD_H1;
   string strperiod = "ipc = "+IntegerToString(ipc)+"\ndpc = "+DoubleToString(dpc,2)+"\nupc = "+DoubleToString(upc,2);
   MessageBox(strperiod);

I tried the above and got 0 when I used PERIOD_Current and 16385 when I used PERIOD_H1 - I was expecting 60 when running H1 chart).

Any help appreciated.

 
Peter Williams:

I am trying to migrate some of my work to MT5 (from MT4) and struggling with the 'Period Constant'. 

I want to pass the current chart information to a routine and return a string containing M1 or M5 or  H1 etc to match against other data.

I tried the above and got 0 when I used PERIOD_Current and 16385 when I used PERIOD_H1 - I was expecting 60 when running H1 chart).

Any help appreciated.

No 'int'. No 'double'. No 'ulong'. Only ENUM_TIMEFRAMES enumeration.

Also use Period () - to get the current period as ENUM_TIMEFRAMES enumeration.

Documentation on MQL5: Constants, Enumerations and Structures / Chart Constants / Chart Timeframes
Documentation on MQL5: Constants, Enumerations and Structures / Chart Constants / Chart Timeframes
  • www.mql5.com
All predefined timeframes of charts have unique identifiers. The PERIOD_CURRENT identifier means the current period of a chart, at which a mql5-program is running.
 
Vladimir Karputov:

No 'int'. No 'double'. No 'ulong'. Only ENUM_TIMEFRAMES enumeration.

Also use Period () - to get the current period as ENUM_TIMEFRAMES enumeration.

Many thanks for swift response.  I am really struggling to identify my current chart

(I guess I don't really understand the meaning relevance of  ENUM_TIMEFRAMES and thought them more of a predefined constant)

   string strperiod = "start";
   if (PERIOD_CURRENT == PERIOD_H1 ) strperiod = strperiod + "\nif (PERIOD_CURRENT == PERIOD_H1 ) is true";
   if (PERIOD_CURRENT == Period()  ) strperiod = strperiod + "\nif (PERIOD_CURRENT == Period() is true";
   if (PERIOD_CURRENT == _Period  )  strperiod = strperiod + "\nif (PERIOD_CURRENT == _Period is true";
   if (_Period == 60 )               strperiod = strperiod + "\nif (_Period == 60 ) ";
   if (PERIOD_CURRENT != PERIOD_H1 ) strperiod = strperiod + "\nif (PERIOD_CURRENT != PERIOD_H1 ) is NOT true";
   MessageBox(strperiod);

only the last expression provides a response when I run on an H1 Chart

2020.03.27 12:01:32.423    Message    if (PERIOD_CURRENT != PERIOD_H1 ) is NOT true  taken from EXPERTS window at bottom of Page

How do I identify the Current Chart ?  (I want to read values from a File and will be different file for different Charts)

Documentation on MQL5: Constants, Enumerations and Structures / Chart Constants / Chart Timeframes
Documentation on MQL5: Constants, Enumerations and Structures / Chart Constants / Chart Timeframes
  • www.mql5.com
All predefined timeframes of charts have unique identifiers. The PERIOD_CURRENT identifier means the current period of a chart, at which a mql5-program is running.
 
Peter Williams:

Many thanks for swift response.  I am really struggling to identify my current chart

(I guess I don't really understand the meaning relevance of  ENUM_TIMEFRAMES and thought them more of a predefined constant)

only the last expression provides a response when I run on an H1 Chart

2020.03.27 12:01:32.423    Message    if (PERIOD_CURRENT != PERIOD_H1 ) is NOT true  taken from EXPERTS window at bottom of Page

How do I identify the Current Chart ?  (I want to read values from a File and will be different file for different Charts)

Seem to have found the answer by process of elimination

2020.03.27 12:47:37.422    Message    if (_Period == PERIOD_H1        )
2020.03.27 12:47:37.422    Message    if ( Period() == PERIOD_H1      )
2020.03.27 12:47:37.422    Message    if (PERIOD_CURRENT != PERIOD_H1 ) is NOT true

Many thanks as always

   string strperiod = "start";
   if (PERIOD_CURRENT == PERIOD_H1 ) strperiod = strperiod + "\nif (PERIOD_CURRENT == PERIOD_H1 )";
   if (PERIOD_CURRENT == Period()  ) strperiod = strperiod + "\nif (PERIOD_CURRENT == Period()  )";
   if (PERIOD_CURRENT == _Period   ) strperiod = strperiod + "\nif (PERIOD_CURRENT == _Period   )";
   if (_Period == 60               ) strperiod = strperiod + "\nif (_Period == 60               )";
   if (_Period == PERIOD_H1        ) strperiod = strperiod + "\nif (_Period == PERIOD_H1        )";
   if ( Period() == 60             ) strperiod = strperiod + "\nif ( Period() == 60             )";
   if ( Period() == PERIOD_H1      ) strperiod = strperiod + "\nif ( Period() == PERIOD_H1      )";
   if (PERIOD_CURRENT != PERIOD_H1 ) strperiod = strperiod + "\nif (PERIOD_CURRENT != PERIOD_H1 ) is NOT true";
   MessageBox(strperiod);
 
string   as_string(ENUM_TIMEFRAMES period){
   if(period == PERIOD_CURRENT)  period   = (ENUM_TIMEFRAMES) _Period;
   string   period_xxx  = EnumToString(period);                // PERIOD_XXX
   return StringSubstr(period_xxx, 7);                         // XXX
}
 
William Roeder:

Many thanks.

Just what I had been looking for.

& thanks again