67评论

0收藏

双线MACD源代码

avatar chzhpfjay | 12040 人阅读 | 67 人评论 | 2020-07-11

基于国内通达信源代码改编,大家习惯了国内两条线的MACD,MT4自带的只有一条。


//+------------------------------------------------------------------+
//|                                                     MACD2017.mq4 |
//|                                         Copyright 2017, fXMeter. |
//|                            https://www.mql5.com/zh/users/fxmeter |
//+------------------------------------------------------------------+
//2017-04-27 13:54:38 按照国内股票软件通达信中的MACD公式编写
/*
SHORT=12,LONG=26,MID=9
DIF:EMA(CLOSE,SHORT)-EMA(CLOSE,LONG);
DEA:EMA(DIF,MID);
MACD:(DIF-DEA)*2,COLORSTICK;

说明:
国内把 DIF-DEA叫MACD(画成柱子)
而实际上国际上,包括MT4的内置公式中都是把DIF叫MACD(画成柱子),把DEA叫Signal,没有(DIF-DEA)*2
颜色按照通达信MCAD配置
*/
#property copyright "Copyright 2017,fxMeter."
#property link      "https://www.mql5.com/zh/users/fxmeter"
#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);
  }
//+------------------------------------------------------------------+


""
还没有人打赏,支持一下

评论|共 67 个

hdw123456

发表于 2020-7-11 13:43:57 来自手机 | 显示全部楼层

多少条线都没用滞后到你烦

茗茗猪

发表于 2020-11-15 16:41:09 | 显示全部楼层

支持下

nhdviwut

发表于 2020-11-23 18:43:47 | 显示全部楼层

顶下

懒驴

发表于 2020-12-12 21:43:48 | 显示全部楼层

支持下

李建玉儿

发表于 2020-12-15 13:53:20 | 显示全部楼层

谢谢

郭刁查弦

发表于 2020-12-16 16:06:04 | 显示全部楼层

支持下

莉达思汀xo

发表于 2020-12-16 21:59:59 | 显示全部楼层

支持下

丸子

发表于 2020-12-18 10:00:56 | 显示全部楼层

hhxuabcj

发表于 2020-12-26 18:17:38 | 显示全部楼层

顶下

您需要登录后才可以回帖 登录 | 注册 微信登录

EA之家评论守则