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

28,372

Solution 1

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

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.

enter image description here

A2: FIX-Protocol is 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-millisecond TimeDOMAIN 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.

ZeroMQ may help liberate your 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.

Anyway:

Enjoy the Wild Worlds of MQL4/MQL5


Interested? May also like reading other MQL4, ZeroMQ distributed processing and low-latency trading posts

Solution 2

You can try MetaApi https://metaapi.cloud cloud service which provides REST API and WebSocket API access to both MetaTrader 4 and MetaTrader 5 accounts.

Official REST API documentation: https://metaapi.cloud/docs/client

SDKs: https://metaapi.cloud/sdks (javascript, python and Java SDKs are provided as per April 2021)

It supports reading account information, positions, orders, trade history, receiving quotes, and accessing market data.

The service also provides copy trading API https://metaapi.cloud/docs/copyfactory and API to calculate forex trading metrics on a MetaTrader account https://metaapi.cloud/docs/metastats.

Solution 3

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.

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.

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.

Share:
28,372
Jay Povey
Author by

Jay Povey

Updated on July 09, 2022

Comments

  • Jay Povey
    Jay Povey almost 2 years

    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 JavoNet and Mt4 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.

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

    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.

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

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

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

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

  • user3666197
    user3666197 over 7 years
    With all due respect, there is no space for synchronous, blocking chains in FX domain professional systems. If one posts based on REST + WebRequest(), it could be for nothing more than for a sand-box-ed "educational" purposes, with a doubfull level of seriousness to the real-world clients. Sorry for speaking in this straight and opened manner.
  • Przemysław Ładyński
    Przemysław Ładyński over 7 years
    Agree with A0. We are not experts in FX tools or protocols however REST or any other HTTP based protocol will never achieve performance like native bridge. If you will need to use .NET DLLs from Java Javonet seems to be preffered choice. I represent Javonet and we have some HFT customers who highlight that it lets them make >250 000 calls /s plus receive callbacks in parallel what is impossible with http service. In comparison on regular machine i7 8gb mem. xml webservice takes 840sec!! to process 250k calls. I hope this comment could be usefull in your architecture planning decisions. Cheers.
  • user3666197
    user3666197 over 7 years
    Thank you, Yvette, a generous move.
  • Cristian Florescu
    Cristian Florescu almost 7 years
    WebRequest() cannot be executed in the Strategy Tester. - docs.mql4.com/common/webrequest