//+------------------------------------------------------------------+
//| 带箭头信号的MACD.mq4 |
//| Copyright 2023, 100w123 |
//| https://100w123.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, 100w123"
#property link "https://100w123.com"
#property version "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 clrSilver
#property indicator_width1 1
#property indicator_color2 clrRed
#property indicator_width2 1
#property indicator_style2 STYLE_DOT
#property indicator_color3 clrLightBlue
#property indicator_width3 2
#property indicator_color4 clrLightPink
#property indicator_width4 2
extern int FastEMA_Period = 12;
extern int SlowEMA_Period = 26;
extern int Signal_Period = 9;
double macd[];
double macd_sig[];
double up[];
double dn[];
double fast_ema[];
double slow_ema[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit() {
IndicatorBuffers(6);
SetIndexBuffer(0, macd);
SetIndexStyle(0, DRAW_HISTOGRAM);
SetIndexBuffer(1, macd_sig);
SetIndexStyle(1, DRAW_LINE);
SetIndexBuffer(2, up);
SetIndexStyle(2, DRAW_ARROW);
SetIndexBuffer(3, dn);
SetIndexStyle(3, DRAW_ARROW);
SetIndexArrow(2, 233);
SetIndexArrow(3, 234);
SetIndexBuffer(4, fast_ema);
SetIndexBuffer(5, slow_ema);
SetIndexLabel(0, "MACD");
SetIndexLabel(1, "Signal");
string short_name = "MACD(" + (string)FastEMA_Period + ",";
short_name += (string)SlowEMA_Period + ",";
short_name += (string)Signal_Period + ")";
IndicatorShortName(short_name);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
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[]) {
int i, j;
int limit;
if (prev_calculated == 0)
limit = rates_total - 1;
else
limit = rates_total - prev_calculated;
for (i = limit; i >= 0; i--) {
fast_ema[i] = iMA(NULL, 0, FastEMA_Period, 0, MODE_EMA, PRICE_CLOSE, i);
slow_ema[i] = iMA(NULL, 0, SlowEMA_Period, 0, MODE_EMA, PRICE_CLOSE, i);
macd[i] = fast_ema[i] - slow_ema[i];
if (i + Signal_Period <= Bars) {
macd_sig[i] = 0;
for (j = 0; j < Signal_Period; j++) macd_sig[i] += macd[i + j];
macd_sig[i] /= Signal_Period;
}
if (i + Signal_Period < Bars) {
up[i] = EMPTY_VALUE;
dn[i] = EMPTY_VALUE;
if (macd[i + 1] <= macd_sig[i + 1] && macd[i] > macd_sig[i]) up[i] = macd_sig[i];
if (macd[i + 1] >= macd_sig[i + 1] && macd[i] < macd_sig[i]) dn[i] = macd_sig[i];
}
}
return(rates_total);
}
//+------------------------------------------------------------------+
暂无评论