下载MetaTrader 5

求助!! MT4编程达人请进来指导MACD面积指标

要添加评论,请登录注册
开始使用市场获利。发布您的产品!
topxjl
11
topxjl 2016.01.19 21:04 


鄙人从俄罗斯的双线MACD指标入手,想添加修改代码在MT4上得到类似缠论MACD的面积指标(参考通达信MACD面积指标图片),即指标会计算每一波红柱或绿柱的面积(累加MACD柱子的值)。通达信指标上可以通过数字文本显示,我对C语言不熟,只想能从指标窗口读出数据就可以了(参考MT4上的MACD效果图)。

现在问题是:MT4代码编译通过,但加载指标就是等待状态,貌似计算量惊人,是不是算法写错了或者有死循环之类?附上源代码请兄弟们帮我攻关修正,万分感激!!!

MACD面积和红绿柱转折指标图(通达信)

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)  return(-1);
   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<limit; i++)
      ind_buffer1[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
//---- DEA counted in the 2-nd additional buffer
   for(i=0; i<limit; i++)
      ind_buffer2[i]=iMAOnArray(ind_buffer1,Bars,SignalEMA,0,MODE_SMA, i);
//---- dispatch values between 2 buffers
   bool up=true;
   for(i=limit-1; i>=0; i--)
     {
      current=ind_buffer1[i]-ind_buffer2[i];
      prev=ind_buffer1[i+1]-ind_buffer2[i+1];
      if(current>prev) up=true;
      if(current<prev) up=false;
      if(!up)
        {
         ind_buffer4[i]=3*current;
         ind_buffer3[i]=0.0;
        }
      else
        {
         ind_buffer3[i]=3*current;
         ind_buffer4[i]=0.0;
        }

        }
  //++++++++++++++++++++++++++++++++++  以下累加连续红绿柱的面积
 
    int r,z,g;                            //定义变量r,z,g分别计数红柱、零柱和绿柱
    r=z=g=0;
    int k=limit;
    for  (k=limit-1-r-z-g; k>=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)                   //如果第一根柱子是绿柱:
       {
        ind_buffer6[k]=ind_buffer4[k];       //最左(早)第1根绿柱赋值给面积,准备累加总面积
        g=1;                                 //绿柱计数为1根
         for (k=limit-1-1; i>=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);
  }
//+------------------------------------------------------------------+




谢谢!

附加的文件:
MACD-2.mq4 3 kb
MACD-3.mq4 12 kb
redmountain
15
redmountain 2016.01.26 13:38  
topxjl:


鄙人从俄罗斯的双线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)  return(-1);
   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<limit; i++)
      ind_buffer1[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
//---- DEA counted in the 2-nd additional buffer
   for(i=0; i<limit; i++)
      ind_buffer2[i]=iMAOnArray(ind_buffer1,Bars,SignalEMA,0,MODE_SMA, i);
//---- dispatch values between 2 buffers
   bool up=true;
   for(i=limit-1; i>=0; i--)
     {
      current=ind_buffer1[i]-ind_buffer2[i];
      prev=ind_buffer1[i+1]-ind_buffer2[i+1];
      if(current>prev) up=true;
      if(current<prev) up=false;
      if(!up)
        {
         ind_buffer4[i]=3*current;
         ind_buffer3[i]=0.0;
        }
      else
        {
         ind_buffer3[i]=3*current;
         ind_buffer4[i]=0.0;
        }

        }
  //++++++++++++++++++++++++++++++++++  以下累加连续红绿柱的面积
 
    int r,z,g;                            //定义变量r,z,g分别计数红柱、零柱和绿柱
    r=z=g=0;
    int k=limit;
    for  (k=limit-1-r-z-g; k>=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)                   //如果第一根柱子是绿柱:
       {
        ind_buffer6[k]=ind_buffer4[k];       //最左(早)第1根绿柱赋值给面积,准备累加总面积
        g=1;                                 //绿柱计数为1根
         for (k=limit-1-1; i>=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);
  }
//+------------------------------------------------------------------+




谢谢!

其实吧多少年以前就有了,我忘了在哪见过了,好像是淘宝上……

唉…… 

Tsin
8
Tsin 2016.05.13 09:29  
支持一下,希望有人能帮助楼主解决这个问题。
chenny
12
chenny 2016.05.17 17:50  

24小时自动智能跟单  

autotrader5
13
autotrader5 2016.07.06 16:45  
额,我给客户写过,不会他拿到淘宝上去卖了吧?
redmountain
15
redmountain 2016.08.06 04:47  
autotrader5:
额,我给客户写过,不会他拿到淘宝上去卖了吧?

你是2016.07.06刚注册的,人家都多很年了那时候还没你呢,哈哈……

再说也不是什么多高难的东西,这样其实很没意思。 

要添加评论,请登录注册