Trying to understand iBarShift

 

Hello all,

I have trouble understanding the basic concept of bar indexes.

From what I see, bar with index 0 is the oldest one on the chart.

The current one has index:

rates_total - 1 

I was trying to use iBarShift, but it seems to give me opposite results? This is what confuses me. In the example below time[0] is the oldest time available. But 

int index_H1=iBarShift(Symbol(),PERIOD_H1, time[0],true);

Gives a big number... Then calling it again with time[1] will give a smaller number. The code below runs in a M15 chart.

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[])
  {

   for(int bar_number=prev_calculated;
       bar_number<rates_total;
       bar_number++)
     {
      int index_H1=iBarShift(Symbol(),PERIOD_H1,time[bar_number],true);
     }

//--- return value of prev_calculated for next call
   return(rates_total);
  }

Could you help?

I'm trying to get a hang on iBarShift as an indicator I want to develop depends on data from 3 different time intervals.


Cheers!

 

No bar 0 is the newest or current one and the highest number is the oldest bar.

https://www.mql5.com/en/docs/series/ibarshift

Documentation on MQL5: Timeseries and Indicators Access / iBarShift
Documentation on MQL5: Timeseries and Indicators Access / iBarShift
  • www.mql5.com
//| Script program start function                                    | //
 

I checked in the debugger now and it seems that's not the case... This is at a breakpoint in OnCalculate. Also, ArrayGetAsSeries(time) shows false.

What am I missing?

 
Documentation on MQL5: Timeseries and Indicators Access
Documentation on MQL5: Timeseries and Indicators Access
  • www.mql5.com
These are functions for working with timeseries and indicators. A timeseries differs from the usual data array by its reverse ordering - elements of timeseries are indexed from the end of an array to its begin (from the most recent data to the oldest ones). To copy the time-series values and indicator data, it's recommended to use dynamic...
 

I read them... I just can't connect that info to what I see in the debug output. Even the bit that the doc says that time[0] should be the newest one, but in the screen I posted above you see that it's the other way round.

By the way, the doc you linked says:

Arrays passed to the function reflect price data, i.e. these arrays have the sign of a timeseries and function ArrayIsSeries() will return true when checking these arrays.


But the first thing I call in OnCalculate is:

   bool as_ser1 = ArrayGetAsSeries(time);
   bool as_ser2 = ArrayGetAsSeries(open);

and both values are false contrary to what the doc says

Reason: