//+------------------------------------------------------------------+
//|                                                  Goen BB SAP.mq4 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, Goen"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Lime
#property indicator_color2 Lime
#property indicator_color3 Lime

extern string ReadMe="4H=240 8H=480 D=1440 W=7200 M=28800 4M=115200 Y=345600 4Y=1382400";
extern int    TimePeriod=1440;
extern double BandsDeviations=1.0;
extern int    BandMode=MODE_SMA;
extern bool   ShowPrice=false;
color  LineColor;

double MA[];
double BBUpper[];
double BBLower[];
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(3);
  SetIndexStyle(0,DRAW_LINE,0,2,LineColor);
  SetIndexBuffer(0,MA);  
  SetIndexStyle(1,DRAW_LINE,2,1,LineColor);
  SetIndexBuffer(1,BBUpper); 
  SetIndexStyle(2,DRAW_LINE,2,1,LineColor);
  SetIndexBuffer(2,BBLower);

  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,deviation;

   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--;
     }
     deviation=BandsDeviations*MathSqrt(sum/BBPer);
     BBUpper[j]=oldval+deviation;
     BBLower[j]=oldval-deviation;
     j--;
   }

   if (ShowPrice)
   {
   string label1 = "Goen BBD UP";
   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, BBUpper[0]);  
  
   string label2 = "Goen BBD MID";
   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, MA[0]);  
   
   string label3 = "Goen BBD LO";
   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, BBLower[0]);  
   }
   return(0);
  }
//+-------------------------------------------------------------------------------------------+