chzhpfjay 发表于 2020-7-11 13:15:25

双线MACD源代码

基于国内通达信源代码改编,大家习惯了国内两条线的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_color1clrSilver
#property indicator_style1STYLE_SOLID
#property indicator_width11
//--- plot DEA
#property indicator_label2"DEA"
#property indicator_type2   DRAW_LINE
#property indicator_color2clrYellow
#property indicator_style2STYLE_SOLID
#property indicator_width21
//--- plot macd hist+
#property indicator_label3"Macd+"
#property indicator_type3   DRAW_HISTOGRAM
#property indicator_color3clrRed
#property indicator_style3STYLE_SOLID
#property indicator_width31
//--- plot macd hist-
#property indicator_label4"Macd-"
#property indicator_type4   DRAW_HISTOGRAM
#property indicator_color4clrAqua
#property indicator_style4STYLE_SOLID
#property indicator_width41


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=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
      DEABuffer=w*DIFBuffer+w1*DEABuffer;
      hst = 2.0*(DIFBuffer-DEABuffer);
      if(hst>=0)
      {
         MacdHistBuffer=hst;
         MacdHistBuffer1=EMPTY_VALUE;
      }
      else
      {
         MacdHistBuffer1=hst;
         MacdHistBuffer=EMPTY_VALUE;
      }

   }

//--- return value of prev_calculated for next call
   return(rates_total);
}
//+------------------------------------------------------------------+


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

{:1_179:}

hhxuabcj 发表于 2020-12-26 18:17:38

顶下
页: [1] 2 3 4 5 6 7
查看完整版本: 双线MACD源代码