Unable to delete object

 
//+------------------------------------------------------------------+
//|                                                        Indi.mq5 |
//|                                                           doshur |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "doshur"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window

enum Style
  {
   SOLID_,        //Solid line
   DASH_,         //Dashed line
   DOT_,          //Dotted line
   DASHDOT_,      //Dot-dash line
   DASHDOTDOT_    //Dot-dash line with double dots
  };

//--- input parameters
input bool     Extended_Levels = false;
input color    Cl_Support     = clrRoyalBlue;
input color    Clr_Resistance  = clrRed;
input color    Clr_Seperator   = clrSilver;
input Style    Line_Style      = DOT_;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+    
void OnDeinit(const int reason)
  {
//----

   if(ObjectFind(0, "Yesterday") >= 0) ObjectDelete(0, "Yesterday");
   if(ObjectFind(0, "Tomorrow") >= 0)  ObjectDelete(0, "Tomorrow");
   if(ObjectFind(0, "Today") >= 0)     ObjectDelete(0, "Today");

//----
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {
//---

   int   start;

   if(prev_calculated==0)
      start = 0;
   else
      start = prev_calculated - 1;

   MqlRates rates[];

   ArraySetAsSeries(rates, true);

   int      copied = CopyRates(Symbol(), PERIOD_D1, 0, 21, rates);

   datetime Tmr = rates[0].time + (24 * 60 * 60);

   //--- calculate
   for(int i = start; i < rates_total && !IsStopped(); i++)
   {
      SetTimeLine("Tomorrow", Tmr, Clr_Seperator, Line_Style);
      SetTimeLine("Today", rates[0].time, Clr_Seperator, Line_Style);
      SetTimeLine("Yesterday", rates[1].time, Clr_Seperator, Line_Style);
   }

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Draw Vertical Lines                                              |
//+------------------------------------------------------------------+
void SetTimeLine(string objname, datetime dt, color clr, int style)
  {
   if(ObjectFind(0, objname) < 0)
   {
      ObjectCreate(0, objname, OBJ_VLINE, 0, dt, 0);

      ObjectSetInteger(0, objname, OBJPROP_COLOR, clr);
      ObjectSetInteger(0, objname, OBJPROP_STYLE, style);

      ObjectMove(0, objname, 0, dt, 0);
   }
   else
   {
      ObjectMove(0, objname, 0, dt, 0);
   }
  }

I not able to delete the 3rd object in the OnDeinit function

No matter how I arrange the 3 lines of code, only the last object doesn't get deleted

Any idea why?

 

You could use ObjectDeleteAll function to delete all objects from the chart. Like - 

//
string PREFIX = "Object";
string objName1 = PREFIX + "Yesterday";
string objName2 = PREFIX + "Tomorrow";
string objName3 = PREFIX + "Today";


void OnDeinit(const int reason)
  {
//----
        // Delete all objects where PREFIX is used 
        ObjectsDeleteAll(0, PREFIX, 0, -1);

//----
  }
Documentation on MQL5: Object Functions / ObjectsDeleteAll
Documentation on MQL5: Object Functions / ObjectsDeleteAll
  • www.mql5.com
ObjectsDeleteAll - Object Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Md Atikur Rahman #:

You could use ObjectDeleteAll function to delete all objects from the chart. Like - 

Thank you. Will try this.

Reason: