MQL4:从CSV读取单个值

时间:2016-09-01 19:38:34

标签: csv algorithmic-trading mql4 metatrader4 quandl

我正在尝试从数据源网站Quandl中获取一个值,以便在MetaTrader4脚本中使用。数据源站点提供了一种通过API formats导出数据的方法,包括.csv.json.xml。我选择了.csv格式,然后数据源网站提供API call格式供我使用,格式如下:

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

通过使用上述rows=1中的API call参数,我可以选择只导出一个值(即latest value)。

Q1。我可以直接从Quandl获取值,还是必须将数据集保存为.csv文件?

由于Quandl提供了API call(如上所示),我是否可以正确地假设我可以从他们的网站上获取value而不必保存数据集我的计算机作为.csv文件,然后我必须从中获取latest value?我更倾向于从value获取Quandl 直接而不保存任何文件。

Q2。如何获取要在我的MT4脚本中使用的值?

未成功尝试使用FileOpen()访问网站上的数据的方法,然后尝试print以便我可以比较{{1}对其他人。只有value个文件的FileOpen()仅保存到我的计算机上吗?我希望能够在检索到脚本后.csv print以便我可以使用它。以下是我到目前为止的情况:

value

如果有人可以帮助我获取此值并将其打印在我的脚本中,那将是一个巨大的帮助。

1 个答案:

答案 0 :(得分:1)

A1:不,您不需要为此API使用代理文件

如果尝试API调用,请使用已发布的Quandl语法:
<pragma>://<URL.ip>/<relative.URL>[?<par.i>=<val.i>[&<par.j>=<val.j>[&...]]]
服务器端将推送您的内容:

Date,Value
2013-12-31,4.0

因此,您的代码可能会像这样使用Quandl API:

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 -------------------------------------
          //...


          }
     }

有4个主要项目需要处理:

0: MT4 允许使用指定的 URL
1: API URL设置 - <pragma>://<URL.ip>/<relative.URL>
2: API const char &data[] 组合。 [?<par.i>=<val.i>[&<par.j>=<val.j>[&...]]]
3: API int data_size 长度计算

附录:这是一个原因列表,为什么要避免使用 - MQL4.56789 {{1}功能变体:

尽管WebRequest()文档承诺简单使用MQL4函数变体,(cit。:)“1。发送类型为”WebRequest()“的简单请求使用标题key=value。“,现实远非承诺的简单用例:

Content-Type: application/x-www-form-urlencoded 完成:MT4管理步骤(弱点:无法强制MT4通过0:协议而不是其默认值port(s)〜{ http | https }

{ :80 | :443 } 1: 包含两个(三个,如果使用URL说明符,不起作用在:port (参考0:正上方))部分。 MT4 是第一个,可以用规范的IPv4格式(<URL.ip_address>)或DNS可翻译的格式(10.38.221.136)表示。第二部分MT4_APAC_PRIMARY.broker.com指定HttpServer本身,在哪里定位文件(它是HttpServer - 相对文件位置)。发布WebRequest许可证使用两个部分连接在一起,参考。 <relative.URL>

aDataSOURCE_URL WebServer API(如果构造的话)可能允许添加一些其他参数,可以指定呈现到WebServer。演示文稿取决于是否在呼叫方选择了{ HTTP GET | HTTP POST }协议选项。

3: 对MT4 4:的每次调用也要求来电者指定 WebRequest() 内容的长度参数(参考data的使用)