//+------------------------------------------------------------------+
//|                                           Keltner ATR Bands .mq4 |
//|                                     This is not Keltner Channels |
//|                                                                  |
//|                                        Converted by : Dr. Gaines |
//|                                      dr_richard_gaines@yahoo.com |
//|                                                                  |
//+------------------------------------------------------------------+

#property copyright " Copyright © 2005, MetaQuotes Software Corp."
#property link      " http://www.metaquotes.net/"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 White
#property indicator_color2 White
#include <stdlib.mqh>
//+------------------------------------------------------------------+
//| Common External variables                                        |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| External variables                                               |
//+------------------------------------------------------------------+
extern double MAPeriod = 50;
extern double ATRMult = 3.75;

//+------------------------------------------------------------------+
//| Special Convertion Functions                                     |
//+------------------------------------------------------------------+

double ExtHistoBuffer[];
double ExtHistoBuffer2[];

//+------------------------------------------------------------------+
//| Initialization                                                   |
//+------------------------------------------------------------------+

int init()
{
   SetIndexStyle(0, DRAW_LINE, STYLE_SOLID);
   SetIndexBuffer(0, ExtHistoBuffer);
   SetIndexStyle(1, DRAW_LINE, STYLE_SOLID);
   SetIndexBuffer(1, ExtHistoBuffer2);
   return(0);
}
int start()
{
//+------------------------------------------------------------------+
//| Local variables                                                  |
//+------------------------------------------------------------------+
double ma = 0;
double atr = 0;
double KU = 0;
double KL = 0;
   int limit, shift;
   int counted_bars=IndicatorCounted();
//---- check for possible errors
   if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;

/*[[
    Name := Keltner ATR Bands
    Author := Copyright © 2005, MetaQuotes Software Corp.
    Link := http://www.metaquotes.net/
    Separate Window := No
    First Color := White
    First Draw Type := Line
    First Symbol := 217
    Use Second Data := Yes
    Second Color := White
    Second Draw Type := Line
    Second Symbol := 218
]]*/

// loop from first bar to current bar (with shift=0)
for(shift=limit;shift>=0 ;shift--){ 
    ma = iMA(NULL, 0, MAPeriod, 0,  MODE_SMA, PRICE_CLOSE,  shift);
    atr = iATR(NULL, 0, MAPeriod, shift);
    KU = ma + ATRMult*atr;
    KL = ma - ATRMult*atr;


  ExtHistoBuffer[shift] = KU;
  ExtHistoBuffer2[shift] = KL;

} 
  return(0);
}