坑爹小游戏14关怎么过:双线MACD指标算法原理

来源:百度文库 编辑:偶看新闻 时间:2024/05/07 12:45:07

双线MACD指标算法原理


#property indicator_separate_window

#property indicator_buffers 4

#property indicator_color1  White

#property indicator_color2  Yellow

#property indicator_color3  Red

#property indicator_color4  Lime

***以上是双线MACD的一些初始化的设置,包括窗体,数据缓冲区区和呈现图像的线的颜色,color1White色来表示DIFcolor2Yellow色来表示DEAcolor3为红色的柱子,color4为绿色的柱子

 

//----indicator parameters

extern intFastEMA=9;

extern intSlowEMA=20;

extern intSignalSMA=9;

***定义MACD的算法参数,FastEMA为通过K线收盘价做EMA加权平均线计算的参数,默认为12,也称为快线; SlowEMA为通过K线收盘价做EMA加权平均线计算的参数,默认为26,也称为慢线;SignalSMA为通过FastEMA-SlowEMA的差值做算术平均得到DIF值得参数,默认为9,即9日算术平均。

 

//----indicator buffers

double    ind_buffer1[];

double    ind_buffer2[];

double    ind_buffer3[];

double    ind_buffer4[];

double    temp;

***定义数据缓冲区数组和变量

 

int init()

  {

//----drawing settings

  SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1);

  SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,1);

  SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,1);

  SetIndexStyle(3,DRAW_HISTOGRAM,STYLE_SOLID,1);

  SetIndexDrawBegin(1,SignalSMA);

  IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+1);

***定义绘图对象线、柱和绘图模板信息

 

//----indicator buffers mapping

  if(!SetIndexBuffer(0,ind_buffer1) &&!SetIndexBuffer(1,ind_buffer2)&&!SetIndexBuffer(2,ind_buffer3)&& !SetIndexBuffer(3,ind_buffer4))

     Print("cannot set indicator buffers!");

***缓冲区数组到绘图对象的一一对应并作错误提示处理

 

//---- namefor DataWindow and indicator subwindow label

  IndicatorShortName("MACD("+FastEMA+","+SlowEMA+","+SignalSMA+")");

  SetIndexLabel(0,"MACD");

  SetIndexLabel(1,"Signal");

//----initialization done

  return(0);

  }

***定义绘图窗口的标签信息

 

int start()

  {

  int limit;

  int counted_bars=IndicatorCounted();

//----check for possible errors

  if(counted_bars<0) return(-1);

//---- lastcounted bar will be recounted

  if(counted_bars>0) counted_bars--;

  limit=Bars-counted_bars;

//---- macdcounted in the 1-st buffer

  for(int i=0; i

    ind_buffer1[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);

***limit引用MT4系统设定的最大极限K线根数的参数,ind_buffer1[i]为记录每一根K线的DIF值的数组,DIF值得计算方式为每一个K线所对应的快线取值减去慢线取值(比如DIF=FastEMA12-SlowEMA26),然后将ind_buffer1[i]数组中的所有DIF值串联起来,便绘制出来了DIF曲线。

 

//----signal line counted in the 2-nd buffer

  for(i=0; i

     ind_buffer2[i]=iMAOnArray(ind_buffer1,Bars,SignalSMA,0,MODE_SMA,i);

***ind_buffer2[i]为记录了通过ind_buffer1数组进行SignalSMA的所有取值,即为DEA。通俗说,将DIF进行SMA9简单算术平均后得到的数值就是DEA的取值,将每一根K线对应的DEA的取值顺序排列,并绘串联起来,就得到了DEA曲线。

 

  for(i=0; i

     {

      temp=1.3*(ind_buffer1[i]-ind_buffer2[i]);

***ind_buffer1代表的DIFind_buffer2代表的DEAtempDIF-DEA的取值乘以系数1.3进行差离放大的取值,这个系数通常大于1,也有的使用2作为系数。

 

      if(temp>0) {ind_buffer3[i]=temp;ind_buffer4[i]=0;}

      else      {ind_buffer3[i]=0;ind_buffer4[i]=temp;}

***ind_buffer3为出红柱的情况,ind_buffer4为出绿柱的情况,意思就是如果temp的取值大于0,那么将差值存入ind_buffer3,对应的就是所在K线的红柱;如果temp的取值小于0,那么将差值存入ind_buffer4,对应的就是所在K线的绿柱。

     }

//---- done

  return(0);

  }

 

以上是双线MACD指标算法原理的解释,适用于MT4平台可以直接使用。算法的非重要信息有删减,如果需要可以导入的完整算法,参见另一篇双线MACD指标的完整算法

 

更多MACD指标用法和MACD交易系统详解请参见http://www.macd9.com