设为首页 收藏本站 切换语言 切换语言

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

| 发表于 昨天 19:31 | 显示全部楼层 |复制链接
最后由 m1800 于 2026-1-28 19:34 编辑

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

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

屏幕截图 2026-01-28 192544.png
屏幕截图 2026-01-28 192630.png
  1. //+------------------------------------------------------------------+
  2. //|                                                   MACDDouble.mq5 |
  3. //|                   Copyright 2009-2026, MetaQuotes Software Corp. |
  4. //|                                              http://www.mql5.com |
  5. //+------------------------------------------------------------------+
  6. #property copyright   "2009-2026, MetaQuotes Software Corp."
  7. #property link        "http://www.mql5.com"
  8. #property description "Moving Average of Oscillator Double Line"
  9. #property description "MACD Double Line"
  10. #include <MovingAverages.mqh>
  11. //--- indicator settings
  12. #property indicator_separate_window
  13. #property indicator_buffers 7
  14. #property indicator_plots   3
  15. #property indicator_label1  "MACD"
  16. #property indicator_type1   DRAW_COLOR_HISTOGRAM
  17. #property indicator_color1  clrLime,clrRed
  18. #property indicator_width1  3
  19. #property indicator_label2  "DIF"
  20. #property indicator_type2   DRAW_COLOR_LINE
  21. #property indicator_color2  clrDodgerBlue,clrMagenta
  22. #property indicator_style2  STYLE_SOLID
  23. #property indicator_width2  2
  24. #property indicator_label3  "DEA"
  25. #property indicator_type3   DRAW_LINE
  26. #property indicator_color3  clrYellow
  27. #property indicator_style3  STYLE_DOT
  28. #property indicator_width3  1
  29. //--- input parameters
  30. input int                InpFastEMAPeriod = 12;       // Fast EMA period
  31. input int                InpSlowEMAPeriod = 26;       // Slow EMA period
  32. input int                InpSignalSMAPeriod = 9;      // Signal SMA period
  33. input ENUM_APPLIED_PRICE InpAppliedPrice = PRICE_CLOSE; // Applied price
  34. //--- indicator buffers
  35. double                   ExtMACDBuffer[];
  36. double                   ExtMACDColorBuffer[];
  37. double                   ExtDifBuffer[];
  38. double                   ExtDifColorBuffer[];
  39. double                   ExtDeaBuffer[];
  40. double                   ExtSignalBuffer[];
  41. double                   ExtFastMaBuffer[];
  42. double                   ExtSlowMaBuffer[];
  43. //--- MA handles
  44. int                      ExtFastMaHandle;
  45. int                      ExtSlowMaHandle;
  46. //+------------------------------------------------------------------+
  47. //| Custom indicator initialization function                         |
  48. //+------------------------------------------------------------------+
  49. void OnInit()
  50.   {
  51. //--- indicator buffers mapping
  52.    SetIndexBuffer(0, ExtMACDBuffer, INDICATOR_DATA);
  53.    SetIndexBuffer(1, ExtMACDColorBuffer, INDICATOR_COLOR_INDEX);
  54.    SetIndexBuffer(2, ExtDifBuffer, INDICATOR_DATA);
  55.    SetIndexBuffer(3, ExtDifColorBuffer, INDICATOR_COLOR_INDEX);
  56.    SetIndexBuffer(4, ExtDeaBuffer, INDICATOR_DATA);
  57.    SetIndexBuffer(5, ExtFastMaBuffer, INDICATOR_CALCULATIONS);
  58.    SetIndexBuffer(6, ExtSlowMaBuffer, INDICATOR_CALCULATIONS);
  59.    IndicatorSetInteger(INDICATOR_DIGITS, _Digits + 2);
  60. //--- sets first bar from what index will be drawn
  61.    PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, InpSlowEMAPeriod + InpSignalSMAPeriod - 2);
  62. //--- name for DataWindow and indicator subwindow label
  63.    IndicatorSetString(INDICATOR_SHORTNAME, "MACD Double Line(" + string(InpFastEMAPeriod) + "," + string(InpSlowEMAPeriod) + "," + string(InpSignalSMAPeriod) + ")");
  64.    PlotIndexSetString(0, PLOT_LABEL, "MACD");
  65.    PlotIndexSetString(1, PLOT_LABEL, "DIF");
  66.    PlotIndexSetString(2, PLOT_LABEL, "DEA");
  67. //--- get MAs handles
  68.    ExtFastMaHandle = iMA(NULL, 0, InpFastEMAPeriod, 0, MODE_EMA, InpAppliedPrice);
  69.    ExtSlowMaHandle = iMA(NULL, 0, InpSlowEMAPeriod, 0, MODE_EMA, InpAppliedPrice);
  70. //--- initialization done
  71.   }
  72. //+------------------------------------------------------------------+
  73. //|  Moving Average of Oscillator                                    |
  74. //+------------------------------------------------------------------+
  75. int OnCalculate(const int rates_total,
  76.                 const int prev_calculated,
  77.                 const datetime &time[],
  78.                 const double &open[],
  79.                 const double &high[],
  80.                 const double &low[],
  81.                 const double &close[],
  82.                 const long &tick_volume[],
  83.                 const long &volume[],
  84.                 const int &spread[])
  85.   {
  86.    if(rates_total < InpSignalSMAPeriod)
  87.       return(0);
  88. //--- not all data may be calculated
  89.    int calculated = BarsCalculated(ExtFastMaHandle);
  90.    if(calculated < rates_total)
  91.      {
  92.       Print("Not all data of ExtFastMaHandle is calculated (", calculated, "bars ). Error", GetLastError());
  93.       return(0);
  94.      }
  95.    calculated = BarsCalculated(ExtSlowMaHandle);
  96.    if(calculated < rates_total)
  97.      {
  98.       Print("Not all data of ExtSlowMaHandle is calculated (", calculated, "bars ). Error", GetLastError());
  99.       return(0);
  100.      }
  101. //--- we can copy not all data
  102.    int to_copy;
  103.    if(prev_calculated > rates_total || prev_calculated < 0)
  104.       to_copy = rates_total;
  105.    else
  106.      {
  107.       to_copy = rates_total - prev_calculated;
  108.       if(prev_calculated > 0)
  109.          to_copy++;
  110.      }
  111. //--- get Fast EMA buffer
  112.    if(IsStopped())
  113.       return(0); //Checking for stop flag
  114.    if(CopyBuffer(ExtFastMaHandle, 0, 0, to_copy, ExtFastMaBuffer) <= 0)
  115.      {
  116.       Print("Getting fast EMA is failed! Error", GetLastError());
  117.       return(0);
  118.      }
  119. //--- get SlowSMA buffer
  120.    if(IsStopped())
  121.       return(0); //Checking for stop flag
  122.    if(CopyBuffer(ExtSlowMaHandle, 0, 0, to_copy, ExtSlowMaBuffer) <= 0)
  123.      {
  124.       Print("Getting slow SMA is failed! Error", GetLastError());
  125.       return(0);
  126.      }
  127. //---
  128.    int i, limit;
  129.    if(prev_calculated == 0)
  130.       limit = 0;
  131.    else
  132.       limit = prev_calculated - 1;
  133. //--- the main loop of calculations
  134.    for(i = limit; i < rates_total; i++)
  135.      {
  136.       //--- calculate Dif
  137.       ExtDifBuffer[i] = ExtFastMaBuffer[i] - ExtSlowMaBuffer[i];
  138.      }
  139. //--- calculate Signal
  140.    SimpleMAOnBuffer(rates_total, prev_calculated, 0, InpSignalSMAPeriod, ExtDifBuffer, ExtDeaBuffer);
  141.    for(i = limit; i < rates_total; i++)
  142.      {
  143.       if(i == 0)
  144.         {
  145.          ExtDifColorBuffer[i] = 0;
  146.         }
  147.       else
  148.          if(ExtDifBuffer[i] > ExtDeaBuffer[i])
  149.            {
  150.             ExtDifColorBuffer[i] = 0;
  151.            }
  152.          else
  153.            {
  154.             ExtDifColorBuffer[i] = 1;
  155.            }
  156.      }
  157. //--- calculate MACD
  158.    for(i = limit; i < rates_total && !IsStopped(); i++)
  159.      {
  160.       ExtMACDBuffer[i] = (ExtDifBuffer[i] - ExtDeaBuffer[i]) * 2;
  161.       if(i == 0)
  162.         {
  163.          ExtMACDColorBuffer[i] = 0;
  164.         }
  165.       else
  166.          if(ExtMACDBuffer[i] > ExtMACDBuffer[i - 1])
  167.            {
  168.             ExtMACDColorBuffer[i] = 0;
  169.            }
  170.          else
  171.            {
  172.             ExtMACDColorBuffer[i] = 1;
  173.            }
  174.      }
  175. //--- OnCalculate done. Return new prev_calculated.
  176.    return(rates_total);
  177.   }
  178. //+------------------------------------------------------------------+
复制代码
filetype

MACDDouble.ex5

14.66 KB, 下载次数: 1, 下载积分: 活跃度 -5  [下载]

评分
  • 1
  • 2
  • 3
  • 4
  • 5
平均分:NAN    参与人数:0    我的评分:未评 下载时遇到问题?
举报

评论 使用道具

精彩评论4

peterzhu2004
DDD
| 发表于 8 小时前 | 显示全部楼层
最喜欢源代码,谢谢分享
举报

点赞 评论 使用道具

1594135666
DDD
| 发表于 8 小时前 | 显示全部楼层
多多留言,会变有钱 这句话让我不得不留言
举报

点赞 评论 使用道具

绿野游狼
D
| 发表于 2 小时前 | 显示全部楼层
源码也不会编译啊?大家知道能自动编程的地方不?
举报

点赞 评论 使用道具

dayu1362007
D
| 发表于 25 分钟前 | 显示全部楼层
最喜欢源代码,谢谢分享大家知道能自动编程
举报

点赞 评论 使用道具

发新帖
EA交易
您需要登录后才可以评论 登录 | 立即注册

广告位
 简体中文国旗 简体中文
 繁體中文国旗 繁體中文
 English国旗 English(英语)
 日本語国旗 日本語(日语)
 Deutsch国旗 Deutsch(德语)
 Русский язык国旗 Русский язык(俄语)
 بالعربية国旗 بالعربية(阿拉伯语)
 Türkçe国旗 Türkçe(土耳其语)
 Português国旗 Português(葡萄牙语)
 ภาษาไทย国旗 ภาษาไทย(泰国语)
 한어国旗 한어(朝鲜语/韩语)
 Français国旗 Français(法语)
翻译