在MT4上使用双线MACD指标源码

MACD指标是股票交易中经典的一款技术分析指标,该指标由两条曲线和柱线组成。

基本用法:MACD金叉:DIFF由下向上突破DEA,为买入信号。MACD死叉:DIFF由上向下突破DEA,为卖出信号。MACD绿转红:MACD值由负变正,市场由空头转为多头。MACD红转绿:MACD值正转负,市场多头转空头。DIFF与DEA均为正值,即都在零轴线以上时,大势属于多头市场,DIFF向上突破DEA,可以做买入信号。DIFF与DEA均为负值,即都在零轴线以下时,大势属于空头市场,DIFF向下跌破DEA,可做卖出信号。DEA在盘整局面失误率高,配合RSI及KDJ指标可以适当弥补缺点。

效果如下:
在这里插入图片描述
源代码:

#property copyright "Copyright 2021,EATrader."

#property link      "https://www.mql5.com/"

#property description  "关注公众号:从零开始学EA交易"

                       "n微信号:XQH20200705"

#property icon      "\Images\001.ico"

#property version   "1.00"

#property strict

#property indicator_separate_window

#property indicator_buffers 4

#property indicator_plots   4

//--- plot DIF

#property indicator_label1  "DIF"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrSilver

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot DEA

#property indicator_label2  "DEA"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrYellow

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot macd hist+

#property indicator_label3  "Macd+"

#property indicator_type3   DRAW_HISTOGRAM

#property indicator_color3  clrRed

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- plot macd hist-

#property indicator_label4  "Macd-"

#property indicator_type4   DRAW_HISTOGRAM

#property indicator_color4  clrAqua

#property indicator_style4  STYLE_SOLID

#property indicator_width4  1





input int FastEMA = 12;

input int SlowEMA = 26;

input int MACDEMA = 9;





//--- indicator buffers

double         DIFBuffer[];

double         DEABuffer[];

double         MacdHistBuffer[];

double         MacdHistBuffer1[];

double w=0;

double w1=0;

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,DIFBuffer);

   SetIndexBuffer(1,DEABuffer);

   SetIndexBuffer(2,MacdHistBuffer);

   SetIndexBuffer(3,MacdHistBuffer1);



   SetIndexEmptyValue(2,EMPTY_VALUE);

   SetIndexEmptyValue(3,EMPTY_VALUE);



   for(int i=0; i<4; i++)

      SetIndexDrawBegin(i,SlowEMA+MACDEMA);



   IndicatorDigits(Digits);



   IndicatorShortName("MACD("+(string)FastEMA+","+(string)SlowEMA+","+(string)MACDEMA+")");



   if(FastEMA<0 || SlowEMA<0 || MACDEMA<0)

      return(INIT_FAILED);



   w = 2.0/(MACDEMA + 1);

   w1= 1.0-w;



//---

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Custom indicator iteration function                              |

//+------------------------------------------------------------------+

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

  {

//---

   int i,limit=0;

   if(rates_total<=0) return(0);

   if(prev_calculated<=0) limit=rates_total-1;

   else limit=rates_total-prev_calculated+1;

   double hst=0.0;

   for(i=limit; i>=0; i--)

     {

      if(i==rates_total-1) continue;

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

      DEABuffer[i]=w*DIFBuffer[i]+w1*DEABuffer[i+1];

      hst = 2.0*(DIFBuffer[i]-DEABuffer[i]);

      if(hst>=0)

        {

         MacdHistBuffer[i]=hst;

         MacdHistBuffer1[i]=EMPTY_VALUE;

        }

      else

        {

         MacdHistBuffer1[i]=hst;

         MacdHistBuffer[i]=EMPTY_VALUE;

        }



     }



//--- return value of prev_calculated for next call

   return(rates_total);

  }

//+------------------------------------------------------------------+

工欲善其事,必先利其器,交易最重要的是遵守规则,严格执行。关注公众号,学习MQL入门到精通EA教程,学习更多EA编程,畅写属于自己的EA,锻造属于自己的神兵利器。
在这里插入图片描述

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>