StringFind Problem

 

Hi,

Been struggle with StringFind for some hours now and Googled with no luck so...

Problem is that the string variable I use (_EventSearch) for search in StringFind does not work?
It works if the StringFind have an EXACT match (ex: "Nonfarm Payrolls") but not a subset ("Nonfarm") as described in reference

Please copy all code and compile and see if you get what I am doing wrong ??

Thanks in Advance!

// Input Variables
input ENUM_TIMEFRAMES _StartTime = PERIOD_D1;
input ENUM_TIMEFRAMES _StopTime = PERIOD_D1;
input string Stringsrch = "Nonfarm";

int OnInit() 
{
   
    isNewsEvent(_StartTime, _StartTime,Stringsrch);

   return(INIT_SUCCEEDED);
}

void OnTick() 
{
   
}

bool isNewsEvent(ENUM_TIMEFRAMES _StartTime, ENUM_TIMEFRAMES _StopTime, string _EventSearch)
{
    int _TotalNews = 0;
    int _TotalImportantNews = 0;
    MqlCalendarValue values[];
    datetime starttime = TimeTradeServer() - PeriodSeconds(_StartTime);   
    datetime endtime = TimeTradeServer(); + PeriodSeconds(_StopTime);
    
    int valuesTotal = CalendarValueHistory(values,starttime,endtime);

    for (int i=0; i < valuesTotal; i++)
    {
      MqlCalendarEvent _Calendar_Events;     
      MqlCalendarCountry _Calendar_Country;  
      
      CalendarEventById(values[i].event_id,_Calendar_Events);             
      CalendarCountryById(_Calendar_Events.country_id,_Calendar_Country); 
      
      if(StringFind(_Symbol,_Calendar_Country.currency) >= 0) // Search for Current Symbol. This StringFind is Working. 
      {   
          _TotalNews++;
          if(_Calendar_Events.importance == CALENDAR_IMPORTANCE_HIGH)
          {
            int _StringFindPos = 0;
            _TotalImportantNews++;
            Print("Eventname: ",_Calendar_Events.name," > Importance:",_Calendar_Events.importance,", TIME:",values[i].time, " TotalNews:",_TotalNews,"/",ArraySize(values),"  Currency:",_Calendar_Country.currency);
            
            _StringFindPos = StringFind(_EventSearch,_Calendar_Events.name);  // Testing to get the Pos value from StringFind to debug..
            Print("_EventSearch: ", _EventSearch," Name: ",_Calendar_Events.name," _StringFindPos: ", string(_StringFindPos));  // Printing so I see all the Variables values..
                        
            if(StringFind(_EventSearch,_Calendar_Events.name) >=0)  // THIS IS NOT WORKING FOR ME. 
            {
            Print("NONFARM PAYROLE FOUND!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            }
          }           
      }
            
      
    }

    return (true);

}
Documentation on MQL5: String Functions / StringFind
Documentation on MQL5: String Functions / StringFind
  • www.mql5.com
Search for a substring in a string. Parameters string_value [in]  String, in which search is made. match_substring [in]  Searched...
 

In your code, you're trying to find _Calendar_Events.name ("Nonfarm Payrolls" or whatever) inside _EventSearch ("Nonfarm"), that is, you're reversing the order of the parameters. Just call in the reverse order and you should be good:


if(StringFind(_Calendar_Events.name,_EventSearch) >=0) // searches for _EventSearch in _Calendar_Events.name
 
Emanuel Cavalcante Amorim Filho #:

In your code, you're trying to find _Calendar_Events.name ("Nonfarm Payrolls" or whatever) inside _EventSearch ("Nonfarm"), that is, you're reversing the order of the parameters. Just call in the reverse order and you should be good:


Gah! You are my hero!! Spend so much time on this :) 

Its in the details !


Thanks alot