//+------------------------------------------------------------------+
//|                                                 peatsfx-HPFZ.mq5 |
//|                                    Copyright 2013, A.E. Ivan, ST |
//|2024.11.04 to MQL5 by: https://www.forexfactory.com/besttraderev  |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, A.E. Ivan, ST"
#property link      "bengkel.ea.forex@gmail.com"

#property indicator_chart_window
#property indicator_plots 0

input group "peatsfx-HPFZ";
input group "bengkel.ea.forex@gmail.com";


// Input parameters for color and text settings
input color BalancePoint_Color = White;
input color Zone3_Color = Red;
input color Zone2_Color = Maroon;
input color Zone4_Color = Navy;
input color Zone5_Color = MidnightBlue;
input color Zone6_Color = DarkGreen; 
input color Zone7_Color = DarkOrange;
input color Text_Color = Goldenrod;
input int Text_Font_Size = 8;
input string Text_Font_Type = "Arial";
input bool Display_Text = true; // Option to display text (Yes/No)
input uint Extend_Right = 0;
input double Fibo_A = 0.618;
input double Fibo_B = 1.382;
input double Fibo_C = 2.423;
input double Fibo_D = 2.618;

// Global variables for decimal precision and calculations
int desimal;
double pengali;
uint ExtRight;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
  ExtRight=Extend_Right+1;
  if(Extend_Right<=0)
  ExtRight=1;
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   // Delete all objects created on the chart
   long id = ChartID();
   ObjectDelete(id,"BP-Text");
   ObjectDelete(id,"BP");
   ObjectDelete(id,"Zone-3");
   ObjectDelete(id,"Zone-3 Value");
   ObjectDelete(id,"Zone-2");
   ObjectDelete(id,"Zone-2 Value");  
   ObjectDelete(id,"Zone-4");
   ObjectDelete(id,"Zone-4 Value");  
   ObjectDelete(id,"Zone-5");
   ObjectDelete(id,"Zone-5 Value"); 
   ObjectDelete(id,"Zone-6");        // New 3rd Band
   ObjectDelete(id,"Zone-6 Value");  // New 3rd Band
   ObjectDelete(id,"Zone-7");        // New 3rd Band
   ObjectDelete(id,"Zone-7 Value");  // New 3rd Band
   ChartRedraw(id);  
  }
//+------------------------------------------------------------------+
//| 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[])
  {
   long id = ChartID();
   if(_Period>PERIOD_D1) 
    {
   ObjectDelete(id,"BP-Text");
   ObjectDelete(id,"BP");
   ObjectDelete(id,"Zone-3");
   ObjectDelete(id,"Zone-3 Value");
   ObjectDelete(id,"Zone-2");
   ObjectDelete(id,"Zone-2 Value");  
   ObjectDelete(id,"Zone-4");
   ObjectDelete(id,"Zone-4 Value");  
   ObjectDelete(id,"Zone-5");
   ObjectDelete(id,"Zone-5 Value"); 
   ObjectDelete(id,"Zone-6");        // New 3rd Band
   ObjectDelete(id,"Zone-6 Value");  // New 3rd Band
   ObjectDelete(id,"Zone-7");        // New 3rd Band
   ObjectDelete(id,"Zone-7 Value");  // New 3rd Band
   return(-1);
    }
   // Get the current day and previous day
   datetime current_day_time = iTime(Symbol(), PERIOD_D1, 0);
   int day_of_week = TimeDayOfWeek(current_day_time);  // 0 = Sunday, 1 = Monday, ..., 6 = Saturday

   int shift = 1;  // Default is 1 day back (for Monday-Friday)

   // If today is Monday, and the last trading day was Friday, set shift to 2 days back to skip Sunday
   if (day_of_week == 1) {
      shift = 2;
   }

   // Get the previous day's OHLC (Open, High, Low, Close) based on the adjusted shift
   double Daily_High_Prev = iHigh(Symbol(), PERIOD_D1, shift);  // Previous day high
   double Daily_Low_Prev = iLow(Symbol(), PERIOD_D1, shift);    // Previous day low
   double Daily_Close_Prev = iClose(Symbol(), PERIOD_D1, shift);// Previous day close

   // Calculate the previous day's range
   double Range_Harian_Prev = Daily_High_Prev - Daily_Low_Prev;

   // Calculate Fibonacci levels based on the previous day's range
   double fibo_range_A = Fibo_A * Range_Harian_Prev;
   double fibo_range_B = Fibo_B * Range_Harian_Prev;
   double fibo_range_C = Fibo_C * Range_Harian_Prev;   // New level
   double fibo_range_D = Fibo_D * Range_Harian_Prev;   // New level

   // Calculate the balance point (pivot point)
   double garis_tengah = (Daily_High_Prev + Daily_Low_Prev + Daily_Close_Prev) / 3;

   // Calculate zones based on the balance point and Fibonacci levels
   double zone_A = garis_tengah - Range_Harian_Prev;
   double zone_A1 = garis_tengah - fibo_range_B;
   double zone_B = garis_tengah - Range_Harian_Prev / 2;
   double zone_B1 = garis_tengah - fibo_range_A;
   double zone_F = garis_tengah - fibo_range_C;  // 3rd Band Support level
   double zone_G = garis_tengah - fibo_range_D;  // 3rd Band Support level
   double zone_D = garis_tengah + Range_Harian_Prev / 2;
   double zone_D1 = garis_tengah + fibo_range_A;
   double zone_E = garis_tengah + Range_Harian_Prev;
   double zone_E1 = garis_tengah + fibo_range_B;
   double zone_H = garis_tengah + fibo_range_C;  // 3rd Band Resistance level
   double zone_I = garis_tengah + fibo_range_D;  // 3rd Band Resistance level

   // Get the current day's time range to display the indicator
   datetime var1 = iTime(Symbol(), PERIOD_D1, 0);  // Start of the current day
   datetime var2 = var1 + PeriodSeconds(PERIOD_D1)*ExtRight;              // End of the current day
   desimal=_Digits;
   // Delete old objects before creating new ones

   if(IsNewBar(PERIOD_D1))
    {
   ObjectDelete(id,"BP-Text");
   ObjectDelete(id,"BP");

   ObjectDelete(id,"Zone-3");
   ObjectDelete(id,"Zone-3 Value");
 
   ObjectDelete(id,"Zone-2");
   ObjectDelete(id,"Zone-2 Value");
   
   ObjectDelete(id,"Zone-4");
   ObjectDelete(id,"Zone-4 Value"); 
   
   ObjectDelete(id,"Zone-5");
   ObjectDelete(id,"Zone-5 Value");    

   // Create Balance Point (BP) rectangle
   if (!ObjectCreate(id,"BP", OBJ_RECTANGLE, 0, var1, garis_tengah, var2, garis_tengah))
      Print("Error creating BP");
   ObjectSetInteger(id,"BP", OBJPROP_COLOR, BalancePoint_Color);
   ObjectSetInteger(id,"BP", OBJPROP_FILL, false);

   // Check if user wants to display text
   if (Display_Text)
     {
      // Create Balance Point (BP) Text
      if (!ObjectCreate(id,"BP-Text", OBJ_TEXT, 0, var2, garis_tengah))
         Print("Error creating BP-Text");
      ObjectSetText("BP-Text", "Balance Point (" + DoubleToString(garis_tengah, desimal) + ")", 
                    Text_Font_Size, Text_Font_Type, Text_Color);
      ObjectSetInteger(id,"BP-Text",OBJPROP_ANCHOR,ANCHOR_LEFT);

      // Create Zone 3 (Support Band - 1) Text
      if (!ObjectCreate(id,"Zone-3 Value", OBJ_TEXT, 0, var2, MathAbs(zone_B+zone_B1)/2))
         Print("Error creating Zone-3 Value");
      ObjectSetText("Zone-3 Value", "Support Band - 1", Text_Font_Size, Text_Font_Type, Text_Color);
      ObjectSetInteger(id,"Zone-3 Value",OBJPROP_ANCHOR,ANCHOR_LEFT);

      // Create Zone 2 (Support Band - 2) Text
      if (!ObjectCreate(id,"Zone-2 Value", OBJ_TEXT, 0, var2, MathAbs(zone_A+zone_A1)/2))
         Print("Error creating Zone-2 Value");
      ObjectSetText("Zone-2 Value", "Support Band - 2", Text_Font_Size, Text_Font_Type, Text_Color);
      ObjectSetInteger(id,"Zone-2 Value",OBJPROP_ANCHOR,ANCHOR_LEFT);

      // Create Zone 6 (Support Band - 3) Text
      if (!ObjectCreate(id,"Zone-6 Value", OBJ_TEXT, 0, var2, MathAbs(zone_G+zone_F)/2))
         Print("Error creating Zone-6 Value");
      ObjectSetText("Zone-6 Value", "Support Band - 3", Text_Font_Size, Text_Font_Type, Text_Color);
      ObjectSetInteger(id,"Zone-6 Value",OBJPROP_ANCHOR,ANCHOR_LEFT);

      // Create Zone 4 (Resistance Band - 1) Text
      if (!ObjectCreate(id,"Zone-4 Value", OBJ_TEXT, 0, var2, MathAbs(zone_D+zone_D1)/2))
         Print("Error creating Zone-4 Value");
      ObjectSetText("Zone-4 Value", "Resistance Band - 1", Text_Font_Size, Text_Font_Type, Text_Color);
      ObjectSetInteger(id,"Zone-4 Value",OBJPROP_ANCHOR,ANCHOR_LEFT);

      // Create Zone 5 (Resistance Band - 2) Text
      if (!ObjectCreate(id,"Zone-5 Value", OBJ_TEXT, 0, var2, MathAbs(zone_E+zone_E1)/2))
         Print("Error creating Zone-5 Value");
      ObjectSetText("Zone-5 Value", "Resistance Band - 2", Text_Font_Size, Text_Font_Type, Text_Color);
      ObjectSetInteger(id,"Zone-5 Value",OBJPROP_ANCHOR,ANCHOR_LEFT);

      // Create Zone 7 (Resistance Band - 3) Text
      if (!ObjectCreate(id,"Zone-7 Value", OBJ_TEXT, 0, var2, MathAbs(zone_I+zone_H)/2))
         Print("Error creating Zone-7 Value");
      ObjectSetText("Zone-7 Value", "Resistance Band - 3", Text_Font_Size, Text_Font_Type, Text_Color);
      ObjectSetInteger(id,"Zone-7 Value",OBJPROP_ANCHOR,ANCHOR_LEFT);
     }

   // Create the support and resistance zones
   if (!ObjectCreate(id,"Zone-3", OBJ_RECTANGLE, 0, var1, zone_B, var2, zone_B1))
      Print("Error creating Zone-3");
   ObjectSetInteger(id,"Zone-3", OBJPROP_COLOR, Zone3_Color);
   ObjectSetInteger(id,"Zone-3", OBJPROP_FILL, true);

   if (!ObjectCreate(id,"Zone-2", OBJ_RECTANGLE, 0, var1, zone_A, var2, zone_A1))
      Print("Error creating Zone-2");
   ObjectSetInteger(id,"Zone-2", OBJPROP_COLOR, Zone2_Color);
   ObjectSetInteger(id,"Zone-2", OBJPROP_FILL, true);

   if (!ObjectCreate(id,"Zone-4", OBJ_RECTANGLE, 0, var1, zone_D, var2, zone_D1))
      Print("Error creating Zone-4");
   ObjectSetInteger(id,"Zone-4", OBJPROP_COLOR, Zone4_Color);
   ObjectSetInteger(id,"Zone-4", OBJPROP_FILL, true);

   if (!ObjectCreate(id,"Zone-5", OBJ_RECTANGLE, 0, var1, zone_E, var2, zone_E1))
      Print("Error creating Zone-5");
   ObjectSetInteger(id,"Zone-5", OBJPROP_COLOR, Zone5_Color);
   ObjectSetInteger(id,"Zone-5", OBJPROP_FILL, true);

   // Create 3rd Band Support and Resistance zones
   if (!ObjectCreate(id,"Zone-6", OBJ_RECTANGLE, 0, var1, zone_F, var2, zone_G))
      Print("Error creating Zone-6");
   ObjectSetInteger(id,"Zone-6", OBJPROP_COLOR, Zone6_Color);
   ObjectSetInteger(id,"Zone-6", OBJPROP_FILL, true);

   if (!ObjectCreate(id,"Zone-7", OBJ_RECTANGLE, 0, var1, zone_H, var2, zone_I))
      Print("Error creating Zone-7");
   ObjectSetInteger(id,"Zone-7", OBJPROP_COLOR, Zone7_Color);
   ObjectSetInteger(id,"Zone-7", OBJPROP_FILL, true); 
   
   ChartRedraw(id);  
    }

   return(rates_total);
  }
//+------------------------------------------------------------------+
bool ObjectSetText(string name,
                       string text,
                       int font_size,
                       string font="",
                       color text_color=CLR_NONE)
  {
   int tmpObjType=(int)ObjectGetInteger(0,name,OBJPROP_TYPE);
   if(tmpObjType!=OBJ_LABEL && tmpObjType!=OBJ_TEXT) return(false);
   if(StringLen(text)>0 && font_size>0)
     {
      if(ObjectSetString(0,name,OBJPROP_TEXT,text)==true
         && ObjectSetInteger(0,name,OBJPROP_FONTSIZE,font_size)==true)
        {
         if((StringLen(font)>0)
            && ObjectSetString(0,name,OBJPROP_FONT,font)==false)
            return(false);
         if(ObjectSetInteger(0,name,OBJPROP_COLOR,text_color)==false)
            return(false);
         return(true);
        }
      return(false);
     }
   return(false);
  }
//+------------------------------------------------------------------+
bool IsNewBar(ENUM_TIMEFRAMES per)
{ 
  static datetime Trend_Candle_prevTime1 = -1;
  
  if(Trend_Candle_prevTime1 != iTime(_Symbol,per,6))
  { 
   Trend_Candle_prevTime1 = iTime(_Symbol,per,6); 
       
   return(true);  
  } 

  return(false); 
}
//+------------------------------------------------------------------+
int TimeDayOfWeek(datetime date)
  {
   MqlDateTime tm;
   TimeToStruct(date,tm);
   return(tm.day_of_week);
  }
//+------------------------------------------------------------------+
