time[0] is giving me a date that is 3 months ago

 

Hello, I am a newbie MQL5 coder - but not new to coding or trading.

I understand to access the open time of the most recent bar inside a custom indicator, in OnCalculate, I need to access time[0]. However this is giving me a time in May (13), while today is August 22. The data window in the chart is displaying the correct date. Can someone help please? 

this is the code...

int OnCalculate(const int32_t rates_total,
                const int32_t prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int32_t &spread[])
{
   if(rates_total > prev_calculated)
   {      
      MqlDateTime curr_GMT_struct  ={};
      TimeToStruct(time[0],curr_GMT_struct);      
      Print (time[0], "    hour  " , curr_GMT_struct.hour , "  min  ", curr_GMT_struct.min , "  dow  ",  curr_GMT_struct.day_of_week);
   }
}

The output is 

2025.08.22 07:34:06.106 ORB (@ENQ,M1) 2025.05.13 02:47:00    hour  2  min  47  dow  2


Improperly formatted code edited by moderator. Please always use the CODE button (Alt-S) when inserting code.

Code button in editor

 
Your topic has been moved to the section: Technical Indicators
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
dosskaranI understand to access the open time of the most recent bar inside a custom indicator, in OnCalculate, I need to access time[0].

No, that is not the case for MQL5, only for the older MQL4.

Please refer to the documentation:

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.

Documentation on MQL5: Event Handling / OnCalculate
Documentation on MQL5: Event Handling / OnCalculate
  • www.mql5.com
The function is called in the indicators when the Calculate event occurs for processing price data changes. There are two function types. Only one...
 
  1. Fernando Carreiro #: No, that is not the case for MQL5, only for the older MQL4.

    For MQL4 that is only the case for Time[] not time[].

  2. dosskaranI understand to access the open time of the most recent bar inside a custom indicator, in OnCalculate, I need to access time[0]. 

    Wrong. In MT5, you must set the direction.

    To define the indexing direction in the time[], open[], high[], low[], close[], tick_volume[], volume[] and spread[] and other arrays (copyXXXX functions), 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
 
dosskaran:

Hello, I am a newbie MQL5 coder - but not new to coding or trading.

I understand to access the open time of the most recent bar inside a custom indicator, in OnCalculate, I need to access time[0]. However this is giving me a time in May (13), while today is August 22. The data window in the chart is displaying the correct date. Can someone help please? 

this is the code...

The output is 


Improperly formatted code edited by moderator. Please always use the CODE button (Alt-S) when inserting code.

On MQL5 those variables from OnCalculate() are not set as Series by default. If you want them working as Series like the shift 0 to be the most recent bar you must first SetArrayAsSeries(true).

For example, SetArrayAsSeries(time, true);

This way, time[0] will be the current time.

 
@William Roeder #:
  1. For MQL4 that is only the case for Time[] not time[].

I disagree! Even on the MQL4 version of the OnCalculate and the use of time[], by default time[0] accesses the current bar, unless you use ArraySetAsSeries() to change it.

On MQL5 the access is reversed by default, where time[rates_total-1] is the current bar, unless you use ArraySetAsSeries() to change it.

 

I'm quickly looking back in the reference to see if it will help you:

https://www.mql5.com/en/docs/event_handlers/oncalculate

Unfortunately, it might have failed you. I can see how it isn't clear for a newcomer. You may need to read the book instead.

time[rates_total - 1]

is the current (newest) bar in MQL5 as buffers are not in series by default


ArraySetAsSeries(time, true);
double currentBarOpenTime = time[0];

Now time[0] references the current bar, and we are lucky this even works because of the fact that the buffer is defined as const and therefore is immutable in theory

Documentation on MQL5: Event Handling / OnCalculate
Documentation on MQL5: Event Handling / OnCalculate
  • www.mql5.com
The function is called in the indicators when the Calculate event occurs for processing price data changes. There are two function types. Only one...