Is there a Period() equivalent that returns as shown in the actual chart or do I have to make this ?

 

Is there a Period() equivalent that returns as shown in the actual chart or do I have to make this ? 

aka M5, H1, H4 etc. ? 

Or only in minutes like Period() ? 
Thanks

 
Agent86: Is there a Period() equivalent that returns as shown in the actual chart or do I have to make this ? aka M5, H1, H4 etc. ? Or only in minutes like Period() ?  Thanks

In MQL4, Period() or _Period does return minutes but for compatibility with MQL5, consider it as returning an ENUM_TIMEFRAMES enumeration. So convert it into a string and remove the initial "PERIOD_".

// Function to convert time-frame to a string
   string TimeframeToString( ENUM_TIMEFRAMES eTimeframe ) {
      string sTimeframe = EnumToString( eTimeframe );
      StringReplace( sTimeframe, "PERIOD_", "" );
      return sTimeframe;
   };
// Not compiled nor tested, just typed out as example code
   string sCurrentTimeframe = TimeframeToString( _Period );
   Print( "Current chart timeframe is ", sCurrentTimeframe );
 
Fernando Carreiro #:

In MQL4, Period() or _Period does return minutes but for compatibility with MQL5, consider it as returning an ENUM_TIMEFRAMES enumeration. So convert it into a string and remove the initial "PERIOD_".

You really understand the OP question ? I don't.
 
Alain Verleyen #: You really understand the OP question ? I don't.
Yes! The OP wants the period or time-frame of chart, as represented by the usual time-frame chart selection—for example "H1" instead of 60 minutes.
 
Fernando Carreiro #:
Yes! The OP wants the period or time-frame of chart, as represented by the usual time-frame chart selection—for example "H1" instead of 60 minutes.

Ah ok, I see. Thank you.

I am using:

#define TIMEFRAME(tf)                   StringSubstr(EnumToString((ENUM_TIMEFRAMES)tf),7)
 

Alain Verleyen #: Ah ok, I see. Thank you. I am using:

#define TIMEFRAME(tf) StringSubstr(EnumToString((ENUM_TIMEFRAMES)tf),7)

Yes, that seems more efficient than my code example. Thanks!

 
Fernando Carreiro #:

In MQL4, Period() or _Period does return minutes but for compatibility with MQL5, consider it as returning an ENUM_TIMEFRAMES enumeration. So convert it into a string and remove the initial "PERIOD_".

Got it
Thanks for the reply

I just didn't want to reinvent something if it already existed as a built in function and I just couldn't find it etc. etc. 
Thanks



 
Alain Verleyen #:

Ah ok, I see. Thank you.

I am using:

Thanks

This seems to work. 

EXAMPLE:

Print(StringSubstr(EnumToString((ENUM_TIMEFRAMES)_
Period ),7));
 
Agent86 #: Thanks. This seems to work. EXAMPLE:

Why complicate it? Use the macro as Alain provided ...

// Not compiled nor tested. Simply typed out as an example

#define TIMEFRAME( tf ) StringSubstr( EnumToString( (ENUM_TIMEFRAMES) tf ), 7 )

void SomeFunction() {

   // other code

   Print( TIMEFRAME( _Period ) );

   // other code 
};
 
Fernando Carreiro #: Why complicate it? Use the macro as Alain provided ...

To handle PERIOD_CURRENT and to make it understandable.

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
}
 
Fernando Carreiro #:

Why complicate it? Use the macro as Alain provided ...

Thanks to everyone. 

This helps a lot.