MQL4: How to read a CSV from a URL
<p>I'm using this <strong><code>URL</code></strong> to fetch some content from a Quandl website: </p>

<p><code>https://www.quandl.com/api/v3/datasets/FRED/PAYEMS.csv?exclude_column_names=true&amp;rows=1&amp;api_key=my_api_key</code></p>

<p>The Quandl server returns in response to the above request a value of this:</p>

<p><code>2016-08-01, 144598.0</code></p>

<p>I need to use the <strong>value</strong> of <strong><code>144598.0</code></strong> within an <code>MQL4</code> <strong><code>Script</code></strong>, so: </p>

<p><strong>Q1. How do I fetch the content from the URL above to be used within an <code>MQL4</code> Script?</strong></p>

<p>A very helpful user from SO (<code>https://stackoverflow.com/users/3666197/user3666197</code>) provided the following <code>script</code> (original found at <a href="https://stackoverflow.com/questions/39279634/mql4-read-single-value-from-csv/39284875#39284875">MQL4: Read single value from CSV</a>) (a couple parts added in by myself) to help me achieve this, however, I couldn't get it to work:</p>

<pre><code>// Code created with the help of Stack Overflow question
// https://stackoverflow.com/questions/39279634/mql4-read-single-value-from-csv/39284875#39284875
// Question by p.luck:
// https://stackoverflow.com/users/5551849/p-luck
// Answer by user3666197:
// https://stackoverflow.com/users/3666197/user3666197

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/FRED/PAYEMS.csv" *
 *   in the list of allowed URLs                                                       *
 *   ( MT4 -&gt; Tools -&gt; Options -&gt; [Tab]: "Expert Advisors" ):                          */

     string aDataSOURCE_URL = "https://www.quandl.com/api/v3/datasets/FRED/PAYEMS.csv";
     string aDataSOURCE_API = "?exclude_column_names=true&amp;rows=1&amp;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             |--&gt; [in]  String to copy.
                                     post,            // uchar   &amp;array[]       &lt;--|    [out] Array of uchar type.
                                     0,               // int      start =  0       |--&gt; [in]  Position from which copying starts. Default - 0. 
                                     WHOLE_ARRAY,     // int      count = -1       |--&gt; [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   |--&gt; [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            |--&gt; [in]  HTTP method.
                        aDataSOURCE_URL,    // const string  URL               |--&gt; [in]  URL.
                        cookie,             // const string  cookie            |--&gt; [in]  Cookie value.
                        NULL,               // const string  referrer          |--&gt; [in]  Value of the Referer header of the HTTP request.
                        timeout,            //       int     timeout           |--&gt; [in]  Timeout in milliseconds.
                        post,               // const char   &amp;data              |--&gt; [in]  Data array of the HTTP message body
                        ArraySize( post ),  //       int     data_size         |--&gt; [in]  Size of the data[] array.
                        result,             //       char   &amp;result         &lt;--|    [out] An array containing server response data.
                        headers             //       string &amp;result_headers &lt;--|    [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 ---------------------------------------
          /*
              "2016-08-01, 144598.0"

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


          }
     }
</code></pre>

<p>I have added the <code>URL</code> of<br> <code>https://www.quandl.com/api/v3/datasets/FRED/PAYEMS.csv</code><br>to the list of <code>allowed URLs</code> in <code>MT4</code>.</p>

<p>If I enable <code>AutoTrading</code> and drag the <code>script</code>, named (<code>tutorial7</code>), on to a <code>chart</code> of <code>USDCAD,M1</code>,<br>I get these messages within the <code>Experts</code> <code>tab</code>:</p>

<ul>
<li><code>Script tutorial7  USDCAD,M1: loaded successfuly</code></li>
<li><code>tutorial7 USDCAD,M1: initialized</code></li>
<li><code>tutorial7 USDCAD,M1: The data has been successfully loaded, size = 0 bytes</code></li>
<li><code>tutorial7 USDCAD,M1: uninit reason 0</code></li>
</ul>

<p>Surely if the "<code>The data has successfully loaded</code>" it shouldn't say "<code>size = 0 bytes</code>"?</p>

<p>Should this <code>script</code> work correctly if I just copy and paste it straight in to the <code>MetaQuotes Language Editor</code> and <code>compile</code> it?</p>

<p>Apart from adding the <code>URL</code> to the <code>allowed URLs</code> in <code>MT4</code> and copying and pasting this code in to a <code>script</code>, is there anything else I must do?</p>

<p>If so, how?</p>

<p>I am running the above code as a <code>Script</code> <strong>not</strong> an <code>Expert Advisor</code>; is this okay?</p>

<p><strong>Q2. Can I set the fetched value of 144598.0 as a variable within my script?</strong> </p>

<p>I need to make the <code>value</code> of <code>144598.0</code> a variable so that it can be compared to another <code>value</code>. </p>

<p>Would something like this work: </p>

<pre><code>void OnStart()
{

... // above code which fetches the value from the .csv URL

double x = 155876.0     // value manually typed in
       y = 144598.0     // value fetched from the .csv URL using the above code
                        // ignores column 1 consisting of 2016-08-01
   if ( x &gt; y ) {
                        // execute code
} 

else { 
                        // execute other code
}

}
</code></pre>