//+------------------------------------------------------------------+
//| ATRBands.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 3
#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_color3 Green
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 1
double upper[];
double middle[];
double lower[];
input int atr_peroid = 14;
input double atr_multiplier = 3.0;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int OnInit() {
IndicatorShortName("ATRBands(" + IntegerToString(atr_peroid) + ")");
//---
SetIndexBuffer(0, middle);
SetIndexBuffer(1, upper);
SetIndexBuffer(2, lower);
//---
SetIndexStyle(0, DRAW_LINE);
SetIndexStyle(1, DRAW_LINE);
SetIndexStyle(2, DRAW_LINE);
//---
SetIndexLabel(0, "Middle");
SetIndexLabel(1, "Upper");
SetIndexLabel(2, "Lower");
SetIndexDrawBegin(0, atr_peroid);
SetIndexDrawBegin(1, atr_peroid);
SetIndexDrawBegin(2, atr_peroid);
IndicatorDigits(_Digits);
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--) {
double atr_value = iATR(NULL, 0, atr_peroid, i);
middle[i] = (High[i] + Low[i]) / 2;
upper[i] = middle[i] + atr_multiplier * atr_value;
lower[i] = middle[i] - atr_multiplier * atr_value;
}
return(rates_total);
}
//+------------------------------------------------------------------+
暂无评论