Popular Posts

Editor'S Choice - 2020

MQL4: learning the WebRequest method


Not so long ago, in a lesson about the open positions of traders, we took data from a website on the network. What we did then in the lesson can be done in another way, without using dll, therefore, without depending on the operating system.

All you need is a Metatrader 4 terminal and mql code editor. As well as knowledge of the standard mql4 WebRequest method, which we will meet today.

WebRequest Method

The WebRequest method can be called in two ways. The first is suitable for sending simple requests to a server with standard http headers. It looks like this:

int WebRequest (
const stringmethod, // HTTP method
const stringurl, // url
const stringcookie, // cookie
const stringreferer, // referer
inttimeout,    // timeout
const char& data, // array of the body of the HTTP message
intdata_size,       // size of the data array in bytes
char& result,        // array with server response data
string& result_headers// server response headers

The second option allows you to fill in the headers array yourself. This is useful in various difficult situations when, for example, you need to send files to the server.

int WebRequest (
const stringmethod, // HTTP method
const stringurl, // url
const stringheaders, // headers
inttimeout,    // timeout
const char& data, // array of the body of the HTTP message
char& result,        // array with server response data
string& result_headers// server response headers

method can be any HTTP method. In the video tutorial, we'll look at using the GET method;

url -The address of the server you are accessing

cookie -accordingly, cookies, for example, for authorization on the server;

referer - value of the Referer header of the HTTP request;

timeout -timeout in milliseconds, that is, the time to wait for a response from the server;

headers -your custom headers. When using the first method call option, by default, the request will be sent with the header: Content-Type: application / x-www-form-urlencoded.

In the case of a browser, the title might look like this, for example:

GET / tutorials / HTTP / 1.1
Host: tlap.com
User-Agent: Mozilla / 5.0 (Windows; U; Windows NT 6.1; en-US; rv: Gecko / 20091102 Firefox / 3.5.5 (.NET CLR 3.5.30729)
Accept: text / html, application / xhtml + xml, application / xml; q = 0.9, * / *; q = 0.8
Accept-Language: en-us, en; q = 0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1, utf-8; q = 0.7, *; q = 0.7
Keep-Alive: 300
Connection: keep-alive
Cookie: PHPSESSID = r2t5uvjq435r4q7ib3vtdjq120
Pragma: no-cache
Cache-control: no-cache

In our case, to send a file you need the following header:

Content-Type: multipart / form-data; boundary = - WebKitFormBoundaryePkpFF7tjBAqx29L
Content-Disposition: form-data; name = "uploadedfile"; filename = "hello.tlap" Content-Type: application / x-object

But sending files is a rather rare task, much more often we just need to get some data from the mql4 program.

The WebRequest method itself returns int, that is, a number. This number is nothing more than a response code from the server, or -1 in the event of a method execution error. The response code is a three-digit number. There are currently five code classes:

1xx: Informational - the request is received and understood, and processing continues;
2xx: Success - the request was successfully received, understood and processed;
3xx: Redirection - further steps must be taken to complete the request;
4xx: Client Error - the request has poor syntax or cannot be executed;
5xx: Server Error - The server is unable to complete a valid request.

Here are a couple of the most common examples:

200 OK - Successful request. If the client requested any data, it is in the header or body of the message. Such code comes in case of success;

301 Moved Permanently. The requested document was finally transferred to the new URL specified in the Location field of the header. This code means that the page has been transferred and is now available at a different address;

400 Bad Request. The request is not understood by the server due to a syntax error. This code means that a typo occurred during the request;

401 Unauthorized. The request requires user identification. This code means that you first need to log in - enter the login and password from the site;

403 Forbidden. The server understood the request, but it refuses to fulfill it due to some access restrictions;

404 Not Found. The server understood the request, but could not find the corresponding resource at the specified URL. Most often, this code indicates that the page was simply deleted;

500 Internal Server Error. Any internal server error that is not part of the rest of the 5xx class errors.

In fact, of course, there are a lot more codes. In each class there are usually from a dozen to several dozen, but there is no point in giving everything here, especially since this information can be independently found on the network. I have given only the most common response codes.

Do not forget to add the used site address to the list of allowed url in the terminal, otherwise nothing will work. To do this, press Ctrl + O or go to service-> settings-> advisors tab.

The settings should be approximately as shown in the figure above.

In your work, it is worth considering that the WebRequest method waits for a response from the server, and until the response arrives, further script execution is impossible. Therefore, this indicator cannot be used in the indicator. In addition, consider that it takes time to execute the method, and during this time the script or adviser will not do anything else. Especially it is worth considering this specificity if you send heavy requests or receive heavy answers, for example, voluminous files with any data.


Today we have analyzed the WebRequest method, which provides a fairly simple way for your advisers and scripts to interact with the outside world. Using this method, you can receive web pages, download and send files, perform automatic manipulations on the site, and much more.

The possibilities of this method are really endless, especially if you do not know other programming languages. Using it, you can solve completely non-trading tasks, for example, make an autoclicker or emulate any other useful work on the network that brings money. It's a funny idea, but if you work hard, you can write your auto-surfer, who will earn for you on clicks while your main bots drain the deposit - a trifle, but nice.

Watch the video: MQL4 Programming 6 (April 2020).

Leave Your Comment