Is there a Period() equivalent that returns as shown in the actual chart or do I have to make this ?
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 );
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_".
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
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 };
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 }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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