//+------------------------------------------------------------------+
//|                                           DailyFibo_ChatGPT4.mq4 |
//|                                          Copyright 2022 mql5.com |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022 mql5.com"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
//---- Inputs
input int DaysInHistory     = 5;  // Number of Days to Plot
input color Color_50        = clrGold;   // 50% Line Color
input ENUM_LINE_STYLE Style_50          = STYLE_SOLID;  // 50% Line Style
input int Width_50          = 2;  // 50% Line Width

input color Color_Top       = clrDodgerBlue;  // Top Lines Color
input ENUM_LINE_STYLE Style_Top         = STYLE_SOLID;  // Top Lines Style
input int Width_Top         = 1;  // Top Lines Width

input color Color_Bottom    = clrOrangeRed;  // Bottom Lines Color
input ENUM_LINE_STYLE Style_Bottom      = STYLE_SOLID;  // Bottom Lines Style
input int Width_Bottom      = 1;  // Bottom Lines Width

string Indi_ID = "FIBO_";
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//---
   //--- Delete old Fibonacci levels
   ObjectsDeleteAll(0, "FIBO_");
   
   datetime todayStart, dayStart;
   int counted = 0;
   
   for(int d = 0; d < DaysInHistory; d++) // Loop over the requested history days
   {
      // Find the day's start time
      dayStart = iTime(NULL, PERIOD_D1, d);
      todayStart = iTime(NULL, PERIOD_D1, 0);
      
      if(dayStart <= 0 || dayStart > todayStart) continue;
      
      // Get the High and Low for the day
      double dayHigh = iHigh(NULL, PERIOD_D1, d);
      double dayLow = iLow(NULL, PERIOD_D1, d);
      
      if(dayHigh == 0 || dayLow == 0) continue;

      // Calculate levels
      double levels[7];
      levels[0] = dayLow;                // 0%
      levels[1] = dayLow + (dayHigh - dayLow) * 0.236; // 23.6%
      levels[2] = dayLow + (dayHigh - dayLow) * 0.382; // 38.2%
      levels[3] = dayLow + (dayHigh - dayLow) * 0.500; // 50.0%
      levels[4] = dayLow + (dayHigh - dayLow) * 0.618; // 61.8%
      levels[5] = dayLow + (dayHigh - dayLow) * 0.764; // 76.4%
      levels[6] = dayHigh;               // 100%
      
      // Draw Levels for the Day
      string prefix = Indi_ID + TimeToString(dayStart, TIME_DATE);
      for(int i = 0; i <= 6; i++)
      {
         color lineColor;
         int lineStyle, lineWidth;
         
         // Set colors and styles
         if(levels[i] == levels[3])  // 50% level
         {
            lineColor = Color_50;
            lineStyle = Style_50;
            lineWidth = Width_50;
         }
         else if(levels[i] > levels[3]) // Top lines
         {
            lineColor = Color_Top;
            lineStyle = Style_Top;
            lineWidth = Width_Top;
         }
         else if(levels[i] < levels[3]) // Bottom lines
         {
            lineColor = Color_Bottom;
            lineStyle = Style_Bottom;
            lineWidth = Width_Bottom;
         }
         
         string objName = prefix + "_L" + IntegerToString(i);
         
         ObjectCreate(0, objName, OBJ_TREND, 0, dayStart, levels[i], dayStart + 86400, levels[i]);
         ObjectSetInteger(0, objName, OBJPROP_COLOR, lineColor);
         ObjectSetInteger(0, objName, OBJPROP_STYLE, lineStyle);
         ObjectSetInteger(0, objName, OBJPROP_WIDTH, lineWidth);
         ObjectSetInteger(0, objName, OBJPROP_RAY_RIGHT, false);
      }
      counted++;
   }
   
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//----
               objectsDelete(Indi_ID);
               ChartRedraw(ChartID());
//----
  }
//+------------------------------------------------------------------+
void objectsDelete(string name)
{
   int objTotal = ObjectsTotal(ChartID());
   for(int i=objTotal-1; i>=0; i--)  
  {
      string objName = ObjectName(ChartID(),i);
      if(StringFind(objName,name,0) >= 0)
       ObjectDelete(0,objName);
   }
}
//+------------------------------------------------------------------+
