How to detrmine the start of a new candle in MQL5 indicator

 

Greetings,

I'm rather new to MQL5 programming and have a question about candle switch in MQL5.

I did program a simple indicator in MQL4 and it was easy to determine the start of a new candle by looking at the value of the time[0] argument passed to the OnCalculate event.

When looking at the time[0] value of the OnCalculate event in MQL5, however, some things are definitely not the same.

One: The datetime value of time[0] always seems to be in the past. For example "2023-12-12 08:13:00" on a one minute interval and "2022-11-16 16:20:00"  on a five minute interval. 

Two: This value does not seem to change at all after 1 or 5 minutes!

I did attach my minimal piece of code and I am aware of the fact that my knowledge of MQL5 is not enough at the moment. But eager to learn.

Hope to hear from you what I am overlooking,

With regards,

Aad Slingerland

Files:
 
Aad Slingerland:

Greetings,

I'm rather new to MQL5 programming and have a question about candle switch in MQL5.

I did program a simple indicator in MQL4 and it was easy to determine the start of a new candle by looking at the value of the time[0] argument passed to the OnCalculate event.

When looking at the time[0] value of the OnCalculate event in MQL5, however, some things are definitely not the same.

One: The datetime value of time[0] always seems to be in the past. For example "2023-12-12 08:13:00" on a one minute interval and "2022-11-16 16:20:00"  on a five minute interval. 

Two: This value does not seem to change at all after 1 or 5 minutes!

I did attach my minimal piece of code and I am aware of the fact that my knowledge of MQL5 is not enough at the moment. But eager to learn.

Hope to hear from you what I am overlooking,

With regards,

Aad Slingerland

You may look at my blog to detect new bars on indicator or EA for mql5

 
Rajesh Kumar Nait #:
You may look at my blog to detect new bars on indicator or EA for mql5

Thank you !
 
Aad Slingerland: When looking at the time[0] value

In MT4, buffers and MT4 predefined arrays are all ordered AsSeries. There is a difference between the arrays passed to OnCalculate (e.g. low[]) and the MT4 predefined variables (e.g. Low[].) The passed arrays have no default direction, just like MT5.

To determine the indexing direction of time[], open[], high[], low[], close[], tick_volume[], volume[] and spread[], call ArrayGetAsSeries(). In order not to depend on default values, you should unconditionally call the ArraySetAsSeries() function for those arrays, which are expected to work with.
          Event Handling Functions - Functions - Language Basics - MQL4 Reference

In MT5, you must set the direction.

To define the indexing direction in the time[], open[], high[], low[], close[], tick_volume[], volume[] and spread[] arrays, call the ArrayGetAsSeries() function. In order not to depend on defaults, call the ArraySetAsSeries() function for the arrays to work with.
          Event Handling / OnCalculate - Reference on algorithmic/automated trading language for MetaTrader 5
Reason: