//+------------------------------------------------------------------+
//|                                                  Goen BB SAP.mq4 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, Goen"

#property indicator_chart_window
#property indicator_buffers 7
#property indicator_color1 Lime
#property indicator_color2 Lime
#property indicator_color3 Lime
#property indicator_color4 Lime
#property indicator_color5 Lime
#property indicator_color6 Lime
#property indicator_color7 Lime

extern string ReadMe1="4H=240 8H=480 D=1440 W=7200";
extern string ReadMe2="M=28800 4M=115200 Y=345600";
extern string ReadMe3="4Y=1382400";
extern int    TimePeriod=1440;
double BandsDeviations1=1.0;
double BandsDeviations2=2.0;
double BandsDeviations3=3.0;
extern int    BandMode=MODE_SMA;
extern bool   ShowPrice=false;
extern bool SD1=true;
extern bool SD2=true;
extern bool SD3=true;
color  LineColor;

double MA[];
double BBUpper1[];
double BBLower1[];
double BBUpper2[];
double BBLower2[];
double BBUpper3[];
double BBLower3[];
int BBPer;

//+------------------------------------------------------------------+
int init()
  {
  switch(TimePeriod)
    {
    case 240 : LineColor = Blue; break;
    case 480 : LineColor = Yellow; break;
    case 1440 : LineColor = Lime; break;
    case 7200 : LineColor = Magenta; break;
    case 28800 : LineColor = Aqua; break;
    case 115200 : LineColor = Gold; break;
    case 345600 : LineColor = Silver; break;
    case 1382400 : LineColor = Pink; break;
    }

  IndicatorBuffers(7);
  SetIndexStyle(0,DRAW_LINE,0,2,LineColor);
  SetIndexBuffer(0,MA);  
  if (SD1)
  {
  SetIndexStyle(1,DRAW_LINE,2,1,LineColor);
  SetIndexBuffer(1,BBUpper1); 
  SetIndexStyle(2,DRAW_LINE,2,1,LineColor);
  SetIndexBuffer(2,BBLower1);
  }
  if (SD2)
  {
  SetIndexStyle(3,DRAW_LINE,2,1,LineColor);
  SetIndexBuffer(3,BBUpper2); 
  SetIndexStyle(4,DRAW_LINE,2,1,LineColor);
  SetIndexBuffer(4,BBLower2);
  }
  if (SD3)
  {
  SetIndexStyle(5,DRAW_LINE,2,1,LineColor);
  SetIndexBuffer(5,BBUpper3); 
  SetIndexStyle(6,DRAW_LINE,2,1,LineColor);
  SetIndexBuffer(6,BBLower3);
  }
  
  BBPer=TimePeriod/Period();
  return(0);
  }

//+------------------------------------------------------------------+
int deinit()
  {
   for(int i = ObjectsTotal() - 1; i >= 0; i--)
     {
       string label = ObjectName(i);
       if(StringSubstr(label, 0, 8) != "Goen BBD")
           continue;
       ObjectDelete(label);   
     }   

   return(0);
  }   

//+------------------------------------------------------------------+
int start()
  {
   if(Bars<=BBPer) return(0);
   int i,j,k,counted_bars=IndicatorCounted();
   if (counted_bars<0)return(-1);
   if (counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars-1;
   double sum,oldval,newres,deviation1,deviation2,deviation3;

   for(i=0; i<limit; i++)MA[i]=iMA(NULL,0,BBPer,0,BandMode,PRICE_WEIGHTED,i);

   j=Bars-BBPer+1;
   if(counted_bars>BBPer-1) j=Bars-counted_bars-1;
   while(j>=0)
   {
     sum=0.0;
     k=j+BBPer-1;
     oldval=MA[j];
     while(k>=j)
     {
       newres=Close[k]-oldval;
       sum+=newres*newres;
       k--;
     }
     deviation1=BandsDeviations1*MathSqrt(sum/BBPer);
     deviation2=BandsDeviations2*MathSqrt(sum/BBPer);
     deviation3=BandsDeviations3*MathSqrt(sum/BBPer);
     BBUpper1[j]=oldval+deviation1;
     BBLower1[j]=oldval-deviation1;
     BBUpper2[j]=oldval+deviation2;
     BBLower2[j]=oldval-deviation2;
     BBUpper3[j]=oldval+deviation3;
     BBLower3[j]=oldval-deviation3;
     j--;
   }

   if (ShowPrice)
   {
   string label1 = "Goen BBD MID";
   ObjectDelete(label1);
   ObjectCreate( label1, OBJ_ARROW, 0, 0, 0 );
   ObjectSet( label1, OBJPROP_ARROWCODE, SYMBOL_RIGHTPRICE);
   ObjectSet( label1, OBJPROP_COLOR, LineColor);
   ObjectSet( label1, OBJPROP_TIME1, Time[0]);
   ObjectSet( label1, OBJPROP_PRICE1, MA[0]);  
   if (SD1)
   {
   string label2 = "Goen BBD UP1";
   ObjectDelete(label2);
   ObjectCreate( label2, OBJ_ARROW, 0, 0, 0 );
   ObjectSet( label2, OBJPROP_ARROWCODE, SYMBOL_RIGHTPRICE);
   ObjectSet( label2, OBJPROP_COLOR, LineColor);
   ObjectSet( label2, OBJPROP_TIME1, Time[0]);
   ObjectSet( label2, OBJPROP_PRICE1, BBUpper1[0]);  
   }
   
   if (SD1)
   {
   string label3 = "Goen BBD LO1";
   ObjectDelete(label3);
   ObjectCreate( label3, OBJ_ARROW, 0, 0, 0 );
   ObjectSet( label3, OBJPROP_ARROWCODE, SYMBOL_RIGHTPRICE);
   ObjectSet( label3, OBJPROP_COLOR, LineColor);
   ObjectSet( label3, OBJPROP_TIME1, Time[0]);
   ObjectSet( label3, OBJPROP_PRICE1, BBLower1[0]);
   }
   
   if (SD2)
   {
   string label4 = "Goen BBD UP2";
   ObjectDelete(label4);
   ObjectCreate( label4, OBJ_ARROW, 0, 0, 0 );
   ObjectSet( label4, OBJPROP_ARROWCODE, SYMBOL_RIGHTPRICE);
   ObjectSet( label4, OBJPROP_COLOR, LineColor);
   ObjectSet( label4, OBJPROP_TIME1, Time[0]);
   ObjectSet( label4, OBJPROP_PRICE1, BBUpper2[0]); 
   }
   
   if (SD2)
   {
   string label5 = "Goen BBD LO2";
   ObjectDelete(label5);
   ObjectCreate( label5, OBJ_ARROW, 0, 0, 0 );
   ObjectSet( label5, OBJPROP_ARROWCODE, SYMBOL_RIGHTPRICE);
   ObjectSet( label5, OBJPROP_COLOR, LineColor);
   ObjectSet( label5, OBJPROP_TIME1, Time[0]);
   ObjectSet( label5, OBJPROP_PRICE1, BBLower2[0]);
   }
   
   if (SD3)
   {
   string label6 = "Goen BBD UP3";
   ObjectDelete(label6);
   ObjectCreate( label6, OBJ_ARROW, 0, 0, 0 );
   ObjectSet( label6, OBJPROP_ARROWCODE, SYMBOL_RIGHTPRICE);
   ObjectSet( label6, OBJPROP_COLOR, LineColor);
   ObjectSet( label6, OBJPROP_TIME1, Time[0]);
   ObjectSet( label6, OBJPROP_PRICE1, BBUpper3[0]); 
   }
   
   if (SD3)
   {
   string label7 = "Goen BBD LO3";
   ObjectDelete(label7);
   ObjectCreate( label7, OBJ_ARROW, 0, 0, 0 );
   ObjectSet( label7, OBJPROP_ARROWCODE, SYMBOL_RIGHTPRICE);
   ObjectSet( label7, OBJPROP_COLOR, LineColor);
   ObjectSet( label7, OBJPROP_TIME1, Time[0]);
   ObjectSet( label7, OBJPROP_PRICE1, BBLower3[0]);
   }
   
   }
   return(0);
  }
//+-------------------------------------------------------------------------------------------+