Calculating the average value of a specific difference in mql4

 

Hello everyone, so i've been trying to create my custom indicator, but i'm having trouble creating the "iMaOnArray" of this calculation. 


What i basically wanna do is retrieve the average value of the difference between close price and LWMA(Linear Weighted Moving Average)  when close price is higher than LWMA and vice versa ( so AVG(Close[i] - LWMA)).

I tried doing this by using iMaOnArray, but i don't get a specific value. Anyone can help?

double CalculateAVGLongRange()
{

double BBMid = 0;
double LWMA  = 0;
double ALWMA[];
double ABBMID[];
double Shift[];
double AVGLongRange;

for(int i=0;i<CalculateBars;i++)
  {
   
   BBMid = iBands(Symbol(),Period(),20,2,0,PRICE_CLOSE,MODE_MAIN,i);
   LWMA  = iMA(Symbol(),Period(),20,0,MODE_LWMA,PRICE_CLOSE,i);

   
   
   if(Close[i] > BBMid && LWMA > BBMid)
      {
       if(Close[i] > LWMA)
         {
            ArrayResize(ALWMA,CalculateBars);
            ArrayResize(Shift,CalculateBars);
            ArrayFill(ALWMA,0,i,LWMA);
            
            double ShiftC= Close[i] - ALWMA[i];
            ArrayFill(Shift,0,i,ShiftC);
            AVGLongRange =iMAOnArray(Shift,CalculateBars,14,0,MODE_SMA,i);
            Print("Shift is :",Shift[0]);
            Sleep(9999);
           
            
         }   
      
      }
   
   
  }
if(AVGLongRange > 0 ) return(AVGLongRange);
else return(0);
}

Thanks in advance to whoever will help me :) 

Step on New Rails: Custom Indicators in MQL5
Step on New Rails: Custom Indicators in MQL5
  • www.mql5.com
Finally we've got an opportunity to try the new trade terminal - MetaTrader 5. No doubt, it is noteworthy and has many new features as compared to its predecessor. The important advantages of this platform among others are: Essentially modified language allowing now to use the object-oriented programming, still allowing to use the rich...
 
  1. Set your arrays to as-series before filling.
  2. Fill your arrays and then compute your average, or count down so you try to compute average of filled elements.
 
William Roeder:
  1. Set your arrays to as-series before filling.
  2. Fill your arrays and then compute your average, or count down so you try to compute average of filled elements.
Ok thank you very much, I’ll try and let you know 
 
William Roeder:
  1. Set your arrays to as-series before filling.
  2. Fill your arrays and then compute your average, or count down so you try to compute average of filled elements.

So i tried, and now i can retrieve the information from the single row in the array. What i don't understand is why if i print it it won't give me the same value on every print. Probably because i set the shift on "i" in the LWMA parameters?

double CalculateAVGLongRange()
{

double BBMid = 0;
double LWMA  = 0;
double ALWMA[];
double ABBMID[];
double Shift[];
double AVGLongRange = 0;
double ShiftC = 0;

 for(int i=Bars-1;i>=0;i--)
  {
   
   BBMid = iBands(Symbol(),Period(),20,2,0,PRICE_CLOSE,MODE_MAIN,i);
   LWMA  = iMA(Symbol(),Period(),20,0,MODE_LWMA,PRICE_CLOSE,i);

   
   
   //if(Close[i] > BBMid && LWMA > BBMid)
      {
       if(Close[i] > LWMA)
         {
            ArrayResize(ALWMA,Bars);
            ArrayResize(Shift,Bars);
            ArraySetAsSeries(ALWMA,true);
            ArrayFill(ALWMA,0,Bars,LWMA);
            ShiftC= Close[i] - ALWMA[i];
            ArraySetAsSeries(Shift,true);

            

            ArrayFill(Shift,0,i,ShiftC);
            AVGLongRange =iMAOnArray(Shift,Bars,14,0,MODE_SMA,0);
            Print(ALWMA[0]);
            
           
            

           
            
         }   
      
      }
   
   
  }
if(AVGLongRange > 0 ) return(AVGLongRange);
else return(0);
}
void Plot(){

for(int i=0;i<CalculateBars;i++)
  {
   double  BBMid = iBands(Symbol(),Period(),20,2,0,PRICE_CLOSE,MODE_MAIN,i);
   double  LWMA  = iMA(Symbol(),Period(),20,0,MODE_LWMA,PRICE_CLOSE,i);
   LongRangeBandDownBuffer[i] = LWMA;
   LongRangeBandUpBuffer[i] = LWMA+CalculateAVGLongRange();
  }

}




Reason: