
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red

//---- input parameters
input int DaysToShow = 10;         // Number of days to show the daily open line
input int OpenHour = 0;           // Hour of the custom open time
input int OpenMinute = 0;         // Minute of the custom open time

//---- buffers
double OpenBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   // Indicator buffers mapping
   SetIndexBuffer(0, OpenBuffer);
   
   // Set the line style and width
   SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2);
   
   // Set the label for the line
   IndicatorShortName("Daily Open Line");
   
   // No need to set indicator level values, we use buffers
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   // Cleanup code here
  }
//+------------------------------------------------------------------+
//| 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[])
  {
   // Ensure we have enough bars
   if(rates_total < 1)
      return(0);
   
   int counted_bars = prev_calculated;

   // Loop through each bar
   for(int i = counted_bars; i < rates_total; i++)
   {
      // Get the date and time of the current bar
      datetime currentTime = time[i];
      int currentDay = TimeDay(currentTime);
      int currentMonth = TimeMonth(currentTime);
      int currentYear = TimeYear(currentTime);
      
      // Loop through the specified number of days to show
      bool found = false;
      for(int d = 0; d < DaysToShow; d++)
      {
         datetime dayStart = iTime(NULL, PERIOD_D1, d);
         datetime customOpenTime = dayStart + OpenHour * 3600 + OpenMinute * 60;
         datetime customCloseTime = customOpenTime + 86400; // 86400 seconds = 24 hours
         
         if (currentTime >= customOpenTime && currentTime < customCloseTime)
         {
            // Get the custom open price for this day
            int barIndex = iBarShift(NULL, PERIOD_M1, customOpenTime, true);
            double customOpenPrice = iOpen(NULL, PERIOD_M1, barIndex);
            OpenBuffer[i] = customOpenPrice;
            found = true;
         }
         else if (currentTime >= customCloseTime)
         {
            break;
         }
         else
         {
            OpenBuffer[i] = EMPTY_VALUE;
         }
      }
      
      // If not found, set buffer to empty value
      if (!found)
      {
         OpenBuffer[i] = EMPTY_VALUE;
      }
   }

   return(rates_total);
  }
//+------------------------------------------------------------------+