Search code examples
mql4

Calculate Standard Deviation in MQL from a custom built array


I am trying to calculate the lower 2SD and higher 2SD for a ratio of the price for two instruments, however, it is giving me 0 results. The expected results look like this. enter image description here

I tried to create an array by declaring the array in global scope and then adding values to it, however it is giving me zero as value, I also tried to add values manually to check if it is working but the function StdDev is also giving me same results. Any help to guide me where I am wrong would be deeply appreciated. These are the codes tried by me, however not giving me any result.

d[z] = iClose(sym1,0,z)/iClose(sym2,0,z);

c = iStdDevOnArray(iClose(sym1,0,z),0,z,0,0,z);

The code I am deploying is as below.

#property indicator_separate_window
#property indicator_buffers 4
extern string sym1    = "AUDUSD";
extern string sym2    = "NZDUSD";
extern int barcount     = 500;

//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];

//Global Variables
int ArrayIndex;
double ArraySum;
double a = 0;
double b=0;
double c=0;
double lowersd=0;
double highersd=0;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE,EMPTY,2,clrYellow);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexStyle(1,DRAW_LINE,EMPTY,2,clrRed);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexStyle(2,DRAW_LINE,EMPTY,2,clrGreen);
   SetIndexBuffer(2,ExtMapBuffer3);
   SetIndexStyle(3,DRAW_LINE,EMPTY,2,clrPink);
   SetIndexBuffer(3,ExtMapBuffer4);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()


  {
  
//----
   for(int z=0;z<barcount;z++)
  {
  // To calculate the average of the ratio
   a=iClose(sym1,0,z)+a; 
   // Alert(d[0]);
   b=iClose(sym2,0,z)+b;
  // Below are the dummy data to create the chart
   lowersd =  1.04;
   highersd = 1.115;

   }
   
   for(int i=0;i<barcount;i++)
  {

   ExtMapBuffer1[i] = iClose(sym1,0,i)/iClose(sym2,0,i);
   ExtMapBuffer2[i] = (a/b);
   ExtMapBuffer3[i] = lowersd; // these are dummy values i am trying to populate this dynamically using the non working code above.
   ExtMapBuffer4[i] = highersd;
   
   }
   
//----
   return(0);
  
  }

Solution

  • You can use this to first initialize the array

      double arr[];
      ArrayResize(arr, barcount);
      arr[z] = iClose(sym1,0,z)/iClose(sym2,0,z);
      c=iStdDevOnArray(arr,barcount,barcount,0,MODE_EMA,0);
    

    Then you can put this value in ExtBuffer

       ExtMapBuffer3[i] = (a/b)-(c*2);
       ExtMapBuffer4[i] = (a/b)+(c*2);