Download MetaTrader 5

Using WinInet.dll for Data Exchange between Terminals via the Internet

17 June 2010, 15:25
11 542

MetaTrader 5 opens up unique opportunities for users, using a number of new user interface elements in its arsenal. Because of this, the functions that were previously unavailable can now be used to the maximum.

In this lesson we will learn to:

  • use basic Internet technologies;
  • exchange data between terminals via the server;
  • create a generic library class to work with the Internet in the MQL5 environment.

The MQL5 CodeBase contains an example of script, which works with the wininet.dll library and shows an example of the server page request. But today we'll go much further, and make the server, not only give us the page, but also to send and store this data for subsequent transfers to other requesting terminals.

Note: for those who don't have access to a server, configured with PHP, we suggest downloading the Denwer kit, and using it as a working platform. Also, we recommend using the Apache server and PHP on your localhost for testing.

To send any request to the server, we will need the 7 major functions of the library.

InternetAttemptConnect  Attempt to locate an Internet connection and establish it
Initializes the structure for the work of the WinInet library functions. This function must be activated before activating any other functions of the library.
InternetConnect Opens the resource specified by the address HTTP URL or FTP. Returns the descriptor to an open connection
HttpOpenRequest Creates a descriptor for HTTP requests for setting up a connection
HttpSendRequest Sends a query, using the created descriptor
InternetReadFile Reads data, received from the server after the query has been processed
InternetCloseHandle Releases the transferred descriptor

A detailed description of all of the functions and their parameters can be found in the MSDN Help system.

The declaration of functions remained the same as in MQL4, with the exception of the use of Unicode calls and line transfers by the link.

#import "wininet.dll"
int InternetAttemptConnect(int x);
int InternetOpenW(string &sAgent,int lAccessType,string &sProxyName,string &sProxyBypass,int lFlags);
int InternetConnectW(int hInternet,string &szServerName,int nServerPort,string &lpszUsername,string &lpszPassword,int dwService,int dwFlags,int dwContext);
int HttpOpenRequestW(int hConnect,string &Verb,string &ObjectName,string &Version,string &Referer,string &AcceptTypes,uint dwFlags,int dwContext);
int HttpSendRequestW(int hRequest,string &lpszHeaders,int dwHeadersLength,uchar &lpOptional[],int dwOptionalLength);
int HttpQueryInfoW(int hRequest,int dwInfoLevel,int &lpvBuffer[],int &lpdwBufferLength,int &lpdwIndex);
int InternetReadFile(int hFile,uchar &sBuffer[],int lNumBytesToRead,int &lNumberOfBytesRead);
int InternetCloseHandle(int hInet);

//To make it clear, we will use the constant names from wininet.h.
#define OPEN_TYPE_PRECONFIG     0           // use the configuration by default
#define FLAG_KEEP_CONNECTION    0x00400000  // do not terminate the connection
#define FLAG_PRAGMA_NOCACHE     0x00000100  // no cashing of the page
#define FLAG_RELOAD             0x80000000  // receive the page from the server when accessing it
#define SERVICE_HTTP            3           // the required protocol

A detailed description of the flags is located in the same section MSDN for each of the functions. If you wish to see the declaration of other constants and functions, then you can download the original wininet.h file, located in the attachments to the article.

1. Guides for creating and deleting Internet session

The first thing we should do is create a session and open a connection to the host. A session should preferably be created only once during the program initialization (eg, in a function OnInit). Or it can be done at the very beginning of launching the Expert Advisor, but it is important to make sure that its successful creation was done only once before the closing of the session. And it shouldn't be invoked repeated and without need, with every new iteration of the implementation OnStart or OnTimer. It's important to avoid frequent calls and creation of the structures required for each call.

Therefore, we will use only one global class instance for describing the session and connection descriptiors.

   string            Host;       // host name
   int               Port;       // port
   int               Session;    // session descriptor
   int               Connect;    // connection descriptor

bool MqlNet::Open(string aHost,int aPort)
      Print("-Host is not specified");
   // checking the DLL resolution in the terminal  
      Print("-DLL is not allowed");
   // if the session was identifies, then we close
   if(Session>0 || Connect>0) Close();
   // record of attempting to open into the journal
   Print("+Open Inet...");
   // if we were not able to check for the presence of an Internet connection, then we exit
      Print("-Err AttemptConnect");
   string UserAgent="Mozilla"; string nill="";
   // open a session
   // if we were not able to open a session, then exit
      Print("-Err create Session");
      Print("-Err create Connect");
   Host=aHost; Port=aPort;
   // otherwise all attempts were successful

After initialization the descriptors Session and Connect can be used in all of the following functions. Once all work is completed and MQL-programs are uninstalled, they must be removed. This is done by using the InternetCloseHandle function.

void MqlNet::CloseInet()
   Print("-Close Inet...");
   if(Session>0) InternetCloseHandle(Session); Session=-1;
   if(Connect>0) InternetCloseHandle(Connect); Connect=-1;

Attention! When working with Internet functions, it is necessary to free up all of the descriptors, derived from them, using InternetCloseHandle.

2. Sending a request to the server and receiving the page

For sending a request and receiving a page in response to this request, we will need the remaining three functions HttpOpenRequest, HttpSendRequest и InternetReadFile. The essence of the receiving the page in response to a request is basically the simple process of saving its contents into a local file.

For the convenience of working with requests and contents we will create two universal functions.

Sending a request:

bool MqlNet::Request(string Verb,string Object,string &Out,bool toFile=false,string addData="",bool fromFile=false)
   if(toFile && Out=="")
      Print("-File is not specified ");
   uchar data[];
   int hRequest,hSend,h;
   string Vers="HTTP/1.1";
   string nill="";
         Print("-Err reading file "+addData);

     } // read file in the array
   else StringToCharArray(addData,data);

   if(Session<=0 || Connect<=0)
         Print("-Err Connect");
   // create a request descriptor
      Print("-Err OpenRequest");
   // send request
   // headline for request
   string head="Content-Type: application/x-www-form-urlencoded";
   // sent file
      Print("-Err SendRequest");
   // read the page 
   // close all handles

Function parameters of MqlNet:: Request:

  • string Verb – request type “GET” or “POST”;
  • string Object – name of the page with its passed on parameters;
  • string &Out – line to which the answer is received;
  • bool toFile – if toFile=true, then Out indicates the name of the file where the answer should be received;
  • string addData - Additional data;
  • bool fromFile - If fromFile = true, then addData is the name of the file that needs to be sent.

Reading the contents of the received descriptor

void MqlNet::ReadPage(int hRequest,string &Out,bool toFile)
   // read the page 
   uchar ch[100];
   string toStr="";
   int dwBytes,h;
      if(dwBytes<=0) break;
   else Out=toStr;

Function parameters of MqlNet:: ReadPage:

  • Int hRequest - request descriptor, from which the data is read;
  • string &Out – line to which the answer is received;
  • bool toFile - If toFile = true, then Out is the name of the file where the answer will be received.

And by gathering all of this into one, we will obtain an MqlNet library class for working with the Internet.

class MqlNet
   string            Host;     // host name
   int               Port;     // port
   int               Session; // session descriptor
   int               Connect; // connection descriptor
                     MqlNet(); // class constructor
                    ~MqlNet(); // destructor
   bool              Open(string aHost,int aPort); // create a session and open a connection
   void              Close(); // close session and connection
   bool              Request(string Verb,string Request,string &Out,bool toFile=false,string addData="",bool fromFile=false); // send request
   bool              OpenURL(string URL,string &Out,bool toFile); // somply read the page into the file or the variable
   void              ReadPage(int hRequest,string &Out,bool toFile); // read the page
   int               FileToArray(string FileName,uchar &data[]); // copy the file into the array for sending

That's basically all of the required functions that are likely to satisfy the diversified needs for working with the Internet. Consider the examples of their use.

Example 1. Automatic download of MQL-programs into the terminal's folders. MetaGrabber script

To begin our testing of the class's work, let us start with the easiest tasks - reading the page and saving its contents to the specified folder. But a simple reading of the pages is unlikely to be very interesting, so in order to gain something from the work of the script, let's assign it a functional of grabber of mql programs from sites. The task of the MetaGrabber script will be:

  • URL analysis and separation of it into the host, the request, and the file name;
  • sending a request to the host, receiving and saving the file into the terminal folder \\ Files;
  • moving it from the Files to one of the required data folders:
    \Experts, \Indicators, \Scripts, \Include, \Libraries, \Tester(set), \Templates.

To solve the second problem we use the MqlNet class. For the third task we use the function MoveFileEx from Kernel32.dll

#import "Kernel32.dll"
bool MoveFileExW(string &lpExistingFileName, string &lpNewFileName, int dwFlags);
#import "Kernel32.dll"

For the first problem, let's make a small service function of parsing the URL line.

We need to allocate three lines from the address: the host, the path to the site, and the file name.
For example, in line

- Host =
- Request = / folder / page.html
- File name = page.html

In the case of CodeBase on the MQL5 site, the path ways have the same structure. For example, the path to the library ErrorDescription.mq5 on page looks like This path is easily obtained by right clicking on the link and selecting "Copy Link". Thus, the URL is split into two parts, one for the request and one for the file name for convenience of file storage.

- Host =
- Request = / data/18/79/5/ErrorDescription.mqh
- File name = ErrorDescription.mqh

This is the kind of line parsing that the following ParseURL function will be dealing with.

void ParseURL(string path,string &host,string &request,string &filename)
   // removed
   int i=StringFind(host,"/"); 
   string file="";
   for(i=StringLen(URL)-1; i>=0; i--)
   if(file!="") filename=file;

In the outer parameters of the script we will make only two parameters - URL (path of the mql5 file) and the type of folder of subsequent placement - that is, into which terminal folder you wish to place it.

As a result, we obtain a short but very useful script.

//|                                                  MetaGrabber.mq5 |
//|                                 Copyright © 2010 |
//|                                         Coding by Sergeev Alexey |
#property copyright "  © 2010"
#property link      ""
#property version               "1.00"
#property description  "Download files from internet"

#property script_show_inputs

#include <InternetLib.mqh>

#import "Kernel32.dll"
bool MoveFileExW(string &lpExistingFileName,string &lpNewFileName,int dwFlags);

enum _FolderType

input string URL="";
input _FolderType FolderType=0;
//------------------------------------------------------------------ OnStart
int OnStart()
   MqlNet INet; // variable for working in the Internet
   string Host,Request,FileName="Recieve_"+TimeToString(TimeCurrent())+".mq5";

   // parse url

   // open session
   if(!INet.Open(Host,80)) return(0);
   Print("+Copy "+FileName+" from  http://"+Host+" to "+GetFolder(FolderType));

   // obtained file
      Print("-Err download "+URL);
   Print("+Ok download "+FileName);

   // move to the target folder
   string to,from,dir;
   // if there is no need to move it elsewhere
   if(FolderType==Files) return(0);

   // from

   // to
   if(FolderType!=Templates && FolderType!=TesterSet) to+="MQL5\\";

   // move file 
      Print("-Err move to "+to);
   Print("+Ok move "+FileName+" to "+GetFolder(FolderType));

//------------------------------------------------------------------ GetFolder
string GetFolder(_FolderType foldertype)
   if(foldertype==Experts) return("Experts");
   if(foldertype==Indicators) return("Indicators");
   if(foldertype==Scripts) return("Scripts");
   if(foldertype==Include) return("Include");
   if(foldertype==Libraries) return("Libraries");
   if(foldertype==Files) return("Files");
   if(foldertype==Templates) return("Profiles\\Templates");
   if(foldertype==TesterSet) return("Tester");
//------------------------------------------------------------------ ParseURL
void ParseURL(string path,string &host,string &request,string &filename)
   // removed
   int i=StringFind(host,"/"); 
   string file="";
   for(i=StringLen(URL)-1; i>=0; i--)
   if(file!="") filename=file;

Let's conduct the experiments on our favorite section The downloaded files will immediately appear in the editor's navigator, and they can be compiled without restarting the terminal or the editor. And they will not wander through the long paths of the file system in the search for the desired folder to move the files into.

Attention! Many sites put up a security against massive content downloading, and your IP-address, in case of such mass download, may be blocked by this resource. So pay careful attention to the use of "machine" downloading of files from resources, which you access often and do not want to be banned from using.

Those who wish to go even further, improving the proposed service, can use the Clipboard script with the interception of the clipboard's contents and the further automatic downloading.

Example 2. Monitoring quotes from multiple brokers on a single chart

So we have learned to obtain files from the Internet. Now let's consider a more interesting question - how to send and store this data on the server. For this we need a small additional PHP-script, which will be located on the server. Using the written MqlNet class, we create an Expert Advisor for monitoring - MetaArbitrage . The task of the expert in conjunction with the PHP-script will be:

  • Sending an Expert Advisor request to the server;
  • formation of the response page (PHP) on the server;
  • reception of this page by the Expert Advisor;
  • its analysis and the delivery of results to the screen.

The schematic diagram of the interaction between the MQL-module and the PHP-script is as follows:

We will use the MqlNet class to solve these tasks.

To avoid the duplication of data, as well as to weed out outdated quotes - we will send 4 main parameters: the name of the broker's server (the source of current prices), the currency, the price and time of quotes in UTC. For example, a request to access the script from the resources of our company is as follows:

These parameters and the actual quote are stored on the server and will be issued on the response page, along with all other stored quotes of this currency.

The "collateral" convenience of this exchange is that quotes can be sent from MT5, as well as from MT4!

The page, which is formed by the server, is a regular CSV file. In this script it looks like this:

ServerName1; Bid1; Time1
ServerName 2; Bid2; Time2
ServerName 3; Bid3; Time3

ServerName N; BidN; TimeN

But you can add to it your own additional parameters (eg, server type - demo or real). We store this CSV-file and parse it line by line, with the output of a table of value and line prices displayed on the screen.

Processing this file can be in be accomplished in many different ways, choosing the once that are required in each particular case. For example, filter out the quotes, received from the MetaTrader 4 demo server, etc.

The advantages of using the Internet server are obvious - you are sending your quotes, which can be received and viewed by another trader. Likewise, you will receive quotes that are sent to other traders. That is, the interaction between the terminals is bilateral, the exchange of data is accomplished as shown in the following scheme:

This scheme serve as the basis for the principle of information exchange between any number of terminals. A full MetaArbitrage Expert Advisor and the PHP-script with commentaries can be downloaded from the link in the attachments. More about PHP-used functions can be read on the following site

Example 3. Exchange of messages (mini chat) within the terminal. MetaChat Expert Advisor

Let's take a step away from trading and numbers, and create an application, which will allow us to chat with several people at once, without exiting the terminal. To do this we will need one more PHP script, which in general is very similar to the previous one. Except for the fact that in this script, instead of analyzing time quotations, we will be analyzing the number of lines in a file. The task of the Expert Advisor will be:

  • Sending a text line to the server;
  • addition of this line into the shared file, controlling the size of the file, issuing the response file (php);
  • receiving the current chat and displaying it on the screen.

The work of MetaChat will not differ from that of the previous Expert Advisor. The same principle, and the same simple CSV file for output.

MetaChat and MetaArbitrage are maintained on the site of its developers. The PHP-scripts for their work are also located there.
Therefore, if you want to test a work or use this service, you can access it through the following link:
MetaСhat -
MetaArbitrage -


And so we have familiarized ourselves with the HTTP-requests. We gained the ability to send and receive data through the Internet, and to organize the working process more comfortably. But any capabilities can always be improves. The following can be considered the new potential directions of their improvements:

  • reading the news or receiving other information directly into the terminal for the analysis of Expert Advisors;
  • remote management of Expert Advisors;
  • Automatic updates of Expert Advisors / indicators;
  • copiers / translators of trades, sending signals;
  • downloading templates along with lights and set-files for Expert Advisors:
  • And much, much more ...

In this article we used the GET type of requests. They sufficiently fulfill the task when you need to obtain a file, or send a request, with a small number of parameters, for a server analyses.

In our next lesson, we will take a careful look at POST requests - sending files to the server or file sharing between terminals, and we will consider the examples of their use.

Useful Resources

Translated from Russian by MetaQuotes Software Corp.
Original article:

Attached files |
internetlib.mqh (8.35 KB)
metaarbitrage.mq5 (9.27 KB)
metachat.mq5 (5.68 KB)
metagrabber.mq5 (3.24 KB) (0.72 KB) (12.81 KB)
Last comments | Go to discussion (4)
Brett Luedtke
Brett Luedtke | 17 Jul 2011 at 06:38


Metachat is a good idea and I'd like to get one working myself.

Nothing referenced on the site is working anymore - can you please provide the PHP source to metachat? Much thanks.

Etienne Chabert
Etienne Chabert | 4 Nov 2013 at 09:10

This article should be seriously edited, the internetLib.mqh included look to be deprecated and can caused a random invalid access in wininet.dll

You should use the internetLib included in this article to avoid difficulties -> Using WinInet in MQL5. Part 2: POST Requests and Files

This library do his work without issues 

Viktor Placek
Viktor Placek | 22 Apr 2016 at 23:43

After couple of hours trying to to implement this buggy object I have found out that MQL5 has its native HTTP communication function WebRequest. See the documentation for more information.

Viktor Placek
Viktor Placek | 23 Apr 2016 at 09:06
OK, I have changed my mind. This object is worth of try because these sick morons from MetaQuotes forbid using WebRequest function in Strategy Tester.
Creating a Multi-Currency Indicator, Using a Number of Intermediate Indicator Buffers Creating a Multi-Currency Indicator, Using a Number of Intermediate Indicator Buffers

There has been a recent rise of interest in the cluster analyses of the FOREX market. MQL5 opens up new possibilities of researching the trends of the movement of currency pairs. A key feature of MQL5, differentiating it from MQL4, is the possibility of using an unlimited amount of indicator buffers. This article describes an example of the creation of a multi-currency indicator.

Step-By-Step Guide to writing an Expert Advisor in MQL5 for Beginners Step-By-Step Guide to writing an Expert Advisor in MQL5 for Beginners

The Expert Advisors programming in MQL5 is simple, and you can learn it easy. In this step by step guide, you will see the basic steps required in writing a simple Expert Advisor based on a developed trading strategy. The structure of an Expert Advisor, the use of built-in technical indicators and trading functions, the details of the Debug mode and use of the Strategy Tester are presented.

An Example of a Trading System Based on a Heiken-Ashi Indicator An Example of a Trading System Based on a Heiken-Ashi Indicator

In this article we look into the question of using a Heiken-Ashi indicator in trading. Based on this indicator, a simple trading system is considered and an MQL5 Expert Advisor is written. Trading operations are implemented on the bases of classes of the Standard class library. The testing results of the reviewed trading strategy, are based on the history, and obtained using the built-in MetaTrader 5 strategy tester, are provided in the article.

Guide to writing a DLL for MQL5 in Delphi Guide to writing a DLL for MQL5 in Delphi

The article examines the mechanism of creating a DLL module, using the popular programming language of ObjectPascal, within a Delphi programming environment. The materials, provided in this article, are designed to primarily target beginner programmers, who are working with problems, which breach the boundaries of the embedded programming language of MQL5, by connecting the outside DLL modules.