从自定义构建的数组计算 MQL 中的标准偏差

Calculate Standard Deviation in MQL from a custom built array

我正在尝试为两种仪器的价格比率计算较低的 2SD 和较高的 2SD,但是,结果为 0。预期结果如下所示。 enter image description here

我试图通过在全局范围内声明数组然后向其添加值来创建一个数组,但是它给我的值为零,我还尝试手动添加值以检查它是否正常工作但是函数StdDev 也给了我同样的结果。任何帮助指导我错误的地方将不胜感激。 这些是我试过的代码,但是没有给我任何结果。

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

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

我部署的代码如下。

#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);
  
  }

你可以用这个先初始化数组

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

然后你可以把这个值放在ExtBuffer

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