//**************************************************
//*  RoundNrMulti.mq4 (No Copyright)               *
//*                                                *
//*  Draws horizontal lines at round price levels  *
//*                                                *
//*  Written by: Totoro                            *
//**************************************************

#property indicator_chart_window
enum LineStyleMenu
{
   LineSolid,
   LineDash,
   LineDot,
   LineDashDot,
   LineDashDotDot
};

extern string              note1                 = "------------------------------";
extern double              Line1Space            = 0.25; // 1 unit = 0.01 of basic value (e.g. 1 USD cent)
extern color               Line1Color            = clrDarkGreen; //clrDeepPink;
extern LineStyleMenu       Line1Style            = 2;
extern string              note2                 = "------------------------------";
extern double              Line2Space            = 0.5;
extern color               Line2Color            = clrLightGreen;
extern LineStyleMenu       Line2Style            = 2;
extern string              note3                 = "------------------------------";
extern double              Line3Space            = 1.0;
extern color               Line3Color            = clrLightBlue;
extern LineStyleMenu       Line3Style            = 2;
//+------------------------------------------------------------------------------------------------------------------+
//template code start1; copy and paste
extern string             button_note1          = "------------------------------";
extern ENUM_BASE_CORNER   btn_corner            = CORNER_LEFT_UPPER;                 //chart btn_corner for anchoring
extern string             btn_text              = "RNrM";
extern string             btn_Font              = "Arial";
extern int                btn_FontSize          = 10;                                //btn__font size
extern color              btn_text_ON_color     = clrWhite;
extern color              btn_text_OFF_color    = clrRed;
extern color              btn_background_color  = clrDimGray;
extern color              btn_border_color      = clrBlack;
extern int                button_x              = 20;                                //btn__x
extern int                button_y              = 13;                                //btn__y
extern int                btn_Width             = 60;                                //btn__width
extern int                btn_Height            = 20;                                //btn__height
extern string             button_note2          = "------------------------------";
bool                      show_data             = true;
string IndicatorName, IndicatorObjPrefix, buttonId;
//template code end1

extern string UniqueLineID      = "FxRoundNrMulti";

double Hoch;
double Tief;
bool FirstRun = true;
//+------------------------------------------------------------------------------------------------------------------+
string GenerateIndicatorName(const string target) //don't change anything in this function
{
   string name = target;
   int try = 2;
   while (WindowFind(name) != -1)
   {
      name = target + " #" + IntegerToString(try++);
   }
   return name;
}
//+------------------------------------------------------------------------------------------------------------------+
int OnInit()  //don't forget to change buttonId name
{
   IndicatorName = GenerateIndicatorName(btn_text);
   IndicatorObjPrefix = "__" + IndicatorName + "__";
   IndicatorShortName(IndicatorName);
   IndicatorDigits(Digits);
   
   double val;
   if (GlobalVariableGet(IndicatorName + "_visibility", val))
      show_data = val != 0;

   ChartSetInteger(ChartID(), CHART_EVENT_MOUSE_MOVE, 1);
   buttonId = IndicatorObjPrefix + "RoundNrMulti";                 //don't forget to change name here
   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);

// put init() here
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------------------------------------------------------+
//don't change anything in this function
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);
}
//+------------------------------------------------------------------------------------------------------------------+
int deinit()
{
   ObjectsDeleteAll(ChartID(), IndicatorObjPrefix);

   //put deinit() here
   deinit2();
	return(0);
}
//+------------------------------------------------------------------------------------------------------------------+
//don't change anything in this function
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, //don't change anything in this function
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
   handleButtonClicks();
}
//+------------------------------------------------------------------------------------------------------------------+
int start()
{
   handleButtonClicks();
   recalc = false;
   
      if (show_data)
         {
          ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_ON_color);
          start2();
         }
      else
         {
          ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_OFF_color);
          deinit2();
         }
   return(0);
}
//+------------------------------------------------------------------------------------------------------------------+

int deinit2()
{
   double Oben    = MathRound(110*Hoch)/100;
   double Unten   = MathRound(80*Tief)/100;
 
   double AbSpace = 0.01*Line1Space;   
   for(double i=0; i<=Oben; i+=AbSpace)
   {
      if(i<Unten) continue;
      ObjectDelete(UniqueLineID+DoubleToStr(i,2));
   }
   
   AbSpace = 0.01*Line2Space;
   for(double j=0; j<=Oben; j+=AbSpace)
   {
      if(j<Unten) continue;
      ObjectDelete(UniqueLineID+DoubleToStr(j,2));
   }
   
   AbSpace = 0.01*Line3Space;
   for(double k=0; k<=Oben; k+=AbSpace)
   {
      if(k<Unten) continue;
      ObjectDelete(UniqueLineID+DoubleToStr(k,2));
   }
   
   return(0);
}

int start2()
{
   if(FirstRun)
   {
      Hoch = NormalizeDouble( High[iHighest(NULL,0,MODE_HIGH,Bars-1,0)], 2 );
      Tief = NormalizeDouble( Low[iLowest(NULL,0,MODE_LOW,Bars-1,0)], 2 );
      FirstRun = false;
   }
   DrawLines();
   return(0);
}

void DrawLines()
{
   double Oben    = MathRound(110*Hoch)/100;
   double Unten   = MathRound(80*Tief)/100;

   double AbSpace = 0.01*Line1Space;
   for(double i=0; i<=Oben; i+=AbSpace)
   {
      if(i<Unten) continue;
      string StringNr1 = DoubleToStr(i,2); // 2 digits number in object name
      if (ObjectFind(UniqueLineID+StringNr1) != 0) // HLine not in main chartwindow
      {                     
         ObjectCreate(UniqueLineID+StringNr1, OBJ_HLINE, 0, 0, i);
         ObjectSet(UniqueLineID+StringNr1, OBJPROP_STYLE, Line1Style);
         ObjectSet(UniqueLineID+StringNr1, OBJPROP_COLOR, Line1Color);
      }
      else
      {
         ObjectSet(UniqueLineID+StringNr1, OBJPROP_STYLE, Line1Style);
         ObjectSet(UniqueLineID+StringNr1, OBJPROP_COLOR, Line1Color);
      }
   }

   AbSpace = 0.01*Line2Space;
   for(double j=0; j<=Oben; j+=AbSpace)
   {
      if(j<Unten) continue;
      string StringNr2 = DoubleToStr(j,2);
      if (ObjectFind(UniqueLineID+StringNr2) != 0)
      {
         ObjectCreate(UniqueLineID+StringNr2, OBJ_HLINE, 0, 0, j);
         ObjectSet(UniqueLineID+StringNr2, OBJPROP_STYLE, Line2Style);
         ObjectSet(UniqueLineID+StringNr2, OBJPROP_COLOR, Line2Color);
      }
      else
      {
         ObjectSet(UniqueLineID+StringNr2, OBJPROP_STYLE, Line2Style);
         ObjectSet(UniqueLineID+StringNr2, OBJPROP_COLOR, Line2Color);
      }
   }
   
   AbSpace = 0.01*Line3Space;
   for(double k=0; k<=Oben; k+=AbSpace)
   {
      if(k<Unten) continue;
      string StringNr3 = DoubleToStr(k,2);
      if (ObjectFind(UniqueLineID+StringNr3) != 0)
      {                     
         ObjectCreate(UniqueLineID+StringNr3, OBJ_HLINE, 0, 0, k);
         ObjectSet(UniqueLineID+StringNr3, OBJPROP_STYLE, Line3Style);
         ObjectSet(UniqueLineID+StringNr3, OBJPROP_COLOR, Line3Color);
      }
      else
      {
         ObjectSet(UniqueLineID+StringNr3, OBJPROP_STYLE, Line3Style);
         ObjectSet(UniqueLineID+StringNr3, OBJPROP_COLOR, Line3Color);
      }
   }

   WindowRedraw();
}