//+------------------------------------------------------------------+
//|                                         Keltner Channel Test.mq4 |
//|                                                 Pipi Brasci corp |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Pipi Brasci corp"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_color2 Red
#property indicator_color3 Red

extern int periodMA=20; //Basic EMA
extern int periodATR=10;//Average True Range period
extern double kshift=2; //shift Lines Upper & Lower

double b0[],b1[],b2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,b0);
   SetIndexLabel (0,"Upper"+periodATR+","+DoubleToStr(kshift,1));

   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,b1);
   SetIndexLabel(1,"Lower"+periodATR+","+DoubleToStr(kshift,1));

   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,b2);
   SetIndexLabel(2,"EMA"+periodMA);
   IndicatorShortName("Keltner_Chanel("+periodMA+","+periodATR+","+DoubleToStr(kshift,1)+")");
//---
   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 counted_bars = IndicatorCounted();
   if(counted_bars < 0)  return(-1);
   if(counted_bars > 0)   counted_bars--;
   int limit = Bars - counted_bars;
   if(counted_bars==0) limit--;
   double r;

   for(int i=0;i<=limit;i++)
     {
      b2[i]=iMA(NULL,0,periodMA,0,MODE_EMA,PRICE_CLOSE,i);
      r=kshift*iATR(NULL,0,periodATR,i);
      b0[i]=b2[i]+r;
      b1[i]=b2[i]-r;
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
