You can't know when a candle closes. Only when a new tick arrives that starts a new bar is the old bar closed.
For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
MT4: New candle - MQL4 programming forum #3 (2014)
MT5: Accessing variables - MQL4 programming forum #3 (2022)
I disagree with making a new bar function, because it can only be called once per tick (second call returns false). A variable can be tested multiple times.
Running EA once at the start of each bar - MQL4 programming forum (2011)
Hello,
In below given link their is a link of code of time to open next candle, it is working on all charts and show time of the candle as per TF of current chart only.
https://www.mql5.com/pt/forum/310104
Can any expert coder please help me identify what changes it need, if we want to show all the TF candle time on each chart.
I mean if we want to see like this on each chart
M5 - 00:00:60
M15 - 00:05:60
H1 - 00:05:60
H4 - 02:05:60
Hello . You can use the following , its not very commented out i can explain what's not clear. (tested on MT5)
//+------------------------------------------------------------------+ //| candle_times_countdown.mq5 | //| Copyright 2022, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2022, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ /* the countdown string assembler */ string return_two_digits(int value){ if(value<10){return("0"+IntegerToString(value));} return(IntegerToString(value)); } string countdown_text(datetime next_bar, datetime time_now){ //difference in seconds int secs=(int)next_bar-(int)time_now; //days //how many full days fit in the seconds difference int days=(int)MathFloor(((double)secs)/86400); //remove the seconds of the full days from the seconds difference secs-=days*86400; //hours int hours=(int)MathFloor(((double)secs)/3600); secs-=hours*3600; //minutes int minutes=(int)MathFloor(((double)secs)/60); secs-=minutes*60; //format return("D:"+return_two_digits(days)+"|H:"+return_two_digits(hours)+"|M:"+return_two_digits(minutes)+"|S:"+return_two_digits(secs)); } ENUM_TIMEFRAMES tfs[]={PERIOD_M1,PERIOD_M5,PERIOD_M15,PERIOD_M30,PERIOD_H1,PERIOD_H4,PERIOD_D1,PERIOD_W1,PERIOD_MN1}; string tf_texts[]={"M1","M5","M15","M30","H1","H4","D1","W1","MN1"}; int OnInit() { while(!EventSetTimer(1)){ Sleep(24); } return(INIT_SUCCEEDED); } void OnTimer() { //--- string comm=""; datetime now=0,server=TimeTradeServer(); for(int i=0;i<ArraySize(tfs);i++){ ResetLastError(); now=iTime(_Symbol,tfs[i],0); if(GetLastError()==0&&(server-now)<=PeriodSeconds(tfs[i])){ comm+=tf_texts[i]+":"+countdown_text((datetime)(now+PeriodSeconds(tfs[i])),TimeCurrent())+"\n"; } } Comment(comm); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { EventKillTimer(); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello,
In below given link their is a link of code of time to open next candle, it is working on all charts and show time of the candle as per TF of current chart only.
https://www.mql5.com/pt/forum/310104
Can any expert coder please help me identify what changes it need, if we want to show all the TF candle time on each chart.
I mean if we want to see like this on each chart
M5 - 00:00:60
M15 - 00:05:60
H1 - 00:05:60
H4 - 02:05:60