//+------------------------------------------------------------------+
//|                                           Rectangle Extender.mq4 |
//|                                    Copyright @ 2022, Billyenaire |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright @ 2022, Billyenaire"
#property link      ""
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_plots 0

int TFMigrate(int tf)
  {
   switch(tf)
     {
      case 0: return(0);
      case 1: return(1);
      case 5: return(5);
      case 15: return(15);
      case 30: return(30);
      case 60: return(60);
      case 240: return(240);
      case 1440: return(1440);
      case 10080: return(10080);
      case 43200: return(43200);
      
      case 2: return(2);
      case 3: return(3);
      case 4: return(4);      
      case 6: return(6);
      case 10: return(10);
      case 12: return(12);
      case 16385: return(60);
      case 16386: return(120);
      case 16387: return(180);
      case 16388: return(240);
      case 16390: return(360);
      case 16392: return(480);
      case 16396: return(720);
      case 16408: return(1440);
      case 32769: return(10080);
      case 49153: return(43200);      
      default: return(PERIOD_CURRENT);
     }
  }

input bool  Rectangle_Fill = true;
input color Rectangle_Color = clrGold;
input color Line_Color = clrWhite;
input int   Right_Offset = 13;


datetime bars = -1;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   //--- indicator buffers mapping
   ChartSetInteger(0,CHART_EVENT_OBJECT_CREATE,true);
   
   //---
   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[])
{
   for (int r = 0; r < ObjectsTotal(0); r++)
      {
         string name = ObjectName(0,r,0);
         if (StringFind(name,"Rectangle")>0&&ObjectGetInteger(0,name,OBJPROP_TYPE) == OBJ_RECTANGLE)
          {
         ObjectSetInteger(0,name, OBJPROP_TIME,1,iTime(_Symbol,_Period,0)+Right_Offset*TFMigrate(_Period)*60);
          }   
      }
   
   //--- return value of prev_calculated for next call
   return(rates_total);
}
//+------------------------------------------------------------------+
//| Custom indicator on chart event function                         |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{ 
   //--- Check the event belongs to the user events
   if (id == CHARTEVENT_OBJECT_CREATE)
   {
      if (StringFind(sparam, "Rectangle") > 0 && ObjectGetInteger(0, sparam, OBJPROP_TYPE) == OBJ_RECTANGLE_LABEL)
      {
         double leftPrice = ObjectGetDouble(0, sparam, OBJPROP_PRICE, 0);
         double rightPrice = ObjectGetDouble(0, sparam, OBJPROP_PRICE, 1);
         double distance = rightPrice - leftPrice;

         ObjectSetInteger(0, sparam, OBJPROP_FILL, Rectangle_Fill);
         ObjectSetInteger(0, sparam, OBJPROP_COLOR, Rectangle_Color);

         // Adjust the time property based on the distance between left and right sides
         ObjectSetInteger(0, sparam, OBJPROP_TIME, 0, iTime(_Symbol, _Period, 0) + int(distance) * TFMigrate(_Period) * 60);
         
         // Make the rectangle non-selectable (background)
         ObjectSetInteger(0, sparam, OBJPROP_SELECTABLE, false);
      }
   }
}


//+------------------------------------------------------------------+
