Show timeframe of chart

 

Hi guys,

i like to receive an Alert message with Alert(); with the Text of the current timeframe in the chart


Alert(Show me the actual chart timeframe);


I can't find a call for that. I think one of you know an easy solution.

 
FamWue:

Hi guys,

i like to receive an Alert message with Alert(); with the Text of the current timeframe in the chart

Alert(Show me the actual chart timeframe);

Alert( Period() );  ?

Or did you want M1, M5, M15 etc ?

 
RaptorUK:

Or did you want M1, M5, M15 etc ?

 


Yes i do.
 
FamWue:

Yes i do.

OK . . .

Alert( "Timeframe is ", GetTimeFrame(Period()) );


//+---------------------------------------------------------------------+
//| GetTimeFrame function - returns the textual timeframe               |
//+---------------------------------------------------------------------+
string GetTimeFrame(int lPeriod)
   {
      switch(lPeriod)
      {
      case 1: return("M1");
      case 5: return("M5");
      case 15: return("M15"); 
      case 30: return("M30");
      case 60: return("H1");
      case 240: return("H4");
      case 1440: return("D1");
      case 10080: return("W1"); 
      case 43200: return("MN1"); 
      }
   
   }
//+---------------------------------------------------------------------+
 
or
string period.txt;
int init(){
   int      chrt.tf[]={ 0, PERIOD_M1, PERIOD_M5, PERIOD_M15, PERIOD_M30,
                           PERIOD_H1, PERIOD_H4, PERIOD_D1,  PERIOD_W1        };
   string   text.tf[]={ "n/a",   "M1",      "M5",      "M15",      "M30",
                                 "H1",      "H4",      "D1",       "W1"       };
   for(int iTF=0; chrt.tf[iTF] != Period(); iTF++){}
   period.txt = text.tf[iTF];
}
/////////////////////////////////////////////////////////
Alert( "Timeframe is ", period.txt );
 
RaptorUK:
case 60: return("H1");
Don't hard code numbers. Write self-documenting code
case PERIOD_H1: return("H1");
 
WHRoeder:
Don't hard code numbers. Write self-documenting code

:-)  point taken.
 
// string strPeriod = GetTimeFrame(Period();
//+---------------------------------------------------------------------+
//| GetTimeFrame function - returns the textual timeframe               |
//+---------------------------------------------------------------------+
string GetTimeFrame(int lPeriod)
   {
      switch(lPeriod)
      {
      case PERIOD_M1:  return("M1");
      case PERIOD_M2:  return("M2");
      case PERIOD_M3:  return("M3");
      case PERIOD_M4:  return("M4");
      case PERIOD_M5:  return("M5");
      case PERIOD_M6:  return("M6");
      case PERIOD_M10: return("M10");
      case PERIOD_M12: return("M12");
      case PERIOD_M15: return("M15");
      case PERIOD_M20: return("M20");
      case PERIOD_M30: return("M30");
      case PERIOD_H1:  return("H1");
      case PERIOD_H2:  return("H2");
      case PERIOD_H3:  return("H3");
      case PERIOD_H4:  return("H4");
      case PERIOD_H6:  return("H6");
      case PERIOD_H8:  return("H8");
      case PERIOD_H12: return("H12");
      case PERIOD_D1:  return("D1");
      case PERIOD_W1:  return("W1");
      case PERIOD_MN1: return("MN1");
      }
   return IntegerToString(lPeriod);
   }
//+---------------------------------------------------------------------+
 
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
}
 
whroeder1:
Interesting.
 
Elton Spode:
I believe it's efficient, you'll just need to add PERIOD_CURRENT
Reason: