//+------------------------------------------------------------------+
//|                                              !DROP_TRENDLINE.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

#include <stdlib.mqh>

int   BarToTheRight  = 5;

int    num=0;
string prefix = "MyRectangle_";
string rectName;
color  rectColor = Lavender;
datetime tLeft=0,tRight=0, tTemp=0;
int tempIndex=0, pos=0;
double pUpper=0, pLower=0;

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   tLeft = ChartTimeOnDropped();
   tRight = Time[0] + BarToTheRight* Period()*60;
   tempIndex = iBarShift(_Symbol, _Period, tLeft, true);

   pUpper = High[tempIndex];
   pLower = Low[tempIndex];

   for(int i=ObjectsTotal(0, 0, OBJ_RECTANGLE)-1; i>=0; i--) {
      if(StringSubstr(ObjectName(i), 0, StringLen(prefix))!="") {
         pos++;
         continue;
      }
   }
   if(ObjectsTotal(OBJ_RECTANGLE)==-1)
      pos=0;

   string temp = prefix+IntegerToString(pos);
   createRectangle(temp, rectColor, tLeft, pUpper, tRight, pLower);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
// Move rectangle                                                    |
//+------------------------------------------------------------------+
void createRectangle (string    &name,      // line name
                      color     &clr,
                      datetime  &time1,
                      double    &price1,
                      datetime  &time2,
                      double    &price2)
{
//ObjectDelete(0, name);
//--- reset the error value
   ResetLastError();
//--- create a rectangle by the given coordinates
   if(ObjectFind(0, name)<0) {
      if(!ObjectCreate(0, name, OBJ_RECTANGLE, 0, time1, price1, time2, price2)) {
         Print(__FUNCTION__, ": failed to create a rectangle! Error code = ", GetLastError());
         return;
      }
   }
//--- set line color
   ObjectSetInteger(0, name, OBJPROP_COLOR, clr);
//--- set line display style
   ObjectSetInteger(0, name, OBJPROP_STYLE, STYLE_DASH);
//--- set line width
   ObjectSetInteger(0, name, OBJPROP_WIDTH, 1);
//--- enable (true) or disable (false) the mode of filling the rectangle
   ObjectSetInteger(0, name, OBJPROP_FILL, true);
//--- display in the foreground (false) or background (true)
   ObjectSetInteger(0, name, OBJPROP_BACK, false);
//--- enable (true) or disable (false) the mode of highlighting the rectangle for moving
//--- when creating a graphical object using ObjectCreate function, the object cannot be
//--- highlighted and moved by default. Inside this method, selection parameter
//--- is true by default making it possible to highlight and move the object
   ObjectSetInteger(0,name,OBJPROP_SELECTABLE,true);
   ObjectSetInteger(0,name,OBJPROP_SELECTED,false);
//--- hide (true) or display (false) graphical object name in the object list
   ObjectSetInteger(0,name,OBJPROP_HIDDEN,false);
//--- visualisation on timeframes
   ObjectSetInteger(0, name, OBJPROP_TIMEFRAMES, OBJ_ALL_PERIODS);
//--- ObjectSetInteger(0, name, OBJPROP_TIMEFRAMES, OBJ_PERIOD_M1|OBJ_PERIOD_M5|OBJ_PERIOD_M15|OBJ_PERIOD_M30|OBJ_PERIOD_H1);
}
//+------------------------------------------------------------------+
