//+------------------------------------------------------------------+
//| 双均线.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_chart_window
#property indicator_buffers 2
#property indicator_color1 clrWhite
#property indicator_color2 clrYellow
input int fastMA_Period = 5;
input int slowMA_Period = 10;
double fastMA[];
double slowMA[];
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int OnInit() {
SetIndexBuffer(0, fastMA);
SetIndexBuffer(1, slowMA);
//---
SetIndexStyle(0, DRAW_LINE);
SetIndexStyle(1, DRAW_LINE);
//--
SetIndexLabel(0, "Fast MA");
SetIndexLabel(1, "Slow MA");
//--
SetIndexDrawBegin(0, fastMA_Period);
SetIndexDrawBegin(1, slowMA_Period);
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 limit;
// 第一次回调计算所有 K 线
if (prev_calculated == 0)
limit = rates_total - 1;
else
limit = rates_total - prev_calculated; // 后续回调,limit = 0 计算最后一根不断更新的 K 线,当新 K 线形成时, limit = 1,需要计算上一根的最终收盘后的价格和最新形成的一根
// 最后最新一根索引是 0,从前往后算
for (int i = limit; i >= 0; i--) {
fastMA[i] = iMA(NULL, 0, fastMA_Period, 0, MODE_SMA, PRICE_CLOSE, i);
slowMA[i] = iMA(NULL, 0, slowMA_Period, 0, MODE_SMA, PRICE_CLOSE, i);
}
return(rates_total);
}
//+------------------------------------------------------------------+
01_MT4 实现一个双均线指标
暂无评论