【发布时间】:2016-09-07 10:19:47
【问题描述】:
我正在使用这个 URL 从 Quandl 网站获取一些内容:
https://www.quandl.com/api/v3/datasets/FRED/PAYEMS.csv?exclude_column_names=true&rows=1&api_key=my_api_key
Quandl 服务器响应上述请求返回如下值:
2016-08-01, 144598.0
我需要在MQL4 Script中使用144598.0的值,所以:
第一季度。如何从上面的 URL 获取内容以在 MQL4 脚本中使用?
来自 SO (https://stackoverflow.com/users/3666197/user3666197) 的一位非常乐于助人的用户提供了以下 script(原始发现于 MQL4: Read single value from CSV)(我自己添加了几个部分)来帮助我实现这一目标,但是,我无法得到它的工作:
// 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 -> Tools -> Options -> [Tab]: "Expert Advisors" ): */
string aDataSOURCE_URL = "https://www.quandl.com/api/v3/datasets/FRED/PAYEMS.csv";
string aDataSOURCE_API = "?exclude_column_names=true&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 ---------------------------------------
/*
"2016-08-01, 144598.0"
*/
//-- consume the content -------------------------------------
//...
}
}
我已将URL ofhttps://www.quandl.com/api/v3/datasets/FRED/PAYEMS.csv
添加到MT4 中的allowed URLs 列表中。
如果我启用 AutoTrading 并将名为 (tutorial7) 的 script 拖到 USDCAD,M1 的 chart 上,
我会在 Experts tab 中收到这些消息:
Script tutorial7 USDCAD,M1: loaded successfulytutorial7 USDCAD,M1: initializedtutorial7 USDCAD,M1: The data has been successfully loaded, size = 0 bytestutorial7 USDCAD,M1: uninit reason 0
如果“The data has successfully loaded”肯定不应该说“size = 0 bytes”吗?
如果我只是将它直接复制并粘贴到MetaQuotes Language Editor 和compile 中,这个script 是否可以正常工作?
除了将URL 添加到MT4 中的allowed URLs 并将此代码复制并粘贴到script 之外,还有什么我必须做的吗?
如果有,怎么做?
我将上面的代码作为Script 运行不是 Expert Advisor;这样好吗?
第二季度。我可以在脚本中将获取的值 144598.0 设置为变量吗?
我需要将144598.0 的value 设为变量,以便可以将其与另一个value 进行比较。
这样的工作是否可行:
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 > y ) {
// execute code
}
else {
// execute other code
}
}
【问题讨论】:
标签: algorithmic-trading mql4 metatrader4 quandl