'How can someone send data from MetaTrader 4/5 Terminal to external server?

this is somehow intended to (maybe) be(come) a guideline post in terms of MetaTrader 4/5 and its corresponding language MQL4 both set into context with sending data to external servers.

In my special case I am building a Django/Python based web application that will process FOREX trading data for further use.

Thus, I am in search for a proper solution to send data from the MetaTrader 4/5 Terminal to an external server periodically (e.g. every 60 seconds) formatted as json or csv (if possible). In particular, the data to be sent is the accounts trade history and the running + pending trades set.

After doing the research I basically found the following approaches:

1.) Using webrequest() in MQL4 wrapped in an expert advisor

As the official MQL4 docu suggests, the webrequest() function sends an http:// request to a specified server.
This is a related SO thread:
How to post from MetaTrader Terminal 5 MQL 5 a request to my nodejs server, which is running locally on my MT5 host?

and the official documentation:

https://docs.mql4.com/common/webrequest

This could be wrapped into an expert advisor to execute the request periodically on defined events given.

Which kind of data from the MT4/5 terminal can be populated into the data array?
How can that data be formatted? Is it possible to format it as json straight away or should that be done on the server-side?

2.) Using ZeroMQ

This is a setup I found in this thread: How to send a message in MQL4/5 from MetaTrader Terminal to python using ZeroMQ?

How would this be accomplished within the MetaTrader environment? Will this still be an expert advisor or some kind of DLL solution? What is the ZeroMQ's role within the setup?

What are the pros and cons compared to the webrequest() function?

3.) Others?

Are there any other possible approaches on achieving this like e.g. with APIs or MQL4 scripts?

Since this is a rare topic, I am loooking forward to any however small idea and input.



Solution 1:[1]

Welcome to the club - my above cited answer has received within 1,5 years zero-votes so far

Nevertheless, having used ZeroMQ since v2.11+, thanks to the R&D work published by Austen CONRAD - thanks and deep respect for his persistence.

Q : "How would this be accomplished within the MetaTrader environment?"

One simply #import-s the DLL and starts to use ZeroMQ-API-wrapper calls. A few details went a bit more complex, after MetaTrader has silently changed internal representation of string to cease to be a string ( becoming a struct in "New"-MQL4.56789, but you will learn how to live with this "Always On Watch"-style, so as to survive in production )

Q : "Will this still be an expert advisor or some kind of DLL solution?"

ZeroMQ can be used in either and/or all of:

  • Expert Advisor-type of MQL4-code
  • Custom Indicator-type of MQL4-code
  • Script-type of MQL4-code
    and can even provide a proxy-signaling/messaging-layer, so as to communicate among these otherwise separate and not-cooperating processes inside the MT4-Terminal Ecosystem.

Example:
I have MT4-Terminal processes cooperate with external AI/ML-based market analyser, which auto-detects windows-of-opportunity plus having an external CLI-console, as a remote-keyboard for MT4-Terminal hosted Control Panel, showing system health-state and listening to a remote-keyboard, used for a remote CLI-command control ( for both configuration and maintenance tasks of the whole multi-party distributed trading system )

Q : "What is the ZeroMQ's role within the setup?"

ZeroMQ provides an independent, smart and industry-standard, smart and low-latency signaling/messaging layer among any kind of needed nodes ( grid-computing, GPU-computing, CLI-treminal, AI/ML-decision making, system-wide consolidated central logging, anything one may need )

Try to setup and use a remote tipc://-transport-class for cross-cluster computing paradigm with any other approach.

Try to setup and use an M:N-redundant Strategy Trading, operated accross a mix of tcp:// + tipc:// + norm:// + vmci:// transport-classes, used so as to interconnect ( A x M + N x B )-nodes' exo-systems.

Try to setup a system, that asks MetaTrader to do some work from outside, without this technology ( webrequest() is not ready for any "questions from outside", is it? )

Q : "What are the pros and cons compared to the webrequest() function?"

Feel free to read about this in Stack Overflow answers.

Solution 2:[2]

Integration with Python, support for Market and Signals services in Wine (Linux/MacOS) and highly optimized strategy tester in MetaTrader 5 build 2085

MetaQuotes Software Corp, 14 June 2019


In the new MetaTrader 5 version, we have added an API which enables request of MetaTrader 5 terminal data through applications, using the Python high-level programming language. The API contains multiple libraries for machine learning, process automation, as well as data analysis and visualization.


MetaTrader 5 integration with Python

MetaTrader package for Python is designed for efficient and fast obtaining of exchange data via interprocessor communication, directly from MetaTrader 5. The data received via this pathway can be used for statistical calculations and machine learning.

Solution 3:[3]

Thus, I am in search for a proper solution to send data from the MetaTrader 4/5 Terminal to an external server periodically (e.g. every 60 seconds) formatted as json or csv (if possible).

For this, metatrader.live may help. The question is just to attach an (open-sourced) expert advisor and have the data online through JSON or WebSockets or something. OR You can use it as a transport layer only for your own logic. Easy enough. And yes, I'm the author :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 halfer
Solution 2 Jansen Simanullang
Solution 3 Eugene FILIN