//+------------------------------------------------------------------+
//|                                              Goen BB SAP 1.2.mq4 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, Goen"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Magenta
#property indicator_color2 Magenta
#property indicator_color3 Magenta


//---- indicator parameters
extern int    FixPeriod=7200;
extern double BandsDeviations=1.0;
extern int    BandMode=MODE_SMA;
extern int    DrawHowLong=25;
extern bool   ShowPrice=false;

//---- buffers
double MA[];
double BBUpper[];
double BBLower[];
int BBPer;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators

   IndicatorBuffers(3);
   SetIndexStyle(0,DRAW_LINE,0,2);
   SetIndexBuffer(0,MA);  
   SetIndexStyle(1,DRAW_LINE,2,0);
   SetIndexBuffer(1,BBUpper); 
   SetIndexStyle(2,DRAW_LINE,2,0);
   SetIndexBuffer(2,BBLower);

   string label0 = "Goen BB Signature"+FixPeriod;
   ObjectCreate( label0, OBJ_LABEL, 0, 0, 0 );
   ObjectSetText(label0,"GOEN BB SAP 1.2",8, "Arial Bold", Silver);
   ObjectSet( label0, OBJPROP_CORNER, 2 );
   ObjectSet( label0, OBJPROP_COLOR, Silver);
   ObjectSet( label0, OBJPROP_XDISTANCE, 3 );
   ObjectSet( label0, OBJPROP_YDISTANCE, 3 );

   BBPer=FixPeriod/Period();
   return(0);
  }
  
  
int deinit()
  {
//----
       ObjectDelete("Goen BB UP"+FixPeriod);   
       ObjectDelete("Goen BB MID"+FixPeriod);   
       ObjectDelete("Goen BB LO"+FixPeriod);   
       ObjectDelete("Goen BB Signature"+FixPeriod);
//----
   return(0);
  }   
  
  
//+------------------------------------------------------------------+
//| Bollinger Bands                                                  |
//+------------------------------------------------------------------+
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;
//----
   SetIndexDrawBegin(0,Bars-DrawHowLong);
   SetIndexDrawBegin(1,Bars-DrawHowLong);
   SetIndexDrawBegin(2,Bars-DrawHowLong);

   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=DrawHowLong;
   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 BB UP"+FixPeriod;
   ObjectDelete(label1);
   ObjectCreate( label1, OBJ_ARROW, 0, 0, 0 );
   ObjectSet( label1, OBJPROP_ARROWCODE, SYMBOL_RIGHTPRICE);
   ObjectSet( label1, OBJPROP_COLOR, Silver);
   ObjectSet( label1, OBJPROP_TIME1, Time[0]);
   ObjectSet( label1, OBJPROP_PRICE1, BBUpper[0]);  
  
   string label2 = "Goen BB MID"+FixPeriod;
   ObjectDelete(label2);
   ObjectCreate( label2, OBJ_ARROW, 0, 0, 0 );
   ObjectSet( label2, OBJPROP_ARROWCODE, SYMBOL_RIGHTPRICE);
   ObjectSet( label2, OBJPROP_COLOR, Silver);
   ObjectSet( label2, OBJPROP_TIME1, Time[0]);
   ObjectSet( label2, OBJPROP_PRICE1, MA[0]);  
   
   string label3 = "Goen BB LO"+FixPeriod;
   ObjectDelete(label3);
   ObjectCreate( label3, OBJ_ARROW, 0, 0, 0 );
   ObjectSet( label3, OBJPROP_ARROWCODE, SYMBOL_RIGHTPRICE);
   ObjectSet( label3, OBJPROP_COLOR, Silver);
   ObjectSet( label3, OBJPROP_TIME1, Time[0]);
   ObjectSet( label3, OBJPROP_PRICE1, BBLower[0]);  
   }
   
   return(0);
  }
//+-------------------------------------------------------------------------------------------+