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

14
2010.11.08 16:27

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);
}
//+------------------------------------------------------------------+

73
2010.11.09 08:59

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

900000 到-900000

14
2010.11.09 13:02
angelstar:

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

900000 到-900000

arrUpper[i] +arrLower[i] =0 这个等于0吗？
73
2010.11.10 10:12

//+------------------------------------------------------------------+
//| test.mq4 |
//| Copyright ?2010, MetaQuotes Software Corp. |
//| http://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);
}
//+------------------------------------------------------------------+

14
2010.11.11 01:56

14
2010.11.11 02:42

//+------------------------------------------------------------------+
//| test.mq4 |
//| Copyright ?2010, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+

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

73
2010.11.17 04:09

73
2010.11.17 04:17

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

/