Download MetaTrader 5

detect time frame

To add comments, please log in or register
rjngh2005
43
rjngh2005  
I understand that there is a variable called
period_current which can be used in a script

I have attached the same script to M1 and M5 time frame
I have an alert which says "event triggered"
however i am not sure if this alert was triggered by M1 or M5

Please assist
Fernando Carreiro
2847
Fernando Carreiro  
rjngh2005:
I understand that there is a variable called
period_current which can be used in a script

I have attached the same script to M1 and M5 time frame
I have an alert which says "event triggered"
however i am not sure if this alert was triggered by M1 or M5

Please assist

There is nothing attached to your post! So, we are unable to help much at the point.

The variable that returns the current Time-frame / Period is "_Period" or you can also use the "Period()" function.

Donald Gibson
495
Donald Gibson  
rjngh2005:
I understand that there is a variable called
period_current which can be used in a script

I have attached the same script to M1 and M5 time frame
I have an alert which says "event triggered"
however i am not sure if this alert was triggered by M1 or M5

Please assist

Print("Current TimeFrame is ..."+IntegerToString(Period(),0));
Fernando Carreiro
2847
Fernando Carreiro  
4x_Gypsy:
Print("Current TimeFrame is ..."+IntegerToString(Period(),0));

Just as a clarification, there is no need for String Concatenation or for Conversion when using the Print() function:

Print( "Current TimeFrame is ... ", Period() ); // Using Function
Print( "Current TimeFrame is ... ", _Period  ); // Using Variable
honest_knave
Moderator
2326
honest_knave  

You may want something a little more intuitive than just the number i.e. "H4" instead of "240"

Print("The current timeframe is ", StringSubstr(EnumToString((ENUM_TIMEFRAMES)_Period),7));  

or a switch, particularly if you use non-standard timeframes or want something more descriptive i.e. "Daily" instead of "D1"

string timeframe()
  {
   switch(_Period)
     {
      case PERIOD_M1:  return("M1");
      case PERIOD_M5:  return("M5");
      case PERIOD_M15: return("M15");
      case PERIOD_M30: return("M30");
      case PERIOD_H1:  return("Hourly");
      case PERIOD_H4:  return("H4");
      case PERIOD_D1:  return("Daily");
      case PERIOD_W1:  return("Weekly");
      case PERIOD_MN1: return("Monthly");
      default: return(string(_Period));
     }
  }
Donald Gibson
495
Donald Gibson  
FMIC:

Just as a clarification, there is no need for String Concatenation or for Conversion when using the Print() function:

Thank you, I didn't know that.
whroeder1
15904
whroeder1  
honest_knave: You may want something a little more intuitive than just the number i.e. "H4" instead of "240"
Print("The current timeframe is ", StringSubstr(EnumToString((ENUM_TIMEFRAMES)_Period),7));  
Write self-documenting code. I use this pattern for enumerations.
/// Convert a ENUM_APPLIED_PRICE to string.
string            as_string(ENUM_APPLIED_PRICE price){
   string   price_xxx  = EnumToString(price);                   // PRICE_XXX
   return StringSubstr(price_xxx, 6);                           // XXX
}
/// Convert a period to string.
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
}
///////////////////////////
Print("The current timeframe is ", as_string(PERIOD_CURRENT));
rjngh2005
43
rjngh2005  

Thanks all for your responses

I shall be trying the following

Alert ("event triggered ",IntegerToString(Period(),0));


Fernando Carreiro
2847
Fernando Carreiro  
rjngh2005: Thanks all for your responses. I shall be trying the following ...

I don't need to convert it for the Alert() function either. This will do ...

Alert( "event triggered ", _Period );
To add comments, please log in or register