//+------------------------------------------------------------------+
//|                                              Goen BB SAP 1.2.mq4 |
//|                                   Modified By:mthdepok@ymail.com |        
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, Goen"

#property indicator_chart_window
#property indicator_buffers 7
#property indicator_color1 Yellow
#property indicator_width1 3
#property indicator_color2 LightCyan
#property indicator_width2 1
#property indicator_color3 LightPink
#property indicator_width3 1
#property indicator_color4 Aqua
#property indicator_width4 1
#property indicator_color5 HotPink
#property indicator_width5 1
#property indicator_color6 LightSkyBlue
#property indicator_width6 1
#property indicator_color7 Tomato
#property indicator_width7 1
//---- indicator parameters
extern int    FixPeriod=1440;
extern double BandsDeviations1=0.5;
extern double BandsDeviations2=1.0;
extern double BandsDeviations3=1.5;
extern int    BandMode=MODE_LWMA;
extern int    DrawHowLong=240;
extern bool   ShowPrice=true;

//---- buffers
double MA[];
double BBUpper1[];
double BBLower1[];
double BBUpper2[];
double BBLower2[];
double BBUpper3[];
double BBLower3[];
int BBPer;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators

   IndicatorBuffers(7);
   SetIndexStyle(0,DRAW_LINE,0,3);
   SetIndexBuffer(0,MA);
     
   SetIndexStyle(1,DRAW_LINE,4,1);
   SetIndexBuffer(1,BBUpper1); 
   
   SetIndexStyle(2,DRAW_LINE,4,1);
   SetIndexBuffer(2,BBLower1);

   SetIndexStyle(3,DRAW_LINE,4,1);
   SetIndexBuffer(3,BBUpper2); 
   
   SetIndexStyle(4,DRAW_LINE,4,1);
   SetIndexBuffer(4,BBLower2);

   SetIndexStyle(5,DRAW_LINE,4,1);
   SetIndexBuffer(5,BBUpper3); 
   
   SetIndexStyle(6,DRAW_LINE,4,1);
   SetIndexBuffer(6,BBLower3);
   
   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, 5 );
   ObjectSet( label0, OBJPROP_YDISTANCE, 20 );

   BBPer=FixPeriod/Period();
   return(0);
  }
  
  
int deinit()
  {
//----
       ObjectDelete("Goen BB Upper1"+FixPeriod);   
       ObjectDelete("Goen BB Middle"+FixPeriod);   
       ObjectDelete("Goen BB Lower1"+FixPeriod);   
       ObjectDelete("Goen BB Signature"+FixPeriod);
       ObjectDelete("Goen BB Upper2"+FixPeriod);
       ObjectDelete("Goen BB Lower2"+FixPeriod);
       ObjectDelete("Goen BB Upper3"+FixPeriod);
       ObjectDelete("Goen BB Lower3"+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,deviation1, deviation2, deviation3;
//----
   SetIndexDrawBegin(0,Bars-DrawHowLong);
   SetIndexDrawBegin(1,Bars-DrawHowLong);
   SetIndexDrawBegin(2,Bars-DrawHowLong);
   SetIndexDrawBegin(3,Bars-DrawHowLong);
   SetIndexDrawBegin(4,Bars-DrawHowLong);
   SetIndexDrawBegin(5,Bars-DrawHowLong);
   SetIndexDrawBegin(6,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--;
     }
     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 BB Upper1"+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, BBUpper1[0]);  
  
   string label2 = "Goen BB Middle"+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 Lower1"+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, BBLower1[0]);
   
   string label4 = "Goen BB Upper2"+FixPeriod;
   ObjectDelete(label4);
   ObjectCreate( label4, OBJ_ARROW, 0, 0, 0 );
   ObjectSet( label4, OBJPROP_ARROWCODE, SYMBOL_RIGHTPRICE);
   ObjectSet( label4, OBJPROP_COLOR, Silver);
   ObjectSet( label4, OBJPROP_TIME1, Time[0]);
   ObjectSet( label4, OBJPROP_PRICE1, BBUpper2[0]);
   
   string label5 = "Goen BB Lower2"+FixPeriod;
   ObjectDelete(label5);
   ObjectCreate( label5, OBJ_ARROW, 0, 0, 0 );
   ObjectSet( label5, OBJPROP_ARROWCODE, SYMBOL_RIGHTPRICE);
   ObjectSet( label5, OBJPROP_COLOR, Silver);
   ObjectSet( label5, OBJPROP_TIME1, Time[0]);
   ObjectSet( label5, OBJPROP_PRICE1, BBLower2[0]);
   
   string label6 = "Goen BB Upper3"+FixPeriod;
   ObjectDelete(label6);
   ObjectCreate( label6, OBJ_ARROW, 0, 0, 0 );
   ObjectSet( label6, OBJPROP_ARROWCODE, SYMBOL_RIGHTPRICE);
   ObjectSet( label6, OBJPROP_COLOR, Silver);
   ObjectSet( label6, OBJPROP_TIME1, Time[0]);
   ObjectSet( label6, OBJPROP_PRICE1, BBUpper3[0]);
   
   string label7 = "Goen BB Lower3"+FixPeriod;
   ObjectDelete(label7);
   ObjectCreate( label7, OBJ_ARROW, 0, 0, 0 );
   ObjectSet( label7, OBJPROP_ARROWCODE, SYMBOL_RIGHTPRICE);
   ObjectSet( label7, OBJPROP_COLOR, Silver);
   ObjectSet( label7, OBJPROP_TIME1, Time[0]);
   ObjectSet( label7, OBJPROP_PRICE1, BBLower3[0]);
   
   ObjectCreate("CoderMark", OBJ_LABEL,0,0,0);
   ObjectSetText("CoderMark","Modified By:mthdepok@ymail.com",10,"Arial Narrow Bold",LightSkyBlue);
   ObjectSet("CoderMark", OBJPROP_CORNER, 2);
   ObjectSet("CoderMark", OBJPROP_XDISTANCE,5);
   ObjectSet("CoderMark", OBJPROP_YDISTANCE,5);  
   }
   
   return(0);
  }
//+-------------------------------------------------------------------------------------------+