象高手求教!!!

 

满足5天和13天均线交叉时候,记录交叉时的13天均线的值,保存到一个数组ma13v[].

怎么去测试,print得的ma值是0??而不是 0.1?

//+------------------------------------------------------------------+
//| EAtest.mq4 |
//| Copyright ?2009, MetaQuotes Software Corp. |
//| https://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright ?2009, MetaQuotes Software Corp."
#property link "https://www.metaquotes.net/"
double ma13v[];
double ma13vv[];


int start()
{
//----
for (int i =1;i<=3000;i++)
{
double ma05,ma013, ma5,ma13;
ma05=iMA(NULL,0,5,0,MODE_SMA,PRICE_CLOSE,i-1) ;
ma013=iMA(NULL,0,13,0,MODE_SMA,PRICE_CLOSE,i-1) ;
ma5=iMA(NULL,0,5,0,MODE_SMA,PRICE_CLOSE,i) ;
ma13=iMA(NULL,0,13,0,MODE_SMA,PRICE_CLOSE,i) ;


if((ma05>ma013&&ma5<ma13)|| (ma013>ma05&&ma13<ma5))
{
ma13v[i]=ma13;
}
else ma13v[i]=0.1;

Print("ma",ma13v[i]);

}

/* double a;
if(ma13v[i]!=0.1)
ma13vv[j]=ma13v[i];
j++;
Print("ma13vv[5]",a);*/

return(0);
}
//+-

 
Print("ma=" + DoubleToStr(ma13v[i], 5));
 
double ma13v[];

double ma13vv[];

在EA中这样的定义是错误的,因为没有指定存储空间大小。数据没地方存

正确定义如下:

double ma13v[3000];
double ma13vv[3000];

 
y2k_connect 写道 >>
Print("ma=" + DoubleToStr(ma13v[i], 5));

谢谢 啊

 
xfxyldj 写道 >>
double ma13v[];

double ma13vv[];

在EA中这样的定义是错误的,因为没有指定存储空间大小。数据没地方存

正确定义如下:

double ma13v[3000];
double ma13vv[3000];

谢谢 啊 ,这个问题也有kaolv 到了 ,只是奇怪为什么指标里不定义大小,也是可以的 。

 
tang15021122935 写道 >>

谢谢 啊 ,这个问题也有kaolv 到了 ,只是奇怪为什么指标里不定义大小,也是可以的 。

double ma13v[];

这样写,在mt4里面是可以的。它表示:定义了一个和当前可见图表中k线长度相同的缓冲区。

但是,这里存在一个问题。没有初始化缓冲区ma13v。

需要在init()中增加:

int init() {
	IndicatorBuffers(1);

	SetIndexBuffer(0, ma13v);
	SetIndexDrawBegin(0, 0);
	SetIndexEmptyValue(0, 0);

	return(0);
}
由于是ea,所以不需要定义“#property ... ...”。
 
tang15021122935 写道 >>

谢谢 啊 ,这个问题也有kaolv 到了 ,只是奇怪为什么指标里不定义大小,也是可以的 。

指标里面没有定义大小但是有

SetIndexBuffer()

函数分配存储空间给这个数组。

另外如果开始不定义数组空间大小的话。

可以在后面的程序里面用

ArrayResize( void array[], int new_size)

来重新初始化分配数组大小

 
y2k_connect 写道 >>

double ma13v[];

这样写,在mt4里面是可以的。它表示:定义了一个和当前可见图表中k线长度相同的缓冲区。

但是,这里存在一个问题。没有初始化缓冲区ma13v。

需要在init()中增加:

由于是ea,所以不需要定义“#property ... ...”。

帮看看,为什么都达不到效果,保存5和13交叉的ma13 的值到ma13v[],过滤去掉0的值,保存到ma13vv[]

如果满足

if((ma13vv[i-1]>ma13vv[i-2]) && (ma13vv[i-1]>ma13vv[i]))
则 gao[i]=ma13vv[i];

if((ma13vv[i-1]<ma13vv[i]) && (ma13vv[i-1]<ma13vv[i-2]))
di[i]=ma13vv[i];

#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_color3 Red
#property indicator_color4 Blue
#property indicator_color5 Yellow


//---- input parameters
//---- indicator buffers
double gao[100000];
double di[100000];
double crossup[100000];
double crossdown[100000];
double ma13v[100000];
double ma13vv[100000];
double fanzhuan[1000000];
datetime tim[1000000];
datetime crosstim[1000000];
int shift[100000];
int xiaobiao[100000];
int count=0;


//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- line shifts when drawing

//---- first positions skipped when drawing

//---- 3 indicator buffers mapping
SetIndexBuffer(0,gao);
SetIndexBuffer(1,di);
SetIndexBuffer(2,crossup);
SetIndexBuffer(3,crossdown);
SetIndexBuffer(4,ma13v);

//---- drawing settings
SetIndexStyle(0,DRAW_ARROW);
SetIndexStyle(1,DRAW_ARROW);
SetIndexStyle(2,DRAW_ARROW,EMPTY, 2, Red);
SetIndexStyle(3,DRAW_ARROW,EMPTY, 2, Blue);
SetIndexStyle(4,DRAW_ARROW,EMPTY, 2, Yellow);

SetIndexArrow(0,67);
SetIndexArrow(1,68);
SetIndexArrow(2,241);
SetIndexArrow(3,242);
SetIndexArrow(4,251);

SetIndexEmptyValue(0, 0);
SetIndexEmptyValue(1, 0);
SetIndexEmptyValue(2, 0);
SetIndexEmptyValue(3, 0);
SetIndexEmptyValue(4, 0);


//---- index labels

//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| Bill Williams' Alligator |
//+------------------------------------------------------------------+
int start()
{
int limit;
int counted_bars=IndicatorCounted();
//---- check for possible errors
if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
//---- main loop
for(int i=1; i<limit; i++)
{
//---- ma_shift set to 0 because SetIndexShift called abowe

double ma05,ma013, ma5,ma13;
ma05=iMA(NULL,0,5,0,MODE_SMA,PRICE_CLOSE,i-1) ;
ma013=iMA(NULL,0,13,0,MODE_SMA,PRICE_CLOSE,i-1) ;
ma5=iMA(NULL,0,5,0,MODE_SMA,PRICE_CLOSE,i) ;
ma13=iMA(NULL,0,13,0,MODE_SMA,PRICE_CLOSE,i) ;

if(ma05>ma013&&ma5<ma13)

crossup[i]=ma13;


if (ma013>ma05&&ma13<ma5)
crossdown[i]=ma13;

if((ma05>ma013&&ma5<ma13)|| (ma013>ma05&&ma13<ma5))

ma13v[i]=ma13;


}


for(i=1;i<20000;i++)
{
if(ma13v[i]!=0)
{
ma13vv[count]=ma13v[i];
count++;
}

Comment("ma13vv[2]", DoubleToStr(ma13vv[2],5) );

}


for(i=3;i<2000;i++)
{
if((ma13vv[i-1]>ma13vv[i-2]) && (ma13vv[i-1]>ma13vv[i]))
gao[i]=ma13vv[i];

if((ma13vv[i-1]<ma13vv[i]) && (ma13vv[i-1]<ma13vv[i-2]))
di[i]=ma13vv[i];
}

 

首先,确定你是编写ea,还是mt4的指标。

如果是ea,请删除“#property ... ...”部分。以及与之相关的SetIndexStyle部分。

#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_color3 Red
#property indicator_color4 Blue
#property indicator_color5 Yellow


如果是mt4的指标,就必须有“#property ... ...”部分。


其次,建议将不会显示在图表上的曲线,其缓冲区用一维数组代替。并且一维数组的长度控制在32以内。对于多个相关的一维数组,建议改成二维数组。例如有10个相关的一维数组,可以写成arr[10][MaxLen]。其中的MaxLen=32。


最后,如果你希望在mt4的指标中,发出买卖信号,请用BarUp[]和BarDown[]缓冲区。

#property indicator_separate_window

#property indicator_buffers	2
#property indicator_color4	Lime		// BarUp

#property indicator_color5	Red		// BarDown


//---- input parameters

... ...

//---- buffers

double	BarUp[], BarDown[];


int init() {

	IndicatorBuffers(2);

	IndicatorDigits(Digits + 1);

	IndicatorShortName("ma_2");



	SetLevelValue(0, 0);

	SetLevelStyle(STYLE_DOT, 1, Silver);



	SetIndexBuffer(0, BarUp);

	SetIndexStyle(0, DRAW_HISTOGRAM);

	SetIndexLabel(0, "Bar+");

	SetIndexDrawBegin(0, ma_period1);

	SetIndexEmptyValue(0, 0);



	SetIndexBuffer(1, BarDown);

	SetIndexStyle(1, DRAW_HISTOGRAM);

	SetIndexLabel(1, "Bar-");

	SetIndexDrawBegin(1, ma_period1);

	SetIndexEmptyValue(1, 0);



	return(0);

}


... ...
原因: