iBands on Array for MQL5

To add comments, please log in or register
Nurudeen Amedu
60586
Nurudeen Amedu  

I am trying to calculate iBandsOnArray for MQL5 but i am only able to get the middle line value in my expert my upper and lower line keep giving me wrong values

i think the problem is from the standard deviation

if i can get a standard deviation function that i can apply to the current bb mid value, i can get it to work


The problem i have with standard deviation is that it expects whole arrays for price and maprice and i only have current bb mid value and current oscillator value

is there a way i can use only current and previous price and maprice values to get deviation?

my code is below

double iBandsOnArray(double &array[],
                     int total,
                     int period,
                     int deviation,
                     int bands_shift,
                     int mode,
                     int shift){
                     
                     
         double buf[],arr[];
         if(total==0) total=ArraySize(array);
         if(total>0 && total<=period) return(0);
         if(shift>total-period-bands_shift) return(0);
         ArrayResize(buf,50,1000);
         ArrayResize(arr,50,1000);
         ArrayInitialize(arr,0);
         //buf[0]=0;
         
      switch(mode)
      {
        
      case MAIN_LINE :
        {     
         total=ArrayCopy(arr,array,shift+bands_shift,shift+bands_shift,period);
         if(ArrayResize(buf,total)<0) return(0);
         double sum=0;
         int    i,pos=total-1;
         for(i=1;i<period;i++,pos--)
            sum+=arr[pos];
         while(pos>=0)
           {
            sum+=arr[pos];
            buf[pos]=sum/period;
            sum-=arr[pos+period-1];
            pos--;
           }
       ExtStdDevBuffer[0]=StdDev_Func(ExtBandsPeriod,indiVal,buf,ExtBandsPeriod);
       
       double high=buf[0]+ExtBandsDeviations*ExtStdDevBuffer[0];
       double low=buf[0]-ExtBandsDeviations*ExtStdDevBuffer[0];
       Comment(low, " ",buf[0], " ", high);
       
       return(buf[0]);
      }
      }
return(0);
}

///Standard Dev function
//+------------------------------------------------------------------+
//| Calculate Standard Deviation                                     |
//+------------------------------------------------------------------+
double StdDev_Func(int position,const double &price[],const double &MAprice[],int period)
  {
//--- variables
   double StdDev_dTmp=0.0;
//--- check for position
//--- calcualte StdDev
   for(int i=0;i<period;i++) {
      StdDev_dTmp+=MathPow(price[position-i]-MAprice[position],2);
   }
   StdDev_dTmp=MathSqrt(StdDev_dTmp/period);
//--- return calculated value
   return(StdDev_dTmp);
  }
To add comments, please log in or register