m1800 发表于 2026-1-28 19:31:31

【漂亮的MT5双线MACD指标[源码]】双色交叉变色

最后由 m1800 于 2026-1-28 19:34 编辑

漂亮的MT5双线MACD指标,交叉变色.附源码.

多多点赞,会变好看,多多留言,会变有钱.



//+------------------------------------------------------------------+
//|                                                   MACDDouble.mq5 |
//|                   Copyright 2009-2026, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright   "2009-2026, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property description "Moving Average of Oscillator Double Line"
#property description "MACD Double Line"
#include <MovingAverages.mqh>
//--- indicator settings
#property indicator_separate_window
#property indicator_buffers 7
#property indicator_plots   3
#property indicator_label1"MACD"
#property indicator_type1   DRAW_COLOR_HISTOGRAM
#property indicator_color1clrLime,clrRed
#property indicator_width13

#property indicator_label2"DIF"
#property indicator_type2   DRAW_COLOR_LINE
#property indicator_color2clrDodgerBlue,clrMagenta
#property indicator_style2STYLE_SOLID
#property indicator_width22

#property indicator_label3"DEA"
#property indicator_type3   DRAW_LINE
#property indicator_color3clrYellow
#property indicator_style3STYLE_DOT
#property indicator_width31


//--- input parameters
input int                InpFastEMAPeriod = 12;       // Fast EMA period
input int                InpSlowEMAPeriod = 26;       // Slow EMA period
input int                InpSignalSMAPeriod = 9;      // Signal SMA period
input ENUM_APPLIED_PRICE InpAppliedPrice = PRICE_CLOSE; // Applied price
//--- indicator buffers
double                   ExtMACDBuffer[];
double                   ExtMACDColorBuffer[];
double                   ExtDifBuffer[];
double                   ExtDifColorBuffer[];
double                   ExtDeaBuffer[];
double                   ExtSignalBuffer[];
double                   ExtFastMaBuffer[];
double                   ExtSlowMaBuffer[];
//--- MA handles
int                      ExtFastMaHandle;
int                      ExtSlowMaHandle;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
{
//--- indicator buffers mapping
   SetIndexBuffer(0, ExtMACDBuffer, INDICATOR_DATA);
   SetIndexBuffer(1, ExtMACDColorBuffer, INDICATOR_COLOR_INDEX);
   SetIndexBuffer(2, ExtDifBuffer, INDICATOR_DATA);
   SetIndexBuffer(3, ExtDifColorBuffer, INDICATOR_COLOR_INDEX);
   SetIndexBuffer(4, ExtDeaBuffer, INDICATOR_DATA);
   SetIndexBuffer(5, ExtFastMaBuffer, INDICATOR_CALCULATIONS);
   SetIndexBuffer(6, ExtSlowMaBuffer, INDICATOR_CALCULATIONS);
   IndicatorSetInteger(INDICATOR_DIGITS, _Digits + 2);
//--- sets first bar from what index will be drawn
   PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, InpSlowEMAPeriod + InpSignalSMAPeriod - 2);
//--- name for DataWindow and indicator subwindow label
   IndicatorSetString(INDICATOR_SHORTNAME, "MACD Double Line(" + string(InpFastEMAPeriod) + "," + string(InpSlowEMAPeriod) + "," + string(InpSignalSMAPeriod) + ")");
   PlotIndexSetString(0, PLOT_LABEL, "MACD");
   PlotIndexSetString(1, PLOT_LABEL, "DIF");
   PlotIndexSetString(2, PLOT_LABEL, "DEA");
//--- get MAs handles
   ExtFastMaHandle = iMA(NULL, 0, InpFastEMAPeriod, 0, MODE_EMA, InpAppliedPrice);
   ExtSlowMaHandle = iMA(NULL, 0, InpSlowEMAPeriod, 0, MODE_EMA, InpAppliedPrice);
//--- initialization done
}
//+------------------------------------------------------------------+
//|Moving Average of Oscillator                                    |
//+------------------------------------------------------------------+
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[])
{
   if(rates_total < InpSignalSMAPeriod)
      return(0);
//--- not all data may be calculated
   int calculated = BarsCalculated(ExtFastMaHandle);
   if(calculated < rates_total)
   {
      Print("Not all data of ExtFastMaHandle is calculated (", calculated, "bars ). Error", GetLastError());
      return(0);
   }
   calculated = BarsCalculated(ExtSlowMaHandle);
   if(calculated < rates_total)
   {
      Print("Not all data of ExtSlowMaHandle is calculated (", calculated, "bars ). Error", GetLastError());
      return(0);
   }
//--- we can copy not all data
   int to_copy;
   if(prev_calculated > rates_total || prev_calculated < 0)
      to_copy = rates_total;
   else
   {
      to_copy = rates_total - prev_calculated;
      if(prev_calculated > 0)
         to_copy++;
   }
//--- get Fast EMA buffer
   if(IsStopped())
      return(0); //Checking for stop flag
   if(CopyBuffer(ExtFastMaHandle, 0, 0, to_copy, ExtFastMaBuffer) <= 0)
   {
      Print("Getting fast EMA is failed! Error", GetLastError());
      return(0);
   }
//--- get SlowSMA buffer
   if(IsStopped())
      return(0); //Checking for stop flag
   if(CopyBuffer(ExtSlowMaHandle, 0, 0, to_copy, ExtSlowMaBuffer) <= 0)
   {
      Print("Getting slow SMA is failed! Error", GetLastError());
      return(0);
   }
//---
   int i, limit;
   if(prev_calculated == 0)
      limit = 0;
   else
      limit = prev_calculated - 1;
//--- the main loop of calculations
   for(i = limit; i < rates_total; i++)
   {
      //--- calculate Dif
      ExtDifBuffer = ExtFastMaBuffer - ExtSlowMaBuffer;
   }
//--- calculate Signal
   SimpleMAOnBuffer(rates_total, prev_calculated, 0, InpSignalSMAPeriod, ExtDifBuffer, ExtDeaBuffer);
   for(i = limit; i < rates_total; i++)
   {
      if(i == 0)
      {
         ExtDifColorBuffer = 0;
      }
      else
         if(ExtDifBuffer > ExtDeaBuffer)
         {
            ExtDifColorBuffer = 0;
         }
         else
         {
            ExtDifColorBuffer = 1;
         }
   }
//--- calculate MACD
   for(i = limit; i < rates_total && !IsStopped(); i++)
   {
      ExtMACDBuffer = (ExtDifBuffer - ExtDeaBuffer) * 2;
      if(i == 0)
      {
         ExtMACDColorBuffer = 0;
      }
      else
         if(ExtMACDBuffer > ExtMACDBuffer)
         {
            ExtMACDColorBuffer = 0;
         }
         else
         {
            ExtMACDColorBuffer = 1;
         }
   }
//--- OnCalculate done. Return new prev_calculated.
   return(rates_total);
}
//+------------------------------------------------------------------+



peterzhu2004 发表于 2026-1-29 05:54:33

最喜欢源代码,谢谢分享

1594135666 发表于 2026-1-29 06:19:30

多多留言,会变有钱 这句话让我不得不留言[呲牙]

绿野游狼 发表于 2026-1-29 12:09:51

源码也不会编译啊?大家知道能自动编程的地方不?

dayu1362007 发表于 2026-1-29 14:23:17

最喜欢源代码,谢谢分享大家知道能自动编程

dengzehua 发表于 2026-1-30 09:06:49

多多点赞

漫步者 发表于 2026-1-30 10:31:12

好看,不错

hehaigm 发表于 2026-1-31 09:40:34

最喜欢源代码,谢谢分享。。。。。

xhvvvv 发表于 2026-2-2 17:54:34

这玩意有用吗

dhy385 发表于 2026-2-3 18:24:38

这个我需要

q570644129 发表于 2026-2-4 03:47:36

多多留言,会变有钱 这句话让我不得不留言,主卧一飞冲天吧

1370367744 发表于 2026-2-7 21:04:17

点赞,谢谢分享

islove 发表于 2026-2-8 00:09:23

真的很棒哦,点赞,谢谢分享

1612471379 发表于 2026-2-21 10:17:12

留言学习在火火

tcjyy 发表于 2026-2-25 10:49:38

源代码分享,感谢,努力学习中

peterzhu2004 发表于 7 天前

复制代码重新做了一个指标,赞

1594135666 发表于 6 天前

谢谢源码

chaoyi996 发表于 4 天前

十分感谢,已下载

snowbuyi 发表于 前天 14:26

怎么加载   谁能教一下

轩轩 发表于 前天 15:07

这个可以
页: [1] 2
查看完整版本: 【漂亮的MT5双线MACD指标[源码]】双色交叉变色