//+------------------------------------------------------------------+
//|                                             Keltner Channels.mq4 |
//|                                                  Coded by Gilani |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| tom jones, twj1usa@yahoo.com                                     |
//| 2010-5-10.......added function to change, ma price,mode,shift    |
//+------------------------------------------------------------------+

#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 Black
#property indicator_color2 Black
#property indicator_color3 Black
#property indicator_color4 Blue
#property indicator_color5 Blue


double upper[], middle[], lower[], histu[], histd[];

extern int    BandsPeriod      =20;
extern string note1            = "method is ma mode..0=sma..1=ema";
extern string note2            = "2=smma..3=lwma";
extern int    BandsMethod      =0;
extern string note3            = "applied price is..0=close..1=open";
extern string note4            = "2=high..3=low";
extern string note5            = "4=median price =(High+Low)/2";
extern string note6            = "5=typical price =(High+Low+Close)/3";
extern string note7            = "6=weighted close =(High+Low+Close*2)/4";
extern int    BandsPrice       =0;
extern int    BandsShift       =0;

int init()
  {
   SetIndexStyle(0,DRAW_LINE,EMPTY,2);
   
   SetIndexShift(0,0);
   SetIndexDrawBegin(0,0);
   SetIndexBuffer(0,upper);

   SetIndexStyle(1,DRAW_LINE,EMPTY,2);
   SetIndexShift(1,0);
   SetIndexDrawBegin(1,0);
   SetIndexBuffer(1,middle);

   SetIndexStyle(2,DRAW_LINE,EMPTY,2);
   SetIndexShift(2,0);
   SetIndexDrawBegin(2,0);
   SetIndexBuffer(2,lower);
    
   SetIndexStyle(3,DRAW_HISTOGRAM);
   SetIndexShift(3,0);
   SetIndexDrawBegin(3,0);
   SetIndexBuffer(3,histu);
   
   SetIndexStyle(4,DRAW_HISTOGRAM);
   SetIndexShift(4,0);
   SetIndexDrawBegin(4,0);
   SetIndexBuffer(4,histd);

//---- indicators
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- TODO: add your code here
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() {
   int limit;
   int counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   
   double avg;
   
   for(int x=0; x<limit; x++) {
      middle[x] = iMA(NULL, 0, BandsPeriod, BandsShift, BandsMethod, BandsPrice, x);
      avg  = findAvg(BandsPeriod, x);
      upper[x] = middle[x] + avg;
      lower[x] = middle[x] - avg;
      histu[x] = (middle[x]+avg);
      histd[x] = (middle[x]-avg);
   }
   return(0);
  }
//+------------------------------------------------------------------+


   double findAvg(int BandsPeriod, int BandsShift) {
      double sum=0;
      for (int x=BandsShift;x<(BandsShift+BandsPeriod);x++) {     
         sum += High[x]-Low[x];
      }
      sum = sum/BandsPeriod;
      return (sum);
   }