Wrong values for close, open, low, high arrays in MetaTrader 5

 

I was getting wrong values in OnCalculate() method in my custom indicator. 

Here is a test indicator just logging the values. 


int OnCalculate(const int rates_total,
                const int 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 int &spread[])
  {
   int startPos = rates_total-prev_calculated-1;
   for(int pos=startPos; pos>=0; pos--)
     {
      Print("pos: ", pos, " | close: ", close[pos], " | open: ", open[pos]);
      Print("pos: ", pos, " | low: ", low[pos], " | high: ", high[pos]);
     }
   return(rates_total);
  }

and the log output is:

log output


as it can be seen, the prices are not correct and not even near to the current market price. 

I've test with other charts and the values are not the same but they are wrong too.


Any help would be appreciated.

Regards,

 
There is nothing wrong. Index 0 is the OLDEST bar, not the current one, as the arrays are not indexed as series by default in MT5.
 

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: