//+------------------------------------------------------------------+
//|ATR channel
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 LightSeaGreen
#property indicator_color2 LightSeaGreen
#property indicator_color3 LightSeaGreen
#property indicator_style1 STYLE_DOT
#property indicator_style2 STYLE_SOLID
#property indicator_style3 STYLE_SOLID
//---- indicator parameters
extern int    MAperiod=56;
extern string MAmode_="SMA; EMA; SMMA; LWMA";
extern string MAmode="EMA";
extern int    ATRperiod=100;
extern int    BandsShift=0;
extern double ATRmultiplier=2.8;
//---- buffers
double MovingBuffer[];
double UpperBuffer[];
double LowerBuffer[];

int MAM;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,MovingBuffer);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,UpperBuffer);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,LowerBuffer);
//----
   SetIndexDrawBegin(0,ATRperiod+BandsShift);
   SetIndexDrawBegin(1,ATRperiod+BandsShift);
   SetIndexDrawBegin(2,ATRperiod+BandsShift);
//----

  //زوُ MA
  if(MAmode=="SMA")       MAM=0;
  else if(MAmode=="EMA")  MAM=1;
  else if(MAmode=="SMMA") MAM=2;
  else if(MAmode=="LWMA") MAM=3;
  else                    MAM=1;
  return(0);
}
//+------------------------------------------------------------------+
//| Bollinger Bands                                                  |
//+------------------------------------------------------------------+
int start()
  {
   int    i,k,counted_bars=IndicatorCounted();
   double deviation;
   double sum,oldval,newres;
//----
   if(Bars<=ATRperiod) return(0);
//---- initial zero
   if(counted_bars<1)
      for(i=1;i<=ATRperiod;i++)
        {
         MovingBuffer[Bars-i]=EMPTY_VALUE;
         UpperBuffer[Bars-i]=EMPTY_VALUE;
         LowerBuffer[Bars-i]=EMPTY_VALUE;
        }
//----
   int limit=Bars-counted_bars;
   if(counted_bars>0) limit++;
   for(i=0; i<limit; i++)
      MovingBuffer[i]=iMA(NULL,0,MAperiod,BandsShift,MAM,PRICE_CLOSE,i);
//----
   i=Bars-ATRperiod+1;
   if(counted_bars>ATRperiod-1) i=Bars-counted_bars-1;
   while(i>=0)
     {
      sum=0.0;
      k=i+ATRperiod-1;
      oldval=MovingBuffer[i];
      /*
      while(k>=i)
        {
         newres=Close[k]-oldval;
         sum+=newres*newres;
         k--;
        }
      deviation=ATRmultiplier*MathSqrt(sum/ATRperiod);
      */
      deviation=ATRmultiplier*iATR(NULL,0,ATRperiod,i);
      
      UpperBuffer[i]=oldval+deviation;
      LowerBuffer[i]=oldval-deviation;
      i--;
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+