📅 财经日历 📊 实时波动 📈 大盘云图 📶 行情走势 🆚 投机情绪 🚀 今日热点

    双线MACD - MetaTrader 5脚本

    韩延 LV1
    2025-03-10 · 884 阅读
    //+------------------------------------------------------------------+
    //|                                                  MACD(2lines).mq5|
    //|                                         Copyright 2017, fXMeter. |
    //|                            https://www.mql5.com/zh/users/fxmeter |
    //+------------------------------------------------------------------+
    //2021-10-15 12:02:34 V3.0 解决MT5在10月14日升级到Build 3080带来的问题
    //2021-10-14 18:27:15 V2.0 Updated
    //2020-2-5 15:13:49 改成MT5版本
    //MT5版本:https://www.mql5.com/zh/code/27838
    //MT4版本https://www.mql5.com/zh/code/27603
    //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   "4.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,w1=0;
    int fastHandle=-1,slowHandle=-1;
    double fast[1],slow[1];
    //+------------------------------------------------------------------+
    //| Custom indicator initialization function                         |
    //+------------------------------------------------------------------+
    int OnInit()
    {
    //--- indicator buffers mapping
       SetIndexBuffer(0,DIFBuffer);
       SetIndexBuffer(1,DEABuffer);
       SetIndexBuffer(2,MacdHistBuffer);
       SetIndexBuffer(3,MacdHistBuffer1);

       ArraySetAsSeries(DIFBuffer,true);
       ArraySetAsSeries(DEABuffer,true);
       ArraySetAsSeries(MacdHistBuffer,true);
       ArraySetAsSeries(MacdHistBuffer1,true);

       for(int i=0; i<4; i++)
       {
          PlotIndexSetDouble(i,PLOT_EMPTY_VALUE,0.0);
          PlotIndexSetInteger(i,PLOT_DRAW_BEGIN,3);
       }


       IndicatorSetInteger(INDICATOR_DIGITS,_Digits);

       string name = "MACD("+(string)FastEMA+","+(string)SlowEMA+","+(string)MACDEMA+")";
       IndicatorSetString(INDICATOR_SHORTNAME,name);

       if(FastEMA<0 || SlowEMA<0 || MACDEMA<0)
          return(INIT_FAILED);

       w = 2.0/(MACDEMA + 1);
       w1= 1.0-w;

       fastHandle = iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE);
       slowHandle = iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE);
       if(fastHandle==-1||slowHandle==-1)
       {
          return(INIT_FAILED);
       }
    //---
       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;

       if(prev_calculated<=0)
       {
          ArrayInitialize(DIFBuffer,0.0);
          ArrayInitialize(DEABuffer,0.0);
          ArrayInitialize(MacdHistBuffer,0.0);
          ArrayInitialize(MacdHistBuffer1,0.0);
       }

       for(i=limit; i>=0; i--)
       {
          if(i>=rates_total-1)
          {
             DIFBuffer = DEABuffer = MacdHistBuffer = MacdHistBuffer1 = 0;
             continue;
          }
          if(CopyBuffer(fastHandle,0,i,1,fast)!=1)return(0);
          if(CopyBuffer(slowHandle,0,i,1,slow)!=1)return(0);
          DIFBuffer=fast[0]-slow[0];
          DEABuffer=w*DIFBuffer+w1*DEABuffer[i+1];
          hst = 2.0*(DIFBuffer-DEABuffer);
          if(hst>=0)
          {
             MacdHistBuffer=hst;
             MacdHistBuffer1=0.0;
          }
          else
          {
             MacdHistBuffer1=hst;
             MacdHistBuffer=0.0;
          }

       }

    //--- return value of prev_calculated for next call
       return(rates_total);
    }
    //+------------------------------------------------------------------+
    ""
    还没有人打赏,支持一下
    回复

    举报

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

    提醒: 禁止引战、谩骂、灌水内容

    微信二维码

    有问题联系客服