Array out of Range Error while using [i+2] in getting Moving Average value in MQL5 - page 2

 

because

ma[i+2]  = ma[(prev_calculated-1) + 2 ] =  ma[prev_calculated+1] ;

which do not exist

Your indicator should not be as series

 ArraySetAsSeries(MA,true);
 ArraySetAsSeries(MA2,true);
 ArraySetAsSeries(MA3,true);

 
I have been battling this issue for many weeks and months.
I have done everything you guys write about, nothing is working.
I was able to get the arrows to plot, but ultimatelly it ends in array out of range, and the indicator stops working.
Can anybody just post a working snippet of the loop where we are comparing at least 2 buffers, so an example:
    for(int i=(int)MathMax(prev_calculated-1,1); i<rates_total && i+2<ArraySize(ExtBuffer0); i++)
      {
        MaxH = iHigh(_Symbol,_Period,iHighestMQL4(_Symbol,_Period,MODE_CLOSE,period,i)); 
        MinL = iLow(_Symbol,_Period,iLowestMQL4(_Symbol,_Period,MODE_CLOSE,period,i));  
        price = (iOpen(_Symbol,_Period,i)+iClose(_Symbol,_Period,i))/2;   

        if(MaxH-MinL == 0) 
        Value = 0.33*2*(0-0.5) + 0.67*Value1;
        else 
        Value = 0.33*2*((price-MaxH)/(MinL-MaxH)-0.5) + 0.67*Value1;

        Value=MathMin(MathMax(Value,-0.999),0.999);
 
        if(1-Value == 0) 
        ExtBuffer0[i]=0.5+0.5*Fish1;
        else 
        ExtBuffer0[i]=0.5*MathLog((1+Value)/(1-Value))+0.5*Fish1;
        Value1=Value;
        Fish1=ExtBuffer0[i];
  
        if ( Arrow )
          {
            if ( ExtBuffer0[i+SIGNAL_BAR+1] > 0.0  && ExtBuffer0[i+SIGNAL_BAR] < 0.0 ) manageArr(i, clArrowBuy,  233, false);
            if ( ExtBuffer0[i+SIGNAL_BAR+1] < 0.0  && ExtBuffer0[i+SIGNAL_BAR] > 0.0 ) manageArr(i, clArrowSell, 234, true );
          }
      }
BTW, this is not working, I am getting nothing, the chart in MT5 isn't even moving even though the connection is live and in the Market overview prices are changing.
Anybody can help, please?

My MT4 is working perfectly fine, and I am having no issues with coding MQL4.
What's the problem with MQL5???

And, yes, my array is set as series exactly like in the post before mine.
 
Dadas #:
    for(int i=(int)MathMax(prev_calculated-1,1); i<rates_total && i+2<ArraySize(ExtBuffer0); i++)
      ⋮
            if ( ExtBuffer0[i+SIGNAL_BAR+1]
  1. Your lookback is SIGNAL_BAR+1, but your i goes to rates_total-1.
              How to do your lookbacks correctly #9#14 & #19 (2016)

  2.         MaxH = iHigh(_Symbol,_Period,iHighestMQL4(_Symbol,_Period,MODE_CLOSE,period,i)); 

    Why are you processing new bars first? № 1. Your index is as-series.

  3. Dadas #: My MT4 is working perfectly fine, and I am having no issues with coding MQL4.

    Always use strict. Fixing the warnings will save you hours of debugging, but you must understand the differences. MT5 is always strict.

 
William Roeder #:
  1. Your lookback is SIGNAL_BAR+1, but your i goes to rates_total-1.
              How to do your lookbacks correctly #9#14 & #19 (2016)

  2. Why are you processing new bars first? № 1. Your index is as-series.

  3. Always use strict. Fixing the warnings will save you hours of debugging, but you must understand the differences. MT5 is always strict.

Thanks for the quick reply!
This is working fine:

    for(int i=0; i<1000; i++)
      {
        MaxH = iHigh(_Symbol,_Period,iHighestMQL4(_Symbol,_Period,MODE_CLOSE,period,i)); 
        MinL = iLow(_Symbol,_Period,iLowestMQL4(_Symbol,_Period,MODE_CLOSE,period,i));  
        price = (iOpen(_Symbol,_Period,i)+iClose(_Symbol,_Period,i))/2;   

        if(MaxH-MinL == 0) 
        Value = 0.33*2*(0-0.5) + 0.67*Value1;
        else 
        Value = 0.33*2*((price-MaxH)/(MinL-MaxH)-0.5) + 0.67*Value1;

        Value=MathMin(MathMax(Value,-0.999),0.999);
 
        if(1-Value == 0) 
        ExtBuffer0[i]=0.5+0.5*Fish1;
        else 
        ExtBuffer0[i]=0.5*MathLog((1+Value)/(1-Value))+0.5*Fish1;
        
        Value1=Value;
        Fish1=ExtBuffer0[i];
      }
       
    for(int i=SIGNAL_BAR+2; i<1000; i++)
      { 
        if(Arrow)
         {
        if(ExtBuffer0[i+SIGNAL_BAR+1]>0&&
           ExtBuffer0[i+SIGNAL_BAR]<0-minCross) 
           manageArr(i, clArrowBuy,  233, false);
        if(ExtBuffer0[i+SIGNAL_BAR+1]<0&&
           ExtBuffer0[i+SIGNAL_BAR]>0+minCross) 
           manageArr(i, clArrowSell, 234, true );
         }
      }
 
This works even better!
    for(int i=1000; i>=0; i--)
      {
        MaxH = iHigh(_Symbol,_Period,iHighestMQL4(_Symbol,_Period,MODE_CLOSE,period,i)); 
        MinL = iLow(_Symbol,_Period,iLowestMQL4(_Symbol,_Period,MODE_CLOSE,period,i));  
        price = (iOpen(_Symbol,_Period,i)+iClose(_Symbol,_Period,i))/2;   

        if(MaxH-MinL == 0) 
        Value = 0.33*2*(0-0.5) + 0.67*Value1;
        else 
        Value = 0.33*2*((price-MaxH)/(MinL-MaxH)-0.5) + 0.67*Value1;

        Value=MathMin(MathMax(Value,-0.999),0.999);
 
        if(1-Value == 0) 
        ExtBuffer0[i]=0.5+0.5*Fish1;
        else 
        ExtBuffer0[i]=0.5*MathLog((1+Value)/(1-Value))+0.5*Fish1;
        
        Value1=Value;
        Fish1=ExtBuffer0[i];
      }
       
    for(int i=1000; i>=SIGNAL_BAR+2; i--)
      { 
        if(Arrow)
         {
        if(ExtBuffer0[i+SIGNAL_BAR+1]>0&&
           ExtBuffer0[i+SIGNAL_BAR]<0-minCross) 
           manageArr(i, clArrowBuy,  233, false);
        if(ExtBuffer0[i+SIGNAL_BAR+1]<0&&
           ExtBuffer0[i+SIGNAL_BAR]>0+minCross) 
           manageArr(i, clArrowSell, 234, true );
         }
      }
Reason: