麻烦帮我改一下这个指标,我这个指标老是运行不了

 

总是显示'PlotBuffer1' - invalid array access ,想问下到底应该怎么改,还是说我这个指标编的有问题


#property indicator_separate_window

#property indicator_buffers 3

#property indicator_color1 DodgerBlue

#property indicator_color2 Lime

#property indicator_color3 Yellow

//---- buffers

double PlotBuffer1[];

double PlotBuffer2[];

double PlotBuffer3[];




int counted_bars;










//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int init()

  {

//---- indicators

   SetIndexStyle(0,DRAW_LINE);   // Plot1

   SetIndexBuffer(0,PlotBuffer1);

   SetIndexStyle(1,DRAW_LINE);   // Plot2

   SetIndexBuffer(1,PlotBuffer2);

   SetIndexStyle(2,DRAW_LINE);   // Plot3

   SetIndexBuffer(2,PlotBuffer3);

   SetIndexDrawBegin(0,0);

   SetIndexDrawBegin(1,0);

   SetIndexDrawBegin(2,0);

   

   IndicatorShortName("MyIndicator");

   counted_bars=IndicatorCounted();

   

//----

   return(0);

  }

//+------------------------------------------------------------------+

//| Custom indicator deinitialization function                       |

//+------------------------------------------------------------------+

int deinit()

  {

//----

//----

   return(0);

  }

//+------------------------------------------------------------------+

//| Custom indicator iteration function                              |

//+------------------------------------------------------------------+

int start(void)

  {

   double RSIDelta;

   double RSIsma;

   int pos = counted_bars;

//----

//Alert("position:  " + pos);

  // Alert("current bar close:  " + Close[pos]);

   //Alert("bars:  " + Bars);

   RSIDelta = iMomentum(NULL,0,9,iRSI(NULL,0,14,PRICE_CLOSE,0),0);

   RSIsma = iMA(NULL,0,3,0,MODE_SMMA,iRSI(NULL,0,3,PRICE_CLOSE,0),0);

   PlotBuffer1[counted_bars] = RSIDelta+RSIsma;

   PlotBuffer2[counted_bars] = iMA(NULL,0,13,0,MODE_SMA,PlotBuffer1,0);

   PlotBuffer3[counted_bars] = iMA(NULL,0,33,0,MODE_SMA,PlotBuffer1,0);
   
  
   

   pos--;

//----

   return(0);

  }

 

先帮你把贴出来的代码用"SRC"包含了一下,看得清楚一点 :)

另外,出错的行号是在哪里呢?是在调用 iMA的时候吗? 

从你的代码中看到的,调用 iMomentum 和 iMA 的参数类型是不大对 - 倒数第二个参数应该是应用到的价格类型吧,你的代码里面不是这样的,应该有问题。

还有,后面的两个 iMA,我想你是打算求数组的 MA, 所以可能要用到 iMAOnArray 函数,如果确实是使用数组里的数值求 MA,那么还要防止数组里面元素过少或者没有初始化,要加一下判断。

总之可能有两方面的问题:

- 函数的使用,参数看看对不对,用的函数对不对

- 加一些判断防止出错。

 

另外,你这里代码好像不全,比如 pos-- 不知道是为什么,你可以参考一下代码库里面的指标,看看它们的写法。

还有,你这里写指标的代码风格还是比较老的 MQL4 方式,可以考虑使用新一些的方式来写,MQL4和MQL5兼容的方式 (OnInit, OnCalculate这种).

 
Jian Chen:

另外,你这里代码好像不全,比如 pos-- 不知道是为什么,你可以参考一下代码库里面的指标,看看它们的写法。

还有,你这里写指标的代码风格还是比较老的 MQL4 方式,可以考虑使用新一些的方式来写,MQL4和MQL5兼容的方式 (OnInit, OnCalculate这种).


多谢,我再研究下

 
Jian Chen:

另外,你这里代码好像不全,比如 pos-- 不知道是为什么,你可以参考一下代码库里面的指标,看看它们的写法。

还有,你这里写指标的代码风格还是比较老的 MQL4 方式,可以考虑使用新一些的方式来写,MQL4和MQL5兼容的方式 (OnInit, OnCalculate这种).


你好,我是按这个下面的句子改编的,不知道应该怎么改,不知道怎么改比较合适,刚接触有点懵:

函数一:

RSIMO9=MOMENTUM(RSI(Close,14),9);

函数二:

 RSI3=AVERAGE(RSI(CLOSE,3),3);

公式:

Plot1(RSIMO(+RSI3,"Plot1"):

Plot2(average((plot1),13),"Plot2");

Plot3(average((plot1),33),"Plot2");

 

上面的是通达信公式吗?我也有点晕 :)

看起来公式不算复杂,但是现在新的 MQL4/MQL5 里面有点问题,就是指标计算都是针对标准的图表价格序列,而对自定义的序列,比如数组里面的数值进行某种指标算法的计算,并没有支持,所以写起来还真是麻烦,这两天英文版论坛也有人问类似的问题,现在还没有明确的方案(有人让他到俄文版那里问问看。。。)

你这里的公式,里面平均(average)当然好做,但是RSI, Momentum算法可能要自己来写,就复杂很多。所以写出代码和做测试还真是要不少时间的,短期内实在没有时间帮到您,不好意思了。

有精力的话还是建议自己学学看,标准指标的源代码在 MetaTrader 里面都有,算法公式可以看代码,也可以看帮助文档。一方面可以看到指标应该怎样写,另一方面可以看到具体的指标算法是怎样的。

 

你难到是第一次编?? 难道例子程序也没看过??

指标是用与随时间变化随时计算显示的,弄明白那个pos的作用吧!!!

修改 start里面:


  int   counted_bars=IndicatorCounted();

double RSIDelta;

double RSIsma;
int pos = Bars-counted_bars-1;
while (pos>=0)
{

   RSIDelta = iMomentum(NULL,0,9,iRSI(NULL,0,14,PRICE_CLOSE,0),pos);

   RSIsma = iMA(NULL,0,3,0,MODE_SMMA,iRSI(NULL,0,3,PRICE_CLOSE,0),pos);

   PlotBuffer1[pos] = RSIDelta+RSIsma;

   PlotBuffer2[pos] = iMA(NULL,0,13,0,MODE_SMA,PlotBuffer1,pos);

   PlotBuffer3[pos] = iMA(NULL,0,33,0,MODE_SMA,PlotBuffer1,pos);
 
   pos--;

}

原因: