Mql5 Arrays does not free memory in a for loop

 

I have EA which prints out  Interest Rate Decision of various countries, I have a for loop which loops through 28 pairs and when the news is true it prints out. For example today there is "ECB Interest Rate Decision" so I expect only pairs with "EUR" to return true but all others are returning true. How can I modify the code to get the correct results?

input string symbols    = "AUDCAD,AUDCHF,AUDJPY,AUDNZD,AUDUSD,CADCHF,CADJPY,CHFJPY,EURAUD,EURCAD,EURCHF,EURGBP,EURJPY,EURNZD,EURUSD,GBPAUD,GBPCAD,GBPCHF,GBPNZD,GBPUSD,GBPJPY,NZDCHF,NZDCAD,NZDJPY,NZDUSD,USDCAD,USDCHF,USDJPY";//Symbol List (separated by " , ")
input    string                  symbolPrefix=""; //Symbol Prefix (Symbol Lists Mode)
input    string                  symbolSuffix=""; //Symbol Suffix (Symbol Lists Mode)

input bool     NewsFilter        = true;
int numSymbols=0; //the number of symbols to scan
string symbolList[]; // array of symbols
string symbolListFinal[]; // array of symbols after merging post and prefix


struct NewsData
  {
   int               NewsIndex; // News index
   ulong             Event_id;
   datetime          ReleaseTime; // Release time
   string            NewsName;
  };




//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   EventSetTimer(2);
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTimer()
  {
   string sep=",";
   ushort u_sep;
   int i;
   u_sep=StringGetCharacter(sep,0);
   StringSplit(symbols,u_sep,symbolList);
   numSymbols=ArraySize(symbolList);//get the number of how many symbols are in the symbolList array
   ArrayResize(symbolListFinal,numSymbols);//resize finals symbol list to correct size
   for(i=0; i<numSymbols; i++)  //combines postfix , symbol , prefix names together
     {
      symbolListFinal[i]=symbolPrefix+symbolList[i]+symbolSuffix;
      CheckNews(symbolListFinal[i])  ;
     }

  }


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason){
//--- release handles
   EventKillTimer();

}



bool CheckNews(string sym){
   string   currencybase      = StringSubstr(sym,0,3) ;
   string   currencyquote      = StringSubstr(sym,3,3) ;
   datetime curdatetime       = TimeTradeServer();
   datetime before            = curdatetime - PeriodSeconds(PERIOD_D1);
   datetime end               = curdatetime + PeriodSeconds(PERIOD_D1);
   bool HaveNews           = false;

   MqlCalendarValue  value[],value1[], value2[];
   MqlCalendarEvent  ev1[];

   int base  = CalendarValueHistory(value,before,end,NULL,currencybase);
   int quote = CalendarValueHistory(value1,before,end,NULL,currencyquote);
   int z = base + quote;

   ArrayCopy(value2,value,ArraySize(value2));
   ArrayCopy(value2,value1,ArraySize(value2));

   NewsData eanewsdata[1000]; // Stored news data to struct
   int k = 0;
   for(int i = 0; i < z; i++){
      ulong event_id = value2[i].event_id;
      MqlCalendarEvent impact;
      CalendarEventById(event_id,impact);
      if(impact.importance == CALENDAR_IMPORTANCE_HIGH)
        {
         //RedNews += 1;
         eanewsdata[k].NewsIndex    = i;
         eanewsdata[k].Event_id    = impact.id;
         eanewsdata[k].NewsName     = impact.name;
         eanewsdata[k].ReleaseTime  = value2[i].time;
         k++;
        }
     }
//--- display event values in the Journal
//ArrayPrint(eanewsdata);

   if(ArraySize(eanewsdata) > 0){
      for(int i = 0; i < ArraySize(eanewsdata); i++){
         datetime BeforeReleaseTime = eanewsdata[i].ReleaseTime - PeriodSeconds(PERIOD_D1);
         datetime AfterReleaseTime = eanewsdata[i].ReleaseTime + PeriodSeconds(PERIOD_D1);
         ulong event = eanewsdata[i].Event_id;

         if(event == 999010007 || event == 826020009 || event == 840050014 || event == 124040006 || event == 756010001 || event == 36030008 || event == 554020009){
            if(TimeTradeServer() > BeforeReleaseTime && TimeTradeServer() < AfterReleaseTime){
               Print(event+" "+sym);
               
               HaveNews = true;
             }

           }
        }
     }
     
     ArrayFree(value);
     ArrayFree(value1);
     ArrayFree(value2);
     ArrayFree(ev1);
     ArrayFree(eanewsdata);

   return HaveNews;

}

/*  NEWS  NAME                               EVENT ID
   "ECB Interest Rate Decision"              999010007
   "BoE Interest Rate Decision"              826020009
   "Fed Interest Rate Decision"              840050014
   "BoC Interest Rate Decision"              124040006
   "SNB Interest Rate Decision"              756010001
   "RBA Interest Rate Decision"               36030008
   "RBNZ Interest Rate Decision"             554020009
   "Nonfarm Payrolls"                        840030016 TO BE REMOVED
*/
//+------------------------------------------------------------------+
 
  1.       symbolListFinal[i]=symbolPrefix+symbolList[i]+symbolSuffix;
          CheckNews(symbolListFinal[i])  ;
    ⋮
    bool CheckNews(string sym){
        string   currencybase      = StringSubstr(sym,0,3) ;
        string   currencyquote      = StringSubstr(sym,3,3) ;

    These fail if you have any prefix.

  2.   int base  = CalendarValueHistory(value,before,end,NULL,currencybase);
      int quote = CalendarValueHistory(value1,before,end,NULL,currencyquote);
       int z = base + quote;

    The function returns bool. What do you think z means?

    for(int i = 0; i < z; i++){
          ulong event_id = value2[i].event_id;

    z is not the size of value2.

 
For 1. I would suggest using the symbols properties Base, profit and margin currency to compose a clean symbol string.

Works much more reliable.



Reason: