前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >在MT4上使用双线MACD指标源码

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

作者头像
全栈程序员站长
发布2022-08-23 18:50:03
5810
发布2022-08-23 18:50:03
举报

大家好,又见面了,我是你们的朋友全栈君。

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

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

效果如下:

在这里插入图片描述
在这里插入图片描述

源代码:

代码语言:javascript
复制
#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);

  }

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

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/139285.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年5月7,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档