// +------------------------------------------------------------------+
// |                                     Timeframe_zoom_per_chart.mq5 |
// |                                                  Girard Matthieu |
// |                                             https://www.mql5.com |
// +------------------------------------------------------------------+
#property copyright "Girard Matthieu / modified by Jagg / adapted for MT5 by Grok 3"
#property link      "https://www.mql5.com"
#property version   "1.6"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1

// Define a dummy plot (to suppress the warning)
#property indicator_label1  "Dummy"
#property indicator_type1   DRAW_NONE
#property indicator_color1  clrNONE

struct SChartProperties
{
   int    scale;
   bool   isFixedScale;
   bool   tradelevel;
} ChartProperties;

bool Init = false;

//--- Global variable to store the latest close price
double latestClosePrice = 0.0;

//--- Input Parameters
input ENUM_BASE_CORNER Corner = CORNER_LEFT_LOWER;   // Corner
input color Border_Color_Active = clrDodgerBlue;     // Border Color (Active)
input color Border_Color_Inactive = clrDimGray;      // Border Color (Inactive)
input color Background_Color_Active = C'32,39,48';   // Background Color (Active)
input color Background_Color_Inactive = C'32,39,48'; // Background Color (Inactive)
input color Text_Color_Active = clrDodgerBlue;       // Text Color (Active)
input color Text_Color_Inactive = clrSilver;         // Text Color (Inactive)
color Text_Color_Inactive2 = clrDimGray;             // Text Color (Inactive)
input int Text_Font_Size = 7;                        // Font Size
input int XShift_2 = 2;                              // X Shift (horizontal)
input int YShift_2 = 2;                              // Y Shift (vertical)
input int ButtonMarginSize = 2;                      // Space between buttons
input string _ = "***********************";
input bool Show_M1 = true;                           // Show M1 Button
input bool Show_M2 = false;                          // Show M2 Button
input bool Show_M3 = false;                          // Show M3 Button
input bool Show_M4 = false;                          // Show M4 Button
input bool Show_M5 = true;                           // Show M5 Button
input bool Show_M6 = false;                          // Show M6 Button
input bool Show_M10 = false;                         // Show M10 Button
input bool Show_M12 = false;                         // Show M12 Button
input bool Show_M15 = true;                          // Show M15 Button
input bool Show_M30 = false;                         // Show M30 Button
input bool Show_H1 = false;                          // Show H1 Button
input bool Show_H2 = false;                          // Show H2 Button
input bool Show_H3 = false;                          // Show H3 Button
input bool Show_H4 = false;                          // Show H4 Button
input bool Show_H6 = false;                          // Show H6 Button
input bool Show_H8 = false;                          // Show H8 Button
input bool Show_H12 = false;                         // Show H12 Button
input bool Show_D1 = false;                          // Show D1 Button
input bool Show_W1 = false;                          // Show W1 Button
input bool Show_MN1 = false;                         // Show MN Button
input bool Show_Zi = true;                           // Show Zoom In Button
input bool Show_Zo = true;                           // Show Zoom Out Button
input bool Show_ChartScaleFix = true;                // Show Chart ScaleFix Button
input bool Show_TradeLevel = false;                  // Show Trade Level Button

int XShift = 1;     // Horizontal shift
int XShift2 = 1;
int YShift = 16;    // Vertical shift

//---
color chart_color_background_active, chart_color_background_inactive;
double DPIScaling;
double dummyBuffer[]; // Dummy buffer for MT5

// +------------------------------------------------------------------+
// | Custom indicator initialization function                         |
// +------------------------------------------------------------------+
int OnInit()
{
   // Set dummy buffer
   SetIndexBuffer(0, dummyBuffer, INDICATOR_DATA);
   PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_NONE); // No drawing

   ChartProperties.isFixedScale = (bool)ChartGetInteger(0, CHART_SCALEFIX);
   ChartProperties.scale = (int)ChartScaleGet();
   ChartProperties.tradelevel = (bool)ChartGetInteger(0, CHART_SHOW_TRADE_LEVELS);

   // Terminal DPI scaling
   int desktopScreenDPI = TerminalInfoInteger(TERMINAL_SCREEN_DPI);
   DPIScaling = desktopScreenDPI > 96.0 ? desktopScreenDPI / 96.0 : 1.0;

   if((Corner == CORNER_RIGHT_LOWER) || (Corner == CORNER_RIGHT_UPPER))
   {
      XShift = 26;     // Horizontal shift
      XShift2 = 26;
   }
   if(Corner == CORNER_RIGHT_UPPER)
   {
      YShift = 20;     // Vertical shift
   }
   if(XShift_2 != 0)
   {
      XShift += XShift_2;
      XShift2 += XShift_2;
   }
   if(YShift_2 != 0)
   {
      YShift += YShift_2;
   }

   if(Background_Color_Active == clrNONE)
   {
      chart_color_background_active = (color)ChartGetInteger(0, CHART_COLOR_BACKGROUND, 0);
   }
   else
   {
      chart_color_background_active = Background_Color_Active;
   }
   if(Background_Color_Inactive == clrNONE)
   {
      chart_color_background_inactive = (color)ChartGetInteger(0, CHART_COLOR_BACKGROUND, 0);
   }
   else
   {
      chart_color_background_inactive = Background_Color_Inactive;
   }

   if(Show_Zi)
   {
      ButtonCreate(0, "TZPC_ZommInButton", 0, XShift, YShift, 25, 15, Corner, "Z+", "Arial", Text_Font_Size + 1, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);
      ObjectSetString(0, "TZPC_ZommInButton", OBJPROP_TOOLTIP, "Zoom In");
      XShift += ButtonMarginSize;
   }
   else
   {
      XShift = MathMin(XShift - 26, 1);
   }
   if(Show_Zo)
   {
      ButtonCreate(0, "TZPC_ZommOutButton", 0, 26 + XShift, YShift, 25, 15, Corner, "Z-", "Arial", Text_Font_Size + 1, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);
      ObjectSetString(0, "TZPC_ZommOutButton", OBJPROP_TOOLTIP, "Zoom Out");
      XShift += ButtonMarginSize;
   }
   else
   {
      XShift = MathMin(XShift - 26, 1);
   }

   if(Show_ChartScaleFix)
   {
      if(ChartGetInteger(0, CHART_SCALEFIX, 0) == true)
      {
         ButtonCreate(0, "TZPC_ScaleFix", 0, XShift2, YShift + 16 + ButtonMarginSize, 25, 15, Corner, "CS", "Arial", Text_Font_Size + 1, Text_Color_Active, chart_color_background_active, Border_Color_Active, false, false, false, false, 0);
         ObjectSetString(0, "TZPC_ScaleFix", OBJPROP_TOOLTIP, "Turn OFF Chart ScaleFix");
         ButtonCreate(0, "TZPC_ScaleFix+", 0, XShift2 + 26 + ButtonMarginSize, YShift + 16 + ButtonMarginSize, 25, 15, Corner, "CS+", "Arial", Text_Font_Size + 1, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);
         ObjectSetString(0, "TZPC_ScaleFix+", OBJPROP_TOOLTIP, "ScaleFix [+]");
         ButtonCreate(0, "TZPC_ScaleFix-", 0, XShift2 + 52 + 2 * ButtonMarginSize, YShift + 16 + ButtonMarginSize, 25, 15, Corner, "CS-", "Arial", Text_Font_Size + 1, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);
         ObjectSetString(0, "TZPC_ScaleFix-", OBJPROP_TOOLTIP, "ScaleFix [-]");
      }
      else
      {
         ButtonCreate(0, "TZPC_ScaleFix", 0, XShift2, YShift + 16 + ButtonMarginSize, 25, 15, Corner, "CS", "Arial", Text_Font_Size + 1, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);
         ObjectSetString(0, "TZPC_ScaleFix", OBJPROP_TOOLTIP, "Turn ON Chart ScaleFix");
      }
   }

   if(Show_TradeLevel)
   {
      if(ChartGetInteger(0, CHART_SHOW_TRADE_LEVELS, 0) == true)
      {
         ButtonCreate(0, "TZPC_TradeLevel", 0, XShift2, YShift + 16 * 2 + ButtonMarginSize * 2, 25, 15, Corner, "TL", "Arial", Text_Font_Size + 1, Text_Color_Active, chart_color_background_active, Border_Color_Active, false, false, false, false, 0);
         ObjectSetString(0, "TZPC_TradeLevel", OBJPROP_TOOLTIP, "Turn OFF Trade Levels");
      }
      else
      {
         ButtonCreate(0, "TZPC_TradeLevel", 0, XShift2, YShift + 16 * 2 + ButtonMarginSize * 2, 25, 15, Corner, "TL", "Arial", Text_Font_Size + 1, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);
         ObjectSetString(0, "TZPC_TradeLevel", OBJPROP_TOOLTIP, "Turn ON Trade Levels");
      }
   }

   // --- M1 button
   if(Show_M1)
   {
      if(Period() == PERIOD_M1)
      {
         ButtonCreate(0, "TZPC_M1Button", 0, 52 + XShift, YShift, 25, 15, Corner, "M1", "Arial", Text_Font_Size, Text_Color_Active, chart_color_background_active, Border_Color_Active, false, false, false, false, 0);
      }
      else
      {
         ButtonCreate(0, "TZPC_M1Button", 0, 52 + XShift, YShift, 25, 15, Corner, "M1", "Arial", Text_Font_Size, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);
      }
      ObjectSetString(0, "TZPC_M1Button", OBJPROP_TOOLTIP, "Switch to M1 period");
      XShift += ButtonMarginSize;
   }
   else
   {
      XShift = MathMin(XShift - 26, 1);
   }

   // --- M2 button
   if(Show_M2)
   {
      if(Period() == PERIOD_M2)
      {
         ButtonCreate(0, "TZPC_M2Button", 0, 52 + XShift, YShift, 25, 15, Corner, "M2", "Arial", Text_Font_Size, Text_Color_Active, chart_color_background_active, Border_Color_Active, false, false, false, false, 0);
      }
      else
      {
         ButtonCreate(0, "TZPC_M2Button", 0, 52 + XShift, YShift, 25, 15, Corner, "M2", "Arial", Text_Font_Size, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);
      }
      ObjectSetString(0, "TZPC_M2Button", OBJPROP_TOOLTIP, "Switch to M2 period");
      XShift += ButtonMarginSize;
   }
   else
   {
      XShift = MathMin(XShift - 26, 1);
   }

   // --- M3 button
   if(Show_M3)
   {
      if(Period() == PERIOD_M3)
      {
         ButtonCreate(0, "TZPC_M3Button", 0, 52 + XShift, YShift, 25, 15, Corner, "M3", "Arial", Text_Font_Size, Text_Color_Active, chart_color_background_active, Border_Color_Active, false, false, false, false, 0);
      }
      else
      {
         ButtonCreate(0, "TZPC_M3Button", 0, 52 + XShift, YShift, 25, 15, Corner, "M3", "Arial", Text_Font_Size, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);
      }
      ObjectSetString(0, "TZPC_M3Button", OBJPROP_TOOLTIP, "Switch to M3 period");
      XShift += ButtonMarginSize;
   }
   else
   {
      XShift = MathMin(XShift - 26, 1);
   }

   // --- M4 button
   if(Show_M4)
   {
      if(Period() == PERIOD_M4)
      {
         ButtonCreate(0, "TZPC_M4Button", 0, 52 + XShift, YShift, 25, 15, Corner, "M4", "Arial", Text_Font_Size, Text_Color_Active, chart_color_background_active, Border_Color_Active, false, false, false, false, 0);
      }
      else
      {
         ButtonCreate(0, "TZPC_M4Button", 0, 52 + XShift, YShift, 25, 15, Corner, "M4", "Arial", Text_Font_Size, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);
      }
      ObjectSetString(0, "TZPC_M4Button", OBJPROP_TOOLTIP, "Switch to M4 period");
      XShift += ButtonMarginSize;
   }
   else
   {
      XShift = MathMin(XShift - 26, 1);
   }

   // --- M5 button
   if(Show_M5)
   {
      if(Period() == PERIOD_M5)
      {
         ButtonCreate(0, "TZPC_M5Button", 0, 52 + XShift, YShift, 25, 15, Corner, "M5", "Arial", Text_Font_Size, Text_Color_Active, chart_color_background_active, Border_Color_Active, false, false, false, false, 0);
      }
      else
      {
         ButtonCreate(0, "TZPC_M5Button", 0, 52 + XShift, YShift, 25, 15, Corner, "M5", "Arial", Text_Font_Size, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);
      }
      ObjectSetString(0, "TZPC_M5Button", OBJPROP_TOOLTIP, "Switch to 5 Minutes period");
      XShift += ButtonMarginSize;
   }
   else
   {
      XShift = MathMin(XShift - 26, 1);
   }

   // --- M6 button
   if(Show_M6)
   {
      if(Period() == PERIOD_M6)
      {
         ButtonCreate(0, "TZPC_M6Button", 0, 52 + XShift, YShift, 25, 15, Corner, "M6", "Arial", Text_Font_Size, Text_Color_Active, chart_color_background_active, Border_Color_Active, false, false, false, false, 0);
      }
      else
      {
         ButtonCreate(0, "TZPC_M6Button", 0, 52 + XShift, YShift, 25, 15, Corner, "M6", "Arial", Text_Font_Size, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);
      }
      ObjectSetString(0, "TZPC_M6Button", OBJPROP_TOOLTIP, "Switch to M6 period");
      XShift += ButtonMarginSize;
   }
   else
   {
      XShift = MathMin(XShift - 26, 1);
   }

   // --- M10 button
   if(Show_M10)
   {
      if(Period() == PERIOD_M10)
      {
         ButtonCreate(0, "TZPC_M10Button", 0, 52 + XShift, YShift, 25, 15, Corner, "M10", "Arial", Text_Font_Size, Text_Color_Active, chart_color_background_active, Border_Color_Active, false, false, false, false, 0);
      }
      else
      {
         ButtonCreate(0, "TZPC_M10Button", 0, 52 + XShift, YShift, 25, 15, Corner, "M10", "Arial", Text_Font_Size, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);
      }
      ObjectSetString(0, "TZPC_M10Button", OBJPROP_TOOLTIP, "Switch to M10 period");
      XShift += ButtonMarginSize;
   }
   else
   {
      XShift = MathMin(XShift - 26, 1);
   }

   // --- M12 button
   if(Show_M12)
   {
      if(Period() == PERIOD_M12)
      {
         ButtonCreate(0, "TZPC_M12Button", 0, 52 + XShift, YShift, 25, 15, Corner, "M12", "Arial", Text_Font_Size, Text_Color_Active, chart_color_background_active, Border_Color_Active, false, false, false, false, 0);
      }
      else
      {
         ButtonCreate(0, "TZPC_M12Button", 0, 52 + XShift, YShift, 25, 15, Corner, "M12", "Arial", Text_Font_Size, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);
      }
      ObjectSetString(0, "TZPC_M12Button", OBJPROP_TOOLTIP, "Switch to M12 period");
      XShift += ButtonMarginSize;
   }
   else
   {
      XShift = MathMin(XShift - 26, 1);
   }

   // --- M15 button
   if(Show_M15)
   {
      if(Period() == PERIOD_M15)
      {
         ButtonCreate(0, "TZPC_M15Button", 0, 52 + XShift, YShift, 25, 15, Corner, "M15", "Arial", Text_Font_Size, Text_Color_Active, chart_color_background_active, Border_Color_Active, false, false, false, false, 0);
      }
      else
      {
         ButtonCreate(0, "TZPC_M15Button", 0, 52 + XShift, YShift, 25, 15, Corner, "M15", "Arial", Text_Font_Size, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);
      }
      ObjectSetString(0, "TZPC_M15Button", OBJPROP_TOOLTIP, "Switch to 15 Minutes period");
      XShift += ButtonMarginSize;
   }
   else
   {
      XShift = MathMin(XShift - 26, 1);
   }

   // --- M30 button
   if(Show_M30)
   {
      if(Period() == PERIOD_M30)
      {
         ButtonCreate(0, "TZPC_M30Button", 0, 52 + XShift, YShift, 25, 15, Corner, "M30", "Arial", Text_Font_Size, Text_Color_Active, chart_color_background_active, Border_Color_Active, false, false, false, false, 0);
      }
      else
      {
         ButtonCreate(0, "TZPC_M30Button", 0, 52 + XShift, YShift, 25, 15, Corner, "M30", "Arial", Text_Font_Size, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);
      }
      ObjectSetString(0, "TZPC_M30Button", OBJPROP_TOOLTIP, "Switch to 30 Minutes period");
      XShift += ButtonMarginSize;
   }
   else
   {
      XShift = MathMin(XShift - 26, 1);
   }

   // --- H1 button
   if(Show_H1)
   {
      if(Period() == PERIOD_H1)
      {
         ButtonCreate(0, "TZPC_H1Button", 0, 52 + XShift, YShift, 25, 15, Corner, "H1", "Arial", Text_Font_Size, Text_Color_Active, chart_color_background_active, Border_Color_Active, false, false, false, false, 0);
      }
      else
      {
         ButtonCreate(0, "TZPC_H1Button", 0, 52 + XShift, YShift, 25, 15, Corner, "H1", "Arial", Text_Font_Size, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);
      }
      ObjectSetString(0, "TZPC_H1Button", OBJPROP_TOOLTIP, "Switch to 1 Hour period");
      XShift += ButtonMarginSize;
   }
   else
   {
      XShift = MathMin(XShift - 26, 1);
   }

   // --- H2 button
   if(Show_H2)
   {
      if(Period() == PERIOD_H2)
      {
         ButtonCreate(0, "TZPC_H2Button", 0, 52 + XShift, YShift, 25, 15, Corner, "H2", "Arial", Text_Font_Size, Text_Color_Active, chart_color_background_active, Border_Color_Active, false, false, false, false, 0);
      }
      else
      {
         ButtonCreate(0, "TZPC_H2Button", 0, 52 + XShift, YShift, 25, 15, Corner, "H2", "Arial", Text_Font_Size, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);
      }
      ObjectSetString(0, "TZPC_H2Button", OBJPROP_TOOLTIP, "Switch to 2 Hours period");
      XShift += ButtonMarginSize;
   }
   else
   {
      XShift = MathMin(XShift - 26, 1);
   }

   // --- H3 button
   if(Show_H3)
   {
      if(Period() == PERIOD_H3)
      {
         ButtonCreate(0, "TZPC_H3Button", 0, 52 + XShift, YShift, 25, 15, Corner, "H3", "Arial", Text_Font_Size, Text_Color_Active, chart_color_background_active, Border_Color_Active, false, false, false, false, 0);
      }
      else
      {
         ButtonCreate(0, "TZPC_H3Button", 0, 52 + XShift, YShift, 25, 15, Corner, "H3", "Arial", Text_Font_Size, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);
      }
      ObjectSetString(0, "TZPC_H3Button", OBJPROP_TOOLTIP, "Switch to 3 Hours period");
      XShift += ButtonMarginSize;
   }
   else
   {
      XShift = MathMin(XShift - 26, 1);
   }

   // --- H4 button
   if(Show_H4)
   {
      if(Period() == PERIOD_H4)
      {
         ButtonCreate(0, "TZPC_H4Button", 0, 52 + XShift, YShift, 25, 15, Corner, "H4", "Arial", Text_Font_Size, Text_Color_Active, chart_color_background_active, Border_Color_Active, false, false, false, false, 0);
      }
      else
      {
         ButtonCreate(0, "TZPC_H4Button", 0, 52 + XShift, YShift, 25, 15, Corner, "H4", "Arial", Text_Font_Size, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);
      }
      ObjectSetString(0, "TZPC_H4Button", OBJPROP_TOOLTIP, "Switch to 4 Hours period");
      XShift += ButtonMarginSize;
   }
   else
   {
      XShift = MathMin(XShift - 26, 1);
   }

   // --- H6 button
   if(Show_H6)
   {
      if(Period() == PERIOD_H6)
      {
         ButtonCreate(0, "TZPC_H6Button", 0, 52 + XShift, YShift, 25, 15, Corner, "H6", "Arial", Text_Font_Size, Text_Color_Active, chart_color_background_active, Border_Color_Active, false, false, false, false, 0);
      }
      else
      {
         ButtonCreate(0, "TZPC_H6Button", 0, 52 + XShift, YShift, 25, 15, Corner, "H6", "Arial", Text_Font_Size, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);
      }
      ObjectSetString(0, "TZPC_H6Button", OBJPROP_TOOLTIP, "Switch to 6 Hours period");
      XShift += ButtonMarginSize;
   }
   else
   {
      XShift = MathMin(XShift - 26, 1);
   }

   // --- H8 button
   if(Show_H8)
   {
      if(Period() == PERIOD_H8)
      {
         ButtonCreate(0, "TZPC_H8Button", 0, 52 + XShift, YShift, 25, 15, Corner, "H8", "Arial", Text_Font_Size, Text_Color_Active, chart_color_background_active, Border_Color_Active, false, false, false, false, 0);
      }
      else
      {
         ButtonCreate(0, "TZPC_H8Button", 0, 52 + XShift, YShift, 25, 15, Corner, "H8", "Arial", Text_Font_Size, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);
      }
      ObjectSetString(0, "TZPC_H8Button", OBJPROP_TOOLTIP, "Switch to 8 Hours period");
      XShift += ButtonMarginSize;
   }
   else
   {
      XShift = MathMin(XShift - 26, 1);
   }

   // --- H12 button
   if(Show_H12)
   {
      if(Period() == PERIOD_H12)
      {
         ButtonCreate(0, "TZPC_H12Button", 0, 52 + XShift, YShift, 25, 15, Corner, "H12", "Arial", Text_Font_Size, Text_Color_Active, chart_color_background_active, Border_Color_Active, false, false, false, false, 0);
      }
      else
      {
         ButtonCreate(0, "TZPC_H12Button", 0, 52 + XShift, YShift, 25, 15, Corner, "H12", "Arial", Text_Font_Size, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);
      }
      ObjectSetString(0, "TZPC_H12Button", OBJPROP_TOOLTIP, "Switch to 12 Hours period");
      XShift += ButtonMarginSize;
   }
   else
   {
      XShift = MathMin(XShift - 26, 1);
   }

   // --- D1 button
   if(Show_D1)
   {
      if(Period() == PERIOD_D1)
      {
         ButtonCreate(0, "TZPC_D1Button", 0, 52 + XShift, YShift, 25, 15, Corner, "D1", "Arial", Text_Font_Size, Text_Color_Active, chart_color_background_active, Border_Color_Active, false, false, false, false, 0);
      }
      else
      {
         ButtonCreate(0, "TZPC_D1Button", 0, 52 + XShift, YShift, 25, 15, Corner, "D1", "Arial", Text_Font_Size, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);
      }
      ObjectSetString(0, "TZPC_D1Button", OBJPROP_TOOLTIP, "Switch to 1 Day period");
      XShift += ButtonMarginSize;
   }
   else
   {
      XShift = MathMin(XShift - 26, 1);
   }

   // --- W1 button
   if(Show_W1)
   {
      if(Period() == PERIOD_W1)
      {
         ButtonCreate(0, "TZPC_W1Button", 0, 52 + XShift, YShift, 25, 15, Corner, "W1", "Arial", Text_Font_Size, Text_Color_Active, chart_color_background_active, Border_Color_Active, false, false, false, false, 0);
      }
      else
      {
         ButtonCreate(0, "TZPC_W1Button", 0, 52 + XShift, YShift, 25, 15, Corner, "W1", "Arial", Text_Font_Size, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);
      }
      ObjectSetString(0, "TZPC_W1Button", OBJPROP_TOOLTIP, "Switch to 1 Week period");
      XShift += ButtonMarginSize;
   }
   else
   {
      XShift = MathMin(XShift - 26, 1);
   }

   // --- MN button
   if(Show_MN1)
   {
      if(Period() == PERIOD_MN1)
      {
         ButtonCreate(0, "TZPC_MNButton", 0, 52 + XShift, YShift, 25, 15, Corner, "MN", "Arial", Text_Font_Size, Text_Color_Active, chart_color_background_active, Border_Color_Active, false, false, false, false, 0);
      }
      else
      {
         ButtonCreate(0, "TZPC_MNButton", 0, 52 + XShift, YShift, 25, 15, Corner, "MN", "Arial", Text_Font_Size, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);
      }
      ObjectSetString(0, "TZPC_MNButton", OBJPROP_TOOLTIP, "Switch to 1 Month period");
      XShift += ButtonMarginSize;
   }
   else
   {
      XShift = MathMin(XShift - 26, 1);
   }

   return(INIT_SUCCEEDED);
}

// +------------------------------------------------------------------+
// | Deinitialization function                                        |
// +------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   ObjectsDeleteAll(0, "TZPC");
}

// +------------------------------------------------------------------+
// | Custom indicator iteration function                              |
// +------------------------------------------------------------------+
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[])
{
   // Set dummy buffer values (to suppress the warning)
   for(int i = 0; i < rates_total; i++)
   {
      dummyBuffer[i] = 0.0; // Dummy data
   }

   // Update the latest close price
   if(rates_total > 0)
   {
      latestClosePrice = close[rates_total - 1]; // Get the latest close price
   }

   if(Init) return(rates_total);
   Init = true;

   return(rates_total);
}

// +------------------------------------------------------------------+
// | ChartEvent function                                              |
// +------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
   if(!Init) return;

   if(id == CHARTEVENT_OBJECT_CLICK)
   {
      if(sparam == "TZPC_ZommInButton")
      {
         int current_chartscale = ChartScaleGet();
         ChartScaleSet(MathMin(5, current_chartscale + 1));
         ObjectSetInteger(0, "TZPC_ZommInButton", OBJPROP_STATE, false);
         return;
      }
      if(sparam == "TZPC_ZommOutButton")
      {
         int current_chartscale = ChartScaleGet();
         ChartScaleSet(MathMax(0, current_chartscale - 1));
         ObjectSetInteger(0, "TZPC_ZommOutButton", OBJPROP_STATE, false);
         return;
      }
      if(sparam == "TZPC_M1Button")
      {
         ChartSetSymbolPeriod(ChartID(), _Symbol, PERIOD_M1);
         ObjectSetInteger(0, "TZPC_M1Button", OBJPROP_STATE, false);
         return;
      }
      if(sparam == "TZPC_M2Button")
      {
         ChartSetSymbolPeriod(ChartID(), _Symbol, PERIOD_M2);
         ObjectSetInteger(0, "TZPC_M2Button", OBJPROP_STATE, false);
         return;
      }
      if(sparam == "TZPC_M3Button")
      {
         ChartSetSymbolPeriod(ChartID(), _Symbol, PERIOD_M3);
         ObjectSetInteger(0, "TZPC_M3Button", OBJPROP_STATE, false);
         return;
      }
      if(sparam == "TZPC_M4Button")
      {
         ChartSetSymbolPeriod(ChartID(), _Symbol, PERIOD_M4);
         ObjectSetInteger(0, "TZPC_M4Button", OBJPROP_STATE, false);
         return;
      }
      if(sparam == "TZPC_M5Button")
      {
         ChartSetSymbolPeriod(ChartID(), _Symbol, PERIOD_M5);
         ObjectSetInteger(0, "TZPC_M5Button", OBJPROP_STATE, false);
         return;
      }
      if(sparam == "TZPC_M6Button")
      {
         ChartSetSymbolPeriod(ChartID(), _Symbol, PERIOD_M6);
         ObjectSetInteger(0, "TZPC_M6Button", OBJPROP_STATE, false);
         return;
      }
      if(sparam == "TZPC_M10Button")
      {
         ChartSetSymbolPeriod(ChartID(), _Symbol, PERIOD_M10);
         ObjectSetInteger(0, "TZPC_M10Button", OBJPROP_STATE, false);
         return;
      }
      if(sparam == "TZPC_M12Button")
      {
         ChartSetSymbolPeriod(ChartID(), _Symbol, PERIOD_M12);
         ObjectSetInteger(0, "TZPC_M12Button", OBJPROP_STATE, false);
         return;
      }
      if(sparam == "TZPC_M15Button")
      {
         ChartSetSymbolPeriod(ChartID(), _Symbol, PERIOD_M15);
         ObjectSetInteger(0, "TZPC_M15Button", OBJPROP_STATE, false);
         return;
      }
      if(sparam == "TZPC_M30Button")
      {
         ChartSetSymbolPeriod(ChartID(), _Symbol, PERIOD_M30);
         ObjectSetInteger(0, "TZPC_M30Button", OBJPROP_STATE, false);
         return;
      }
      if(sparam == "TZPC_H1Button")
      {
         ChartSetSymbolPeriod(ChartID(), _Symbol, PERIOD_H1);
         ObjectSetInteger(0, "TZPC_H1Button", OBJPROP_STATE, false);
         return;
      }
      if(sparam == "TZPC_H2Button")
      {
         ChartSetSymbolPeriod(ChartID(), _Symbol, PERIOD_H2);
         ObjectSetInteger(0, "TZPC_H2Button", OBJPROP_STATE, false);
         return;
      }
      if(sparam == "TZPC_H3Button")
      {
         ChartSetSymbolPeriod(ChartID(), _Symbol, PERIOD_H3);
         ObjectSetInteger(0, "TZPC_H3Button", OBJPROP_STATE, false);
         return;
      }
      if(sparam == "TZPC_H4Button")
      {
         ChartSetSymbolPeriod(ChartID(), _Symbol, PERIOD_H4);
         ObjectSetInteger(0, "TZPC_H4Button", OBJPROP_STATE, false);
         return;
      }
      if(sparam == "TZPC_H6Button")
      {
         ChartSetSymbolPeriod(ChartID(), _Symbol, PERIOD_H6);
         ObjectSetInteger(0, "TZPC_H6Button", OBJPROP_STATE, false);
         return;
      }
      if(sparam == "TZPC_H8Button")
      {
         ChartSetSymbolPeriod(ChartID(), _Symbol, PERIOD_H8);
         ObjectSetInteger(0, "TZPC_H8Button", OBJPROP_STATE, false);
         return;
      }
      if(sparam == "TZPC_H12Button")
      {
         ChartSetSymbolPeriod(ChartID(), _Symbol, PERIOD_H12);
         ObjectSetInteger(0, "TZPC_H12Button", OBJPROP_STATE, false);
         return;
      }
      if(sparam == "TZPC_D1Button")
      {
         ChartSetSymbolPeriod(ChartID(), _Symbol, PERIOD_D1);
         ObjectSetInteger(0, "TZPC_D1Button", OBJPROP_STATE, false);
         return;
      }
      if(sparam == "TZPC_W1Button")
      {
         ChartSetSymbolPeriod(ChartID(), _Symbol, PERIOD_W1);
         ObjectSetInteger(0, "TZPC_W1Button", OBJPROP_STATE, false);
         return;
      }
      if(sparam == "TZPC_MNButton")
      {
         ChartSetSymbolPeriod(ChartID(), _Symbol, PERIOD_MN1);
         ObjectSetInteger(0, "TZPC_MNButton", OBJPROP_STATE, false);
         return;
      }

      // Button Chart Scale
      if(sparam == "TZPC_ScaleFix")
      {
         if(ChartGetInteger(0, CHART_SCALEFIX, 0) == true)
         {
            ChartSetInteger(0, CHART_SCALEFIX, 0, false);
            ObjectDelete(ChartID(), "TZPC_ScaleFix+");
            ObjectDelete(ChartID(), "TZPC_ScaleFix-");
            ObjectDelete(ChartID(), "TZPC_ScaleFix");
            ButtonCreate(0, "TZPC_ScaleFix", 0, XShift2, YShift + 16 + ButtonMarginSize, 25, 15, Corner, "CS", "Arial", Text_Font_Size + 1, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);
            ObjectSetString(0, "TZPC_ScaleFix", OBJPROP_TOOLTIP, "Turn ON Chart ScaleFix");
            ChartRedraw();
         }
         else
         {
            int chart_space_in_percent = 10;
            int iLeft = iBars(_Symbol, _Period) - 1; // Most recent bar
            int barsPerChart = (int)ChartGetInteger(0, CHART_VISIBLE_BARS);
            int iRight = iLeft - barsPerChart;
            if(iRight < 0) iRight = 0;

            double bars_price_max = iHigh(_Symbol, _Period, iHighest(_Symbol, _Period, MODE_HIGH, iLeft - iRight + 1, iRight));
            double bars_price_min = iLow(_Symbol, _Period, iLowest(_Symbol, _Period, MODE_LOW, iLeft - iRight + 1, iRight));

            double chart_price_max_to_min_diff_half = ((bars_price_max - bars_price_min) / 2);
            double chart_price_max_adjusted_close_in_the_middle = latestClosePrice + chart_price_max_to_min_diff_half;
            double chart_price_min_adjusted_close_in_the_middle = latestClosePrice - chart_price_max_to_min_diff_half;

            ChartSetInteger(0, CHART_SCALEFIX, 0, true);
            ChartSetDouble(0, CHART_FIXED_MAX, chart_price_max_adjusted_close_in_the_middle);
            ChartSetDouble(0, CHART_FIXED_MIN, chart_price_min_adjusted_close_in_the_middle);

            ObjectDelete(ChartID(), "TZPC_ScaleFix");
            ButtonCreate(0, "TZPC_ScaleFix", 0, XShift2, YShift + 16 + ButtonMarginSize, 25, 15, Corner, "CS", "Arial", Text_Font_Size + 1, Text_Color_Active, chart_color_background_active, Border_Color_Active, false, false, false, false, 0);
            ObjectSetString(0, "TZPC_ScaleFix", OBJPROP_TOOLTIP, "Turn OFF Chart ScaleFix");
            ButtonCreate(0, "TZPC_ScaleFix+", 0, XShift2 + 26 + ButtonMarginSize, YShift + 16 + ButtonMarginSize, 25, 15, Corner, "CS+", "Arial", Text_Font_Size + 1, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);
            ButtonCreate(0, "TZPC_ScaleFix-", 0, XShift2 + 52 + 2 * ButtonMarginSize, YShift + 16 + ButtonMarginSize, 25, 15, Corner, "CS-", "Arial", Text_Font_Size + 1, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);

            ChartRedraw();
         }
      }

      // Button [+] Chart Scale
      if(sparam == "TZPC_ScaleFix+")
      {
         ChartSetDouble(0, CHART_FIXED_MAX, ChartPriceMax() - ((ChartPriceMax() - ChartPriceMin()) / 5));
         ChartSetDouble(0, CHART_FIXED_MIN, ChartPriceMin() + ((ChartPriceMax() - ChartPriceMin()) / 5));
         ObjectSetInteger(0, "TZPC_ScaleFix+", OBJPROP_STATE, false);
         ChartRedraw();
         return;
      }

      // Button [-] Chart Scale
      if(sparam == "TZPC_ScaleFix-")
      {
         ChartSetDouble(0, CHART_FIXED_MAX, ChartPriceMax() + ((ChartPriceMax() - ChartPriceMin()) / 5));
         ChartSetDouble(0, CHART_FIXED_MIN, ChartPriceMin() - ((ChartPriceMax() - ChartPriceMin()) / 5));
         ObjectSetInteger(0, "TZPC_ScaleFix-", OBJPROP_STATE, false);
         ChartRedraw();
         return;
      }

      // Button Trade Level
      if(sparam == "TZPC_TradeLevel")
      {
         int h = (int)ChartGetInteger(0, CHART_SHOW_TRADE_LEVELS);
         ObjectDelete(ChartID(), "TZPC_TradeLevel");
         if(h != 0)
         {
            ChartSetInteger(ChartID(), CHART_SHOW_TRADE_LEVELS, false);
            ButtonCreate(0, "TZPC_TradeLevel", 0, XShift2, YShift + 16 * 2 + ButtonMarginSize * 2, 25, 15, Corner, "TL", "Arial", Text_Font_Size + 1, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);
            ObjectSetString(0, "TZPC_TradeLevel", OBJPROP_TOOLTIP, "Turn ON Trade Levels");
         }
         else
         {
            ChartSetInteger(ChartID(), CHART_SHOW_TRADE_LEVELS, true);
            ButtonCreate(0, "TZPC_TradeLevel", 0, XShift2, YShift + 16 * 2 + ButtonMarginSize * 2, 25, 15, Corner, "TL", "Arial", Text_Font_Size + 1, Text_Color_Active, chart_color_background_active, Border_Color_Active, false, false, false, false, 0);
            ObjectSetString(0, "TZPC_TradeLevel", OBJPROP_TOOLTIP, "Turn OFF Trade Levels");
         }
      }
   }

   if(id == CHARTEVENT_CHART_CHANGE)
   {
      // Detect a change of the trade level property 'from outside'
      if(Show_TradeLevel)
      {
         int tl = (int)ChartGetInteger(0, CHART_SHOW_TRADE_LEVELS);
         if(tl != ChartProperties.tradelevel)
         {
            ObjectDelete(ChartID(), "TZPC_TradeLevel");
            ChartProperties.tradelevel = tl;
            if(!tl)
            {
               ChartSetInteger(ChartID(), CHART_SHOW_TRADE_LEVELS, false);
               ButtonCreate(0, "TZPC_TradeLevel", 0, XShift2, YShift + 16 * 2 + ButtonMarginSize * 2, 25, 15, Corner, "TL", "Arial", Text_Font_Size + 1, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);
               ObjectSetString(0, "TZPC_TradeLevel", OBJPROP_TOOLTIP, "Turn ON Trade Levels");
            }
            else
            {
               ChartSetInteger(ChartID(), CHART_SHOW_TRADE_LEVELS, true);
               ButtonCreate(0, "TZPC_TradeLevel", 0, XShift2, YShift + 16 * 2 + ButtonMarginSize * 2, 25, 15, Corner, "TL", "Arial", Text_Font_Size + 1, Text_Color_Active, chart_color_background_active, Border_Color_Active, false, false, false, false, 0);
               ObjectSetString(0, "TZPC_TradeLevel", OBJPROP_TOOLTIP, "Turn OFF Trade Levels");
            }
         }
      }

      // Detect a change in chart scalefix 'from outside'
      if(Show_ChartScaleFix)
      {
         bool isFixedScale = (bool)ChartGetInteger(0, CHART_SCALEFIX);
         if(isFixedScale != ChartProperties.isFixedScale)
         {
            ChartProperties.isFixedScale = isFixedScale;
            if(!isFixedScale)
            {
               ObjectDelete(ChartID(), "TZPC_ScaleFix+");
               ObjectDelete(ChartID(), "TZPC_ScaleFix-");
               ObjectDelete(ChartID(), "TZPC_ScaleFix");
               ButtonCreate(0, "TZPC_ScaleFix", 0, XShift2, YShift + 16 + ButtonMarginSize, 25, 15, Corner, "CS", "Arial", Text_Font_Size + 1, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);
               ObjectSetString(0, "TZPC_ScaleFix", OBJPROP_TOOLTIP, "Turn ON Chart ScaleFix");
            }
            else
            {
               ObjectDelete(ChartID(), "TZPC_ScaleFix");
               ButtonCreate(0, "TZPC_ScaleFix", 0, XShift2, YShift + 16 + ButtonMarginSize, 25, 15, Corner, "CS", "Arial", Text_Font_Size + 1, Text_Color_Active, chart_color_background_active, Border_Color_Active, false, false, false, false, 0);
               ObjectSetString(0, "TZPC_ScaleFix", OBJPROP_TOOLTIP, "Turn OFF Chart ScaleFix");
               ButtonCreate(0, "TZPC_ScaleFix+", 0, XShift2 + 26 + ButtonMarginSize, YShift + 16 + ButtonMarginSize, 25, 15, Corner, "CS+", "Arial", Text_Font_Size + 1, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);
               ButtonCreate(0, "TZPC_ScaleFix-", 0, XShift2 + 52 + 2 * ButtonMarginSize, YShift + 16 + ButtonMarginSize, 25, 15, Corner, "CS-", "Arial", Text_Font_Size + 1, Text_Color_Inactive, chart_color_background_inactive, Border_Color_Inactive, false, false, false, false, 0);
            }
         }
      }

      // Detect a change in chart size or properties 'from outside'
      if(Show_Zo || Show_Zi)
      {
         int scale = (int)ChartScaleGet();
         if(scale != ChartProperties.scale)
         {
            ChartProperties.scale = scale;
            if(scale == 0)
            {
               ObjectSetInteger(0, "TZPC_ZommOutButton", OBJPROP_COLOR, Text_Color_Inactive2);
            }
            else if(scale == 5)
            {
               ObjectSetInteger(0, "TZPC_ZommInButton", OBJPROP_COLOR, Text_Color_Inactive2);
            }
            else
            {
               ObjectSetInteger(0, "TZPC_ZommInButton", OBJPROP_COLOR, Text_Color_Inactive);
               ObjectSetInteger(0, "TZPC_ZommOutButton", OBJPROP_COLOR, Text_Color_Inactive);
            }
         }
      }
   }
}

// +------------------------------------------------------------------+
// | Button creation function                                         |
// +------------------------------------------------------------------+
bool ButtonCreate(const long chart_ID = 0,
                  const string name = "Button",
                  const int sub_window = 0,
                  const int xx = 0,
                  const int yy = 0,
                  const int width = 50,
                  const int height = 18,
                  const ENUM_BASE_CORNER cornerr = CORNER_LEFT_UPPER,
                  const string text = "Button",
                  const string font = "Arial",
                  const int font_size = 10,
                  const color clr = clrBlack,
                  const color back_clr = C'236,233,216',
                  const color border_clr = clrNONE,
                  const bool state = false,
                  const bool back = false,
                  const bool selection = false,
                  const bool hidden = true,
                  const long z_order = 0)
{
   ResetLastError();
   if(ObjectFind(0, name) < 0)
   {
      if(!ObjectCreate(chart_ID, name, OBJ_BUTTON, sub_window, 0, 0))
      {
         Print(__FUNCTION__, ": failed to create the button! Error code = ", GetLastError());
         return(false);
      }
      ObjectSetInteger(chart_ID, name, OBJPROP_XDISTANCE, (int)(xx * DPIScaling));
      ObjectSetInteger(chart_ID, name, OBJPROP_YDISTANCE, (int)(yy * DPIScaling));
      ObjectSetInteger(chart_ID, name, OBJPROP_XSIZE, (int)(width * DPIScaling));
      ObjectSetInteger(chart_ID, name, OBJPROP_YSIZE, (int)(height * DPIScaling));
      ObjectSetInteger(chart_ID, name, OBJPROP_CORNER, cornerr);
      ObjectSetString(chart_ID, name, OBJPROP_TEXT, text);
      ObjectSetString(chart_ID, name, OBJPROP_FONT, font);
      ObjectSetInteger(chart_ID, name, OBJPROP_FONTSIZE, font_size);
      ObjectSetInteger(chart_ID, name, OBJPROP_COLOR, clr);
      ObjectSetInteger(chart_ID, name, OBJPROP_BGCOLOR, back_clr);
      ObjectSetInteger(chart_ID, name, OBJPROP_BORDER_COLOR, border_clr);
      ObjectSetInteger(chart_ID, name, OBJPROP_BACK, back);
      ObjectSetInteger(chart_ID, name, OBJPROP_STATE, state);
      ObjectSetInteger(chart_ID, name, OBJPROP_SELECTABLE, selection);
      ObjectSetInteger(chart_ID, name, OBJPROP_SELECTED, selection);
      ObjectSetInteger(chart_ID, name, OBJPROP_HIDDEN, hidden);
      ObjectSetInteger(chart_ID, name, OBJPROP_ZORDER, z_order);
   }
   return(true);
}

// +------------------------------------------------------------------+
// | Get chart scale (from 0 to 5).                                   |
// +------------------------------------------------------------------+
int ChartScaleGet(const long chart_ID = 0)
{
   long result = -1;
   ResetLastError();
   if(!ChartGetInteger(chart_ID, CHART_SCALE, 0, result))
   {
      Print(__FUNCTION__ + ", Error Code = ", GetLastError());
   }
   return((int)result);
}

// +------------------------------------------------------------------+
// | Set chart scale (from 0 to 5).                                   |
// +------------------------------------------------------------------+
bool ChartScaleSet(const long value, const long chart_ID = 0)
{
   ResetLastError();
   if(!ChartSetInteger(chart_ID, CHART_SCALE, 0, value))
   {
      Print(__FUNCTION__ + ", Error Code = ", GetLastError());
      return(false);
   }
   return(true);
}

// +------------------------------------------------------------------+
// | Get chart maximum price                                          |
// +------------------------------------------------------------------+
double ChartPriceMax(const long chart_ID = 0, const int sub_window = 0)
{
   double result = EMPTY_VALUE;
   ResetLastError();
   if(!ChartGetDouble(chart_ID, CHART_PRICE_MAX, sub_window, result))
   {
      Print(__FUNCTION__ + ", Error Code = ", GetLastError());
   }
   return(result);
}

// +------------------------------------------------------------------+
// | Get chart minimum price                                          |
// +------------------------------------------------------------------+
double ChartPriceMin(const long chart_ID = 0, const int sub_window = 0)
{
   double result = EMPTY_VALUE;
   ResetLastError();
   if(!ChartGetDouble(chart_ID, CHART_PRICE_MIN, sub_window, result))
   {
      Print(__FUNCTION__ + ", Error Code = ", GetLastError());
   }
   return(result);
}