MQL5学习之MACD的编写

MACD用通达信公式写起来很简单:

DIF:EMA(CLOSE,SHORT)-EMA(CLOSE,LONG);
DEA:EMA(DIF,MID);
MACD:(DIF-DEA)*2,COLORSTICK;

简单来讲即DIF为快线-慢线的差值,DEA为DIF的EMA均线,MACD即为DIF与其均线的差值。

关键代码讲解

这段表示指标缓存有几个,要画几个对应的变量,一般前面的大于等于后面的,注意,如果你要画3个,indicator_plots设成3,如果画两个,则是设成2,如果这儿设成2的时候,你其它东西再怎么设置成123或是更多,也只能画两个指标

#property indicator_buffers 3
#property indicator_plots   3

这段代码分别表示依次显示macd, dif, dea三种不同的变量

   SetIndexBuffer(0,ExtMacdBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,ExtDifBuffer,INDICATOR_DATA);
   SetIndexBuffer(2,ExtDeaBuffer,INDICATOR_DATA);

这段代码表示三种不同变量表现的样式,第一种为上下柱形,第二,三种都为线型

#property indicator_type1   DRAW_HISTOGRAM
#property indicator_type2   DRAW_LINE
#property indicator_type3   DRAW_LINE

这段代码表示三种不同的颜色,分别为灰,白,黄

#property indicator_color1  Silver
#property indicator_color2  White
#property indicator_color3  clrYellowGreen

这段代码表达的为三种图案的宽度,分别为3, 1, 1

#property indicator_width1  3
#property indicator_width2  1
#property indicator_width3  1

这段代码为设定的5个全局变量,都为数组,其中三个将会用图形的形式画出来

double ExtMacdBuffer[]; // macd
double ExtDifBuffer[];  // dif
double ExtDeaBuffer[];  // dea
double ExtFastMaBuffer[];
double ExtSlowMaBuffer[];

这段代码表达出变量和图形的关系,对应前面的即为1, 2, 3的关系

   //--- indicator buffers mapping
   SetIndexBuffer(0,ExtMacdBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,ExtDifBuffer,INDICATOR_DATA);
   SetIndexBuffer(2,ExtDeaBuffer,INDICATOR_DATA);

这段代码获取价格的均值句柄,便于后面拷贝到对应的buffer中

   ExtFastMaHandle=iMA(NULL,0,InpFastEMA,0,MODE_EMA,InpAppliedPrice);
   ExtSlowMaHandle=iMA(NULL,0,InpSlowEMA,0,MODE_EMA,InpAppliedPrice);

这段代码将均线句柄中的数据拷贝到数组中,需要注意的是,这种方式仅限于价格的高开低收的均值 ,若是类似于dif(即不是价格相关的)均值,则需要手动计算

   if(!CopyBuffer(ExtFastMaHandle,0,0,to_copy,ExtFastMaBuffer) || !CopyBuffer(ExtSlowMaHandle,0,0,to_copy,ExtSlowMaBuffer))
     {
      Print("Getting EMA buffer failed! Error ",GetLastError());
      return(0);
     }

这段代码即为调用函数计算非价格的均值

SimpleMAOnBuffer(rates_total,prev_calculated,0,InpSignalSMA,ExtDifBuffer,ExtDeaBuffer);

整体的代码量也很小,全部贴出来

//+------------------------------------------------------------------+
//|                                                         MACD.mq5 |
//|                             Copyright 2000-2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright   "Copyright 2000-2024, MetaQuotes Ltd."
#property link        "https://www.mql5.com"
#property description "Moving Average Convergence/Divergence"
#include <MovingAverages.mqh>
//--- indicator settings
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_plots   3

#property indicator_type1   DRAW_HISTOGRAM
#property indicator_type2   DRAW_LINE
#property indicator_type3   DRAW_LINE
#property indicator_color1  Silver
#property indicator_color2  White
#property indicator_color3  clrYellowGreen
#property indicator_width1  3
#property indicator_width2  1
#property indicator_width3  1
#property indicator_label1  "MACD"
#property indicator_label2  "Dif"
#property indicator_label3  "Dea"
//--- input parameters
input int                InpFastEMA=12;               // Fast EMA period
input int                InpSlowEMA=26;               // Slow EMA period
input int                InpSignalSMA=9;              // Signal SMA period
input ENUM_APPLIED_PRICE InpAppliedPrice=PRICE_CLOSE; // Applied price
//--- indicator buffers
double ExtMacdBuffer[]; // macd
double ExtDifBuffer[];  // dif
double ExtDeaBuffer[];  // dea
double ExtFastMaBuffer[];
double ExtSlowMaBuffer[];

int    ExtFastMaHandle;
int    ExtSlowMaHandle;
int OnInit()
  {
   //--- indicator buffers mapping
   SetIndexBuffer(0,ExtMacdBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,ExtDifBuffer,INDICATOR_DATA);
   SetIndexBuffer(2,ExtDeaBuffer,INDICATOR_DATA);
  
   //--- name for indicator subwindow label
   string short_name=StringFormat("MACD(%d,%d,%d)",InpFastEMA,InpSlowEMA,InpSignalSMA);
   IndicatorSetString(INDICATOR_SHORTNAME,short_name);
   //--- get MA handles
   ExtFastMaHandle=iMA(NULL,0,InpFastEMA,0,MODE_EMA,InpAppliedPrice);
   ExtSlowMaHandle=iMA(NULL,0,InpSlowEMA,0,MODE_EMA,InpAppliedPrice);
   return 0;
  }

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<InpSignalSMA)
      return(0);
   int to_copy = rates_total;
   if(!CopyBuffer(ExtFastMaHandle,0,0,to_copy,ExtFastMaBuffer) || !CopyBuffer(ExtSlowMaHandle,0,0,to_copy,ExtSlowMaBuffer))
     {
      Print("Getting EMA buffer failed! Error ",GetLastError());
      return(0);
     }

   for(int i=0; i<rates_total && !IsStopped(); i++)
      {
         ExtDifBuffer[i]=ExtFastMaBuffer[i]-ExtSlowMaBuffer[i];
      }
   
   SimpleMAOnBuffer(rates_total,prev_calculated,0,InpSignalSMA,ExtDifBuffer,ExtDeaBuffer);
   for(int i=0; i<rates_total && !IsStopped(); i++)
       ExtMacdBuffer[i]=(ExtDifBuffer[i]-ExtDeaBuffer[i])*2;
       
   return(rates_total);
  }

最后运行效果如下:

文华的效果图如下:

嗯,走到这儿,如何写一个简单的指标就基本清晰了,特别是针对非价格的数据的均值计算啥的,之前卡在这儿好一会才找到方法。每个变量是怎样画出来的,线宽多少,颜色几何都很明确。

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MQL4是一种用于MetaTrader 4平台的编程语言,它可以用来创建自定义交易指标、脚本和专家顾问。MACD(Moving Average Convergence Divergence)是一种技术分析指标,用于显示价格趋势的变化。 在MQL4中,我们可以使用一些内置函数来计算MACD指标。首先,我们需要使用iMACD函数来计算MACD线、信号线和直方图。iMACD函数接受四个参数:Symbol,时间帧,快线周期和慢线周期。我们可以将这些参数设置为我们想要的值。 计算MACD指标后,我们可以使用iMACDOnArray函数来获取MACD指标的历史数据。该函数接受一个包含指标数据的数组作为参数,并返回MACD线、信号线和直方图的值。 高级的MACD策略可能涉及到不同时间帧的交叉验证和其他技术分析指标的结合。我们可以使用iMACross函数来检测两条移动平均线的交叉,并在交叉出现时执行相应的操作。另外,我们还可以使用其他MQL4内置函数来计算移动平均线、布林带等指标。 在编写MQL4 MACD高级策略时,我们还应该考虑到风险管理和资金管理。我们可以设置止损和获利目标,以确保交易具有适当的风险和回报比。这可以通过使用OrderSend函数来下单,并使用OrderClose函数来平仓。 总之,MQL4是用于MetaTrader 4平台的编程语言,可以用于开发高级的MACD交易策略。我们可以使用内置函数来计算MACD指标,并结合其他技术分析指标来制定有效的交易规则。此外,风险管理和资金管理也是重要的考虑因素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

永远的麦田

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值