新手学习编写指标,求达人指教

 

我想写一个指标,完成下面的功能,但是自己根据看mql4的帮助写的程序,编译ok,但是加载后无反应,求达人指教。

功能:

1、ma26=26日中间价均线;

2、ma9= 9日中间均价;

3、upp = ma26 + 2.7*26日标准方差;

4、low = ma9 - 2.7*26日标准方差;

5、a1 =( upp -ma9 ) / (ma9 - low );

6、a2 = 前3日a1的均值;

7、在附图上画出a2线段;

代码如下:

double arrA2[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
SetIndexStyle(0,DRAW_HISTOGRAM);
SetIndexStyle(1,DRAW_LINE);
SetIndexDrawBegin(1,9);
IndicatorDigits(Digits+1);
//---- indicator buffers mapping
SetIndexBuffer(0,arrA2);
//---- name for DataWindow and indicator subwindow label
IndicatorShortName("A2:");
SetIndexLabel(1,"A2");
return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
//----
double arrMa26[], arrUpper[], arrLower[], arrMa9[], arrA1[];
int iArrLen, i, cnt, ticket, total;

if(counted_bars>0) counted_bars--;
iArrLen=Bars-counted_bars;

for( i=0; i<iArrLen; i++ )
{
arrMa26[i] = iMA(NULL,0,26,0,MODE_SMMA,PRICE_MEDIAN,i);
arrMa9[i] = iMA(NULL,0,9,0,MODE_SMMA,PRICE_MEDIAN,i);

arrUpper[i] = arrMa26[i] + 2.7*iStdDev(NULL,0,26,0,MODE_SMMA,PRICE_MEDIAN,i);
arrLower[i] = arrMa9[i] - 2.7*iStdDev(NULL,0,26,0,MODE_SMMA,PRICE_MEDIAN,i);

arrA1[i] = (arrUpper[i]-arrMa9[i])/(arrMa9[i]-arrLower[i]);
}
for( i=2; i<iArrLen; i++ )
{
arrA2[i] = ( MathAbs(arrA1[i-2])+MathAbs(arrA1[i-1])+MathAbs(arrA1[i]) )/3;
}

//----
return(0);
}
//+------------------------------------------------------------------+

 

你的 arrUpper[i] +arrLower[i] =0 你确定你的arrA1[i] = (arrUpper[i]-arrMa9[i])/(arrMa9[i]-arrLower[i]); 没有写错?

2010.11.09 15:23:56 test EURUSD,H1: zero divide

你有没发现,你的纵坐标数值?

900000 到-900000

 
angelstar:

你的 你确定你的arrA1[i] = (arrUpper[i]-arrMa9[i])/(arrMa9[i]-arrLower[i]); 没有写错?

2010.11.09 15:23:56 test EURUSD,H1: zero divide

你有没发现,你的纵坐标数值?

900000 到-900000


arrUpper[i] +arrLower[i] =0 这个等于0吗?
 

//+------------------------------------------------------------------+
//| test.mq4 |
//| Copyright ?2010, MetaQuotes Software Corp. |
//| https://www.metaquotes.net// |
//+------------------------------------------------------------------+
#property copyright "Copyright ?2010, MetaQuotes Software Corp."
#property link "https://www.metaquotes.net//"

#property indicator_separate_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+

#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Red
double arrA1[];
double arrA2[];
int init()
{
SetIndexStyle(0,DRAW_LINE);
SetIndexStyle(1,DRAW_LINE);
IndicatorDigits(Digits+1);
//---- indicator buffers mapping
SetIndexBuffer(0,arrA1);
SetIndexBuffer(1,arrA2);
//---- name for DataWindow and indicator subwindow label
SetIndexLabel(0,"First");
SetIndexLabel(1,"Seconds");
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
//----
double arrMa26[], arrUpper[], arrLower[], arrMa9[];//, arrA1[];
int iArrLen, i, cnt, ticket, total;

if(counted_bars>0) counted_bars--;
iArrLen=Bars-counted_bars;

for( i=0; i<iArrLen; i++ )
{
arrMa26[i] = iMA(NULL,0,26,0,MODE_SMMA,PRICE_MEDIAN,i);
arrMa9[i] = iMA(NULL,0,9,0,MODE_SMMA,PRICE_MEDIAN,i);

arrA1[i] = arrMa26[i] + 2.7*iStdDev(NULL,0,26,0,MODE_SMMA,PRICE_MEDIAN,i); /////arrUpper
arrA2[i] = arrMa9[i] - 2.7*iStdDev(NULL,0,26,0,MODE_SMMA,PRICE_MEDIAN,i);/////arrLower

//arrA1[i] = (arrUpper[i]-arrMa9[i])/(arrMa9[i]-arrLower[i]);

}
//for( i=0; i<iArrLen; i++ )
//{
//arrA2[i] = ( MathAbs(arrA1[i-2])+MathAbs(arrA1[i-1])+MathAbs(arrA1[i]) )/3;
//}

//----
return(0);
}
//+------------------------------------------------------------------+

你试试这个代码 arrUpper[i] 和 arrLower[i] 图形是对称的。相加是0的。

 

确实啊,这太奇怪了啊,一个是26日均线,一个是9日,怎么会这样呢?

 

很感谢star老大的指教,明白了很多东西,还有下面一个疑问:

线段A1已经可以画出来了,为什么A2不行呢?

//+------------------------------------------------------------------+
//| test.mq4 |
//| Copyright ?2010, MetaQuotes Software Corp. |
//| https://www.metaquotes.net// |
//+------------------------------------------------------------------+
#property copyright "zys"
#property link ""

#property indicator_separate_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+

#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_color2 Red
#property indicator_color3 Blue
double arrUpper[];
double arrLow[];
double arrA1[];
double arrA2[];
int iArrLen;

int init()
{
SetIndexStyle(0,DRAW_LINE);
SetIndexStyle(1,DRAW_LINE);
SetIndexStyle(2,DRAW_LINE);
IndicatorDigits(Digits+1);
//---- indicator buffers mapping
SetIndexBuffer(0,arrUpper);
SetIndexBuffer(1,arrLow);
SetIndexBuffer(2,arrA2);
//---- name for DataWindow and indicator subwindow label
SetIndexLabel(0,"Upp");
SetIndexLabel(1,"Low");
SetIndexLabel(2,"A2");
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
//----
double arrMaMax[], arrMaMin[];
int i, cnt, ticket, total;
int iMax = 26, iMin = 9;

if(counted_bars>0) counted_bars--;
iArrLen=Bars-counted_bars;

//iArrLen=Bars;
for( i=0; i<iArrLen; i++ )
{
arrMaMax[i] = iMA(NULL,0,iMax,0,MODE_SMA,PRICE_MEDIAN,i);
arrMaMin[i] = iMA(NULL,0,iMin,0,MODE_SMA,PRICE_MEDIAN,i);

arrUpper[i] = arrMaMax[i] + 2.7*iStdDev(NULL,0,iMax,0,MODE_SMA,PRICE_MEDIAN,i); /////arrUpper
arrLow[i] = arrMaMax[i] - 2.7*iStdDev(NULL,0,iMin,0,MODE_SMA,PRICE_MEDIAN,i);/////arrLower

arrA1[i] = (MathAbs(arrUpper[i]-arrMaMin[i])/(arrMaMin[i]-arrLow[i]))/3;

}

arrA2[0] = 0;
arrA2[1] = 0;

for( i=2; i<iArrLen; i++ )
{
arrA2[i] = arrA1[i-2]+arrA1[i-1]+arrA1[i];
//arrA2[i] = iMAOnArray(arrA1,0,3,0,MODE_SMA,i);
}

//----
return(0);
}
//+------------------------------------------------------------------+

 

我也发现了!!

发现一个问题就是 当第一次编译运行程序时候 iArrLen=5000+

然后第二次运行时候就是2了。

由此可知道MT4历史数据相当于初始化一次,然后,价格变动时候,只是变动最近2个周期或者多长时间的

你可以Alert(Bars-counted_bars); Alert(iArrLen );调用看看。

 

出现错误

ERR_ARRAY_INDEX_OUT_OF_RANGE 4002 企图获取数组项目,其中一些是出于数组范围

你在程序最后

for( i=2; i<iArrLen; i++ )
{
arrA2[i] = arrA1[i-2]+arrA1[i-1]+arrA1[i];
//arrA2[i] = iMAOnArray(arrA1,0,3,0,MODE_SMA,i);
}

Print("error:"+GetLastError());

//----
return(0);
就会看到错误信息了。