Delete objects with string in name?

 

I am trying to modify a sessions indicator so it only shows on lower time frames and does not clutter up my charts on larger timeframes. I had added just a delete all objects function which works but also ends up deleting trendlines or anything else I add to my chart. Is there a way to cycle through all objects on the chart and only delete the ones that start with a specific string (NY,Tokyo,London)? Thanks.

 
of course
 
qjol:
of course

Any advice as to which functions to look at? I'm not asking someone to do it for me but I am still pretty new to coding and would appreciate someone pointing me in the right direction. Thanks.
 
string Name;
for(int i = ObjectsTotal() -1 ;i >= 0; i--)
   {
   Name = ObjectName(i);
   if (StringSubstr(Name, 0, 2) == "NY" ||
       StringSubstr(Name, 0, 5) == "Tokyo" ||
       StringSubstr(Name, 0, 6) == "London")
       {
       ObjectDelete(Name);
       }
   }
something like this not checked or tested
 
qjol:
something like this not checked or tested

Thank you. I will give that a try.
 
azFIN:

... Is there a way to cycle through all objects on the chart and only delete the ones that start with a specific string (NY,Tokyo,London)? Thanks.


And you can do LIKE type of operation with

if (StringFind(sSymbol, "EURUSD", 0) > -1)

-BB-

 
StringFind > -1 means the string is somewhere in the object name. StringFind == 0 means the string STARTS with the name, which is what OP asked
    for(int iObj=ObjectsTotal()-1; iObj >= 0; iObj--){
        string on = ObjectName(iObj);
        if (StringFind(on, "Tokyo") == 0)  ObjectDelete(on);
        :
 

When I want the indicator to remove only the objects it created on exit I do it like this:

I declare in itit()

string ID = WindowExpertName();

Then the string name of every object the indicator creates is prefixed with ID for example:

string arrowcount=DoubleToStr(cnt,0);
string sellarrow=StringConcatenate(ID," Sell ",arrowcount);
       ObjectCreate(sellarrow,OBJ_ARROW,0,Time[0],Bid); 

So then to remove all the objects in deinit() search for all objects whose name begins with the ID and delete them.

int deinit()
  {int i,ot,length;
   string name;
//---- find objects
   length=StringLen(ID);
   ot=ObjectsTotal()-1;
   for(i=ot;i>=0;i--)
    {name=ObjectName(i);
//---- delete objects
     if(StringSubstr(name,0,length)==ID)
      {ObjectDelete(name);
    } }
//----
   return(0);
  }
 
WHRoeder:
StringFind > -1 means the string is somewhere in the object name. StringFind == 0 means the string STARTS with the name, which is what OP asked

Indeed he did - but I was expanding the topic for the forum watchers and anticipating <the next> question :)
-BB-
 
BarrowBoy:
Indeed he did - but I was expanding the topic for the forum watchers and anticipating <the next> question :)
-BB-

Thank you for all the responses. I will try this out after lunch here and let you know once I get it. Been busy with first NFL game and work since posting. Thanks again.
 

Got it to work the way I was hoping. Thanks for everyones input.

Reason: