Cannot Delete Objects in Deinit()

 

Dear experts,

I have BUTTON and LABEL Objects on my chart and I want to delete all these chart objects during the removal of EA from Chart.
I tried both of these in Deinit() and they did not work:


void Deinit(const int reason)
  {
   StopTimer();  
   if(reason == 1)  { ObjectsDeleteAll(0);};
   ChartRedraw(0); 
  }
  


void Deinit(const int reason)
  {
   StopTimer();  
   ObjectsDeleteAll(0);
   ChartRedraw(0); 
  }
  


What is the proper way to achieve this ?

Thank you

Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types
Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types
  • www.mql5.com
Object Types - Objects Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Robobiee :

Here is an example:

Code Base

DeleteAllObject

Mohamad Zulhairi Baba, 0001.01.01 00:00

Simple script to delete all object on the current chart.

 
Robobiee:

Dear experts,

I have BUTTON and LABEL Objects on my chart and I want to delete all these chart objects during the removal of EA from Chart.
I tried both of these in Deinit() and they did not work:




What is the proper way to achieve this ?

Thank you

I use my own solution as I came across trouble deleting objects several times in custom indicators.

1. Create an string array to hold all object names that are created(and should be removed later). You fill the array as you create objects.

2. in DeInit function(or wherever you need) you remove objects like this:

void DeleteObjects()
{  
   int size=ArraySize(name_list);
   for(int i=0;i<size;i++)
      ObjectDelete(ChartID(), name_list[i]);
}
 
Yashar Seyyedin #:

I use my own solution as I came across trouble deleting objects several times in custom indicators.

1. Create an string array to hold all object names that are created(and should be removed later). You fill the array as you create objects.

2. in DeInit function(or wherever you need) you remove objects like this:

The funny thing is name_list must be created manually when creating objects. The built in function to retrieve objects names create trouble.
 
Yashar Seyyedin #:
The funny thing is name_list must be created manually when creating objects. The built in function to retrieve objects names create trouble.
#property copyright "You got Lucky!"
#property link      "https://www.mql5.com/"
#property version   "1.00"
#property indicator_chart_window

string prefix="p_";
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit() {
//--- indicator buffers mapping

   for(int i=0; i<10; i++) {
      ArrowCreate(0,prefix+"arrow_"+IntegerToString(i),0,iTime(NULL,0,i),iHigh(NULL,0,i),234,ANCHOR_BOTTOM,clrMagenta);
   }
//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
      ObjectsDeleteAll(0,prefix);
}
//+------------------------------------------------------------------+
//| 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);
}

//+------------------------------------------------------------------+
//| Create the arrow                                                 |
//+------------------------------------------------------------------+
bool ArrowCreate(const long              chart_ID=0,           // chart's ID
                 const string            name="Arrow",         // arrow name
                 const int               sub_window=0,         // subwindow index
                 datetime                time=0,               // anchor point time
                 double                  price=0,              // anchor point price
                 const uchar             arrow_code=252,       // arrow code
                 const ENUM_ARROW_ANCHOR anchor=ANCHOR_BOTTOM, // anchor point position
                 const color             clr=clrRed,           // arrow color
                 const ENUM_LINE_STYLE   style=STYLE_SOLID,    // border line style
                 const int               width=1,              // arrow size
                 const bool              back=false,           // in the background
                 const bool              selection=false,       // highlight to move
                 const bool              hidden=true,          // hidden in the object list
                 const long              z_order=0) {          // priority for mouse click
   ResetLastError();
   if(!ObjectCreate(chart_ID,name,OBJ_ARROW,sub_window,time,price)) {
      Print(__FUNCTION__,
            ": failed to create an arrow! Error code = ",GetLastError());
      return(false);
   }
   ObjectSetInteger(chart_ID,name,OBJPROP_ARROWCODE,arrow_code);
   ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor);
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);
   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
   return(true);
}

//+------------------------------------------------------------------+

Use ObjectsDeleteAll() using prefix.

 
Rodger Sen #:
string prefix="p_";

I did stupid mistake. Apologizes. Normally this is more than enough to delete all chart objects : 

 ObjectsDeleteAll(0);
 ChartRedraw(0); 
 

But I was running this inside Deinit()  instead of OnDeinit().
Sorry :-(

 
Robobiee #: Normally this is more than enough to delete all chart objects : 
 ObjectsDeleteAll(0);

You should never use that. You should only delete those objects that your code added.
Reason: