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);
}
最后运行效果如下:
文华的效果图如下:
嗯,走到这儿,如何写一个简单的指标就基本清晰了,特别是针对非价格的数据的均值计算啥的,之前卡在这儿好一会才找到方法。每个变量是怎样画出来的,线宽多少,颜色几何都很明确。