求教,高手进来看一下

 

这个是真实波幅14日移动平均线的程序。

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

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 DodgerBlue
//---- input parameters
extern int AtrPeriod=14;
//---- buffers
double AtrBuffer[];
double TempBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
string short_name;
//---- 1 additional buffer used for counting.
IndicatorBuffers(2);
//---- indicator line
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,AtrBuffer);
SetIndexBuffer(1,TempBuffer);
//---- name for DataWindow and indicator subwindow label
short_name="ATR("+AtrPeriod+")";
IndicatorShortName(short_name);
SetIndexLabel(0,short_name);
//----
SetIndexDrawBegin(0,AtrPeriod);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Average True Range |
//+------------------------------------------------------------------+
int start()
{
int i,counted_bars=IndicatorCounted();
//----
if(Bars<=AtrPeriod) return(0);
//---- initial zero
if(counted_bars<1)
for(i=1;i<=AtrPeriod;i++) AtrBuffer[Bars-i]=0.0;
//----
i=Bars-counted_bars-1;
while(i>=0)
{
double high=High[i];
double low =Low[i];
if(i==Bars-1) TempBuffer[i]=high-low;
else
{
double prevclose=Close[i+1];
TempBuffer[i]=MathMax(high,prevclose)-MathMin(low,prevclose);
}
i--;
}
//----
if(counted_bars>0) counted_bars--;
int limit=Bars-counted_bars;
for(i=0; i<limit; i++)
AtrBuffer[i]=iMAOnArray(TempBuffer,Bars,AtrPeriod,0,MODE_SMA,i);
//----
return(0);
}
//+------------------------------------------------------------------+

然后呢,我想让它既显示移动平均线 (在这个数组里AtrBuffer),也想同时让它显示每天的真实波幅,所以我在 int init()函数里增加了显示它的语句。也就是说,后面的int init()变成了这样

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 DodgerBlue
#property indicator_color2 White

int init()
{
string short_name;
//---- 1 additional buffer used for counting.
IndicatorBuffers(2);
//---- indicator line
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,AtrBuffer);
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,TempBuffer);
//---- name for DataWindow and indicator subwindow label
short_name="ATR("+AtrPeriod+")";
IndicatorShortName(short_name);
SetIndexLabel(0,short_name);
//----
SetIndexDrawBegin(0,AtrPeriod);
SetIndexDrawBegin(1,AtrPeriod);
//----
return(0);
}

红色部分是增加的语句。

现在问题来了,它就是显示不了 TempBuffer(每个周期的真实波幅)。

而假如我把函数换成只显示TempBuffer,而不显示 AtrBuffer的。它(TempBuffer)就可以画出来。

只显示TempBuffer,不显示 AtrBuffer的函数如下:

int init()
{
string short_name;
//---- 1 additional buffer used for counting.
IndicatorBuffers(2);
//---- indicator line
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,TempBuffer);
SetIndexBuffer(1,AtrBuffer);
//---- name for DataWindow and indicator subwindow label
short_name="ATR("+AtrPeriod+")";
IndicatorShortName(short_name);
SetIndexLabel(0,short_name);
//----
SetIndexDrawBegin(0,AtrPeriod);

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

红色部分互换

这样说明, TempBuffer数组的数据是没有问题的。但为什么我想让TempBuffer(真实波幅)和AtrBuffer(真实波幅的移动平均线)都一起显示的时候,就不行呢?语法方面也没看到问题啊。高手,帮帮忙,多谢了。

 

ATR程序是系统自带的,肯定是没问题的。我不过是想让它显示每天的真实波幅。真是奇了怪了

原因: