如何使用 JAVA(或任何东西!)向/从 MetaTrader 终端 4 发送/接收数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39592718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 04:30:24  来源:igfitidea点击:

How to send/receive data to/from MetaTrader Ternminal 4 with JAVA (or anything!)

javaapialgorithmic-tradingmql4metatrader4

提问by Jay Povey

I have been working on an algorithm ( Not mine, I am just modifying it ) that predicts when to buy and sell on the FOREX market. I need to be able to open and close orders, dynamically update parameters of the orders ( such as stoploss, maximum stop etc. ) and receive real time tick data.

我一直在研究一种算法(不是我的,我只是在修改它),它可以预测何时在外汇市场上买卖。我需要能够打开和关闭订单,动态更新订单的参数(例如止损、最大止损等)并接收实时报价数据。

I have been researching for well over a week, and have no success.

我已经研究了一个多星期,但没有成功。

The closest I have gotten is using JavoNetand Mt4 Api

我得到的最接近的是使用JavoNetMt4 Api

I managed to import the DLL into java and use a MQL4 function, which was AccountBalance(), however this has returned 0.0, which was not the account balance, I messed around with the code and the settings on MT4 client but still no luck.

我设法将 DLL 导入 java 并使用了 MQL4 函数,AccountBalance()但是这已经返回0.0,这不是帐户余额,我弄乱了 MT4 客户端上的代码和设置但仍然没有运气

Q0:Can anyone please point me in the right direction?

Q0:任何人都可以指出我正确的方向吗?

I am new to automated FOREX trading but from what I understand there is a broker somewhere with a MT4 server and I connect to that server with my MT4 client on my windows machine.

我是自动外汇交易的新手,但据我所知,某处有一个带有 MT4 服务器的经纪人,我使用 Windows 机器上的 MT4 客户端连接到该服务器。

Q1:If this is the case, do I need to make an API work with the server side instead of my client side?

Q1:如果是这种情况,我是否需要让 API 与服务器端而不是客户端一起工作?

All these DLL's I have tried so far have been used with the MT4 client software on my machine.

到目前为止,我尝试过的所有这些 DLL 都与我机器上的 MT4 客户端软件一起使用。

I have also been doing some reading on the FIX-Protocol and ZeroMQ.

我也一直在阅读 FIX-Protocol 和 ZeroMQ。

Q2:Can these help me achieve my goal in any way (instead of creating some bridges between JAVA and MT4 DLL's)?

Q2:这些能以任何方式帮助我实现我的目标吗(而不是在 JAVA 和 MT4 DLL 之间建立一些桥梁)?

采纳答案by user3666197

A0:yes, forget straight about REST and synchronous, blocking chains in FX-trading domain

A0:是的,直接忘记外汇交易领域中的 REST 和同步阻塞链

A1:well, not a typical way. MetaTrader Server is a proprietary suite of systems on the Broker-side and theirs API are not disclosed to allow some 3rd party integrations against.

A1:嗯,不是典型的方式。MetaTrader Server 是经纪商端的一套专有系统,他们的 API 未公开以允许某些 3rd 方集成。

enter image description here

在此处输入图片说明

A2:FIX-Protocolis the industry standard LP-interfacing lingua franca. In case you have contracted relations with your institutional trading provider, incl. the FIX-Protocol GWY-port, this may provide you an A-level access to the Market and to integrate your trading tools against. If this is the case, forget about MT4 instrumentation, as prime-time cadences are far beyond the MT4 Terminal localhost processing architecture ( multiple events with a sub-millisecondTimeDOMAIN resolution are common, whereas MQL4 does not provide any direct support for multithreaded-concurrent / better parallel programme scheduling designs ). FIX-Protocol events are simply off-the picture above, being far left, "before" the graph starts from 1st [ms] column.

A2:FIX-Protocol是行业标准的 LP 接口通用语言。如果您与您的机构交易提供商签订了合同,包括 FIX-Protocol GWY 端口,这可以为您提供 A 级市场访问权限并集成您的交易工具。如果是这种情况,请忘记 MT4 工具,因为黄金时段的节奏远远超出 MT4 终端本地主机处理架构(具有亚毫秒级TimeDOMAIN 分辨率的多个事件很常见,而 MQL4 不提供对多线程并发的任何直接支持/ 更好的并行程序调度设计)。FIX-Protocol 事件与上图不同,位于最左侧,“之前”图表从第 1 [ms] 列开始。

ZeroMQmay help liberateyour further designs from MQL4 limitations. May like to read my other posts on distributed systems, where MQL4 / ZeroMQ / ML-AI-predictors / GPU-processing infrastructures appear.

ZeroMQ可能有助于您的进一步设计从 MQL4 限制中解放出来。可能喜欢阅读我关于分布式系统的其他文章,其中出现了 MQL4 / ZeroMQ / ML-AI 预测器 / GPU 处理基础设施。

Anyway:

反正:

Enjoy the Wild Worlds of MQL4/MQL5

享受狂野的世界 MQL4/MQL5


Interested?May also like reading other MQL4, ZeroMQ distributed processingand low-latencytrading posts


感兴趣的?可能还喜欢阅读其他MQL4ZeroMQ 分布式处理低延迟交易帖子

回答by Pleplius

I started to code an expert with MQL5, naturally on MT5 platform, and I must admit that the difficulty of managing the application along with the increase of its complexity is high. It's not only due to a missing garbage collector, that of course imposes the deletion of the new instances, but also because Java offers a set of powerful data structures and syntax that MQL5 naturally doesn't have. Last but not least, talking about the community and the third party libraries available, there's a light year of the distance between Java and MQL5. I.e. if I need to find a library for a JSON conversion on the Java side I find dozens of official and stable versions, in the MQL5 community I have found only rubbish that I had to modify myself.

我开始用 MQL5 编写专家代码,自然是在 MT5 平台上,我必须承认,随着其复杂性的增加,管理应用程序的难度很高。这不仅是因为缺少垃圾收集器,这当然会强制删除新实例,还因为 Java 提供了一组 MQL5 自然不具备的强大数据结构和语法。最后但并非最不重要的一点是,谈到社区和可用的第三方库,Java 和 MQL5 之间还有一光年的距离。即,如果我需要在 Java 端找到一个用于 JSON 转换的库,我会找到数十个官方和稳定版本,在 MQL5 社区中,我只找到了我必须自己修改的垃圾。

So, after numerous failed tries on coding my expert in MQL5 (not a simple one of course), I decided to adopt a radical approach: coding an application, client-side MQL5, and server-side Java, that provides a Java facade for the MT5 platform. Same API, same basic events and so on. Even though I thought more than once that I was getting stuck in a blind path, I kept coding and eventually, I made it, obtaining a really solid result. Naturally, the REST interface drastically reduces the performances, and each request, even with Tomcat and MT5 running in the same localhost, is in the order of milliseconds, not micros, but on the other side this reduces only the suitability of this architecture, it doesn't make it useless at all.

因此,在多次尝试用 MQL5 编写我的专家代码失败后(当然不是简单的一个),我决定采用一种激进的方法:编写一个应用程序、客户端 MQL5 和服务器端 Java,它提供了一个 Java 外观MT5平台。相同的 API,相同的基本事件等等。尽管我不止一次认为自己陷入了盲道,但我一直在编码,最终我成功了,获得了非常可靠的结果。自然,REST 接口大大降低了性能,每个请求,即使 Tomcat 和 MT5 运行在同一个 localhost 中,也是以毫秒为单位,而不是微秒,但另一方面,这只会降低这种架构的适用性,它并没有让它变得毫无用处。

Strategies like scalpelling and every kind of high-frequency trading are not good for such kind of scenario, vice-versa every other strategy in the longer period, even if intraday's ones, can be implemented successfully without any cons. Last but not least, it isn't necessary to use the WebRequest() MQL5 method to call any Servlet container, it is possible to import the wininet.dll from the OS (talking about Windows) and the strategy tester will work as if the strategy has been coded in MQL5, maybe just a little bit slower. To sum up, I wouldn't be so sarcastic on the Java facade approach for the FX trading platforms, citing only the nude performances without contextualizing the overall scenario is a naive approach to face the argument.

像手术刀和各种高频交易的策略不适合这种情况,反之亦然,在较长时期内的所有其他策略,即使是盘中策略,也可以成功实施而没有任何缺点。最后但并非最不重要的一点是,没有必要使用 WebRequest() MQL5 方法来调用任何 Servlet 容器,可以从操作系统(谈到 Windows)导入 wininet.dll,策略测试器将像策略已在 MQL5 中编码,可能只是慢了一点。总而言之,我不会对 FX 交易平台的 Java 外观方法如此讽刺,仅引用裸体表演而不将整体场景置于上下文中是一种面对争论的幼稚方法。

回答by Xilin Zang

In order to collect the tick information and open, update or close orders, you can use mt4 server api.

为了收集报价信息和开仓、更新或平仓订单,您可以使用mt4 server api。

please check this url.

请检查这个网址。

http://mtapi.online/#overlappable-4

http://mtapi.online/#overlappable-4

Maybe you will find what you want.

也许你会找到你想要的。

And then I have also mt4 server api. If you have any questions please update me.

然后我还有 mt4 服务器 api。如果您有任何问题,请更新我。

回答by Eric

If you need to send/receive synchronous message between MT4 and Java application, REST would be the best approach because fast response matters in this scenario. Message Queue solutions like ZeroMQ fits better in asynchronous solutions, so it won't help you. Once you choose REST approach, you can use MQL4 WebRequest()to call your Java application.

如果您需要在 MT4 和 Java 应用程序之间发送/接收同步消息,REST 将是最好的方法,因为在这种情况下快速响应很重要。像 ZeroMQ 这样的消息队列解决方案更适合异步解决方案,所以它不会帮助你。一旦您选择了 REST 方法,您就可以使用 MQL4WebRequest()来调用您的 Java 应用程序。