//+------------------------------------------------------------------+
//|                                                        Bands.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Red
#property indicator_color2 Red
#property indicator_color3 MediumBlue
#property indicator_color4 MediumBlue
//---- indicator parameters
extern int    VarPeriod=20;
extern int    Veroyatnost=60;
//---- buffers
double UpperBufferSL[];
double LowerBufferSL[];
double UpperBufferTP[];
double LowerBufferTP[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,UpperBufferSL);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,LowerBufferSL);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,UpperBufferTP);
   SetIndexStyle(3,DRAW_LINE);
   SetIndexBuffer(3,LowerBufferTP);
//----
   SetIndexDrawBegin(0,VarPeriod);
   SetIndexDrawBegin(1,VarPeriod);
   SetIndexDrawBegin(2,VarPeriod);
   SetIndexDrawBegin(3,VarPeriod);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Bollinger Bands                                                  |
//+------------------------------------------------------------------+
int start()
  {
   int    i,counted_bars=IndicatorCounted();
   double deviationSL,deviationTP;
//----
   if(Bars<=VarPeriod) return(0);
//---- initial zero
   if(counted_bars<1)
      for(i=1;i<=VarPeriod;i++)
        {
         UpperBufferSL[Bars-i]=EMPTY_VALUE;
         LowerBufferSL[Bars-i]=EMPTY_VALUE;
         UpperBufferTP[Bars-i]=EMPTY_VALUE;
         LowerBufferTP[Bars-i]=EMPTY_VALUE;
        }
//----
   int limit=Bars-counted_bars;
   if(counted_bars>0) limit++;
//----
   i=Bars-VarPeriod+1;
   if(counted_bars>VarPeriod-1) i=Bars-counted_bars-1;
   while(i>=0)
     {
      deviationSL=NormalizeDouble(Argument(Veroyatnost)*(iATR(Symbol(),Period(),VarPeriod,i)/Close[i])*100000*MathSqrt(VarPeriod),0)*Point;
      deviationTP=NormalizeDouble(Argument(100-Veroyatnost)*(iATR(Symbol(),Period(),VarPeriod*3,i)/Close[i])*100000*MathSqrt(VarPeriod*3),0)*Point;
      UpperBufferSL[i]=Close[i]+deviationSL;
      LowerBufferSL[i]=Close[i]-deviationSL;
      UpperBufferTP[i]=Close[i]+deviationTP;
      LowerBufferTP[i]=Close[i]-deviationTP;
      i--;
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Функция получения аргумента кумулятивной функции                 |
//| нормального распределения                                        |
//+------------------------------------------------------------------+
double Argument(int Ver)
{
double VARCoeff=0;
switch(Ver)
   {
   case 1: VARCoeff=2.32635; break;
   case 5: VARCoeff=1.64485; break;
   case 10: VARCoeff=1.28155; break;
   case 15: VARCoeff=1.03643; break;
   case 20: VARCoeff=0.84162; break;
   case 25: VARCoeff=0.67449; break;
   case 30: VARCoeff=0.52440; break;
   case 35: VARCoeff=0.38532; break;
   case 40: VARCoeff=0.25335; break;
   case 45: VARCoeff=0.12566; break;
   case 50: VARCoeff=0.00000; break;
   case 55: VARCoeff=0.12566; break;
   case 60: VARCoeff=0.25335; break;
   case 65: VARCoeff=0.38532; break;
   case 70: VARCoeff=0.52440; break;
   case 75: VARCoeff=0.67449; break;
   case 80: VARCoeff=0.84162; break;
   case 85: VARCoeff=1.03643; break;
   case 90: VARCoeff=1.28155; break;
   case 95: VARCoeff=1.64485; break;
   case 99: VARCoeff=2.32635; break;
   default: return(-1);
   }
return (VARCoeff);
}