//+------------------------------------------------------------------+
//|                                                     BB cloud.mq4 |
//+------------------------------------------------------------------+

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 clrNONE
#property indicator_color2 clrNONE
#property indicator_color3 clrNONE
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 1
#property indicator_style3 0

extern int    MAperiod   = 20;
extern int    StdDevs    = 2;
extern int    MAmethod   = 0;
extern int    PriceType  = 0;
extern color  colBBup    = C'0,48,70';
extern color  colBBdown  = C'0,48,70';
extern color  cBand1     = clrNONE;
extern color  cBand2     = clrNONE;
extern int    BandWidth  = 1;
extern color  cMedian    = clrDimGray;
extern int    LineWidth  = 1;
extern int    LineStyle  = 0;
extern ENUM_BASE_CORNER   btn_corner            = CORNER_LEFT_UPPER; 
extern string             btn_text              = "bb";
extern string             btn_Font              = "Arial";      //"Arial Black"
extern int                btn_FontSize          = 8;                            
extern color              btn_text_ON_color     = clrWhite;
extern color              btn_text_OFF_color    = clrRed;
extern string             btn_pressed           = "bb off";            
extern string             btn_unpressed         = "bb on";
extern color              btn_background_color  = clrDimGray;
extern color              btn_border_color      = clrDarkGray;
extern int                button_x              = 900;                                 
extern int                button_y              = 0;                                   
extern int                btn_Width             = 60;                                 
extern int                btn_Height            = 20;               
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
bool                      show_data             = true;
string IndicatorName, IndicatorObjPrefix ,buttonId ;

int ExtCountedBars=0;
//+------------------------------------------------------------------+
string GenerateIndicatorName(const string target)
{
   string name = target;
   int try = 2;
   while (WindowFind(name) != -1)
   {
      name = target + " #" + IntegerToString(try++);
   }
   return name;
}
int init() 
  {
//+------------------------------------------------------------------+
//SetIndexStyle(0,DRAW_HISTOGRAM, 0, BandWidth, cBand1);
   SetIndexStyle(0,DRAW_LINE,0,BandWidth,cBand1);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexEmptyValue(0,0.0);
   ArrayInitialize(ExtMapBuffer1,0.0);

//SetIndexStyle(1,DRAW_HISTOGRAM, 0, BandWidth, cBand2);
   SetIndexStyle(1,DRAW_LINE,0,BandWidth,cBand2);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexEmptyValue(1,0.0);
   ArrayInitialize(ExtMapBuffer2,0.0);

   SetIndexStyle(2,DRAW_LINE,LineStyle,LineWidth,cMedian);
   SetIndexBuffer(2,ExtMapBuffer3);
   SetIndexEmptyValue(2,0.0);
   ArrayInitialize(ExtMapBuffer3,0.0);
 IndicatorShortName("Bollinger Bands");
  IndicatorName = GenerateIndicatorName(btn_text);
   IndicatorObjPrefix = "__" + IndicatorName + "__";
   IndicatorShortName(WindowExpertName());
   IndicatorDigits(Digits);
   
   double val;
   if (GlobalVariableGet(IndicatorName + "_visibility", val))
   show_data = val != 0;

   ChartSetInteger(ChartID(), CHART_EVENT_MOUSE_MOVE, 1);
   buttonId = IndicatorObjPrefix+btn_text;
   createButton(buttonId, btn_text, btn_Width, btn_Height, btn_Font, btn_FontSize, btn_background_color, btn_border_color, btn_text_ON_color);
   ObjectSetInteger(ChartID(), buttonId, OBJPROP_YDISTANCE, button_y);
   ObjectSetInteger(ChartID(), buttonId, OBJPROP_XDISTANCE, button_x);
   return(0);
  }
//+------------------------------------------------------------------+
int deinit() 
  {
  ObjectsDeleteAll(0,"bb");
   ObjectsDeleteAll(ChartID(), IndicatorObjPrefix);
  	ObjectsDeleteAll(0,OBJ_TRIANGLE);
   return(0);
  }
  void createButton(string buttonID,string buttonText,int width,int height,string font,int fontSize,color bgColor,color borderColor,color txtColor)
{
      ObjectDelete    (ChartID(),buttonID);
      ObjectCreate    (ChartID(),buttonID,OBJ_BUTTON,0,0,0);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_COLOR,txtColor);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_BGCOLOR,bgColor);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_BORDER_COLOR,borderColor);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_XSIZE,width);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_YSIZE,height);
      ObjectSetString (ChartID(),buttonID,OBJPROP_FONT,font);
      ObjectSetString (ChartID(),buttonID,OBJPROP_TEXT,buttonText);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_FONTSIZE,fontSize);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_SELECTABLE,0);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_CORNER,btn_corner);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_HIDDEN,1);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_XDISTANCE,9999);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_YDISTANCE,9999);
}
bool recalc = true;

void handleButtonClicks()
{
   if (ObjectGetInteger(ChartID(), buttonId, OBJPROP_STATE))
   {
      ObjectSetInteger(ChartID(), buttonId, OBJPROP_STATE, false);
      show_data = !show_data;
      GlobalVariableSet(IndicatorName + "_visibility", show_data ? 1.0 : 0.0);
      recalc = true;
      start();
   }
}
void OnChartEvent(const int id, 
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
   handleButtonClicks();
   
}

//
//
//

int start()
{
//template code 
   handleButtonClicks();
   recalc = false;
   
   start2();
   
      SetIndexStyle( 0,   DRAW_LINE );
      SetIndexStyle( 1,   DRAW_LINE );
      SetIndexStyle( 2,   DRAW_LINE );
   
   if (show_data)
   {
      ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_ON_color);
      ObjectSetString(ChartID(),buttonId,OBJPROP_TEXT,btn_unpressed);
   }
   else
   {
      ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_OFF_color);
      ObjectSetString(ChartID(),buttonId,OBJPROP_TEXT,btn_pressed);
         SetIndexStyle(0,DRAW_NONE);
         SetIndexStyle(1,DRAW_NONE);
         SetIndexStyle(2,DRAW_NONE);
         ObjectsDeleteAll(0,OBJ_TRIANGLE);
           
   }
   return(0);
}
//+------------------------------------------------------------------+
int start2() 
  {
   if(Bars<=10) return(0);
   ExtCountedBars=IndicatorCounted();
   if(ExtCountedBars<0) return(-1);
   if(ExtCountedBars>0) ExtCountedBars--;
   int pos=Bars-ExtCountedBars-1;
   while(pos>=0) 
     {
      ExtMapBuffer1[pos]=iBands(NULL,0,MAperiod,StdDevs,0,PriceType,2,pos);
      ExtMapBuffer2[pos]=iBands(NULL,0,MAperiod,StdDevs,0,PriceType,1,pos);
      ExtMapBuffer3[pos]=iMA(NULL,0,MAperiod,0,MAmethod,PriceType,pos);
      pos--;
     }

   for(int i=300; i>=0; i--)
     {
      string obj_name="BB_cloud-"+IntegerToString((int)Time[i])+"-1";

      if(ObjectFind(obj_name)==-1)
        {
         ObjectCreate(obj_name,OBJ_TRIANGLE,0,Time[i+1],ExtMapBuffer1[i+1],Time[i+1],ExtMapBuffer2[i+1],Time[i],ExtMapBuffer2[i]);
         ObjectSet(obj_name,OBJPROP_COLOR,colBBup);
         ObjectSet(obj_name,OBJPROP_BACK,true);
        }

      obj_name="BB_cloud-"+IntegerToString((int)Time[i])+"-2";

      if(ObjectFind(obj_name)==-1)
        {
         ObjectCreate(obj_name,OBJ_TRIANGLE,0,Time[i+1],ExtMapBuffer1[i+1],Time[i],ExtMapBuffer1[i],Time[i],ExtMapBuffer2[i]);
         ObjectSet(obj_name,OBJPROP_COLOR,colBBdown);
         ObjectSet(obj_name,OBJPROP_BACK,true);
        }
     }
   return(0);
  }
//+------------------------------------------------------------------+
