Generally, to check whether you are on a new bar via mql4, the program logic is the following create a variable and store the current server time in it. Create a function that checks the time and returns true if it is a new candle and false if not. Compare the variables contents with the start time of the current candle.
Time value for the bar of specified symbol with timeframe and shift. To check errors, one has to call the getlasterror() function. For the current chart, the information about open bar times is in the time predefined array.
If the starttime and stoptime parameters are defined, the function returns the number of bars in the specified time interval, otherwise it returns the total number of bars.
If you want to execute some code just after a bar is closed or a new one is starting, the code is very simple. You will be using the variable time 0, which contains the start time of the current bar (or candle).
The bar closing time is not considered in the online trading system metatrader 4 (formally, the time of the last tick coming within a timeframe or the beginning time of the next timeframe can be considered as the bar closing time, as shown in fig. Bar forming sequence in the online trading platform metatrader 4.).
edit its also the only way you want to check for new bars with mtf on mql4 bool isnewbar () static datetime lastbar datetime curbar (datetime)seriesinfointeger (symbol,period,serieslastbardate) if (lastbar ! Curbar) lastbar curbar return true return false ignored.
this indicator shows the time before the new bar or candle opens. Information in the left top corner of the chart shows minutes and seconds left before the next bar opens. The chart will also show the time passed after the last bar or candle was open.
bool isnewbarfalse copying the last bar time to the element newtime 0 int copiedcopytime (symbol,period,0,1,newtime) if (copied0) ok, the data has been copied successfully. If (oldtime!newtime 0) if old time isnt equal to new bar time.
This option can also be enabled with the button of the charts toolbar or by the charts chart shift menu command. Chart autoscroll allowforbid to shift the chart to the left automatically after the new bar has started to form. If this option is enabled, the latest bar will always be shown in the chart.
the new input parameter alertshift(0-current bar,1-previous bar) was added.