鄙人从俄罗斯的双线MACD指标入手,想添加修改代码在MT4上得到类似缠论MACD的面积指标(参考通达信MACD面积指标图片),即指标会计算每一波红柱或绿柱的面积(累加MACD柱子的值)。通达信指标上可以通过数字文本显示,我对C语言不熟,只想能从指标窗口读出数据就可以了(参考MT4上的MACD
效果图)。
现在问题是:MT4代码编译通过,但加载指标就是等待状态,貌似计算量惊人,是不是算法写错了或者有死循环之类?附上源代码请兄弟们帮我攻关修正,万分感激!!!
附件MACD-2.mq4是俄罗斯双线MACD指标,MACD-3.mq4是本人写的需要修正的指标,MACD-3.mq4代码如下:
#property indicator_separate_window
#property indicator_buffers 6
#property indicator_color1 White
#property indicator_color2 Yellow
#property indicator_color3 Red
#property indicator_color4 Green
//---- indicator parameters
extern int FastEMA=5;
extern int SlowEMA=9;
extern int SignalEMA=4;
//---- indicator buffers
double ind_buffer1[]; //DIF-WHITE
double ind_buffer2[]; //DEA-YELLOW
double ind_buffer3[]; //MACD-RED
double ind_buffer4[]; //MACD-GREEN
double ind_buffer5[]; //RED AREA
double ind_buffer6[]; //GREEN AREA
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- 2 additional buffers are used for counting.
// IndicatorBuffers(6);
//---- drawing settings
SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,1);
SetIndexStyle(3,DRAW_HISTOGRAM,STYLE_SOLID,1);
IndicatorDigits(int(MarketInfo(Symbol(),MODE_DIGITS)+1));
SetIndexDrawBegin(2,SignalEMA);
SetIndexDrawBegin(3,SignalEMA);
//---- 4 indicator buffers mapping
SetIndexBuffer(0,ind_buffer1); //DIF-WHITE
SetIndexBuffer(1,ind_buffer2); //DEA-YELLOW
SetIndexBuffer(2,ind_buffer3); //MACD-RED
SetIndexBuffer(3,ind_buffer4); //MACD-GREEN
SetIndexBuffer(4,ind_buffer5); //RED AREA
SetIndexBuffer(5,ind_buffer6); //GREEN AREA
SetIndexLabel(0,"DIF"); //DIF-WHITE
SetIndexLabel(1,"DEA"); //DEA-YELLOW
SetIndexLabel(2,"Red"); //MACD-RED
SetIndexLabel(3,"Green"); //MACD-GREEN
SetIndexLabel(4,"Red_Area"); //RED AREA
SetIndexLabel(5,"Green_Area");//GREEN AREA
//---- name for DataWindow and indicator subwindow label
IndicatorShortName("MACD-3 ("+string(FastEMA)+","+string(SlowEMA)+","+string(SignalEMA)+")");
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| Oscillator |
//+------------------------------------------------------------------+
int start()
{
double prev,current;
int counted_bars = IndicatorCounted();
if(counted_bars 0) counted_bars--;
int limit = Bars - counted_bars;
if(counted_bars==0) limit-=2;
//---- DIF counted in the 1-st additional buffer
for(int i=0; i=0; i--)
{
current=ind_buffer1-ind_buffer2;
prev=ind_buffer1[i+1]-ind_buffer2[i+1];
if(current>prev) up=true;
if(current=0;k--) //
{
if (ind_buffer3[k]>0) //如果第一根柱子是红柱:
{
ind_buffer5[k]=ind_buffer3[k]; //最左(早)第1根红柱赋值给面积,准备累加总面积
r=1; //红柱计数为1根
for (k=limit-1-1; k>=0;k--) //从最左第2根开始判断右边1根是否仍是红柱,若是红柱,累加红柱面积并开始循环
{
if (ind_buffer3[k]>0) //如果右边1根仍是红柱
{
ind_buffer5[k]=ind_buffer3[k]+ind_buffer5[k+1]; //将左边红柱面积累加到当前总面积
r++; //红柱计数增加1根
}
break;
} //循环完成,完成一波连续红柱的面积累计,红柱数量共计r根
} //上面循环完成r根连续红柱计算,剩下共k-r根继续累加,并进入零柱判断循环
k=limit-1-r;
if (ind_buffer3[k]==0&&ind_buffer4[k]==0) //如果第一根柱子是零柱:
{
ind_buffer5[k]=ind_buffer3[k]; //最左(早)第1根零柱柱赋值给面积,准备累加总面积
ind_buffer6[k]=ind_buffer4[k];
z=1; //零柱计数为1根
for (k=limit-1-1; k>=0;k--) //从最左第2根开始判断右边1根是否仍是零柱,若是零柱,累加零柱面积并开始循环
{
if (ind_buffer3[k]==0&&ind_buffer4[k]==0) //如果右边1根仍是零柱
{
ind_buffer5[k]=ind_buffer3[k]; //将左边零柱面积累加到当前总面积
ind_buffer6[k]=ind_buffer4[k];
z++; //零柱计数增加1根
}
break;
} //循环完成,完成一波连续零柱的面积累计,零柱数量共计z根
} //上面循环完成z根连续红柱及零柱计算,剩下共k-r-z根继续累加,并进入绿柱循环
k=limit-1-r-z;
if (ind_buffer4[k]=0;k--) //从最左第2根开始判断右边1根是否仍是绿柱,若是绿柱,累加绿柱面积并开始循环
{
if (ind_buffer4[k]<0) //如果右边1根仍是绿柱
{
ind_buffer6[k]=ind_buffer4[k]+ind_buffer6[k+1]; //将左边绿柱面积累加到当前总面积
g++; //绿柱计数增加1根
}
break;
} //循环完成,完成一波连续绿柱的面积累计,绿柱数量共计g根
} //上面循环完成g根连续绿柱计算,剩下共k-r-z-g根继续累加,并进入红柱判断循环
k=limit-1-r-z-g;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
return(0);
}
//+------------------------------------------------------------------+
谢谢!
|