0

I'm trying to fetch one value from the data source website Quandlto be used within a MetaTrader4 script. The data source site provides a method to export data via API formats including .csv, .json or .xml. I have chosen the .csv format, which the data source website then provides an API call for me to use in the following format:

https://www.quandl.com/api/v3/datasets/ADB/LAB_UNEMP_JPN.csv?rows=1&api_key=my_api_key

By using the rows=1parameter in the above API call, I can choose to just export one value (which is the latest value).

Q1. Can I fetch the value straight from Quandl or do I have to save the dataset as a .csv file?

Because Quandl provides the API call (as seen above), would I be correct in assuming I can just fetch the value from their website and won't have to save the dataset to my computer as a .csvfile, which I would then have to fetch the latest value from? I would much prefer to fetch the value straight from Quandl without saving any files.

Q2. How can I fetch the value to be used within my MT4 script?

I have unsuccessfully tried a method using FileOpen() to access the data on the site, and have then tried to print it so that I can compare the value to others. Is FileOpen() only for .csv files only saved to my computer? I'd like to be able to print the value within my script once retrieved so that I can use it. Here is what I have so far:

int start() {
  while (!IsStopped()) {
    Sleep(2000);

  int handle;
  int value;
  handle=FileOpen("https://www.quandl.com/api/v3/datasets/ADB/LAB_UNEMP_JPN.csv?rows=1&api_key=my_api_key", FILE_CSV, ';');
  if(handle>0)
    {
     value=FileReadNumber(handle);
     Print(handle);
     FileClose(handle);
    }
}

If anyone could aid me in my pursuit to fetch this value and print it within my script, it would be a huge help.

p.luck
  • 401
  • 2
  • 6
  • 26

1 Answers1

1

A1: No, you need not use a proxy-file for this API

If one tries the API call, using a published Quandl syntax of:
<pragma>://<URL.ip>/<relative.URL>[?<par.i>=<val.i>[&<par.j>=<val.j>[&...]]]
the server side will push you the content of:

Date,Value
2013-12-31,4.0

So, your code may use Quandl API with like this:

void OnStart()
{    
     string cookie = NULL,
            headers; 
     char   post[],
            result[]; 
     int    res; 

/*   TODO:                                                                             *
 *   Must allow MT4 to access the server URL,                                          *
 *   you should add URL "https://www.quandl.com/api/v3/datasets/ADB/LAB_UNEMP_JPN.csv" *
 *   in the list of allowed URLs                                                       *
 *   ( MT4 -> Tools -> Options -> [Tab]: "Expert Advisors" ):                          */

     string aDataSOURCE_URL = "https://www.quandl.com/api/v3/datasets/ADB/LAB_UNEMP_JPN.csv";
     string aDataSOURCE_API = "rows = 1&api_key=<My_API_Key>";

     //-- Create the body of the POST request for API specifications and API-authorization
     ArrayResize( post,
                  StringToCharArray( aDataSOURCE_API, // string   text             |--> [in]  String to copy.
                                     post,            // uchar   &array[]       <--|    [out] Array of uchar type.
                                     0,               // int      start =  0       |--> [in]  Position from which copying starts. Default - 0. 
                                     WHOLE_ARRAY,     // int      count = -1       |--> [in]  Number of array elements to copy. Defines length of a resulting string. Default value is -1, which means copying up to the array end, or till terminating '\0'. Terminating zero will also be copied to the recipient array, in this case the size of a dynamic array can be increased if necessary to the size of the string. If the size of the dynamic array exceeds the length of the string, the size of the array will not be reduced.
                                     CP_UTF8          // uint     cp    = CP_ACP   |--> [in]  The value of the code page. For the most-used code pages provide appropriate constants.
                                     )
                  - 1
                  );

//-- Reset the last error code
     ResetLastError();

//-- Loading a html page from Quandl
     int timeout = 5000;                                                //-- Timeout below 1000 (1 sec.) is not enough for slow Internet connection

     res = WebRequest( "POST",              // const string  method            |--> [in]  HTTP method.
                        aDataSOURCE_URL,    // const string  URL               |--> [in]  URL.
                        cookie,             // const string  cookie            |--> [in]  Cookie value.
                        NULL,               // const string  referrer          |--> [in]  Value of the Referer header of the HTTP request.
                        timeout,            //       int     timeout           |--> [in]  Timeout in milliseconds.
                        post,               // const char   &data              |--> [in]  Data array of the HTTP message body
                        ArraySize( post ),  //       int     data_size         |--> [in]  Size of the data[] array.
                        result,             //       char   &result         <--|    [out] An array containing server response data.
                        headers             //       string &result_headers <--|    [out] Server response headers.
                        );
//-- Check errors
     if ( res == -1 )
     {    Print( "WebRequest Error. Error code  = ", GetLastError() );  //-- Perhaps the URL is not listed, display a message about the necessity to add the address
          MessageBox( "Add the address '" + aDataSOURCE_URL + "' in the list of allowed URLs on tab 'Expert Advisors'", "Error", MB_ICONINFORMATION );
          }
     else //-- Load was successfull
     {    
          PrintFormat( "The data has been successfully loaded, size = %d bytes.", ArraySize( result ) );

          //-- parse the content ---------------------------------------
          /*
              "Date,Value
               2013-12-31,4.0"

          */
          //-- consume the content -------------------------------------
          //...


          }
     }

There are 4 principal items to take care of:

0: an MT4 permission to use a given URL
1: an API URL setup - <pragma>://<URL.ip>/<relative.URL>
2: an API const char &data[] assy. [?<par.i>=<val.i>[&<par.j>=<val.j>[&...]]]
3: an API int data_size length calculation


Addendum: This is more a list of reasons, why avoiding use of the New-MQL4.56789 WebRequest() function variants:

Whereas MQL4 documentation promises a simple use of WebRequest() function variants, (cit.:) "1. Sending simple requests of type "key=value" using the header Content-Type: application/x-www-form-urlencoded.", the reality is far from a promised simple use-case:

0: DONE: an MT4 administrative step ( weakness: cannot enforce MT4 to communicate { http | https } protocol(s) over other than their default port(s) ~ { :80 | :443 }

1: URL consists of two ( three, if using a :port specifier, which does not work in MT4 (ref. 0: right above ) ) parts. <URL.ip_address> is the first one and can be expressed in a canonical IPv4 form ( 10.38.221.136 ) or in a DNS-translateable form ( MT4_APAC_PRIMARY.broker.com ). The second part, the <relative.URL>, specifies the HttpServer itself, where to locate a file ( it is a HttpServer--relative file location ). Published WebRequest permit to use the both parts joined together, ref. aDataSOURCE_URL.

3: WebServer API, if constructed so, may permit to add some additional parameters, that can be specified and presented to the WebServer. The presentation depends whether the { HTTP GET | HTTP POST } protocol-option is selected in on a caller side.

4: each call to MT4 WebRequest() also requires the caller to specify a length of a data content parameter ( ref. the use of ArraySize( post ), // int data_size )

Community
  • 1
  • 1
user3666197
  • 1
  • 6
  • 43
  • 77
  • Thanks for your help. I have run the script, but I cannot find where the fetched value is shown. In the Experts tab it says "The data has been successfully loaded, size = 0 bytes". I had to change `cookies` in line 3 to `cookie` to avoid an `undeclared identifier error`- will this adversely affect the script? Also, the `(?)` character which separates `.csv` from `rows=1` within the API call is missing, does it have to be included for this to work? – p.luck Sep 02 '16 at 17:14
  • I'm not sure I understand the last part of your answer under "**There are 4 principle items to take care of:**". I have added `https://www.quandl.com/api/v3/datasets/ADB/LAB_UNEMP_JPN.csv` to the Expert Advisors tab, but I haven't grasped the API `URL` setup of `:///`, the `const char &data[]` assembly `[?=[&=[&...]]]` and finally, `int data_size`. Forgive me for my lack of knowledge surrounding `MQL4`, but what are these things? Do they need to be added within the code? If so, would you please tell me how? – p.luck Sep 05 '16 at 18:59