iBarShift returns Zero values on non 5 minute bars

 

Dear Forum Members

I am trying to use iBarShift function and noticed that for a 5 minute chart, it returns ZERO values except when a new 5 minute candle is opened.

I will be thankful, if someone can help me out to locate the reasons for this error. On my other indicators, iBarShift is working fine.

MJ 0 15:10:10.225 FxEAPrintDataStats (US30,M5) M05_NormII     [0.000] M05_ChangeNormII  [-100.000] M01_ChangeII [-25.928]

LN 0 15:11:19.301 FxEAPrintDataStats (US30,M5) M05_NormII     [-0.142] M05_ChangeNormII  [-55.041] M01_ChangeII [48.294]

OQ 0 15:12:09.433 FxEAPrintDataStats (US30,M5) M05_NormII     [0.000] M05_ChangeNormII  [-100.000] M01_ChangeII [-64.263]

PD 0 15:12:51.292 FxEAPrintDataStats (US30,M5) M05_NormII     [0.000] M05_ChangeNormII  [-100.000] M01_ChangeII [193.439]

NJ 0 15:13:49.649 FxEAPrintDataStats (US30,M5) M05_NormII     [0.000] M05_ChangeNormII  [-100.000] M01_ChangeII [-4.327]

RN 0 15:14:58.799 FxEAPrintDataStats (US30,M5) M05_NormII     [0.000] M05_ChangeNormII  [-100.000] M01_ChangeII [33.936]

OS 0 15:15:53.583 FxEAPrintDataStats (US30,M5) M05_NormII     [-0.039] M05_ChangeNormII  [-72.395] M01_ChangeII [35.671]

GD 0 15:16:39.417 FxEAPrintDataStats (US30,M5) M05_NormII     [0.000] M05_ChangeNormII  [-100.000] M01_ChangeII [-32.377]

EO 0 15:17:20.650 FxEAPrintDataStats (US30,M5) M05_NormII     [0.000] M05_ChangeNormII  [-100.000] M01_ChangeII [-53.103]


double M01_ChangeNormII = 0; // ((M01_NormII[index] - M01_NormII[index+1])/M01_NormII[index+1])*100;
double M05_ChangeNormII = 0; // ((M05_NormII[index] - M05_NormII[index+1])/M05_NormII[index+1])*100;
if(M01_NormII[barShift_M01+1] != 0)
  M01_ChangeNormII = ((M01_NormII[barShift_M01] - M01_NormII[barShift_M01+1])/M01_NormII[barShift_M01+1])*100;
if(M05_NormII[barShift_M05+1] != 0)
  M05_ChangeNormII = ((M05_NormII[barShift_M05] - M05_NormII[barShift_M05+1])/M05_NormII[barShift_M05+1])*100;
//+----------------------------------------------------------------------------------------------------------+
//| METHOD:       Get_NormII()
//| APPLICATION:  Calculate Normalized Intraday Intensity Index (II) Indicator Value
//| Intraday Intensity Index = ((Close×2) − High − Low) / ((High − Low) × Volume)
//+----------------------------------------------------------------------------------------------------------+
bool FxCIndicator::Get_NormII(ENUM_TIMEFRAMES timeFrame,int barCount,double &NormII[])
  {
    ArrayResize(NormII,barCount+1);
    // First Loop to calculate Normalize II from Bar Zero to barCount
    for(int i = 0; i < barCount; i++)
      {
        double sumPrice  = 0;
        long   sumVolume = 0;
        int    loopCount = (i + AD_NormalizePeriod);
        // Internal Loop to calculate II value for current bar index
        for(int j = i; j < loopCount; j++)
          {
            double Close  = iClose(m_Symbol,timeFrame,j);
            double High   = iHigh(m_Symbol,timeFrame,j);
            double Low    = iLow(m_Symbol,timeFrame,j);
            long   Volume = iTickVolume(m_Symbol,timeFrame,j);
            if(High - Low != 0)
              {
                sumPrice  = sumPrice + ((Close * 2) - High - Low)/(High - Low) * Volume;
                sumVolume = sumVolume + Volume;
              }
          }
        if(sumVolume != 0)
          {
            NormII[i] = NormalizeDouble((sumPrice / sumVolume),5);
          }
        ArraySetAsSeries(NormII,true);
      }
  //---
    return(true);
  } // END Of Get_NormII() method
 
Anil Varma: I am trying to use iBarShift function
  1. Your posted code doesn't have a iBarShift.
  2. Set asSeries before filling the array.
 
Anil Varma:

Hi William Thanks for your quick response. I have highlighted the previous code with blue to show where I have used iBarShift. In fact I have created method attached below to call the iBarShift on different timeframes. This method is working well on my other RSI, MFI and BandWidth indicators very well all these have inbuilt MQL5 technical indicators. Normalize Intraday Intensity (II) have calculations based on parameters, that is main difference and I thought that could be the reason, why iBarShift not working.

It is late night here and I am already exhausted, but surely will try your suggestions tomorrow.

Hi William

I have tried again, changing ArraySetAsSeries before copying data helped.

However I still not able to get the correct data, i.e. values of TF_M5 on Index[1] and Index[0] with iBarShift on TF_M1. I have attached code of complete TestEA to enable you to run and check.

Your help is highly appreciated.

Thanks.

//+----------------------------------------------------------------------------------------------------------+
//| FxEAPrintDataStats.mq5
//+----------------------------------------------------------------------------------------------------------+
  int AD_NormalizePeriod = 14;
//+----------------------------------------------------------------------------------------------------------+
//| Expert initialization function
//+----------------------------------------------------------------------------------------------------------+
int OnInit()
  {
//---
   return(INIT_SUCCEEDED);
  }
//+----------------------------------------------------------------------------------------------------------+
//| Expert de initialization function
//+----------------------------------------------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+----------------------------------------------------------------------------------------------------------+
//| Expert tick function
//+----------------------------------------------------------------------------------------------------------+
void OnTick()
  {
    int barCount = 5;
    int index    = 1;
    int barShift_M01 = Get_iBarShift(PERIOD_M1,barCount,index);
    int barShift_M05 = Get_iBarShift(PERIOD_M5,barCount,index);
  //---
    if(Is_NewBar(PERIOD_M1))
      {
        //+--------------------------------------------------------------------------------------------------+
        //| Calculate Rate of Change (%) for Normalize A/D
        //+--------------------------------------------------------------------------------------------------+
/* OBJECT is to calculate M5 value on each NewBarM1 (breaking M5 candle price(s) to 5 candles each at 1 minute)*/
          double M05_NormII[];
            Get_NormII(PERIOD_M5,barCount,M05_NormII);
          //--- Rate of Change % = (IndexM5[1] - IndexM5[0]) / IndexM5[1] * 100 ... but calculated on IsNewBarM1
          double aM05_ROC_NormII = (M05_NormII[barShift_M01] - M05_NormII[barShift_M01-1]) / M05_NormII[barShift_M01] * 100;
          //--- print to check results
          Print("aM05_NormII[1] [",DoubleToString(M05_NormII[barShift_M01],5),"] M05_NormII[0] [",DoubleToString(M05_NormII[barShift_M01-1],5),"] M05_ROC_NormII [",DoubleToString(aM05_ROC_NormII,3),"]");
          //--- below statement gives "array out of range in 'FxEATestScripts.mq5' (46,74)"
          double bM05_ROC_NormII = (M05_NormII[barShift_M05] - M05_NormII[barShift_M05-1]) / M05_NormII[barShift_M05] * 100;
          Print("bM05_NormII[1] [",DoubleToString(M05_NormII[barShift_M05],5),"] M05_NormII[0] [",DoubleToString(M05_NormII[barShift_M05-1],5),"] M05_ROC_NormII [",DoubleToString(bM05_ROC_NormII,3),"]");
    }
  } // END Of expert advisor
//+----------------------------------------------------------------------------------------------------------+

//+----------------------------------------------------------------------------------------------------------+
//| METHOD:       Get_NormII()
//| APPLICATION:  Calculate Normalized Intraday Intensity Index (II) Indicator Value
//| Intraday Intensity Index = ((Close×2) − High − Low) / ((High − Low) × Volume)
//+----------------------------------------------------------------------------------------------------------+
bool Get_NormII(ENUM_TIMEFRAMES timeFrame,int barCount,double &NormII[])
  {
    ArrayResize(NormII,barCount+1);
    ArraySetAsSeries(NormII,true);
    // First Loop to calculate Normalize II from Bar Zero to barCount
    for(int i = 0; i < barCount; i++)
      {
        double sumPrice  = 0;
        long   sumVolume = 0;
        int    loopCount = (i + AD_NormalizePeriod);
        // Internal Loop to calculate II value for current bar index
        for(int j = i; j < loopCount; j++)
          {
            double Close  = iClose(_Symbol,timeFrame,j);
            double High   = iHigh(_Symbol,timeFrame,j);
            double Low    = iLow(_Symbol,timeFrame,j);
            long   Volume = iTickVolume(_Symbol,timeFrame,j); // incorporate TickVolume Method in CSymbol
            if(High - Low != 0)
              {
                sumPrice  = sumPrice + ((Close * 2) - High - Low)/(High - Low) * Volume;
                sumVolume = sumVolume + Volume;
              }
          }
        if(sumVolume != 0)
          {
            NormII[i] = (sumPrice / sumVolume);
          }
      }
  //---
    return(true);
  } // END Of Get_NormII() method
//+----------------------------------------------------------------------------------------------------------+
//| METHOD:       Get_iBarShift()
//| APPLICATION:  Method to get iBarShift value for specified timeFrame
//+----------------------------------------------------------------------------------------------------------+
int Get_iBarShift(const ENUM_TIMEFRAMES timeFrame, const int barCount, const int index)
  {
    datetime  M01_TimeShift[];
    //--- Default BaseTime chart is set at M1
    if(CopyTime(_Symbol,PERIOD_M1,0,barCount+1,M01_TimeShift) <= 0) // copy from Index[0] to barCount
      {
        Print(__FUNCTION__,": Error copying time to TimeShift[]");
        return(false);
      }
    ArraySetAsSeries(M01_TimeShift,true);

    int idxBarShift = iBarShift(_Symbol,timeFrame,M01_TimeShift[index]);
  //---
    return(idxBarShift);
  } // END Of method Get_iBarShift()
//+----------------------------------------------------------------------------------------------------------+
//| METHOD:       Is_NewBar()
//| APPLICATION:  Check if it is a New Candle on specified instrument & Time Frame
//+----------------------------------------------------------------------------------------------------------+
bool Is_NewBar(const ENUM_TIMEFRAMES timeFrame)
{
  bool   newBar;
  static datetime dtBarCurrent  = WRONG_VALUE;
         datetime dtBarPrevious = dtBarCurrent;
  // "SERIES_LASTBAR_DATE" Open time of the last bar of the symbol - period
  dtBarCurrent = (datetime)SeriesInfoInteger(_Symbol,timeFrame,SERIES_LASTBAR_DATE);  
  if(dtBarCurrent != dtBarPrevious)  newBar = true;
  else                               newBar = false;
  //---
  return(newBar);
} // END Of Is_NewBar()
 
Anil Varma:

Hi William

I have tried again, changing ArraySetAsSeries before copying data helped.

However I still not able to get the correct data, i.e. values of TF_M5 on Index[1] and Index[0] with iBarShift on TF_M1. I have attached code of complete TestEA to enable you to run and check.

Your help is highly appreciated.

Thanks.

 int barShift_M01 = Get_iBarShift(PERIOD_M1,barCount,index);


why are you making your life harder when you can use time calculation

ulong ElapsedSec = barCount*(PeriodSeconds(ChartPeriod(ChartID())));
    datetime TargetBarCountTime = datetime(ulong(iTime(Symbol(),PERIOD_CURRENT,index))-ElapsedSec);
    int barShift_M01 = iBarShift(Symbol(),PERIOD_M1,TargetBarCountTime);
    Print("barShift_M01 is ",IntegerToString(barShift_M01));
 
Sardion Maranatha: why are you making your life harder when you can use time calculation
ulong ElapsedSec = barCount*(PeriodSeconds(ChartPeriod(ChartID())));
    datetime TargetBarCountTime = datetime(ulong(iTime(Symbol(),PERIOD_CURRENT,index))-ElapsedSec);

This assumes every bar every exists — they don't. What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific), requires knowledge of when your broker stops and starts (not necessary the same as the market.)
          "Free-of-Holes" Charts - MQL4 Articles 20 June 2006
          No candle if open = close ? - MQL4 programming forum

 
William Roeder:

This assumes every bar every exists — they don't. What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific), requires knowledge of when your broker stops and starts (not necessary the same as the market.)
          "Free-of-Holes" Charts - MQL4 Articles 20 June 2006
          No candle if open = close ? - MQL4 programming forum

Does it not iBarshift return the nearest index when you dont specify exact flag?
 
In the absence of bars, n bars ago and shift(m seconds ago) are not the same. On the M1 one bar ago is 2880*60 seconds ago over the weekend.
 
William Roeder:
In the absence of bars, n bars ago and shift(m seconds ago) are not the same. On the M1 one bar ago is 2880*60 seconds ago over the weekend.
Agreed. Thank you for the correction. The simplest solution would be stepping back to n bars of current timeframe, get its open time and do the iBarshift of m1
 
Sardion Maranatha:
Agreed. Thank you for the correction. The simplest solution would be stepping back to n bars of current timeframe, get its open time and do the iBarshift of m1

Thanks William and Sardion

"The simplest solution would be stepping back to n bars of current timeframe, get its open time and do the iBarshift of m1"

Sardion: I will still consider myself as Beginner(Advance Level), I would be grateful, if you help me with some code to show how to implement your suggestion.  

But I still did not got the way out to calculate required information. I realized to forget including ArrayResize and ArraySetAsSeries in iBarShift(). The corrected version is ...

//+----------------------------------------------------------------------------------------------------------+
//| METHOD:       Get_iBarShift()
//| APPLICATION:  Method to get iBarShift value for specified timeFrame
//+----------------------------------------------------------------------------------------------------------+
int Get_iBarShift(const ENUM_TIMEFRAMES timeFrame, const int barCount, const int index)
  {
    datetime  M01_TimeShift[];
    ArrayResize(M01_TimeShift,barCount+1);
    ArraySetAsSeries(M01_TimeShift,true);
    //--- Default BaseTime chart is set at M1
    if(CopyTime(_Symbol,PERIOD_M1,0,barCount+1,M01_TimeShift) <= 0) // copy from Index[0] to barCount
      {
        Print(__FUNCTION__,": Error copying time to TimeShift[]");
        return(false);
      }

    int idxBarShift = iBarShift(_Symbol,timeFrame,M01_TimeShift[index]);
  //---
    return(idxBarShift);
  } // END Of method Get_iBarShift()

How with this I was able to get current M5 value on Index[0], while Index[1] gives values at close of previous M5 Bar.

2021.04.22 09:57:55.704 FxEATestWilliam (US30,M5) aM05_NormII[1] [-0.07423] M05_NormII[0] [-0.02845] M05_ROC_NormII [61.680]

2021.04.22 09:58:12.111 FxEATestWilliam (US30,M5) aM05_NormII[1] [-0.07423] M05_NormII[0] [-0.02750] M05_ROC_NormII [62.955]

2021.04.22 09:59:07.984 FxEATestWilliam (US30,M5) aM05_NormII[1] [-0.07423] M05_NormII[0] [-0.06511] M05_ROC_NormII [12.289]

2021.04.22 10:00:01.296 FxEATestWilliam (US30,M5) aM05_NormII[1] [-0.06425] M05_NormII[0] [-0.07494] M05_ROC_NormII [-16.633]

2021.04.22 10:01:03.195 FxEATestWilliam (US30,M5) aM05_NormII[1] [-0.06425] M05_NormII[0] [-0.05910] M05_ROC_NormII [8.020]

2021.04.22 10:02:01.182 FxEATestWilliam (US30,M5) aM05_NormII[1] [-0.06425] M05_NormII[0] [-0.04889] M05_ROC_NormII [23.910]

2021.04.22 10:03:02.120 FxEATestWilliam (US30,M5) aM05_NormII[1] [-0.06425] M05_NormII[0] [-0.03944] M05_ROC_NormII [38.618]

2021.04.22 10:04:00.742 FxEATestWilliam (US30,M5) aM05_NormII[1] [-0.06425] M05_NormII[0] [-0.03924] M05_ROC_NormII [38.921]

2021.04.22 10:05:01.630 FxEATestWilliam (US30,M5) aM05_NormII[1] [-0.09210] M05_NormII[0] [-0.07041] M05_ROC_NormII [23.551]


 
Anil Varma:

Thanks William and Sardion

"The simplest solution would be stepping back to n bars of current timeframe, get its open time and do the iBarshift of m1"

Sardion: I will still consider myself as Beginner(Advance Level), I would be grateful, if you help me with some code to show how to implement your suggestion.  

But I still did not got the way out to calculate required information. I realized to forget including ArrayResize and ArraySetAsSeries in iBarShift(). The corrected version is ...

How with this I was able to get current M5 value on Index[0], while Index[1] gives values at close of previous M5 Bar.

2021.04.22 09:57:55.704 FxEATestWilliam (US30,M5) aM05_NormII[1] [-0.07423] M05_NormII[0] [-0.02845] M05_ROC_NormII [61.680]

2021.04.22 09:58:12.111 FxEATestWilliam (US30,M5) aM05_NormII[1] [-0.07423] M05_NormII[0] [-0.02750] M05_ROC_NormII [62.955]

2021.04.22 09:59:07.984 FxEATestWilliam (US30,M5) aM05_NormII[1] [-0.07423] M05_NormII[0] [-0.06511] M05_ROC_NormII [12.289]

2021.04.22 10:00:01.296 FxEATestWilliam (US30,M5) aM05_NormII[1] [-0.06425] M05_NormII[0] [-0.07494] M05_ROC_NormII [-16.633]

2021.04.22 10:01:03.195 FxEATestWilliam (US30,M5) aM05_NormII[1] [-0.06425] M05_NormII[0] [-0.05910] M05_ROC_NormII [8.020]

2021.04.22 10:02:01.182 FxEATestWilliam (US30,M5) aM05_NormII[1] [-0.06425] M05_NormII[0] [-0.04889] M05_ROC_NormII [23.910]

2021.04.22 10:03:02.120 FxEATestWilliam (US30,M5) aM05_NormII[1] [-0.06425] M05_NormII[0] [-0.03944] M05_ROC_NormII [38.618]

2021.04.22 10:04:00.742 FxEATestWilliam (US30,M5) aM05_NormII[1] [-0.06425] M05_NormII[0] [-0.03924] M05_ROC_NormII [38.921]

2021.04.22 10:05:01.630 FxEATestWilliam (US30,M5) aM05_NormII[1] [-0.09210] M05_NormII[0] [-0.07041] M05_ROC_NormII [23.551]


//+------------------------------------------------------------------+
//|                                           GetBarShiftMultiTF.mq5 |
//|                             Copyright 2020, Rosh Jardine Capital |
//|                                      https://www.roshjardine.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, Rosh Jardine Capital"
#property link      "https://www.roshjardine.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
  //--- get corresponding barshift relative to current timeframe
  ENUM_TIMEFRAMES CurrTimeFrame       = ChartPeriod(ChartID());
  int             LookBackUpTo        = 5;
  int             StartFromIdx        = 1;
  ENUM_TIMEFRAMES AllMql5Tframes[21]  = {PERIOD_M1,PERIOD_M2,PERIOD_M3,PERIOD_M4,PERIOD_M5,PERIOD_M6,PERIOD_M10,PERIOD_M12,PERIOD_M15,PERIOD_M20,PERIOD_M30,
                                         PERIOD_H1,PERIOD_H2,PERIOD_H3,PERIOD_H4,PERIOD_H6,PERIOD_H8,PERIOD_H12,PERIOD_D1,PERIOD_W1,PERIOD_MN1 };
  int             CurrTFLookBackIdx   = LookBackUpTo+StartFromIdx;
  datetime        RefOpenTime         = iTime(Symbol(),PERIOD_CURRENT,CurrTFLookBackIdx);
  
  
  int             AllMql5BarShift[];
  ArrayResize(AllMql5BarShift,ArraySize(AllMql5Tframes));
  ArrayInitialize(AllMql5BarShift,-1);
  
  for (int i=(ArraySize(AllMql5Tframes)-1);i>=0;i--)
  {
    if (AllMql5Tframes[i]==CurrTimeFrame) { AllMql5BarShift[i] = CurrTFLookBackIdx; continue; }
    AllMql5BarShift[i]  = iBarShift(Symbol(),AllMql5Tframes[i],RefOpenTime);
  }
  
  //--- re-do one more time if barshift returns -1
  for (int j=(ArraySize(AllMql5BarShift)-1);j>=0;j--)
  {
    if (AllMql5BarShift[j]>-1) { continue; }
    //--- re-do one more time
    AllMql5BarShift[j] = iBarShift(Symbol(),AllMql5Tframes[j],RefOpenTime);
  }
  
  //--- just for checking
  Print("current timeframe is ",EnumToString(CurrTimeFrame));
  for (int k=(ArraySize(AllMql5BarShift)-1);k>=0;k--)
  {
    if (AllMql5BarShift[k]>-1) 
    { 
      Print("Barshift of ",EnumToString(AllMql5Tframes[k])," is ",IntegerToString(AllMql5BarShift[k]));
      Print("Open Time of ",EnumToString(AllMql5Tframes[k])," is ",TimeToString(iTime(Symbol(),AllMql5Tframes[k],AllMql5BarShift[k])));
    }
    //---
    if (TerminalInfoInteger(TERMINAL_MAXBARS)<10000000)      
    { 
      Print("Barshift not available on ",EnumToString(AllMql5Tframes[k]),", Possible terminal max bar limitation");
    }
  }
}
//+------------------------------------------------------------------+


as an example only

MI      0       13:49:13.327    GetBarShiftMultiTF (EURUSD,M5)  current timeframe is PERIOD_M5
KI      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Barshift of PERIOD_MN1 is 0
MP      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Open Time of PERIOD_MN1 is 2021.04.01 00:00
GJ      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Barshift of PERIOD_W1 is 0
KF      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Open Time of PERIOD_W1 is 2021.04.18 00:00
FP      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Barshift of PERIOD_D1 is 0
IJ      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Open Time of PERIOD_D1 is 2021.04.22 00:00
DF      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Barshift of PERIOD_H12 is 0
EO      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Open Time of PERIOD_H12 is 2021.04.22 00:00
EK      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Barshift of PERIOD_H8 is 0
RG      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Open Time of PERIOD_H8 is 2021.04.22 08:00
KP      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Barshift of PERIOD_H6 is 0
NJ      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Open Time of PERIOD_H6 is 2021.04.22 06:00
IE      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Barshift of PERIOD_H4 is 0
NL      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Open Time of PERIOD_H4 is 2021.04.22 08:00
HJ      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Barshift of PERIOD_H3 is 0
LG      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Open Time of PERIOD_H3 is 2021.04.22 09:00
KP      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Barshift of PERIOD_H2 is 0
HJ      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Open Time of PERIOD_H2 is 2021.04.22 08:00
RE      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Barshift of PERIOD_H1 is 0
FM      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Open Time of PERIOD_H1 is 2021.04.22 09:00
HI      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Barshift of PERIOD_M30 is 1
QD      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Open Time of PERIOD_M30 is 2021.04.22 09:00
RS      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Barshift of PERIOD_M20 is 2
JI      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Open Time of PERIOD_M20 is 2021.04.22 09:00
DF      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Barshift of PERIOD_M15 is 2
PO      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Open Time of PERIOD_M15 is 2021.04.22 09:15
RH      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Barshift of PERIOD_M12 is 3
HD      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Open Time of PERIOD_M12 is 2021.04.22 09:12
HS      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Barshift of PERIOD_M10 is 3
PJ      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Open Time of PERIOD_M10 is 2021.04.22 09:10
RF      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Barshift of PERIOD_M6 is 6
IL      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Open Time of PERIOD_M6 is 2021.04.22 09:12
KK      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Barshift of PERIOD_M5 is 6
IG      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Open Time of PERIOD_M5 is 2021.04.22 09:15
MP      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Barshift of PERIOD_M4 is 9
OI      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Open Time of PERIOD_M4 is 2021.04.22 09:12
CF      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Barshift of PERIOD_M3 is 11
IL      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Open Time of PERIOD_M3 is 2021.04.22 09:15
LH      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Barshift of PERIOD_M2 is 17
CG      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Open Time of PERIOD_M2 is 2021.04.22 09:14
DS      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Barshift of PERIOD_M1 is 34
KI      0       13:49:13.328    GetBarShiftMultiTF (EURUSD,M5)  Open Time of PERIOD_M1 is 2021.04.22 09:15
 
Sardion Maranatha:

as an example only

Thanks a lot Sardion. This is great piece of code and will surely implement in my EA after grasping its concept.

Just a note on my original issue, getting M5 values at each NewBar on M1. Basically I will need M5 Index[0] at open of each new bar at M1 for last 'n' bars into a single array to compare rate of change over previous value v/s current value.

I have tried following rather simple code and appreciate your valuable comments and SUGGETION how can I have ONE SINGLE Method for specified TimeFrame to calculate values. Currently I had to define method for each time frame I need.

NOTICE that I have not used iBarShift function.

//+----------------------------------------------------------------------------------------------------------+
//| EA ROCNormII.mq5
//+----------------------------------------------------------------------------------------------------------+
  int II_NormalizePeriod = 14;
//+----------------------------------------------------------------------------------------------------------+
//| Expert initialization function
//+----------------------------------------------------------------------------------------------------------+
int OnInit()
  {
//---
   return(INIT_SUCCEEDED);
  }
//+----------------------------------------------------------------------------------------------------------+
//| Expert de initialization function
//+----------------------------------------------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+----------------------------------------------------------------------------------------------------------+
//| Expert tick function
//+----------------------------------------------------------------------------------------------------------+
void OnTick()
  {
    int barCount = 5;
    int index    = 1;
  //---
    if(Is_NewBar(PERIOD_M1))
      {
        //+--------------------------------------------------------------------------------------------------+
        //| Calculate Rate of Change (%) for Normalize A/D
        //+--------------------------------------------------------------------------------------------------+
/* OBJECT is to calculate M5 value on each NewBarM1 (breaking M5 candle price(s) to 5 candles each at 1 minute)*/
          double M05_IIData[3], M05_IIROC[2];
          Get_M05_ROCII(M05_IIData,M05_IIROC);
          Print("Time ",TimeToString(TimeCurrent())," M05_IIData[2] [",DoubleToString(M05_IIData[2],5),"] M05_IIData[1] [",DoubleToString(M05_IIData[1],5),"] M05_IIData[0] [",DoubleToString(M05_IIData[0],5),
              "] M05_IIROC[1] [",DoubleToString(M05_IIROC[1],3),"] M05_IIROC[0] [",DoubleToString(M05_IIROC[0],3),"]");
    }
  } // END Of expert advisor
//+----------------------------------------------------------------------------------------------------------+
//+----------------------------------------------------------------------------------------------------------+
//| METHOD:       Get_M05_ROCII()
//| APPLICATION:  Method to get Rate Of Change for Normalized Intraday Intensity
//+----------------------------------------------------------------------------------------------------------+
bool Get_M05_ROCII(double &ROC_IIData[],double &ROC_II[])
  {
    int    index = 1, barCount = 5;
    static double Idx1_Value, Idx2_Value, Idx1_ROC;
    double II[];
    ArrayResize(II,barCount);
    ArraySetAsSeries(II,true);
    Get_NormII(PERIOD_M5,barCount,II);
    //--- Fill/Assign current/previous values into Array 
    ROC_IIData[2] = Idx2_Value;                 // assign from temporary variable to Element[1] in array
    ROC_IIData[1] = Idx1_Value;                 // assign from temporary variable to Element[1] in array
    ROC_IIData[0] = II[index - 1];   // update new value to Element [0] in array
    Idx2_Value = ROC_IIData[1];          // assign element[0] to a temporary variable
    Idx1_Value = ROC_IIData[0];          // assign element[0] to a temporary variable
    //--- Calculate Rate of Change for BandWidth M5
    //---  RISING Index[1] < Index[0] | FALLING RISING Index[1] > Index[0]
    ROC_II[1] = Idx1_ROC;
    if(ROC_IIData[index] == 0)
      ROC_II[0] = 0;
    else
      if(ROC_IIData[index-1] < 0)
        ROC_II[0] = -(ROC_IIData[index-1] - ROC_IIData[index]) / ROC_IIData[index] * 100; // falling
      else
        ROC_II[0] = (ROC_IIData[index-1] - ROC_IIData[index]) / ROC_IIData[index] * 100; // rising
    Idx1_ROC = ROC_II[0];
  //---
    return(true);
  } // END Of method Get_M05_ROCII()
//+----------------------------------------------------------------------------------------------------------+
//| METHOD:       Get_NormII()
//| APPLICATION:  Calculate Normalized Intraday Intensity Index (II) Indicator Value
//| Intraday Intensity Index = ((Close×2) − High − Low) / ((High − Low) × Volume)
//+----------------------------------------------------------------------------------------------------------+
bool Get_NormII(ENUM_TIMEFRAMES timeFrame,int barCount,double &NormII[])
  {
    ArrayResize(NormII,barCount+1);
    ArraySetAsSeries(NormII,true);
    // First Loop to calculate Normalize II from Bar Zero to barCount
    for(int i = 0; i < barCount; i++)
      {
        double sumPrice  = 0;
        long   sumVolume = 0;
        int    loopCount = (i + II_NormalizePeriod);
        // Internal Loop to calculate II value for current bar index
        for(int j = i; j < loopCount; j++)
          {
            double Close  = iClose(_Symbol,timeFrame,j);
            double High   = iHigh(_Symbol,timeFrame,j);
            double Low    = iLow(_Symbol,timeFrame,j);
            long   Volume = iTickVolume(_Symbol,timeFrame,j); // incorporate TickVolume Method in CSymbol
            if(High - Low != 0)
              {
                sumPrice  = sumPrice + ((Close * 2) - High - Low)/(High - Low) * Volume;
                sumVolume = sumVolume + Volume;
              }
          }
        if(sumVolume != 0)
          {
            NormII[i] = (sumPrice / sumVolume);
          }
      }
  //---
    return(true);
  } // END Of Get_NormII() method
//+----------------------------------------------------------------------------------------------------------+
//| METHOD:       Is_NewBar()
//| APPLICATION:  Check if it is a New Candle on specified instrument & Time Frame
//+----------------------------------------------------------------------------------------------------------+
bool Is_NewBar(const ENUM_TIMEFRAMES timeFrame)
{
  bool   newBar;
  static datetime dtBarCurrent  = WRONG_VALUE;
         datetime dtBarPrevious = dtBarCurrent;
  // "SERIES_LASTBAR_DATE" Open time of the last bar of the symbol - period
  dtBarCurrent = (datetime)SeriesInfoInteger(_Symbol,timeFrame,SERIES_LASTBAR_DATE);  
  if(dtBarCurrent != dtBarPrevious)  newBar = true;
  else                               newBar = false;
  //---
  return(newBar);
} // END Of Is_NewBar()
//+----------------------------------------------------------------------------------------------------------+

Test results, first and second row will be with zero values (as I move values in array from [0] to [1] and to [2], but 3 row onwards it gives correct results.

2021.04.23 13:01:47.563 FxEATestWilliam (US30,M1) Time 2021.04.23 10:31 M05_IIData[2] [0.00000] M05_IIData[1] [0.00000] M05_IIData[0] [0.10339] M05_IIROC[1] [0.000] M05_IIROC[0] [0.000]

2021.04.23 13:02:00.813 FxEATestWilliam (US30,M1) Time 2021.04.23 10:32 M05_IIData[2] [0.00000] M05_IIData[1] [0.10339] M05_IIData[0] [0.10551] M05_IIROC[1] [0.000] M05_IIROC[0] [2.048]

2021.04.23 13:03:00.034 FxEATestWilliam (US30,M5) Time 2021.04.23 10:33 M05_IIData[2] [0.10339] M05_IIData[1] [0.10551] M05_IIData[0] [0.08813] M05_IIROC[1] [2.048] M05_IIROC[0] [-16.476]

2021.04.23 13:03:59.723 FxEATestWilliam (US30,M5) Time 2021.04.23 10:34 M05_IIData[2] [0.10551] M05_IIData[1] [0.08813] M05_IIData[0] [0.07624] M05_IIROC[1] [-16.476] M05_IIROC[0] [-13.484]

Reason: