Chart leaves Rectangle Object on the chart after deletion

 

I am trying to delete some chartobjects that I created. I am doing this in the onDelete().

There are 3 objects...

1. A rectangle

2. The other two are trend object.


The two trend objects successfully gets deleted and removed from the chart.

The rectangle is confirmed to be deleted but it stays on the chart...
I need help to get the rectangle object to stop showing on the chart after it has been deleted.

Here are a few things I have tried

1. I tried to change the chart background to black ... hoping it will repaint and overide the rectangle... it does notwork.

2. I changed the color of the rectangle to the same color as the background of the chart and then delete the rectangle. This works but this is just a hack...and unnecessary extra coding... I think there should be a better way to erase objects that have been deleted on the chart. I am also curious why the behaviour of RECTANGLE chartsObject is different from TREND ChartObject even though they are both from the same class library of MQL5.


Here is the function that I wrote to delete it. I call this function in the onDelete function of the EA;

// This the code that creates the objects...
// the two functions below are in class candleInfo.

/// draw candle on the chart
 bool DrawCandleOnChart(ENUM_TIMEFRAMES mperiod,string msymbol,int mindex)
 {
      setData(mperiod,msymbol,mindex); 
       Print("Open: ", candleStructure.mopen);
       Print("High: ", candleStructure.mhigh);
       Print("Low : ", candleStructure.mlow);
       Print("Close : ",candleStructure.mclose);
       Print("Time: ", candleStructure.mtime);
      
      
      color fillColor = clrRed;
      if(isBull())
         fillColor = clrGreen;
      
     
     if(!ObjectCreate(0,"candleRectangle1",OBJ_RECTANGLE,0,candleStructure.mtime + (2*60*60),candleStructure.mopen,candleStructure.mtime + (2*60*60) + 600,candleStructure.mclose))
           Print(__FUNCTION__,
              ": failed to create candle body! Error code = ",GetLastError());
     else
     {
     
         ObjectSetInteger(0,"candleRectangle1",OBJPROP_COLOR,(isBull()) ? clrGreen : clrRed);
         ObjectSetInteger(0,"candleRectangle1",OBJPROP_BACK,true);
         ObjectSetInteger(0,"candleRectangle1",OBJPROP_FILL,true);
         
     }
          
     double tempOpen = isBull() ? candleStructure.mclose: candleStructure.mopen; // swap for proper drawing of wicks....
     double tempClose = isBull() ? candleStructure.mopen : candleStructure.mclose;
     

      if(!ObjectCreate(0,"CandleHighWick1",OBJ_TREND,0,candleStructure.mtime + (2*60*60) + 300,tempOpen,candleStructure.mtime + (2*60*60) +300,candleStructure.mhigh))
               Print(__FUNCTION__,
                      ": failed to create candle High wick! Error code = ",GetLastError());
      else
     {
     
         ObjectSetInteger(0,"CandleHighWick1",OBJPROP_COLOR,clrRed);
         ObjectSetInteger(0,"CandleHighWick1",OBJPROP_BACK,true);
   //      ObjectSetInteger(0,"candleRectangle1",OBJPROP_FILL,true);
         
     }


      if(!ObjectCreate(0,"CandleLowWick1",OBJ_TREND,0,candleStructure.mtime + (2*60*60)+300,tempClose,candleStructure.mtime + (2*60*60) +300,candleStructure.mlow))
               Print(__FUNCTION__,
                      ": failed to create candle High wick! Error code = ",GetLastError());
     {
     
         ObjectSetInteger(0,"CandleLowWick1",OBJPROP_COLOR,clrRed);
         ObjectSetInteger(0,"CandleLowWick1",OBJPROP_BACK,true);
         
     }
     

 return true;
 }









//This is the function that deletes the objects

void DeleteCandleObjects (void)
{
   
   ObjectSetInteger(0,"candleRectangle1",OBJPROP_FILL,false);  
// changing the color of the rectangle to the same color of the background chart before deleting it works...
// that code not shown here..
   if(ObjectDelete(0,"CandleRectangle1"))
      Print("Rectangle Deletion is successful"); // I confirm this working as it prints this out 
   ObjectDelete(0,"CandleHighWick1");
   ObjectDelete(0,"CandleLowWick1");
   ChartRedraw();
   
}



// This is how I call it in the onDelete() Function of the EA
candleinfo.DeleteCandleObjects();