Clean objects on chart period change?

 

Hello again,

I would like to detect a timeframe change and clean all objects on the chart from my indicator. Deinit() seems not to execute on a timeframe change on the same chart. -this is, clicking H4 from D1 for example.

I would really appreciate some help, and thanks in advance for all your support.

int deinit()
  {
    // Eliminate my objects
   int obj_total=ObjectsTotal();
   for(int i = obj_total - 1; i >= 0; i--)
     {
       string label = ObjectName(i);
       if(StringFind(label, "MyEntryPoints")==-1) continue;
       ObjectDelete(label); 
     }     
    return(0);
  }
 

Try using UninitializeReason( ), REASON_CHARTCHANGE:

https://docs.mql4.com/check/UninitializeReason

https://docs.mql4.com/constants/uninit

Another way off the top of my head.

if( Period() != Saved_Variable_Old_Period ){

Saved_Variable_Old_Period = Period();

Obj_Delete_All();

}

 

Is this helping you flaab ??

int deinit()
  {
//----
   int i, ot=ObjectsTotal()-1;
   string id;
//----
   for(i=ot;i>=0;i--)
    {label=ObjectName(i);
     if(StringSubstr(label,0,13)=="MyEntryPoints")
      {ObjectDelete(label);
      }
    }
   return(0);
  }  
 
Thank you, I will try ASAP.
 

flaab:

I would like to detect a timeframe change and clean all objects on the chart from my indicator. Deinit() seems not to execute on a timeframe change on the same chart. -this is, clicking H4 from D1 for example.

if(StringFind(label, "MyEntryPoints")==-1) continue;
       ObjectDelete(label); 

You code looks good and Deinit is executed on a timeframe change.

I suspect your code isn't creating objects with that EXACT pattern. Try:

#define MEP "MyEntryPoints"
:
ObjectCreate(MEP + ...
:
if(StringFind(label, MEP) >= 0) ObjectDelete(label); 
 
WHRoeder:

You code looks good and Deinit is executed on a timeframe change.

I suspect your code isn't creating objects with that EXACT pattern. Try:

I will, thank you.
 
dabbler:

why not just use

in the deinit() function?


from post 1 I would like to detect a timeframe change and clean all objects on the chart from my indicator

read it again "from my indicator" not all objects I think

 
deVries:


from post 1 I would like to detect a timeframe change and clean all objects on the chart from my indicator

read it again "from my indicator" not all objects I think

The problem is that objects are not always cleaned when deinit() executes... sometimes it does, and sometimes it does not :)
 
WHRoeder:

You code looks good and Deinit is executed on a timeframe change.

I suspect your code isn't creating objects with that EXACT pattern. Try:


Works flawlessly. Thanks!