Please Help. Array out of range error in 1 line of coding

 
void xmlOperation()
{
//---define the xml Tags, Vars
   string sTags[7] = {"<title>","<country>","<date><![CDATA[","<time><![CDATA[","<impact><![CDATA[","<forecast><![CDATA[","<previous><![CDATA["};
   string eTags[7] = {"</title>","</country>","]]></date>","]]></time>","]]></impact>","]]></forecast>","]]></previous>"};
   int index = 0;
   int next = -1;
   int BoEvent = 0,begin = 0,end = 0;
   string myEvent = "";
//---minutes calculation
   datetime EventTime = 0;
   int EventMinute = 0;
//---split the currencies into the two parts
   string MainSymbol = StringSubstr(Symbol(),0,3);
   string SecondSymbol = StringSubstr(Symbol(),3,3);
//---loop to get the data from xml tags
   while (true)
   {
      BoEvent = StringFind(sData,"<event>",BoEvent);
      if (BoEvent == -1) break;
      BoEvent += 7;
      next = StringFind(sData,"</event>",BoEvent);
      if (next == -1) break;
      myEvent = StringSubstr(sData,BoEvent,next-BoEvent);
      BoEvent = next;
      begin = 0;
      for (int i=0; i<7; i++)
      {
         Event[index][i] = "";
         next = StringFind(myEvent,sTags[i],begin);
         //---within this event, if tag not found, then it must be missing; skip it
         if (next == -1) continue;
         else
         {
            //---we must have found the sTag okay
            //---advance past the start tag
            begin = next + StringLen(sTags[i]);
            end = StringFind(myEvent,eTags[i],begin);
            //---find start of end tag and Get data between start and end tag
            if (end>begin && end != -1)
            Event[index][i] = StringSubstr(myEvent,begin,end-begin);
         }
      }
      //---filters that define whether we want to skip this particular currencies or events
      //if (news_evmethod && MainSymbol != Event[index][COUNTRY] && SecondSymbol != Event[index][COUNTRY])
      if (news_evmethod == Current && MainSymbol != Event[index][COUNTRY] && SecondSymbol != Event[index][COUNTRY])
      continue;
      if (!IsCurrency(Event[index][COUNTRY]))
      continue;
      if (!news_low && Event[index][IMPACT] == "Low")
      continue;
      if (!news_med && Event[index][IMPACT] == "Medium")
      continue;  
      if (!news_high && Event[index][IMPACT] == "High")
      continue;
      if (Event[index][TIME] == "All Day" ||
          Event[index][TIME] == "Tentative" ||
          Event[index][TIME] == "")
      continue;
      //---sometimes they forget to remove the tags :)
      if (StringFind(Event[index][TITLE],"<![CDATA[") != -1)
      StringReplace(Event[index][TITLE],"<![CDATA[","");
      if (StringFind(Event[index][TITLE],"]]>") != -1)
      StringReplace(Event[index][TITLE],"]]>","");
      if (StringFind(Event[index][FORECAST],"&lt;") != -1)
      StringReplace(Event[index][FORECAST],"&lt;","");
      if (StringFind(Event[index][PREVIOUS],"&lt;") != -1)
      StringReplace(Event[index][PREVIOUS],"&lt;","");
      //---set some values (dashes) if empty
      if (Event[index][FORECAST] == "") Event[index][FORECAST] = "---";
      if (Event[index][PREVIOUS] == "") Event[index][PREVIOUS] = "---";
      //---convert Event time to MT4 time
      EventTime = datetime(MakeDateTime(Event[index][DATE],Event[index][TIME]));
      //---calculate how many minutes before the event (may be negative)
      EventMinute = int(EventTime-TimeGMT())/60;
      //---only alert once
      if (EventMinute == 0 && AlertTime != EventTime)
      {
         FirstAlert  = false;
         SecondAlert = false;
         AlertTime = EventTime;
      }
      //---remove the event after x minutes
      if (EventMinute+news_evhide<0) continue;
      //---set buffers
      MinuteBuffer[index] = EventMinute; <--- array out of range here before 'index'
      ImpactBuffer[index] = ImpactToNumber(Event[index][IMPACT]);
      index++;
   }
//---loop to set arrays/buffers that uses to draw objects and alert
   for(int i=0; i<index; i++)
   {
      for(int n=i; n<200; n++)
      {
         eTitle[n]    = Event[i][TITLE];
         eCountry[n]  = Event[i][COUNTRY];
         eImpact[n]   = Event[i][IMPACT];
         eForecast[n] = Event[i][FORECAST];
         ePrevious[n] = Event[i][PREVIOUS];
         eTime[n]     = datetime(MakeDateTime(Event[i][DATE],Event[i][TIME]))-TimeGMTOffset();
         eMinutes[n]  = (int)MinuteBuffer[i];
         //---check if there are any events
         if (ObjectFind(eTitle[n]) != 0) IsEvent = true;
         else IsEvent = false;
      }
   }
   return;
}

Hi.

I stumble upon an indicator to read news from ff and show it on panel. however as I'm testing it, sometimes the indicator gives out array out of range issue on the highlighted line.

Would be glad if anyone could help suggest what line of coding should I add to make it works properly without the array issue.  Oh btw the array is declared as double, double MinuteBuffer[],ImpactBuffer[];

Thanks in advance.

The checks a trading robot must pass before publication in the Market
The checks a trading robot must pass before publication in the Market
  • www.mql5.com
Before any product is published in the Market, it must undergo compulsory preliminary checks in order to ensure a uniform quality standard. This article considers the most frequent errors made by developers in their technical indicators and trading robots. An also shows how to self-test a product before sending it to the Market.
 

The code you have show is insufficient. You do not show the code where or how the array "MinuteBuffer" is declared, nor where its size is set.

In fact, in the above code, various arrays are accessed but their sizes are never verified, and the indexing is simply incremented without regard for their size limits.

 
Khairul Ammar Izzuddin Bin Mohd Khairul Nizam: Would be glad if anyone could help suggest what line of coding should I add to make it works properly without the array issue.  Oh btw the array is declared as double, double MinuteBuffer[],ImpactBuffer[];
      MinuteBuffer[index] = EventMinute; <--- array out of range here before 'index'
      ImpactBuffer[index] = ImpactToNumber(Event[index][IMPACT]);

Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. Always post all relevant code (using Code button) or attach the source file.
     How To Ask Questions The Smart Way. (2004)
          Be precise and informative about your problem

We have no idea what those two arrays are sized at.

 
Fernando Carreiro #:

The code you have show is insufficient. You do not show the code where or how the array "MinuteBuffer" is declared, nor where its size is set.

In fact, in the above code, various arrays are accessed but their sizes are never verified, and the indexing is simply incremented without regard for their size limits.

William Roeder #:

Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. Always post all relevant code (using Code button) or attach the source file.
     How To Ask Questions The Smart Way. (2004)
          Be precise and informative about your problem

We have no idea what those two arrays are sized at.

I'm so sorry for not providing the full code. okie dokie guys here we go
 
Khairul Ammar Izzuddin Bin Mohd Khairul Nizam #: I'm so sorry for not providing the full code. okie dokie guys here we go

The array "MinuteBuffer" is being set as an Indicator buffer, which means that its size is defined by "rates_total" returned by the OnCalculate() event handler.

Unfortunately, there is a design flaw in the code, that does not take this into account and just assumes that there is sufficient elements. It simply processes the XML data and ignores this. That is why it gives the "array out of range" error.

Since, obviously you don't have the coding knowledge to fix this yourself, I suggest you contact the author and have them fix the design logic.

As a workaround, it may be possible to resolve the issue temporarily if you increase the maximum bars allowed for charts, in the terminal's options.

 

Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
          General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
Next time, post in the correct place. The moderators will likely move this thread there soon.

 
Fernando Carreiro #:

The array "MinuteBuffer" is being set as an Indicator buffer, which means that its size is defined by "rates_total" returned by the OnCalculate() event handler.

Unfortunately, there is a design flaw in the code, that does not take this into account and just assumes that there is sufficient elements. It simply processes the XML data and ignores this. That is why it gives the "array out of range" error.

Since, obviously you don't have the coding knowledge to fix this yourself, I suggest you contact the author and have them fix the design logic.

As a workaround, it may be possible to resolve the issue temporarily if you increase the maximum bars allowed for charts, in the terminal's options.

ouh okay I understand what you are saying. so when the calculation of EventMinute for n number of events (because the loop is infinite while(true) until no more <event> is found and keeps increment the index) exceeds the MinuteBuffer array size (which in this case the rates_total) then the array out of range issue occurs. when you said that the indicator buffer is sized based on the rates_total now it makes sense for the error thanks for the explanation and responses Fernando


is there any way that i could fix it? like declare it as a proper element like array or something? if there is any posting reference of code that i could refer to would be great because i don't know exactly what keyword i should look for right now to fix it.

 
William Roeder #:

Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
          General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
Next time, post in the correct place. The moderators will likely move this thread there soon.

oh yes initially i actually want to make a post on mq4@mt4 section but couldn't find the link to reach there. after you post the root link now that i know you need to scroll down a bit and there it is mq4@mt4 section. lol. sorry and thanks

 
Fernando Carreiro #:

The array "MinuteBuffer" is being set as an Indicator buffer, which means that its size is defined by "rates_total" returned by the OnCalculate() event handler.

Unfortunately, there is a design flaw in the code, that does not take this into account and just assumes that there is sufficient elements. It simply processes the XML data and ignores this. That is why it gives the "array out of range" error.

Since, obviously you don't have the coding knowledge to fix this yourself, I suggest you contact the author and have them fix the design logic.

As a workaround, it may be possible to resolve the issue temporarily if you increase the maximum bars allowed for charts, in the terminal's options.

Hey! so yeah thanks for this specific reply of yours Fernando I managed to solve the error once and for all. Thanks a lot Fernando your comments definitely helps ^^

Again thanks a lot Fernando you own my respect on your knowledge and your kind replies.

Reason: