问题描述
如何从 MQL5
中的URL读取 JSON
?
How can I read JSON
from a url in MQL5
?
例如,以下简单的JSON
来自: https://api.myjson.com/bins/56z28
For example this simple JSON
from: https://api.myjson.com/bins/56z28
{ "employees": [ { "firstName": "John",
"lastName": "Doe"
},
{ "firstName": "Anna",
"lastName": "Smith"
},
{ "firstName": "Peter",
"lastName": "Jones"
}
]
}
推荐答案
简单,但有限制.
MetaTrader终端5是一个代码执行环境,可以分别通过两个HTTP/HTTPS
协议通过端口80/443
与外部URL目标(如果明确配置为允许的URL)进行通信.
MetaTrader Terminal 5 is a code-execution environment, that can communicate with an external URL target (if explicitly configured as a permitted URL) via both HTTP/HTTPS
protocols over port 80/443
respectively.
string aCookieHOLDER = NULL,
aHttpHEADERs;
char postBYTEs[],
replBYTEs[];
int aRetCODE;
string aTargetURL = "https://api.myjson.com/bins/56z28";
/* to enable access to the URL-> pointed server,
you should append "https://api.myjson.com/bins/56z28"
to the list of allowed URLs in
( Main Menu -> Tools -> Options, tab "Expert Advisors" ):
*/
ResetLastError(); // Reset the last error code
int aTIMEOUT = 5000; // less than 1 sec. is NOT
// enough for slow Internet connection
aRetCODE = WebRequest( "GET",
aTargetURL,
aCookieHOLDER,
NULL,
aTIMEOUT,
postBYTEs,
0,
replBYTEs,
aHttpHEADERs
);
if ( aRetCODE == EMPTY ) // Check errors
{ Print( "Error in WebRequest(). Error code = ", GetLastError() );
}
else
{ // Load was successfull, PROCESS THE STRING ... assumed to be a JSON
}
如代码中所述,要使用WebRequest()
功能,必须在选项"窗口的专家顾问"选项卡的允许的URL
列表中添加所有必需的URL
(服务器)先验地址. .根据指定的协议自动选择服务器端口-对于http://
是80
,对于https://
是443
(不是免费选项...).
As noted in code,to use the WebRequest()
function, one has to add the addresses of all the required URL
s (servers) a-priori in the list of allowed URL
s in the "Expert Advisors" tab of the "Options" window. Server port is automatically selected on the basis of the specified protocol - 80
for "http://
" and 443
for "https://
" (not a free option...).
WebRequest()
函数是同步的,这意味着它中断/阻止(!)程序执行,并等待来自所请求URL的响应.由于接收响应的延迟可能很大,因此该功能不适用于指标调用,因为指标运行在一个符号上所有指标和图表共享的公共线程中.一个符号图表之一上的指标性能延迟可能会停止更新同一符号的所有图表(!!!!).
The WebRequest()
function is synchronous, which means its breaks/blocks(!) the program execution and waits for the response from the requested URL. Since the delays in receiving a response can be large, the function is not available for calls from the indicators, because indicators run in a common thread shared by all indicators and charts on one symbol. Indicator performance delay on one of the charts of a symbol may stop updating of all charts of the same symbol(!!!!).
该函数只能在它们自己的执行线程中运行,因此只能从Expert Advisors
和scripts
调用.如果尝试从Custom Indicator
调用函数,则GetLastError()
将返回错误4060
– "Function is not allowed for call".
The function can be called only from Expert Advisors
and scripts
, as they run in their own execution threads. If you try to call the function from a Custom Indicator
, GetLastError()
will return error 4060
– "Function is not allowed for call".
WebRequest()
不能在策略测试器中执行.
WebRequest()
cannot be executed in the Strategy Tester.
坏消息?
如果这一切听起来对您的项目来说都是个坏消息,请不要放弃. MQL
代码可以调用DLL函数,因此可以集成一个公平,分布式,无阻塞的通信器,该通信器可以与MQL
代码流畅地协作,并且在生产系统中不包含上面列出的任何限制.
If all this sounds as a bad news to your Project, do not give up. MQL
code can call DLL-functions, so one can integrate a fair, distributed, non-blocking communicator, that cooperates with MQL
code smoothly and does not include any of the above listed limitations in a production system.
这篇关于如何从MQL5中的URL读取JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!