0

我正在尝试从数据源网站获取一个值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它,以便我可以与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);
    }
}

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

4

1 回答 1

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使用给定API设置的权限 - API组件。API长度计算URL
1:URL<pragma>://<URL.ip>/<relative.URL>
2:const char &data[][?<par.i>=<val.i>[&<par.j>=<val.j>[&...]]]
3:int data_size


附录:这更多是一个原因列表,为什么要避免使用New -MQL4.56789 WebRequest()函数变体:

尽管MQL4文档承诺简单使用WebRequest()函数变体,(cit.:) “1.key=value使用标头发送类型为“”的简单请求Content-Type: application/x-www-form-urlencoded。”,但现实远非承诺的简单用例:

0:完成:MT4 管理步骤(弱点:不能强制 MT4{ http | https }通过默认端口以外的协议进行通信~{ :80 | :443 }

1: URL由两个(三个,如果使用:port说明符,这在(参考 0:右上方)中不起作用)部分组成。MT4<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:每次调用 MT4WebRequest()还需要调用者指定内容参数的长度(参考使用)data ArraySize( post ), // int data_size

于 2016-09-02T05:28:29.343 回答