Get all objects programatically?

 

Hi,

 

is it possible to get a list of all objects in a chart?

I'm looking for a solution to remove some, but not all objects on a chart, but do not know the object's IDs - only a constant prefix that is given.

 

Any ideas?

 

Thanks and all the best.

 

Me 

 

Try this

  string Search  = "Retail"; // This is the object name you want to remove
  int TotalObject;
  TotalObject = ObjectsTotal(0,0,-1);

  for(int i=0;i<TotalObject;i++) 
      {
         if (StringFind(ObjectName(0,i,0,-1),Search,0)>0) ObjectDelete(0,ObjectName(0,i,0,-1));

      }


 
biantoro:

Try this

I don't know if you have experience with MQL4, but we usually don't count up but count down. Here's what wrong with your code : Supposed there's 10 objects on chart. Since you count up, the iteration start from first object and delete it. Now, on the object list, object number 2 become object number 1, object number 3 become object number 2, and so on to object number 10 become object no 9, this happen because we delete object no 1. The iteration start again for the second time and this time the iteration point at object number 2 which used to be object number 3 and delete it. The iteration does not touch object number 1 again but this object number 1 used to be object number 2.

Believe or not, even if there are 10 objects on chart and we want to delete them all, the iteration count only 5 ! 

What you should do is ...

for(int i = TotalObject; i >= 0 ; i--) {}

... count it down to zero.

Have fun :) 

 

 

 
onewithzachy:

I don't know if you have experience with MQL4, but we usually don't count up but count down. Here's what wrong with your code : Supposed there's 10 objects on chart. Since you count up, the iteration start from first object and delete it. Now, on the object list, object number 2 become object number 1, object number 3 become object number 2, and so on to object number 10 become object no 9, this happen because we delete object no 1. The iteration start again for the second time and this time the iteration point at object number 2 which used to be object number 3 and delete it. The iteration does not touch object number 1 again but this object number 1 used to be object number 2.

Believe or not, even if there are 10 objects on chart and we want to delete them all, the iteration count only 5 ! 

What you should do is ...

... count it down to zero.

Have fun :) 

 

 

thanks for correction
 

First - thanks to all, your support is very appreciated!

 

After changing the search expression to

if (StringFind(ObjectName(0, i, 0, -1), needle, 0) > -1) 

 (n.b. - -1 instead of 0 as the object names start with the identifier string), it worked :-)

 

All the best,

 

me 

Documentation on MQL5: Standard Constants, Enumerations and Structures / Objects Constants / Object Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Objects Constants / Object Properties - Documentation on MQL5
 
onewithzachy:

I don't know if you have experience with MQL4, but we usually don't count up but count down. Here's what wrong with your code : Supposed there's 10 objects on chart. Since you count up, the iteration start from first object and delete it. Now, on the object list, object number 2 become object number 1, object number 3 become object number 2, and so on to object number 10 become object no 9, this happen because we delete object no 1. The iteration start again for the second time and this time the iteration point at object number 2 which used to be object number 3 and delete it. The iteration does not touch object number 1 again but this object number 1 used to be object number 2.

Believe or not, even if there are 10 objects on chart and we want to delete them all, the iteration count only 5 ! 

What you should do is ...

... count it down to zero.

Have fun :) 

 

 

shouldnt the for loop start with i=TotalObject-1?

Reason: