WebRequest Error EA

 

Hi All,

Im having trouble with an Auto News filter.Im sending the code - what is wrong is that after installed and no errors occur, when attached to chart generates error, but does not show error code, also the url is added to the menu.

//+------------------------------------------------------------------+
//| Get today's high impact events from ForexFactory.com
//+------------------------------------------------------------------+
bool  GetHighImpactEvents()
  {
   string   cookie=NULL,headers,HTML;
   string   url="https://www.forexfactory.com/calendar.php?day=";
   string   time=NULL,lasttime=NULL,currency,impact,title;
   char     post[],result[];
   int      res,cntr=0,timeout=5000;

// If offline, just exit as if it was properly read
   if(!IsConnected() || IsTesting()) return(true);

// Clear daily event structure
   for(res=0; res<MaxDailyEvents; res++)
     { DailyEvents[res].time=DailyEvents[res].title=DailyEvents[res].currency=NULL; DailyEvents[res].displayed=false;++res; }

// Send web request
   url+=MthName(Month())+DoubleToStr(Day(),0)+"."+DoubleToStr(Year(),0);
   ResetLastError();
   res=WebRequest("GET",url,cookie,NULL,timeout,post,0,result,headers);

// Check for errors
   if(res==-1)
     {
      Print("Error in WebRequest. Error code = ",GetLastError());
      MessageBox("Add the address 'http://forexfactory.com/' in the\nlist of allowed URLs on tab 'Expert Advisors'","Error",MB_ICONINFORMATION);
     }
   else
     {
      // Convert character array to a string
      HTML=CharArrayToString(result);

      // Calendar loaded, make sure it's for today's date
      int i= StringFind(HTML,"<span class=\"date\">");
      if(i == -1) return(false);
      HTML = StringSubstr(HTML, i);
      string date=GetHTMLElement(HTML,"<span>","</span>");
      if(date != MthName(Month()) + " " + DoubleToStr(Day(), 0)) return(false);

      // Now get table rows for each event
      lasttime=NULL;
      date=DoubleToStr(Year(),0)+"."+DoubleToStr(Month(),0)+"."+DoubleToStr(Day(),0)+" ";
      do
        {
         // Get event information
         time=GetHTMLElement(HTML,"<td class=\"calendar__cell calendar__time time\">","</td>");
         if(StringFind(time,"<a name=\"upnext\"")==0) time=GetHTMLElement(time,"class=\"upnext\">","</span>");
         if(StringLen(time)!=0) lasttime=time;
         if(StringLen(time)==0) time=lasttime;

         // If the time has 'pm' in it, add 12 hours.  StrToTime only understands a 24 hour clock.
         if(StringFind(time,"pm")!=-1) time=TimeToStr(StrToTime(time)+(12*60*60));
         time=date+time;

         // Get the other elements we need
         currency=GetHTMLElement(HTML,"<td class=\"calendar__cell calendar__currency currency\">","</td>");
         impact=GetHTMLElement(HTML,"<span title=\"","\" class=\"");
         i=StringFind(impact," Impact");
         if(i != -1) impact = StringSubstr(impact, 0, i);
         title = GetHTMLElement(HTML, "\"calendar__event-title\">", "</span>");

         // Is this a high impact event for my currency pair?
         if(StringFind(Symbol(),currency)!=-1 && impact=="High")
           {
            // Add to daily event structure
            DailyEvents[cntr].displayed=false;
            DailyEvents[cntr].time=time;
            DailyEvents[cntr].title=title;
            DailyEvents[cntr++].currency=currency;
           }

         // Cut HTML string to the next table row
         i=StringFind(HTML,"</tbody> </table> </td> </tr> ");
         if(i!=-1) HTML=StringSubstr(HTML,i+30);
         if(StringFind(HTML,"</table> <div class=\"foot\">")==0) i=-1;
        }
      while(i!=-1 || cntr==MaxDailyEvents);
     }

// Display the high impact events, if any
   lasttime = NULL;
   for(cntr = 0; cntr < MaxDailyEvents; cntr++)
     {
      if(StringLen(DailyEvents[cntr].time)==0) break;

      // Create event marker on chart if last market wasn't the same time
      if(lasttime!=DailyEvents[cntr].time)
        {
         res=cntr;
         if(ObjectCreate(0,"Event"+DoubleToStr(cntr,0),OBJ_EVENT,0,StrToTime(DailyEvents[cntr].time),0))
           {
            ObjectSetString(0,"Event"+DoubleToStr(cntr,0),OBJPROP_TEXT,DailyEvents[cntr].title+" ("+DailyEvents[cntr].currency+")");
            ObjectSetInteger(0,"Event"+DoubleToStr(cntr,0),OBJPROP_COLOR,Red);
            ObjectSetInteger(0,"Event"+DoubleToStr(cntr,0),OBJPROP_WIDTH,2);
            ObjectSetInteger(0,"Event"+DoubleToStr(cntr,0),OBJPROP_BACK,true);
            ObjectSetInteger(0,"Event"+DoubleToStr(cntr,0),OBJPROP_SELECTABLE,false);
            ObjectSetInteger(0,"Event"+DoubleToStr(cntr,0),OBJPROP_SELECTED,false);
            ObjectSetInteger(0,"Event"+DoubleToStr(cntr,0),OBJPROP_HIDDEN,true);
            ObjectSetString(0,"Event"+DoubleToStr(cntr,0),OBJPROP_TOOLTIP,DailyEvents[cntr].title+" ("+DailyEvents[cntr].currency+")");
           }

         // Create vertical line if event is in the future
         if(TimeCurrent()<StrToTime(DailyEvents[cntr].time))
           {
            if(ObjectCreate(0,"VLine"+DoubleToStr(cntr,0),OBJ_VLINE,0,StrToTime(DailyEvents[cntr].time),0))
              {
               ObjectSetInteger(0,"VLine"+DoubleToStr(cntr,0),OBJPROP_COLOR,Red);
               ObjectSetInteger(0,"VLine"+DoubleToStr(cntr,0),OBJPROP_WIDTH,1);
               ObjectSetInteger(0,"VLine"+DoubleToStr(cntr,0),OBJPROP_BACK,true);
               ObjectSetInteger(0,"VLine"+DoubleToStr(cntr,0),OBJPROP_SELECTABLE,false);
               ObjectSetInteger(0,"VLine"+DoubleToStr(cntr,0),OBJPROP_SELECTED,false);
               ObjectSetInteger(0,"VLine"+DoubleToStr(cntr,0),OBJPROP_HIDDEN,true);
               ObjectSetString(0,"VLine"+DoubleToStr(cntr,0),OBJPROP_TOOLTIP,DailyEvents[cntr].title+" ("+DailyEvents[cntr].currency+")");
               ObjectSetInteger(0,"VLine"+DoubleToStr(cntr,0),OBJPROP_TIMEFRAMES,OBJ_PERIOD_M1|OBJ_PERIOD_M5|OBJ_PERIOD_M15|OBJ_PERIOD_M30|OBJ_PERIOD_H1);
              }
           }
         else
            DailyEvents[cntr].displayed=true;
        }
      else
        {
         title = ObjectGetString(0,"Event"+ DoubleToStr(res,0),OBJPROP_TOOLTIP);
         title+= "\n" + DailyEvents[cntr].title + " (" + DailyEvents[cntr].currency + ")";
         ObjectSetString(0,"Event"+DoubleToStr(res,0),OBJPROP_TOOLTIP,title);
         if(TimeCurrent()<StrToTime(DailyEvents[cntr].time)) ObjectSetString(0,"Vline"+DoubleToStr(res,0),OBJPROP_TOOLTIP,title);
        }
      lasttime=DailyEvents[cntr].time;
     }

// Exit
   return(true);
  }
 

I wrote a library to download and parse the ff cal xml rss feed (fast) that you may prefer... 


https://bitbucket.org/nicholishen/mql4-ffevents/src/master/

README
  • 2017.07.26
  • bitbucket.org
This is a working demo of the FFEvents library. In order to test this in your MT4 terminal you will need to clone this entire repo into a subfolder in the ../MQL4/Scripts folder and compile with metaeditor. What is this repository for? How do I get set up? Clone repo to a subfolder in the ../MQL4/Scripts folder. Compile FFEvents_example...
 
nicholi shen:

I wrote a library to download and parse the ff cal xml rss feed (fast) that you may prefer... 


https://bitbucket.org/nicholishen/mql4-ffevents/src/master

Ok, thank you, just one thing which of the files should i use to retrive data on the event time?

Best Regards