//+------------------------------------------------------------------+ //| TMA Bands with Keltner Channel.mq4 | //| Naguisa Unada | //| https://www.mql5.com/en/users/unadajapon/news | //+------------------------------------------------------------------+ #property copyright "Naguisa Unada" #property link "https://www.mql5.com/en/users/unadajapon/news" #property icon "\\Images\\Naguisa.ICO" #property version "1.00" #property strict #property indicator_chart_window #property indicator_buffers 7 #property indicator_plots 7 //--- plot Histogram_ #property indicator_label1 "Histogram_" #property indicator_type1 DRAW_HISTOGRAM #property indicator_color1 clrNavy #property indicator_style1 STYLE_SOLID #property indicator_width1 1 //--- plot LowerLine_K #property indicator_label2 "LowerLine_K" #property indicator_type2 DRAW_LINE #property indicator_color2 clrGreen #property indicator_style2 STYLE_SOLID #property indicator_width2 1 //--- plot UpperLine_K #property indicator_label3 "UpperLine_K" #property indicator_type3 DRAW_LINE #property indicator_color3 clrGreen #property indicator_style3 STYLE_SOLID #property indicator_width3 1 //--- plot UpperLine_T #property indicator_label4 "UpperLine_T" #property indicator_type4 DRAW_LINE #property indicator_color4 clrGold #property indicator_style4 STYLE_SOLID #property indicator_width4 2 //--- plot LowerLine_T #property indicator_label5 "LowerLine_T" #property indicator_type5 DRAW_LINE #property indicator_color5 clrGold #property indicator_style5 STYLE_SOLID #property indicator_width5 2 //--- plot ArrowUp #property indicator_label6 "ArrowUp" #property indicator_type6 DRAW_ARROW #property indicator_color6 clrAqua #property indicator_style6 STYLE_SOLID #property indicator_width6 2 //--- plot ArrowDn #property indicator_label7 "ArrowDn" #property indicator_type7 DRAW_ARROW #property indicator_color7 clrRed #property indicator_style7 STYLE_SOLID #property indicator_width7 2 //--- input parameters input int Length = 53; //MA 本数 int MA_period; int ATR_period = 100; //ATR 本数 double ATR_multiply = 3.0; //ATR 倍率 ENUM_APPLIED_PRICE aPrice = PRICE_WEIGHTED; //適用価格 //--- indicator buffers double Histogram_Buffer[]; double KeltnerLower_Buffer[]; double KeltnerUpper_Buffer[]; double UpperLine_Buffer[]; double LowerLine_Buffer[]; double ArrowUp_Buffer[]; double ArrowDn_Buffer[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping SetIndexBuffer(0, Histogram_Buffer); SetIndexBuffer(1, KeltnerLower_Buffer); SetIndexBuffer(2, KeltnerUpper_Buffer); SetIndexBuffer(3, UpperLine_Buffer); SetIndexBuffer(4, LowerLine_Buffer); SetIndexBuffer(5, ArrowUp_Buffer); SetIndexBuffer(6, ArrowDn_Buffer); SetIndexArrow(5, 233); SetIndexArrow(6, 234); switch (_Period) { case PERIOD_M1: MA_period = Length * 60; break; case PERIOD_M5: MA_period = Length * 12; break; case PERIOD_M15: MA_period = Length * 4; break; case PERIOD_M30: MA_period = Length * 2; break; case PERIOD_H4: MA_period = (int)(Length / 2.65); break; default: MA_period = Length; } IndicatorShortName("TMA Bands with Keltner Channel(" + (string)MA_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 i, j, k, limit1, limit2; double ATR_value, MA_value, sum, sumw; if(prev_calculated < 0) return(-1); if (prev_calculated == 0) { limit1 = rates_total - MA_period; limit2 = rates_total - MA_period; } else { limit1 = rates_total - prev_calculated; limit2 = rates_total - prev_calculated + MA_period; } for (i = limit1; i >= 0; i--) { MA_value = iMA(NULL, PERIOD_CURRENT, MA_period, 0, MODE_LWMA, aPrice, i + 1); ATR_value = iATR(NULL, PERIOD_CURRENT, ATR_period, i + 10) * ATR_multiply; KeltnerUpper_Buffer[i] = MA_value + ATR_value; KeltnerLower_Buffer[i] = MA_value - ATR_value; Histogram_Buffer[i] = KeltnerUpper_Buffer[i]; } for (i = limit2; i >= 0; i--) { sum = (MA_period + 1) * iMA(NULL, PERIOD_CURRENT, 1, 0, MODE_SMA, aPrice, i); sumw = (MA_period + 1); for (j = 1, k = MA_period; j <= MA_period; j++, k--) { sum += k * iMA(NULL, PERIOD_CURRENT, 1, 0, MODE_SMA, aPrice, i + j); sumw += k; if (j <= i) { sum += k * iMA(NULL, PERIOD_CURRENT, 1, 0, MODE_SMA, aPrice, i - j); sumw += k; } } MA_value = sum / sumw; ATR_value = iATR(NULL, PERIOD_CURRENT, ATR_period, i + 10) * ATR_multiply; UpperLine_Buffer[i] = MA_value + ATR_value; LowerLine_Buffer[i] = MA_value - ATR_value; if (KeltnerLower_Buffer[i + 1] > LowerLine_Buffer[i + 1] && KeltnerLower_Buffer[i] < LowerLine_Buffer[i]) ArrowUp_Buffer[i] = LowerLine_Buffer[i]; else if (KeltnerUpper_Buffer[i + 1] < UpperLine_Buffer[i + 1] && KeltnerUpper_Buffer[i] > UpperLine_Buffer[i]) ArrowDn_Buffer[i] = UpperLine_Buffer[i]; else { ArrowUp_Buffer[i] = EMPTY_VALUE; ArrowDn_Buffer[i] = EMPTY_VALUE; } } //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+