MT5 News Filter using Calendar Functions

 


Hi everyone 

I would like some feedback on my code or how I can make it more efficient as it uses several conditionals and loops. 

I wanted to create a multicurrency news filter for when 30 minutes before and after the current time there is an important news event.

For each  news event I need its Time, Importance Level, Currency

I read the Mql5 Calendar Documentation  and I found that to get each one of those I need to use three different structs

Time in  MqlCalendarValue
Importance in MqlCalendarEvent
Currency  in  MqlCalendarCountry

Hence my way of thinking is the following 

Make a function that creates a string of all the base currencies that have important events coming up.

 1) Use CalendarValueHistory() to store in  MqlCalendarValue array all event IDs within the needed times.
 2) Loop thought the above above array and use CalendarEventById() on the IDs collected from(1),to gather important enough events and country ID
 3) Use CalendarCountryById() on the countryIDs  collected from (2)  in order to gather the currency.
 4) Store all the Currencies in a string
The above function is to run every x minutes to gather new events.

 5) Check if a symbol's  base/profit/margin currency exists in the  string created in 4 to determine if it has news affecting it

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
string GetNewsCurrencies( int MinutesBefore=100, int MinutesAfter=100,int importanceLevel=2 )
{
   //returs a string of upcoming news currencies that have news off specific importance 
   
   string blockedCurrencies="";

   MqlCalendarValue values[];  //for saving the events time and id
   MqlCalendarEvent event;    //for saving the events importance and country code
   MqlCalendarCountry country; //for saving the events currency and country
 
   datetime date_from=TimeCurrent()-MinutesBefore*60;  // take all events from 
   datetime date_to=TimeCurrent()+MinutesBefore*60;     // take all events to  
 
 
   if(CalendarValueHistory(values,date_from,date_to))  //get all the events
   {
       for (int i=0; i<ArraySize(values);i++)
      {
          //get the event  info
          if(CalendarEventById(values[i].event_id,event))
            {
               // if is important enought and time based
               if (event.importance>=importanceLevel && event.time_mode==0)
               {
                  // get the countries currency info  add it to the list           
                  if (CalendarCountryById(event.country_id,country))
                  {
                        string cur=country.currency;
                        if (StringFind(blockedCurrencies,cur)==-1 )  blockedCurrencies+=","+cur;
                  }               
               }
           }
      }
   }
   return blockedCurrencies;
}


 

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool CheckNewsCurrency(string symbol)
{
   //check if a symbol's base/margin/profit currencies have news
   
   
   string baseCurrency =SymbolInfoString( symbol,SYMBOL_CURRENCY_BASE);
   string profitCurrency =SymbolInfoString( symbol,SYMBOL_CURRENCY_PROFIT);
   string marginCurrency =SymbolInfoString( symbol,SYMBOL_CURRENCY_MARGIN);
   
   if (StringFind(blockedCurrencies,baseCurrency)>=0) return (false);
   if (StringFind(blockedCurrencies,profitCurrency)>=0) return (false);
   if (StringFind(blockedCurrencies,marginCurrency)>=0) return (false);
   
   else return(true);

}
 
Calendar
Calendar
  • www.mql5.com
Календарь - фундаментальный анализ на истории и в реал-тайме.
 
Used this code today with minor mods for myself. Very compact and efficient. You did a good job of not overdoing it.
Reason: