detect time frame

Back to topics list To post a new topic, please log in or register
avatar
4
rjngh2005 2016.09.06 17:26
 
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

avatar
1223
FMIC 2016.09.06 21:13 #
 
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.

avatar
106
4x_Gypsy 2016.09.07 00:34 #
 
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));

avatar
1223
FMIC 2016.09.07 00:55 #
 
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
avatar
736
honest_knave 2016.09.07 01:29 #
 

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));
     }
  }

 

avatar
106
4x_Gypsy 2016.09.07 09:37 #
 
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.
avatar
13006
WHRoeder 2016.09.07 16:31 #
 
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));

avatar
4
rjngh2005 2016.09.09 19:59 #
 

Thanks all for your responses

I shall be trying the following

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


avatar
1223
FMIC 2016.09.09 20:15 #
 
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 );
Back to topics list  

To add comments, please log in or register