mql语言macd日金叉报警 日死叉报警
时间: 2023-09-01 12:11:19 浏览: 164
以下是MQL语言中MACD日金叉和日死叉报警的示例代码:
MACD日金叉报警:
```
// 定义MACD参数
input int fastEMA = 12;
input int slowEMA = 26;
input int signalEMA = 9;
// 定义变量
double macdCurrent, macdPrevious;
double signalCurrent, signalPrevious;
// 初始化指标
int OnInit() {
// 计算MACD指标
ArraySetAsSeries(true);
SetIndexBuffer(0, macdBuffer);
SetIndexBuffer(1, signalBuffer);
SetIndexStyle(0, DRAW_LINE);
SetIndexStyle(1, DRAW_LINE);
SetIndexLabel(0, "MACD");
SetIndexLabel(1, "Signal");
// 计算初始值
int limit = Bars - IndicatorCounted();
for (int i = limit; i >= 0; i--) {
macdCurrent = iMACD(NULL, 0, fastEMA, slowEMA, signalEMA, PRICE_CLOSE, MODE_MAIN, i);
macdPrevious = iMACD(NULL, 0, fastEMA, slowEMA, signalEMA, PRICE_CLOSE, MODE_MAIN, i + 1);
signalCurrent = iMACD(NULL, 0, fastEMA, slowEMA, signalEMA, PRICE_CLOSE, MODE_SIGNAL, i);
signalPrevious = iMACD(NULL, 0, fastEMA, slowEMA, signalEMA, PRICE_CLOSE, MODE_SIGNAL, i + 1);
macdBuffer[i] = macdCurrent;
signalBuffer[i] = signalCurrent;
}
return INIT_SUCCEEDED;
}
// 循环计算
void 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 limit = rates_total - prev_calculated;
for (int i = limit; i >= 0; i--) {
macdCurrent = iMACD(NULL, 0, fastEMA, slowEMA, signalEMA, PRICE_CLOSE, MODE_MAIN, i);
macdPrevious = iMACD(NULL, 0, fastEMA, slowEMA, signalEMA, PRICE_CLOSE, MODE_MAIN, i + 1);
signalCurrent = iMACD(NULL, 0, fastEMA, slowEMA, signalEMA, PRICE_CLOSE, MODE_SIGNAL, i);
signalPrevious = iMACD(NULL, 0, fastEMA, slowEMA, signalEMA, PRICE_CLOSE, MODE_SIGNAL, i + 1);
macdBuffer[i] = macdCurrent;
signalBuffer[i] = signalCurrent;
// 判断金叉
if (macdCurrent > signalCurrent && macdPrevious < signalPrevious) {
Alert("MACD日金叉");
}
}
}
```
MACD日死叉报警:
```
// 定义MACD参数
input int fastEMA = 12;
input int slowEMA = 26;
input int signalEMA = 9;
// 定义变量
double macdCurrent, macdPrevious;
double signalCurrent, signalPrevious;
// 初始化指标
int OnInit() {
// 计算MACD指标
ArraySetAsSeries(true);
SetIndexBuffer(0, macdBuffer);
SetIndexBuffer(1, signalBuffer);
SetIndexStyle(0, DRAW_LINE);
SetIndexStyle(1, DRAW_LINE);
SetIndexLabel(0, "MACD");
SetIndexLabel(1, "Signal");
// 计算初始值
int limit = Bars - IndicatorCounted();
for (int i = limit; i >= 0; i--) {
macdCurrent = iMACD(NULL, 0, fastEMA, slowEMA, signalEMA, PRICE_CLOSE, MODE_MAIN, i);
macdPrevious = iMACD(NULL, 0, fastEMA, slowEMA, signalEMA, PRICE_CLOSE, MODE_MAIN, i + 1);
signalCurrent = iMACD(NULL, 0, fastEMA, slowEMA, signalEMA, PRICE_CLOSE, MODE_SIGNAL, i);
signalPrevious = iMACD(NULL, 0, fastEMA, slowEMA, signalEMA, PRICE_CLOSE, MODE_SIGNAL, i + 1);
macdBuffer[i] = macdCurrent;
signalBuffer[i] = signalCurrent;
}
return INIT_SUCCEEDED;
}
// 循环计算
void 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 limit = rates_total - prev_calculated;
for (int i = limit; i >= 0; i--) {
macdCurrent = iMACD(NULL, 0, fastEMA, slowEMA, signalEMA, PRICE_CLOSE, MODE_MAIN, i);
macdPrevious = iMACD(NULL, 0, fastEMA, slowEMA, signalEMA, PRICE_CLOSE, MODE_MAIN, i + 1);
signalCurrent = iMACD(NULL, 0, fastEMA, slowEMA, signalEMA, PRICE_CLOSE, MODE_SIGNAL, i);
signalPrevious = iMACD(NULL, 0, fastEMA, slowEMA, signalEMA, PRICE_CLOSE, MODE_SIGNAL, i + 1);
macdBuffer[i] = macdCurrent;
signalBuffer[i] = signalCurrent;
// 判断死叉
if (macdCurrent < signalCurrent && macdPrevious > signalPrevious) {
Alert("MACD日死叉");
}
}
}
```
阅读全文