从自定义构建的阵列计算MQL中的标准偏差

时间:2018-12-15 07:20:47

标签: mql4

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

我试图通过在全局范围内声明该数组然后向其中添加值来创建一个数组,但是它给了我零值,我还尝试手动添加值以检查它是否有效,但是函数{{ 3}}也给我相同的结果。如有任何指导我做错事情的帮助,将深表感谢。 这些是我尝试过的代码,但是没有给我任何结果。

  

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

  }

1 个答案:

答案 0 :(得分:1)

您可以使用它来首先初始化数组

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