指标名称:MACD趋势带
版本:MT4 ver. 2.01
MACD 趋势带指标是一种基于差离移动平均线(MACD)的交易辅助工具,旨在帮助用户识别市场的主要趋势(上涨或下跌)。该指标利用两个不同周期的EMA(指数移动平均线)与信号线的关系,动态计算出市场的买卖信号,并以图形形式在图表中展示。
以下展示与原始MACD的区别:
参数:
部分代码展示:
//+——————————————————————+
//| MACD 趋势带.mq4 |
//| Copyright © 2009-2024, http://www.QChaos.com |
//| https://www.qchaos.com/ |
//+——————————————————————+
property copyright "Copyright © 量化混沌, http://www.qchaos.com"
property link "https://www.qchaos.com"
property version "2.01"
property description "———————————————"
property description "EA、指标公式分享"
property description "EA、指标编写业务承接"
property description "———————————————"
property description "更多资源,关注公众号:量化程序"
property description "微 信:QChaos001"
property description "手机号:134-8068-5281"
property description "———————————————"
//——————————————————————
property indicator_separate_window
property indicator_buffers 6
property indicator_color1 LimeGreen
property indicator_color2 Red
property indicator_color3 Aqua
property indicator_color4 OrangeRed
property indicator_color5 Aqua
property indicator_color6 OrangeRed
property indicator_width1 2
property indicator_width2 0
property indicator_width3 1
property indicator_width4 1
property indicator_width5 0
property indicator_width6 0
property indicator_levelcolor DarkSlateGray
//
extern double FastEMA = 36; // 设置快速EMA的周期,默认为36
extern double SlowEMA = 104; // 设置慢速EMA的周期,默认为104
extern double SignalEMA = 36; // 设置信号线的周期,默认为36
extern int Price = PRICE_CLOSE; // 设置用于计算MACD的价格类型,默认为收盘价(PRICE_CLOSE)
extern string note = "turn on Alert = true; turn off = false"; // 提示信息,开启或关闭警报
extern bool alertsOn = true; // 是否启用警报,默认为启用(true)
extern bool alertsOnCurrent = true; // 是否对当前柱进行警报,默认为启用(true)
extern bool alertsMessage = true; // 是否显示警报信息,默认为显示(true)
extern bool alertsSound = true; // 是否播放声音警报,默认为启用(true)
extern bool alertsEmail = false; // 是否发送邮件警报,默认为不启用(false)
extern string soundfile = "alert2.wav"; // 设置警报声音文件,默认为"alert2.wav"
extern int BarsToCount = 300; // 设置计算的最大历史K线数,默认为300
extern bool ShowTape = true; // 是否显示趋势带,默认为显示(true)
extern color TapeColorUp = Green; // 上升趋势带的颜色,默认为绿色(Green)
extern color TapeColorDown = Red; // 下降趋势带的颜色,默认为红色(Red)
extern int TapeBarsWidth = 1; // 设置趋势带的线宽,默认为1
extern string UniqueID = "MACD 趋势带"; // 设置唯一标识符,默认为"MACD 趋势带"
double macd[];
double signal[];
double upDot[];
double dnDot[];
double arrUp[];
double arrDn[];
double trend1[];
double trend2[];
string shortName;
int Window;
int timeFrame;
string indicatorFileName;
bool returnBars;
//+——————————————————————+
//| |
//+——————————————————————+
int init()
{
IndicatorBuffers(8);
SetIndexBuffer(0,macd);
SetIndexBuffer(1,signal);
SetIndexBuffer(2,upDot);
SetIndexStyle(2,DRAW_ARROW);
SetIndexArrow(2,108);
SetIndexBuffer(3,dnDot);
SetIndexStyle(3,DRAW_ARROW);
SetIndexArrow(3,108);
SetIndexBuffer(4,arrUp);
SetIndexStyle(4,DRAW_ARROW);
SetIndexArrow(4,89);
SetIndexBuffer(5,arrDn);
SetIndexStyle(5,DRAW_ARROW);
SetIndexArrow(5,89);
SetIndexBuffer(6,trend1);
SetIndexBuffer(7,trend2);
SetLevelValue(0,0);
BarsToCount = MathMax(BarsToCount,300);
shortName = UniqueID+" ("+DoubleToStr(FastEMA,2)+","+DoubleToStr(SlowEMA,2)+","+DoubleToStr(SignalEMA,2)+")";
IndicatorShortName(shortName);
return(0);
}
//+——————————————————————+
//| |
//+——————————————————————+
int deinit()
{
DeleteTape();
return(0);
}
//+——————————————————————+
//| |
//+——————————————————————+
int start()
{
int counted_bars = IndicatorCounted();
if(counted_bars < 0)
return(-1);
if(counted_bars > 0)
counted_bars–;
int limit = MathMin(Bars-counted_bars,Bars-1);
CheckWindow();
for(int i = limit; i >= 0; i–)
{
double price = iMA(NULL,0,1,0,MODE_SMA,Price,i);
macd[i] = iDema(price,FastEMA,i,0)-iDema(price,SlowEMA,i,1);
signal[i] = iDema(macd[i],SignalEMA,i,2);
arrUp[i] = EMPTY_VALUE;
arrDn[i] = EMPTY_VALUE;
trend1[i] = trend1[i+1];
trend2[i] = trend2[i+1];
if(macd[i] > signal[i])
trend1[i] = 1;
if(macd[i] < signal[i])
trend1[i] =-1;
if(macd[i] > 0)
trend2[i] = 1;
if(macd[i] < 0)
trend2[i] =-1;
if(trend2[i] == 1)
arrUp[i] = 0;
if(trend2[i] ==-1)
arrDn[i] = 0;
upDot[i] = EMPTY_VALUE;
dnDot[i] = EMPTY_VALUE;
if(trend1[i]!= trend1[i+1])
if(trend1[i] == 1)
upDot[i] = macd[i];
else
dnDot[i] = macd[i];
}
DeleteTape();
if(ShowTape)
for(i=0; i<BarsToCount ; i++)
DrawTape(macd[i],signal[i],i);
if(alertsOn)
{
if(alertsOnCurrent)
int whichBar = 0;
else
whichBar = 1;
if(trend1[whichBar] != trend1[whichBar+1])
if(trend1[whichBar] == 1)
doAlert("uptrend");
else
doAlert("downtrend");
}
for(i=0; i<indicator_buffers; i++)
SetIndexDrawBegin(i,Bars-BarsToCount);
return(0);
}