Download MetaTrader 5
To add comments, please log in or register
Trade like successful traders - subscribe to their signals!
Raja K.
25
Raja K. 2016.04.26 10:35 

I am trying to find an object based on a wildcard search using ObjectFind only on the current bar.  I know that if I know the exact name of the object I can use:

ObjectFind("label"+Time[0]); 

However, what if I wanted to find label2 or label3 or whatever number is at the end?

ObjectFind("label*"+Time[0]);  does not work. 

 So my next idea was to try and loop through every object and search using StringFind:

int total=ObjectsTotal();
string obj_name;
for(int ix=-1;ix<total;ix++)
  {

        obj_name=ObjectName(ix);

       if (StringFind(obj_name,"label",0))

        {

        string foundstr = ObjectName(ix);
        break;

        }

  } 

But I don't really know how to make this one work since StringFind doesn't seem to use wildcards either.  Does anyone have any suggestions? Any help is appreciated.

whroeder1
13641
whroeder1 2016.04.26 12:53  
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Perhaps you should read the manual. StringFind - String Functions - MQL4 Reference returns the position of the string, so if(StringFind(obj_name,"label")==0) is true if the obj_name starts with "label".

Raja K.
25
Raja K. 2016.04.26 14:15  
WHRoeder:

  1. Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Perhaps you should read the manual. StringFind - String Functions - MQL4 Reference returns the position of the string, so if(StringFind(obj_name,"label")==0) is true if the obj_name starts with "label".

Yes I have been doing that all morning.  Here's where I am now (edited to make more readable):

  int total=ObjectsTotal();
  string obj_name;
  int finder;

     for(int ix=-1;ix<total;ix++)                                   //loop through all objects
        {
             obj_name=ObjectName(ix);                               //store current object name in obj_name
                   if (StringFind(obj_name,"label")>=0)             //is string "label" found inside obj_name?  if so, proceed.
                       {
                       string foundstr = ObjectName(ix);            //foundstr gets value of current Objectname(ix)
                       finder = ObjectFind((foundstr) + Time[0]);   //HERE'S WHERE I HAVE ISSUE.  IF I REMOVE Time[0], then my string test is "found."  If I keep Time[0] in, then string test is always "not found yet", no matter if there is a label on current bar. 
                               if (finder>-1)                       //I have also tried to change from Time[0] to Time[1], but same results.
                                  {
                                  string test = "found";
                                  break;
                                  }
                               else
                                  {
                                  test="not found yet";
                                  }
                       }
        }

Posted my question in the comments next to code.  Am I missing something trivial? Any advice from someone more experienced?  Thank you.

whroeder1
13641
whroeder1 2016.04.26 16:39  
  1. Assuming the creating code uses "label"+Time[i], then foundstr is "labelnnnnnnnn" or "label2016.04.26 19:00:00" (depending on #property strict)
    finder = ObjectFind((foundstr) + Time[0]);

    This is now looking for labelnnnnnnnnnnnnnnnn" or "label2016.04.26 19:00:002016.04.26 19:00:00" which will never be found.


  2. IF I REMOVE Time[0], then my string test is "found."
    Of course foundstr exists; you already found it. Why are you finding again?
Raja K.
25
Raja K. 2016.04.26 18:52  
Is there any way for me to reference the most recent foundstr based on the Time property of the object?  I am not sure how to implement this. 
Keith Watford
Moderator
7984
Keith Watford 2016.04.26 23:58  
  string prefix="label";
  int length=StringLen(prefix);
  string obj_name, time_string;
  datetime latest_time=0, this_time;
  string latest_object_name="";
  
  for(int x=ObjectsTotal()-1;x>=0;x--)
     {
     obj_name=ObjectName(x);
     if (StringFind(obj_name,prefix)==0)
     time_string=StringSubstr(obj_name,length);
     this_time=StrToTime(time_string);
     if(this_time>latest_time)
       {
       latest_time=this_time;
       latest_object_name=obj_name;
       }
     }
  if(latest_object_name=="")
     Print("Object with prefix ",prefix," not found");
  else
     Print("Latest Object with prefix ",prefix," is ",latest_object_name);
  

This may help you, remember that if you have a space between the prefix and the time, the space must be included.

  string prefix="label";
  //Or
  string prefix="label ";
Raja K.
25
Raja K. 2016.04.29 13:53  
GumRai:

This may help you, remember that if you have a space between the prefix and the time, the space must be included.


Thank you for this.  It gets me going in the right direction I need.
Raja K.
25
Raja K. 2016.04.29 18:03  

I am almost there.  I've found how to extract the time from the object.  Now I just don't know how to compare object time to current time to see which is most recent.


  string prefix="label";
  int length=StringLen(prefix);
  string obj_name, time_string, obj_name2;
  datetime latest_time=0, this_time;
  string latest_object_name="";
  long obj_time2;
  
  for(int x=ObjectsTotal()-1;x>=0;x--)
     {
     obj_name=ObjectName(x);
     obj_name2=ObjectName(x+1);
     if (StringFind(obj_name,prefix)>=0)
    long obj_time=ObjectGetInteger(0,obj_name,OBJPROP_TIME);
    obj_time2=ObjectGetInteger(0,obj_name2,OBJPROP_TIME);

     if(obj_time2>obj_time)
       {
     //  latest_time=this_time;
       latest_object_name=obj_name;
       }
     
       else
       {
       latest_object_name=obj_name2;

       }
     }
  if(latest_object_name=="")
     string test = "Object with prefix " + prefix + " not found";
  else
     test = (latest_object_name + " time: " + TimeToStr(obj_time) );
sokramm
49
sokramm 2016.04.29 20:04  
/
To add comments, please log in or register