how to delete object after close custom indicator?

 

Hi i write an Indicator that Draw to many rectangle objects on main chart. i want delete all the rectangle after close my indicator.

I use this code but the object not remove and remain on chart

void OnDeinit(const int reason)
  {
  int qq=0,
   if(reason == 1)
     {
       while(ObjectFind(Symbol(),"Line"+IntegerToString(qq)) >=0)
        {
        ObjectDelete(Symbol(),"Line"+IntegerToString(qq));       
         qq++;
        }
     }
  }

even debug my code show the code is running but the rectangle is still not remove. how can i fix this? please help

 
justlink:

Hi i write an Indicator that Draw to many rectangle objects on main chart. i want delete all the rectangle after close my indicator.

I use this code but the object not remove and remain on chart

void OnDeinit(const int reason)
  {
  int qq=0,
   if(reason == 1)
     {
       while(ObjectFind(Symbol(),"Line"+IntegerToString(qq)) >=0)
        {
        ObjectDelete(Symbol(),"Line"+IntegerToString(qq));       
         qq++;
        }
     }
  }

even debug my code show the code is running but the rectangle is still not remove. how can i fix this? please help

The syntax variable is not correct to delete

Objectdelete(chart_id, name)
//--- Check the reference Guide

In your case, no need for 

Symbol()

that is suppose to be the chart id.

 

No different even when I use 0 for chart_id.

I use the All method of "ObjectsDeleteAll" but that is not working to.

and now when i use blow code to see the result of code execute in d the CE variable is 1.


void OnDeinit(const int reason)
  {
  int qq=0, CE =0;
   if(reason == 1)
     {
       while(ObjectFind(Symbol(),"Line"+IntegerToString(qq)) >=0)
        {
        CE =ObjectDelete(Symbol(),"Line"+IntegerToString(qq));       
         qq++;
        }
     }
  }
 
Please use the code button (Alt+S) when pasting code.
 
i try another method. the ObjectDelete work currectly in OnInit function. So i Write my code for delete object in OnInit and call this function in onDeinit. Now the object delete after Indicator Close. but Before Closing the indicator I should first go to H1 TimeFrame and not work on another time frame again
 
justlink:
i try another method. the ObjectDelete work currently in OnInit function. So i Write my code for delete object in OnInit and call this function in onDeinit. Now the object delete after Indicator Close. but Before Closing the indicator I should first go to H1 TimeFrame and not work on another time frame again

Your method may work but it is accidental and not a good method.

You have already been told

Thank-god Avwerosuoghene Odukudu:

The syntax variable is not correct to delete

In your case, no need for 

Symbol()

that is suppose to be the chart id.

and yet you have totally ignored the advice.

 
Keith Watford:

Your method may work but it is accidental and not a good method.

You have already been told

and yet you have totally ignored the advice.

I am not ignoring advice.

I use this too. But noting change.

void OnDeinit(const int reason)
  {
   
  int qq=0;
   if(reason == 1)
     {
       while(ObjectFind(0,"Line"+IntegerToString(qq)) >=0)
        {
       
          ObjectDelete(0,"Line"+IntegerToString(qq));
         qq++;
        }
     }
     ChartRedraw(0); 


  }
 

for example chart remain like this and object not remove if i close indicator from D1 Time Frame. even when  use "0".

my Object name for rectangle is Line0, Line1, Line2, etc

Files:
1234.png  51 kb
 
Your issue is related to the fact around ChartID.

When reading the documentation AND digesting ALL information, as suggested, you would have come across eventually following:

ChartID-> quote: "... returns 0 as the current Chart ID".

Therefore you need to go this way:

https://www.mql5.com/en/search#!keyword=chartid&module=mql5_module_documentation

See both, the first and second entry.

Then read following:

I know, it's hard to get a hold of all the behaviour of an API, but try to read and take serious as much details as possible within the documentation.

Why you are pointed at not having taken advice into account is, ChartID = 0 is the >current chart< which refers to what you are seeing on screen.


 
Dominik Egert:
Then read following:

I know, it's hard to get a hold of all the behaviour of an API, but try to read and take serious as much details as possible within the documentation.

Why you are pointed at not having taken advice into account is, ChartID = 0 is the >current chart< which refers to what you are seeing on screen.


I don't know why you have linked to ChartWindowFind() as you do not have to specify the window for ObjectFind() or ObjectDelete().

Just to be totally clear

 ChartID = 0 is the >current chart< which refers to what you are seeing on screen


0 is the current chart, ie. the chart that the indicator or EA is attached to, not necessarily the chart that is currently on the screen.
 
I try some another code beside of ObjectDelete. for example for print something, draw new object, change the property of another existence object and etc. none of them work in OnDeinit but run correctly in another part of program like OnCalculate and OnInit. So why the code in OnDeinit run but nothing happens?