Download MetaTrader 5

Reading RSS News Feeds by Means of MQL4

26 June 2013, 07:49
vgs
3
3 636


Introduction

This article deals with an example of reading RSS markup by means of MQL4 using the functions from the article HTML Walkthrough Using MQL4. It is assumed that the reader has read the article or at least has the general understanding of the ideas described there.


What Is RSS and Why Do We Need It in MQL4?

RSS is an XML format for transferring various data from one source to another.

RSS is actively used by news agencies, companies, as well as various news web sites.

RSS can be aggregated (or read) by a variety of special applications (readers) and delivered to users in a convenient form. In this article, we will try to make a work piece which can then be turned into a news indicator or just an RSS reader on MQL4 language. What kind of information are we interested in RSS? It is the news, of course.

As mentioned above, RSS is an XML document. So, what is XML?

Xml (eXtensible Markup Language) is a text format for storing structured data. The structure can be visually represented as a tree of elements. XML elements are described by the tags.

Below is an example of a simple XML document:

<!--?xml version="1.0" encoding="windows-1252"?-->
<weeklyevents>
        <event>
                <title>Rightmove HPI m/m</title>
                <country>GBP</country>
                <date><!--[CDATA[05-15-2011]]--></date>
                <time><!--[CDATA[23:01]]--></time>
                <impact><!--[CDATA[Medium]]--></impact>
                <forecast>
                <previous><!--[CDATA[1.7%]]--></previous>
        </forecast></event>
</weeklyevents>


Implementation

As we can see from the above example, XML is somewhat similar to HTML. Therefore, in order not to "reinvent the wheel", we will use the code from the article HTML Walkthrough Using MQL4.

The first thing we need to do is connect HTML walkthrough functions to our project (indicator). To do this, download ReportHTMLtoCSV-2.mq4 file and put it to experts/include folder. Since we are going to use the file as a function library, start() function should be commented out in it.

I would also suggest to rename the file (for example, into HTMLTagsLib.mq4) for more clarity.

The file is ready. Now, connect it to the indicator (the work piece file for the indicator is attached below):

#include <htmltagslib.mq4>

Now we need to include wininet.dll Windows standard library to work with the links:

#include <winuser32.mqh>
#import "wininet.dll"
  int InternetAttemptConnect(int x);
  int InternetOpenA(string sAgent, int lAccessType, 
                    string sProxyName = "", string sProxyBypass = "", 
                    int lFlags = 0);
  int InternetOpenUrlA(int hInternetSession, string sUrl, 
                       string sHeaders = "", int lHeadersLength = 0,
                       int lFlags = 0, int lContext = 0);
  int InternetReadFile(int hFile, int& sBuffer[], int lNumBytesToRead, 
                       int& lNumberOfBytesRead[]);
  int InternetCloseHandle(int hInet);
#import

We will use ReadWebResource(string url) function for reading URL. The function's operation is not a topic of this article. Therefore, we will not dwell on it.

We are only interested in the input and output arguments. The function receives a link to be read and returns the resource content as a string.

In order to analyze the tags, we will use two functions from HTMLTagsLib.mq4 file - FillTagStructure() and GetContent(). These functions are described in details in the article HTML Walkthrough Using MQL4. It should be noted that input data for analysis is passed as an array. Therefore, after the data has been received, it should be converted into array using ReadWebResource(string url) function.

ArrayFromString() function will help us in that:

//+------------------------------------------------------------------+
int ArrayFromString(string & webResData[], string inputStr, string divider) 
{   
   if (inputStr == "") 
   {
     Print ("Input string is not set"); 
     return(0);
   }
   if (divider == "") 
   {
      Print ("Separator is not set"); 
      return(0);
   }
   int i, stringCounter = 0;
   
   string tmpChar, tmpString, tmpArr[64000];   
   int inputStringLen = StringLen(inputStr);
   for (i = 0; i < inputStringLen; i++ ) 
   {
      tmpChar = StringSubstr(inputStr, i, 1);
      tmpString = tmpString + tmpChar;
      tmpArr[stringCounter] = tmpString; 
      if (tmpChar == divider) 
      {          
          stringCounter++;
          tmpString = "";
      }               
   }
   if (stringCounter > 0) 
   {
      ArrayResize(webResData, stringCounter);   
      for (i = 0; i < stringCounter; i++) webResData[i] = tmpArr[i];
   }
   return (stringCounter);
}

Three arguments are passed to the function's input. The first one is the link to the array where the function's operation result is stored, the second one is a string that should be converted into an array and the third one is a separator, by which the string is divided. The function returns the number of rows in the resulting array.

Now our data is ready for analysis.

In the next fragment, we analyze data and display the values of title and country tags in the terminal's console:

   string webRss = ReadWebResource(rssUrl);
   int i, stringsCount = ArrayFromString(webResData, webRss, ">");      
            
   string tags[];    // array for storing the tags
   int startPos[][2];// tag start coordinates
   int endPos[][2];  // tag end coordinates
   
   FillTagStructure(tags, startPos, endPos, webResData);
   int tagsNumber = ArraySize(tags);
   
   string text = "";
   string currTag;
   int start[1][2];
   int end[1][2];
  
   for (i = 0; i < tagsNumber; i++)
      {
      currTag = tags[i];     

      if (currTag == "<weeklyevents>")
         {
            Print("News block start;");
         }

      if (currTag == "<event>")
         {
            text = "";
            start[0][0] = -1;
            start[0][1] = -1;
         }

      if (currTag == "<title>")
         {// coordinates of the initial position for selecting the content between the tags
            start[0][0] = endPos[i][0];
            start[0][1] = endPos[i][1];
         }
                 
      if (currTag == "</title>")
         {// coordinates of the end position for selecting the contents between the tags
            end[0][0] = startPos[i][0];
            end[0][1] = startPos[i][1];
            text = text + GetContent(webResData, start, end) + ";";
         }

      if (currTag == "<country>")
         {// coordinates of the initial position for selecting the content between the tags
            start[0][0] = endPos[i][0];
            start[0][1] = endPos[i][1];
         }
                       
      if (currTag == "</country>")
         {// coordinates of the end position for selecting the contents between the tags
            end[0][0] = startPos[i][0];
            end[0][1] = startPos[i][1];
            text = text + GetContent(webResData, start, end) + " ;";
         }

      if (currTag == "</event>")
         {
            Print(text);
         }

      if (currTag == "</weeklyevents>")
         {
            Print("end of the news;");
         }

      }

Using FillTagStructure() function, we receive the number and the structure of the tags, while GetContent() function provides us with their value.

Script operation results:

Script operation results

Fig. 1. NewsRss script operation results

In the results, we can see the news title and the currency symbol of the country the news is related to.


Conclusions

We have examined the way of reading RSS by means of MQL4 using the functions for HTML tags analysis. The drawbacks of this method are described in details in the article HTML Walkthrough Using MQL4. I would also like to add that one of the drawbacks of the method is an "inconvenience" of using the functions in the code in contrast to other standard libraries for reading XML.

Now that the article and the script have been completed, I am going to consider connection of the external library for working with XML. As for the advantages, I would name implementation speed as one of them.


Translated from Russian by MetaQuotes Software Corp.
Original article: https://www.mql5.com/ru/articles/1366

Attached files |
HTMLTagsLib.mqh (10.02 KB)
NewsRSS.mq4 (6.03 KB)
Last comments | Go to discussion (3)
Lime
Lime | 25 Jun 2015 at 09:42
Does anybody know a Build 600+ compatible RSS reader?

Update:
I'm looking for an MT4 version.
Mohammad Hanif Ansari
Mohammad Hanif Ansari | 23 Sep 2015 at 07:44
Lime:
Does anybody know a Build 600+ compatible RSS reader?

Update:
I'm looking for an MT4 version.
You can order in freelance section.
iJSmile
iJSmile | 3 Jun 2016 at 21:49
Great article. It works. Thank you.
Step on New Rails: Custom Indicators in MQL5 Step on New Rails: Custom Indicators in MQL5

I will not list all of the new possibilities and features of the new terminal and language. They are numerous, and some novelties are worth the discussion in a separate article. Also there is no code here, written with object-oriented programming, it is a too serous topic to be simply mentioned in a context as additional advantages for developers. In this article we will consider the indicators, their structure, drawing, types and their programming details, as compared to MQL4. I hope that this article will be useful both for beginners and experienced developers, maybe some of them will find something new.

Here Comes the New MetaTrader 5 and MQL5 Here Comes the New MetaTrader 5 and MQL5

This is just a brief review of MetaTrader 5. I can't describe all the system's new features for such a short time period - the testing started on 2009.09.09. This is a symbolical date, and I am sure it will be a lucky number. A few days have passed since I got the beta version of the MetaTrader 5 terminal and MQL5. I haven't managed to try all its features, but I am already impressed.

False trigger protection for Trading Robot False trigger protection for Trading Robot

Profitability of trading systems is defined not only by logic and precision of analyzing the financial instrument dynamics, but also by the quality of the performance algorithm of this logic. False trigger is typical for low quality performance of the main logic of a trading robot. Ways of solving the specified problem are considered in this article.

Using text files for storing input parameters of Expert Advisors, indicators and scripts Using text files for storing input parameters of Expert Advisors, indicators and scripts

The article describes the application of text files for storing dynamic objects, arrays and other variables used as properties of Expert Advisors, indicators and scripts. The files serve as a convenient addition to the functionality of standard tools offered by MQL languages.