//+------------------------------------------------------------------+
//|                                             MultiLookbackLines.mq5|
//+------------------------------------------------------------------+
#property copyright "ChatGPT"
#property version   "1.40"
#property strict

input ENUM_TIMEFRAMES InpTimeframe      = PERIOD_H1;   // Timeframe to look back on
input int            InpLookbackCandles = 10;          // Base number of candles
input double         InpMultiplier      = 7.0;          // Max multiplier (integer part used)

#define PREFIX_HIGH "MultiHL_High_"
#define PREFIX_LOW  "MultiHL_Low_"

datetime lastBarTime = 0;

//+------------------------------------------------------------------+
int OnInit()
  {
   ClearLines();
   Print("MultiLookbackLines EA initialized");
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   ClearLines();
  }
//+------------------------------------------------------------------+
void OnTick()
  {
   datetime currentTime = iTime(_Symbol, _Period, 0);
   if(currentTime == lastBarTime)
      return; // no new bar
   lastBarTime = currentTime;

   int maxMult = (int)MathFloor(InpMultiplier);
   if(maxMult < 1)
      maxMult = 1;

   for(int m=1; m<=maxMult; m++)
     {
      int barsToLookBack = InpLookbackCandles * m;

      MqlRates rates[];
      int copied = CopyRates(_Symbol, InpTimeframe, 1, barsToLookBack, rates); // skip current forming bar
      if(copied <= 0)
        {
         PrintFormat("CopyRates failed for %d bars on timeframe %s", barsToLookBack, EnumToString(InpTimeframe));
         continue;
        }

      double highest = -DBL_MAX;
      double lowest  =  DBL_MAX;

      for(int i=0; i<copied; i++)
        {
         if(rates[i].high > highest)
            highest = rates[i].high;
         if(rates[i].low < lowest)
            lowest = rates[i].low;
        }

      string highLineName = PREFIX_HIGH + IntegerToString(m);
      string lowLineName  = PREFIX_LOW  + IntegerToString(m);

      DrawOrUpdateLine(highLineName, highest, clrRed);
      DrawOrUpdateLine(lowLineName, lowest, clrBlue);

      // Optional debug:
      PrintFormat("Multiplier %d (%d bars): High=%.5f  Low=%.5f", m, barsToLookBack, highest, lowest);
     }
  }
//+------------------------------------------------------------------+
void DrawOrUpdateLine(string name, double price, color clr)
  {
   if(ObjectFind(0, name) == -1)
     {
      if(!ObjectCreate(0, name, OBJ_HLINE, 0, 0, price))
        {
         Print("Failed to create line: " + name);
         return;
        }
      ObjectSetInteger(0, name, OBJPROP_COLOR, clr);
      ObjectSetInteger(0, name, OBJPROP_WIDTH, 2);
      ObjectSetInteger(0, name, OBJPROP_STYLE, STYLE_DOT);
      ObjectSetInteger(0, name, OBJPROP_BACK, true);
      ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false);
      ObjectSetInteger(0, name, OBJPROP_SELECTED, false);
     }
   else
     {
      ObjectSetDouble(0, name, OBJPROP_PRICE, price);
     }
  }
//+------------------------------------------------------------------+
void ClearLines()
  {
   int total = ObjectsTotal(0);
   for(int i=total-1; i>=0; i--)
     {
      string name = ObjectName(0, i);
      if(StringFind(name, PREFIX_HIGH) == 0 || StringFind(name, PREFIX_LOW) == 0)
         ObjectDelete(0, name);
     }
  }
//+------------------------------------------------------------------+