Print seconds remaining in bar

 

Hi there,

I want to print how much time is left remaining for a bar to close, so I can open an order just before the bar closes.

Print("Time left: ",PeriodSeconds()-TimeSeconds(Time[0]));

But on the M1 chart "60" prints out for every tick.

On the D1 chart "86400" prints out for every tick. 

It should print numbers approaching 0.

Can you please advise?

Thanks again. 

 

Bars open on round minutes, so the seconds will always be 0 when you call Time[]... remember that Time[] gives you the bar open time.

Try using TimeCurrent() instead. 

 
  datetime bar_start_time=Time[0];
  datetime bar_end_time=bar_start_time+PeriodSeconds();
  int seconds_remaining=bar_end_time-TimeCurrent();
.
 
brad: I want to print how much time is left remaining for a bar to close, so I can open an order just before the bar closes.
  1. TimeCurrent() exactly as GumRai posted.
  2. Also remember the bar doesn't close until a new tick is received, therefor seconds_remaining can go negative.
  3. If you had used this you would have found Free download of the 'Candle Closing Time Remaining - CCTR - Version 3' indicator by 'Arshen' for MetaTrader 4 in the MQL5 Code Base.
Reason: