// July 26th, 2020
// not for sale, rent, auction, nor lease
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Define the Variables to use.....                                 |
//+------------------------------------------------------------------+
extern string             note1                 = "Fibonacci colors";
extern bool               DisplayUpperFibo      = true;
extern color              UpperFiboColor        = clrDarkGreen;
extern ENUM_LINE_STYLE    UpperFiboStyle        = STYLE_DASH;
extern int                UpperFiboWidth        = 1;
extern string             note2                 = "------------------------------";
extern bool               DisplayMainFibo       = true;
extern color              MainFiboColor         = clrDarkSeaGreen;
extern ENUM_LINE_STYLE    MainFiboStyle         = STYLE_DASHDOT;
extern int                MainFiboWidth         = 1;
extern string             note3                 = "------------------------------";
extern bool               DisplayLowerFibo      = true;
extern color              LowerFiboColor        = clrMaroon;
extern ENUM_LINE_STYLE    LowerFiboStyle        = STYLE_DASHDOTDOT;
extern int                LowerFiboWidth        = 1;
extern uint               MonthlyShift          = 1;
//template code start1
extern string             button_note1          = "------------------------------";
extern ENUM_BASE_CORNER   btn_corner            = CORNER_LEFT_UPPER; // chart btn_corner for anchoring
extern string             btn_text              = "FiboM";
extern string             btn_Font              = "Arial";
extern int                btn_FontSize          = 9;                             //btn__font size
extern color              btn_text_ON_color     = clrLime;
extern color              btn_text_OFF_color    = clrRed;
extern color              btn_background_color  = clrDimGray;
extern color              btn_border_color      = clrBlack;
extern int                button_x              = 180;                                     //btn__x
extern int                button_y              = 25;                                     //btn__y
extern int                btn_Width             = 80;                                 //btn__width
extern int                btn_Height            = 20;                                //btn__height
extern string             UniqueButtonID        = "SpudFiboMonthly2022";                               
extern string             button_note2          = "------------------------------";
bool                      show_data             = true;
string buttonId, IndicatorName, IndicatorObjPrefix;
//template code end1
double HiPrice, LoPrice, Range;
datetime StartTime;
//+------------------------------------------------------------------+
string GenerateIndicatorName(const string target) //don't change anything here
{
   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;

   ChartSetInteger(ChartID(), CHART_EVENT_MOUSE_MOVE, 1);
   buttonId = IndicatorObjPrefix + UniqueButtonID;
   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 here
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(), buttonId);

//put deinit() here
      ObjectDelete("FiboUpMonthly");
      ObjectDelete("FiboDownMonthly");
      ObjectDelete("FiboInMonthly");
	return(0);
}
//+------------------------------------------------------------------+
//don't change anything here
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 here
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
   handleButtonClicks();
}
//+------------------------------------------------------------------+
int DrawFibo()
{
  int banzai	= iBarShift(NULL,PERIOD_MN1,Time[0]) + MonthlyShift;	
	if(DisplayUpperFibo)
	{
	   HiPrice		= iHigh(NULL,PERIOD_MN1,banzai);
	   StartTime	= iTime(NULL,PERIOD_MN1,banzai);

	if(ObjectFind("FiboUpMonthly") == -1)
		ObjectCreate("FiboUpMonthly",OBJ_FIBO,0,StartTime,HiPrice+Range,StartTime,HiPrice);
	else
	{
		ObjectSet("FiboUpMonthly",OBJPROP_TIME2, StartTime);
		ObjectSet("FiboUpMonthly",OBJPROP_TIME1, StartTime);
		ObjectSet("FiboUpMonthly",OBJPROP_PRICE1,HiPrice+Range);
		ObjectSet("FiboUpMonthly",OBJPROP_PRICE2,HiPrice);
   }
   ObjectSet("FiboUpMonthly",OBJPROP_LEVELSTYLE,UpperFiboStyle);
   ObjectSet("FiboUpMonthly",OBJPROP_LEVELWIDTH,UpperFiboWidth);
   ObjectSet("FiboUpMonthly",OBJPROP_LEVELCOLOR,UpperFiboColor);
   ObjectSet("FiboUpMonthly",OBJPROP_FIBOLEVELS,15);
   ObjectSet("FiboUpMonthly",OBJPROP_FIRSTLEVEL+0,0.0);	   
	if (!DisplayMainFibo)
       ObjectSetFiboDescription("FiboUpMonthly",0,"(100.0%) -  %$  "); 
   else    
       ObjectSetFiboDescription("FiboUpMonthly",0,""); 

   ObjectSet("FiboUpMonthly",OBJPROP_FIRSTLEVEL+1,0.236);  	ObjectSetFiboDescription("FiboUpMonthly",1,"(123.6%) -  %$"  ); 
   ObjectSet("FiboUpMonthly",OBJPROP_FIRSTLEVEL+2,0.382); 	ObjectSetFiboDescription("FiboUpMonthly",2,"(138.2%) -  %$"  ); 
   ObjectSet("FiboUpMonthly",OBJPROP_FIRSTLEVEL+3,0.500); 	ObjectSetFiboDescription("FiboUpMonthly",3,"(150.0%) -  %$"  ); 
   ObjectSet("FiboUpMonthly",OBJPROP_FIRSTLEVEL+4,0.618); 	ObjectSetFiboDescription("FiboUpMonthly",4,"(161.8%) -  %$"  ); 
   ObjectSet("FiboUpMonthly",OBJPROP_FIRSTLEVEL+5,0.764); 	ObjectSetFiboDescription("FiboUpMonthly",5,"(176.4%) -  %$"  ); 
   ObjectSet("FiboUpMonthly",OBJPROP_FIRSTLEVEL+6,1.000); 	ObjectSetFiboDescription("FiboUpMonthly",6,"(200.0%) -  %$"  ); 
   ObjectSet("FiboUpMonthly",OBJPROP_FIRSTLEVEL+7,1.236);   ObjectSetFiboDescription("FiboUpMonthly",7,"(223.6%) -  %$"  ); 
   ObjectSet("FiboUpMonthly",OBJPROP_FIRSTLEVEL+8,1.500);   ObjectSetFiboDescription("FiboUpMonthly",8,"(250.0%) -  %$"  ); 
   ObjectSet("FiboUpMonthly",OBJPROP_FIRSTLEVEL+9,1.618);	ObjectSetFiboDescription("FiboUpMonthly",9,"(261.8%) -  %$"  ); 
   ObjectSet("FiboUpMonthly",OBJPROP_FIRSTLEVEL+10,2.000);	ObjectSetFiboDescription("FiboUpMonthly",10,"(300.0%) -  %$"  ); 
   ObjectSet("FiboUpMonthly",OBJPROP_FIRSTLEVEL+11,2.500);	ObjectSetFiboDescription("FiboUpMonthly",11,"(350.0%) -  %$"  ); 
   ObjectSet("FiboUpMonthly",OBJPROP_FIRSTLEVEL+12,3.000);	ObjectSetFiboDescription("FiboUpMonthly",12,"(400.0%) -  %$"  ); 
   ObjectSet("FiboUpMonthly",OBJPROP_FIRSTLEVEL+13,3.500);	ObjectSetFiboDescription("FiboUpMonthly",13,"(450.0%) -  %$"  ); 
   ObjectSet("FiboUpMonthly",OBJPROP_FIRSTLEVEL+14,4.000);	ObjectSetFiboDescription("FiboUpMonthly",14,"(500.0%) -  %$"  ); 
   ObjectSet("FiboUpMonthly",OBJPROP_RAY,true);
   ObjectSet("FiboUpMonthly",OBJPROP_BACK,true);
}
   else
	   ObjectDelete("FiboUpMonthly");
//~~~~~~~~~~~~~~~
	if(DisplayLowerFibo)
	{	
	   LoPrice		= iLow (NULL,PERIOD_MN1,banzai);
	   StartTime	= iTime(NULL,PERIOD_MN1,banzai);
	if(ObjectFind("FiboDownMonthly") == -1)
		ObjectCreate("FiboDownMonthly",OBJ_FIBO,0,StartTime,LoPrice-Range,StartTime,LoPrice);
	else
	{
		ObjectSet("FiboDownMonthly",OBJPROP_TIME2, StartTime);
		ObjectSet("FiboDownMonthly",OBJPROP_TIME1, StartTime);
		ObjectSet("FiboDownMonthly",OBJPROP_PRICE1,LoPrice-Range);
		ObjectSet("FiboDownMonthly",OBJPROP_PRICE2,LoPrice);
	}
   ObjectSet("FiboDownMonthly",OBJPROP_LEVELSTYLE,LowerFiboStyle);
   ObjectSet("FiboDownMonthly",OBJPROP_LEVELWIDTH,LowerFiboWidth);
   ObjectSet("FiboDownMonthly",OBJPROP_LEVELCOLOR,LowerFiboColor);      
   ObjectSet("FiboDownMonthly",OBJPROP_FIBOLEVELS,19);
   ObjectSet("FiboDownMonthly",OBJPROP_FIRSTLEVEL+0,0.0);	   
	if (!DisplayMainFibo)
       ObjectSetFiboDescription("FiboDownMonthly",0,"(0.0%) -  %$  "); 
   else
       ObjectSetFiboDescription("FiboDownMonthly",0,""); 
   ObjectSet("FiboDownMonthly",OBJPROP_FIRSTLEVEL+1,0.236); 	ObjectSetFiboDescription("FiboDownMonthly",1,"(-23.6%) -  %$"  ); 
   ObjectSet("FiboDownMonthly",OBJPROP_FIRSTLEVEL+2,0.382); 	ObjectSetFiboDescription("FiboDownMonthly",2,"(-38.2%) -  %$"  ); 
   ObjectSet("FiboDownMonthly",OBJPROP_FIRSTLEVEL+3,0.500); 	ObjectSetFiboDescription("FiboDownMonthly",3,"(-50.0%) -  %$"  ); 
   ObjectSet("FiboDownMonthly",OBJPROP_FIRSTLEVEL+4,0.618); 	ObjectSetFiboDescription("FiboDownMonthly",4,"(-61.8%) -  %$"  ); 
   ObjectSet("FiboDownMonthly",OBJPROP_FIRSTLEVEL+5,0.764); 	ObjectSetFiboDescription("FiboDownMonthly",5,"(-76.4%) -  %$"  ); 
   ObjectSet("FiboDownMonthly",OBJPROP_FIRSTLEVEL+6,1.000);  	ObjectSetFiboDescription("FiboDownMonthly",6,"(-100.0%) -  %$"  ); 
   ObjectSet("FiboDownMonthly",OBJPROP_FIRSTLEVEL+7,1.236); 	ObjectSetFiboDescription("FiboDownMonthly",7,"(-123.6%) -  %$"  ); 
   ObjectSet("FiboDownMonthly",OBJPROP_FIRSTLEVEL+8,1.382);	   ObjectSetFiboDescription("FiboDownMonthly",8,"(-138.2%) -  %$"  ); 
   ObjectSet("FiboDownMonthly",OBJPROP_FIRSTLEVEL+9,1.500); 	ObjectSetFiboDescription("FiboDownMonthly",9,"(-150.0%) -  %$"  ); 
   ObjectSet("FiboDownMonthly",OBJPROP_FIRSTLEVEL+10,1.618);	ObjectSetFiboDescription("FiboDownMonthly",10,"(-161.8%) -  %$  "); 
   ObjectSet("FiboDownMonthly",OBJPROP_FIRSTLEVEL+11,1.764);	ObjectSetFiboDescription("FiboDownMonthly",11,"(-176.4%) -  %$  "); 
   ObjectSet("FiboDownMonthly",OBJPROP_FIRSTLEVEL+12,2.000);	ObjectSetFiboDescription("FiboDownMonthly",12,"(-200.0%) -  %$  "); 
   ObjectSet("FiboDownMonthly",OBJPROP_FIRSTLEVEL+13,2.500);	ObjectSetFiboDescription("FiboDownMonthly",13,"(-250.0%) -  %$  "); 
   ObjectSet("FiboDownMonthly",OBJPROP_FIRSTLEVEL+14,3.000);	ObjectSetFiboDescription("FiboDownMonthly",14,"(-300.0%) -  %$  "); 
   ObjectSet("FiboDownMonthly",OBJPROP_FIRSTLEVEL+15,3.500);	ObjectSetFiboDescription("FiboDownMonthly",15,"(-350.0%) -  %$  "); 
   ObjectSet("FiboDownMonthly",OBJPROP_FIRSTLEVEL+16,4.000);	ObjectSetFiboDescription("FiboDownMonthly",16,"(-400.0%) -  %$  "); 
   ObjectSet("FiboDownMonthly",OBJPROP_FIRSTLEVEL+17,4.500);	ObjectSetFiboDescription("FiboDownMonthly",17,"(-450.0%) -  %$  "); 
   ObjectSet("FiboDownMonthly",OBJPROP_FIRSTLEVEL+18,5.000);	ObjectSetFiboDescription("FiboDownMonthly",18,"(-500.0%) -  %$  "); 
   ObjectSet("FiboDownMonthly",OBJPROP_RAY,true);
   ObjectSet("FiboDownMonthly",OBJPROP_BACK,true);
}
   else
	   ObjectDelete("FiboDownMonthly");
//~~~~~~~~~~~
	if(DisplayMainFibo)
	{
	   HiPrice		= iHigh(NULL,PERIOD_MN1,banzai);
	   LoPrice		= iLow (NULL,PERIOD_MN1,banzai);
	   StartTime	= iTime(NULL,PERIOD_MN1,banzai);
		if(ObjectFind("FiboInMonthly") == -1)
			ObjectCreate("FiboInMonthly",OBJ_FIBO,0,StartTime,HiPrice,StartTime+PERIOD_MN1*60,LoPrice);
		else
		{
			ObjectSet("FiboInMonthly",OBJPROP_TIME2, StartTime);
			ObjectSet("FiboInMonthly",OBJPROP_TIME1, StartTime+PERIOD_MN1*60);
			ObjectSet("FiboInMonthly",OBJPROP_PRICE1,HiPrice);
			ObjectSet("FiboInMonthly",OBJPROP_PRICE2,LoPrice);
		}
      ObjectSet("FiboInMonthly",OBJPROP_LEVELSTYLE,MainFiboStyle);
      ObjectSet("FiboInMonthly",OBJPROP_LEVELWIDTH,MainFiboWidth);
   	ObjectSet("FiboInMonthly",OBJPROP_LEVELCOLOR,MainFiboColor); 
   	ObjectSet("FiboInMonthly",OBJPROP_FIBOLEVELS,7);
   	ObjectSet("FiboInMonthly",OBJPROP_FIRSTLEVEL+0,0.0);	   ObjectSetFiboDescription("FiboInMonthly",0,"Last Month LOW (0.0) -  %$  "); 
   	ObjectSet("FiboInMonthly",OBJPROP_FIRSTLEVEL+1,0.236);	ObjectSetFiboDescription("FiboInMonthly",1,"Month (23.6) -  %$  "); 
   	ObjectSet("FiboInMonthly",OBJPROP_FIRSTLEVEL+2,0.382);	ObjectSetFiboDescription("FiboInMonthly",2,"Month (38.2) -  %$  "); 
   	ObjectSet("FiboInMonthly",OBJPROP_FIRSTLEVEL+3,0.500);	ObjectSetFiboDescription("FiboInMonthly",3,"Month (50.0) -  %$  "); 
   	ObjectSet("FiboInMonthly",OBJPROP_FIRSTLEVEL+4,0.618);	ObjectSetFiboDescription("FiboInMonthly",4,"Month (61.8) -  %$  "); 
   	ObjectSet("FiboInMonthly",OBJPROP_FIRSTLEVEL+5,0.764);	ObjectSetFiboDescription("FiboInMonthly",5,"Month (76.4) -  %$  "); 
   	ObjectSet("FiboInMonthly",OBJPROP_FIRSTLEVEL+6,1.000);	ObjectSetFiboDescription("FiboInMonthly",6,"Last Month HIGH (100.0) -  %$  "); 
   	ObjectSet("FiboInMonthly",OBJPROP_RAY,true);
   	ObjectSet("FiboInMonthly",OBJPROP_BACK,true);
   }
   else
	   ObjectDelete("FiboInMonthly");
	return (0);
}
//+------------------------------------------------------------------+
int start()
{
   handleButtonClicks();
   recalc = false;
	   int banzai	= iBarShift(NULL,PERIOD_MN1,Time[0]) + 1;	// yesterday
	   HiPrice		= iHigh(NULL,PERIOD_MN1,banzai);
	   LoPrice		= iLow (NULL,PERIOD_MN1,banzai);
	   StartTime	= iTime(NULL,PERIOD_MN1,banzai);

	   if(TimeDayOfWeek(StartTime)==0/*Sunday*/)
	   {//Add fridays high and low
		   HiPrice = MathMax(HiPrice,iHigh(NULL,PERIOD_MN1,banzai+1));
		   LoPrice = MathMin(LoPrice,iLow(NULL,PERIOD_MN1,banzai+1));
	   } //	   if(TimeDayOfWeek(StartTime)==0/*Sunday*/)

	   Range = HiPrice-LoPrice;   

      if (show_data)
      {
        ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_ON_color);
        DrawFibo();
      }
      else
      {
        ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_OFF_color);
        ObjectDelete("FiboUpMonthly");
        ObjectDelete("FiboDownMonthly");
        ObjectDelete("FiboInMonthly");
      }
   return(0);
}
//+------------------------------------------------------------------+
