Deleting all graphical objects from a chart

 

Hi,

My problem now is: How to delete all graphical objects on a chart when one of the following happens:

1. Change indicator settings

2. Remove indicator from chart

3. Change timeframe

My understanding is that it should be done on the OnDeinit event handler.

Thanks for the help.


Br, Candles

Documentation on MQL5: Object Functions / ObjectsDeleteAll
Documentation on MQL5: Object Functions / ObjectsDeleteAll
  • www.mql5.com
Object Functions / ObjectsDeleteAll - Documentation on MQL5
 

Correct me if I'm wrong, but I think I partly figured it out already.

To delete objects on events 1 and 3 the following might work:

int OnInit()
  {
//--- indicator buffers mapping
        for(int i=0;i<2500;i++)
          {
           ObjectDelete(0,"End "+i);
          }
//---
   return(0);
  }

My objects are named as End 1, End 2 etc.

On event 2 the following seems to work:

void OnDeinit(const int reason)
  {
        for(int i=0;i<2500;i++)
          {
           ObjectDelete(0,"End "+i);
          }
  }
 
Candles:

Hi,

My problem now is: How to delete all graphical objects on a chart when one of the following happens:

1. Change indicator settings

2. Remove indicator from chart

3. Change timeframe

My understanding is that it should be done on the OnDeinit event handler.

Thanks for the help.


Br, Candles

Yes OnDeinit using UninitializeReason() using Uninitialization reason codes.

Maybe this is help you how to delete Get all objects programaticaly, please read the whole thread.

 

 
Thanks again, phi.nuts you are very kind :) UninitializeReason() did the trick!
 

I think I found a better way to do remove objects when timeframe is changed. There is a Object function ObjectsDeleteAll(long,int,int) that deletes all object of specified type on a specified chart. It works perfectly when placed in the OnInit() event handler, but for some reason won't work when it is located in the OnDeinit(). Shouldn't OnDeinit() be called when the chart timeframe is changed?

EDIT: Actually it nicely deletes all objects from chart on timeframe change no problems, but when removing the indicator from chart it removes objects down until 5m timeframe or less in which it won't delete any objects... Very strange anyone know why is this so?

 
Candles:

I think I found a better way to do remove objects when timeframe is changed. There is a Object function ObjectsDeleteAll(long,int,int) that deletes all object of specified type on a specified chart. It works perfectly when placed in the OnInit() event handler, but for some reason won't work when it is located in the OnDeinit(). Shouldn't OnDeinit() be called when the chart timeframe is changed?

EDIT: Actually it nicely deletes all objects from chart on timeframe change no problems, but when removing the indicator from chart it removes objects down until 5m timeframe or less in which it won't delete any objects... Very strange anyone know why is this so?

1. I have several indicators that draw its own object, so I can not use ObjectDeleteAll()

2. Sorry, I'm a little bit not clear about this "  but when removing the indicator from chart it removes objects down until 5m timeframe or less in which it won't delete any objects... " Can you give an example?

 

 
phi.nuts:

1. I have several indicators that draw its own object, so I can not use ObjectDeleteAll()

2. Sorry, I'm a little bit not clear about this "  but when removing the indicator from chart it removes objects down until 5m timeframe or less in which it won't delete any objects... " Can you give an example?

 

1. That is a good point :) In my rudimentary indicator ObjectDeleteAll() works fine :)

2. I think I might have figured it out already. I get error "array ouf of range" and it seems to be causing the problem.

Br, Candles

Yes, got it to work perfectly after figuring out it was an array out of range that caused the problem all along :)

 
Candles:

1. That is a good point :) In my rudimentary indicator ObjectDeleteAll() works fine :)

2. I think I might have figured it out already. I get error "array ouf of range" and it seems to be causing the problem.

Br, Candles

Yes, got it to work perfectly after figuring out it was an array out of range that caused the problem all along :)

Congrats :)
 

Why does this, or any method of deleting indicator created objects, not seem to work in OnDeinit()? I've tried all of the following but nothing seems to work if I remove the indicator from the chart, yet it seems to work if I change time frames for example.


void OnDeinit(const int reason) {
   Print(__FUNCTION__,"_UninitReason = ",getUninitReasonText(_UninitReason));
   //InitialiseBuffers();     
   //ObjectNames ias a dynamic string array containing the names of all created objects. 
   for(int i=0;i<ObjectNames.Total();i++) {
      string Name = ObjectNames.At(i);
      VLineDelete(0,ObjectNames.At(i));   
   }
    
   DeleteObjectsByString(IndicatorShortName);
   ObjectsDeleteAll(0,0,OBJ_VLINE);

}

void DeleteObjectsByString(string Search, int ObjectType=-1) {
   int TotalObject = ObjectsTotal(0,0,-1);
   for (int i = TotalObject; i >= 0 ; i--) {
      if (StringFind(ObjectName(0, i, 0, ObjectType), Search, 0) > -1)  {
         ObjectDelete(0,ObjectName(0,i,0,-1));
      }      
   }
}

//+------------------------------------------------------------------+
//| Delete the vertical line                                         |
//+------------------------------------------------------------------+
bool VLineDelete(const long   chart_ID=0,   // chart's ID
                 const string name="VLine") // line name
  {
//--- reset the error value
   ResetLastError();
//--- delete the vertical line
   if(!ObjectDelete(chart_ID,name)) {
      Print(__FUNCTION__,
            ": failed to delete the vertical line! Error code = ",ErrorDescription(GetLastError()));
      return(false);
   }
//--- successful execution
   return(true);
}
Documentation on MQL5: Standard Constants, Enumerations and Structures / Objects Constants / Object Types
Documentation on MQL5: Standard Constants, Enumerations and Structures / Objects Constants / Object Types
  • www.mql5.com
Standard Constants, Enumerations and Structures / Objects Constants / Object Types - Documentation on MQL5
 
whitebloodcell:

Why does this, or any method of deleting indicator created objects, not seem to work in OnDeinit()? I've tried all of the following but nothing seems to work if I remove the indicator from the chart, yet it seems to work if I change time frames for example.


You need to count down,  not up . . . .

Not this . . .

   for(int i=0; i<ObjectNames.Total(); i++  ) {
      string Name = ObjectNames.At(i);
      VLineDelete(0,ObjectNames.At(i));   
   }

  . . . but this . . 

   for(int i = ObjectNames.Total() - 1; i >= 0; i-- ) {
      string Name = ObjectNames.At(i);
      VLineDelete(0,ObjectNames.At(i));   
   }

  . . .  for the same reason as this:  Loops and Closing or Deleting Orders

Loops and Closing or Deleting Orders - MQL4 forum
  • www.mql5.com
Loops and Closing or Deleting Orders - MQL4 forum
Reason: