//+------------------------------------------------------------------+
//|                                           Rectangle Extender.mq4 |
//|                                      Copyright @ 2022, samsha786 |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright @ 2022, samsha786"
#property link      ""
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_plots 0

input color LineColour = White;

datetime bars = -1;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   //--- indicator buffers mapping
   EventSetMillisecondTimer(100);
   ChartSetInteger(0, CHART_EVENT_OBJECT_CREATE, true);
   ChartSetInteger(0, CHART_EVENT_OBJECT_DELETE, 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[])
{
   
   //--- return value of prev_calculated for next call
   return(rates_total);
}
//+------------------------------------------------------------------+
void OnTimer()
 {
      for (int r = 0; r < ObjectsTotal(0); r++)
      {
         string name = ObjectName(0,r,0);
         string name_line = name+"Line";
         if (ObjectGetInteger(0,name,OBJPROP_TYPE) == OBJ_RECTANGLE)
          {
         long t1 = ObjectGetInteger(0,name, OBJPROP_TIME);
         long t2 = ObjectGetInteger(0,name, OBJPROP_TIME,1);
         double p1 = ObjectGetDouble(0,name, OBJPROP_PRICE);
         double p2 = ObjectGetDouble(0,name, OBJPROP_PRICE,1);
         double line = (p1 + p2) / 2.0; 
         ObjectSetInteger(0,name, OBJPROP_COLOR, LineColour);
         ObjectSetInteger(0,name, OBJPROP_BACK, false);
         ObjectSetInteger(0,name, OBJPROP_TIME,1,iTime(_Symbol,_Period,0));
         if(ObjectFind(0,name_line)<0)
         ObjectCreate(0,name_line, OBJ_TREND, 0, 0, 0);
         ObjectSetInteger(0,name_line, OBJPROP_TIME, t1);
         ObjectSetInteger(0,name_line, OBJPROP_TIME,1, t2);
         ObjectSetDouble(0,name_line, OBJPROP_PRICE, line);
         ObjectSetDouble(0,name_line, OBJPROP_PRICE,1, line);
         ObjectSetInteger(0,name_line, OBJPROP_STYLE, STYLE_SOLID);
         ObjectSetInteger(0,name_line, OBJPROP_COLOR, LineColour);
         ObjectSetInteger(0,name_line, OBJPROP_BACK, false);
         ObjectSetInteger(0,name_line, OBJPROP_TIME,1,iTime(_Symbol,_Period,0));
          }    
      }
 }
//+------------------------------------------------------------------+
//| 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 (ObjectGetInteger(0,sparam,OBJPROP_TYPE) == OBJ_RECTANGLE)
      {
         ObjectSetInteger(0,sparam, OBJPROP_TIME,1,iTime(_Symbol,_Period,0));
      }
   }
   else if (id == CHARTEVENT_OBJECT_CLICK)
   {
      if (ObjectGetInteger(0,sparam,OBJPROP_TYPE) == OBJ_RECTANGLE&&ObjectGetInteger(0,sparam,OBJPROP_SELECTED) == true)
       {
         ObjectDelete(0,sparam);
         ObjectDelete(0,sparam+"Line");
       }
   }
}
//+------------------------------------------------------------------+
