MQL4:从CSV读取单个值

jogvjijk  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(236)

我正在尝试从数据源网站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

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

问题1:我可以直接从Quandl获取值吗?还是必须将数据集保存为.csv文件?

因为Quandl提供了API call(如上所示),假设我可以从他们的网站上获取value,而不必将数据集作为.csv文件保存到我的计算机上,我更喜欢从Quandl直接**获取value,而不保存任何文件。

问题2:如何获取要在MT4脚本中使用的值?

尝试过使用FileOpen()访问站点上的数据,但没有成功**,然后尝试print它,以便我可以比较value与其他。是FileOpen()只为.csv文件只保存到我的计算机?我'我希望能够在我的脚本中printvalue一旦检索,以便我可以使用它。以下是我目前所拥有的:

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);
    }
}

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

eit6fx6z

eit6fx6z1#

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

如果使用已发布的Quandl语法尝试API调用:
<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 successful
     {    
          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:使用给定URL**的MT4权限
**1:**一个API URL设置-<pragma>://<URL.ip>/<relative.URL>

一个API**组件

**3:一个APIint data_size**长度计算
附录:这是避免使用*新 *-MQL4.56789WebRequest()函数变体的更多原因列表:

尽管MQL 4文档承诺了WebRequest()函数变体的简单使用,(cit.:)“1.使用头部Content-Type: application/x-www-form-urlencoded发送类型“key=value“的简单请求。",但实际情况远非承诺的简单用例:

**0:**完成日期:MT4管理步骤(**弱点:无法强制MT4通过默认端口~ { :80 | :443 }以外的其他端口与{ http | https }协议通信
1:URL由两个(三个,如果使用:port说明符,在MT4中不起作用(引用0:))部分。<URL.ip_address>是第一部分,可以用规范的IPv4格式(10.38.221.136)或DNS可转换格式(MT4_APAC_PRIMARY.broker.com)表示。第二部分<relative.URL>指定HttpServer本身,文件的位置(HttpServer--相对文件位置)。发布的WebRequest允许使用连接在一起的两个部分,参考aDataSOURCE_URL
3:WebServer API(如果是这样构造的)可能允许添加一些附加参数,这些参数可以指定
呈现
给WebServer。呈现方式取决于是否在调用方侧选择了{ HTTP GET | HTTP POST }协议选项。
**4:每个对MT4 WebRequest()的调用还要求调用者指定一个data***内容 * 参数的长度(参考ArraySize( post ), // int data_size的使用)

相关问题