//+------------------------------------------------------------------+
//|                                                     ATP Worm.mq4|
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1  clrDodgerBlue
#property indicator_color2  clrRed
#property indicator_color3  clrDodgerBlue
#property indicator_color4  clrRed

#property indicator_width1  2
#property indicator_width2  2
#property indicator_width3  2
#property indicator_width4  2


#property strict

//
//
//
//
//

extern double Pips      = 0.5; // pips
extern int    AtrLength = 14;  // ATR length
extern int    Multiple  = 2;   // Multiplier
extern int                btn_Subwindow = 0;
extern ENUM_BASE_CORNER   btn_corner            = CORNER_LEFT_UPPER;
extern string             btn_text              = "band";
extern string             btn_Font              = "Arial";
extern int                btn_FontSize          = 10;
extern color              btn_text_ON_color     = clrLime;
extern color              btn_text_OFF_color    = clrRed;
extern int                button_x              = 900;
extern int                button_y              = 0;

bool show_data = true;
bool recalc    = true;
string IndicatorName, IndicatorObjPrefix,buttonId;

double Up1Buffer[],Up2Buffer[],Dn1Buffer[],Dn2Buffer[];

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
string GenerateIndicatorName(const string target) 
{
   string name = target;
   int try = 2;
   while (WindowFind(name) != -1)
   {
      name = target + " #" + IntegerToString(try++);
   }
   return name;
}
//
//
//

int OnInit()
{
IndicatorName = GenerateIndicatorName(btn_text);
   IndicatorObjPrefix = "__" + IndicatorName + "__";
   IndicatorShortName(IndicatorName);
   IndicatorDigits(Digits);
   
   double val;
   if (GlobalVariableGet(IndicatorName + "_visibility", val))
      show_data = val != 0;
      
   IndicatorBuffers(4);
   SetIndexBuffer(0,Up1Buffer, INDICATOR_DATA); SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(1,Up2Buffer, INDICATOR_DATA); SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(2,Dn1Buffer, INDICATOR_DATA); SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(3,Dn2Buffer, INDICATOR_DATA); SetIndexStyle(3,DRAW_LINE); 
   
   IndicatorShortName(" ATP  ");

 ChartSetInteger(0, CHART_EVENT_MOUSE_MOVE, 1);
   buttonId = IndicatorObjPrefix + (btn_text);
   createButton(buttonId, btn_text, 60, 20, btn_Font, btn_FontSize, clrDimGray, clrBlack, btn_text_ON_color);
   ObjectSetInteger(0, buttonId, OBJPROP_YDISTANCE, button_y);
   ObjectSetInteger(0, buttonId, OBJPROP_XDISTANCE, button_x);  
     
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason){ ObjectsDeleteAll(ChartID(), IndicatorObjPrefix);  }

//
void createButton(string buttonID,string buttonText,int width,int height,string font,int fontSize,color bgColor,color borderColor,color txtColor)
  {
   ObjectDelete(0,buttonID);
      ObjectCreate (0,buttonID,OBJ_BUTTON,btn_Subwindow,0,0);
   ObjectSetInteger(0,buttonID,OBJPROP_COLOR,txtColor);
   ObjectSetInteger(0,buttonID,OBJPROP_BGCOLOR,bgColor);
   ObjectSetInteger(0,buttonID,OBJPROP_BORDER_COLOR,borderColor);
   ObjectSetInteger(0,buttonID,OBJPROP_BORDER_TYPE,BORDER_RAISED);
   ObjectSetInteger(0,buttonID,OBJPROP_XSIZE,width);
   ObjectSetInteger(0,buttonID,OBJPROP_YSIZE,height);
   ObjectSetString(0,buttonID,OBJPROP_FONT,font);
   ObjectSetString(0,buttonID,OBJPROP_TEXT,buttonText);
   ObjectSetInteger(0,buttonID,OBJPROP_FONTSIZE,fontSize);
   ObjectSetInteger(0,buttonID,OBJPROP_SELECTABLE,0);
   ObjectSetInteger(0,buttonID,OBJPROP_CORNER,btn_corner);
   ObjectSetInteger(0,buttonID,OBJPROP_HIDDEN,1);
   ObjectSetInteger(0,buttonID,OBJPROP_XDISTANCE,9999);
   ObjectSetInteger(0,buttonID,OBJPROP_YDISTANCE,9999);
  }
//+------------------------------------------------------------------+
void handleButtonClicks()
  {
   if(ObjectGetInteger(0, buttonId, OBJPROP_STATE))
     {
      ObjectSetInteger(0, 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();
   if(id==CHARTEVENT_OBJECT_CLICK && ObjectGet(sparam,OBJPROP_TYPE)==OBJ_BUTTON)

    for (int tp=0; tp<indicator_buffers; tp++)
            SetIndexStyle(tp,DRAW_LINE);
   

   if(show_data)
     {
      handleButtonClicks();
      ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_ON_color);
        
        
     }
   else
     {
      ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_OFF_color);
      
       for (int tp=0; tp<indicator_buffers; tp++)
            SetIndexStyle(tp,DRAW_NONE);
     
     }
}
//
//

int OnCalculate (const int       rates_total,
                 const int       prev_calculated,
                 const datetime& time[],
                 const double&   open[],
                 const double&   high[],
                 const double&   low[],
                 const double&   close[],
                 const long&     tick_volume[],
                 const long&     volume[],
                 const int&      spread[])
{
   int i,limit=fmin(rates_total-prev_calculated+1,rates_total-1);
    
          
   double pipMultiplier = Point*MathPow(10,Digits%2);   
   for(i=limit;i>=0; i--)
   {  
      double atr = iATR(Symbol(), 0, AtrLength, i);
      Up1Buffer[i] = (i<rates_total-1) ? low[i+1]  - Pips * pipMultiplier + atr* Multiple : 0;
      Up2Buffer[i] = low[i]    - Pips * pipMultiplier + atr * Multiple;
      Dn1Buffer[i] = (i<rates_total-1) ? high[i+1] + Pips * pipMultiplier - atr* Multiple : 0;
      Dn2Buffer[i] = high[i]   + Pips * pipMultiplier - atr * Multiple;
   }
return(rates_total);
}

//------------------------------------------------------------------
//                                                                  
//------------------------------------------------------------------

