how to show currecnt candle time in M1 and M5 in one chart

 

hi every one

I write below code in expert to show current candle time in chart and it works in all time frame M1,M3,M15,....

void OnTimer()
  {
   long time=TimeCurrent();
   long ekh=(Period()*60+Time[0]-time);
   
   if(Period()==1)
   Comment(ekh," s");
   else if(Period()==5||Period()==15||Period()==30||Period()==60)
          {
           m=(char)(ekh/60);
           s=(char)(ekh-(m*60));
           Comment(m,"m ",s,"s");
          }        
        
      else if(Period()==240||Period()==1440)
      {
           h=(char)(ekh/3600);
           m=(char)((ekh-(h*3600))/60);
           s=(char)((ekh-(h*3600))-(m*60));
            Comment(h,"h ",m,"m ",s," s");
 
      }  
         else if(Period()==10080||Period()==43200)
      {
           d=(char)(ekh/86400);
           h=(char)((ekh-d*86400)/3600);
           m=(char)(((ekh-d*86400)-(h*3600))/60);
           s=(char)((ekh-d*86400)-(h*3600)-(m*60));
            Comment(d," d",h," h",m,"m ",s," s");
 
      } 
  }




now I run this expert in time frame: M1 it will show current candle time in M1 now if I want to see current candle time in M5 I should  change time frame but I want without changing time frame I can see current candle time in M1 and M5  all together

can anyone help me?

Best regards

Neda

 
I won't even study your code as you do not use descriptive variable names.
 

You can use TimeToStruct() to break down a number of seconds into hours and days. To get the candle time of another timeframe use iTime().

And you seem to be mixing long and char.

string TimeTillNextBar(ENUM_TIMEFRAMES tf=PERIOD_CURRENT)
  {
   datetime now=TimeCurrent();
   datetime bartime=iTime(NULL,tf,0);
   datetime remainingTime=bartime+PeriodSeconds(tf)-now;
   MqlDateTime mdt;
   TimeToStruct(remainingTime,mdt);
   if(mdt.day_of_year>0) return StringFormat("%d d %d h %d m %d s",mdt.day_of_year,mdt.hour,mdt.min,mdt.sec);
   if(mdt.hour>0) return StringFormat("%d h %d m %d s",mdt.hour,mdt.min,mdt.sec);
   if(mdt.min>0) return StringFormat("%d m %d s",mdt.min,mdt.sec);
   return StringFormat("%d s",mdt.sec);
  }

And call it with PERIOD_M1, PERIOD_M5 like so:

Comment("M1: "+TimeTillNextBar(PERIOD_M1)+"\n"+"M5: "+TimeTillNextBar(PERIOD_M5));
Documentation on MQL5: Date and Time / TimeToStruct
Documentation on MQL5: Date and Time / TimeToStruct
  • www.mql5.com
Date and Time / TimeToStruct - Reference on algorithmic/automated trading language for MetaTrader 5
 
lippmaje:

You can use TimeToStruct() to break down a number of seconds into hours and days. To get the candle time of another timeframe use iTime().

And you seem to be mixing long and char.

And call it with PERIOD_M1, PERIOD_M5 like so:

hi

Thanks alot

that works correctly

 

Hello

How to adding in the chart

Reason: