Discussion of article "MetaTrader 5 and Python integration: receiving and sending data"

 

New article MetaTrader 5 and Python integration: receiving and sending data has been published:

Comprehensive data processing requires extensive tools and is often beyond the sandbox of one single application. Specialized programming languages are used for processing and analyzing data, statistics and machine learning. One of the leading programming languages for data processing is Python. The article provides a description of how to connect MetaTrader 5 and Python using sockets, as well as how to receive quotes via the terminal API.

We will write a simple program which will create a socket server and receive the necessary information from the client (the MQL5 program), handle it and send back the result. This seems to be the most efficient interaction method. Suppose we need to use a machine learning library, such as for example scikit learn, which will calculate linear regression using prices and return coordinates, based on which a line can be drawn in the MetaTrader 5 terminal. This is the basic example. However, such interaction can also be used for training a neural network, for sending to it data from the terminal (quotes), learning and returning the result to the terminal.

Now we can proceed to creating a class responsible for socket manipulation:

class socketserver:
    def __init__(self, address = '', port = 9090):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.address = address
        self.port = port
        self.sock.bind((self.address, self.port))
        self.cummdata = ''
        
    def recvmsg(self):
        self.sock.listen(1)
        self.conn, self.addr = self.sock.accept()
        print('connected to', self.addr)
        self.cummdata = ''

        while True:
            data = self.conn.recv(10000)
            self.cummdata+=data.decode("utf-8")
            if not data:
                break    
            self.conn.send(bytes(calcregr(self.cummdata), "utf-8"))
            return self.cummdata
            
    def __del__(self):
        self.sock.close()

We can see a good correlation between GBPUSD and GBPJPY in the above heat map.

Author: Maxim Dmitrievsky

 
Thank you. Too bad, no casting.
[Deleted]  
fxsaber:
Thank you. Too bad, without casting.

As I will do - I'll upload an example here for sure, it's just that I didn't have such a task before.

Also, if sockets will get their development in the tester, then it will be very relevant.

 
Useful. Just started python)
 

For history, there is a large standard maths library in MQL5 in the sources right in the terminal:

 

I always read @Maxim Dmitrievsky 's articles with pleasure, I like his approach to writing articles - the implementation of the idea itself is described concisely, I don't like lately "kilometre-long spools" of code and chewing up every line to "and now we'll name the variable XY, where we'll store ...".

Thanks!

 
Igor Makanu:

With pleasure I always read articles by @Maxim Dmitrievsky, I like his approach to writing articles - the implementation of the idea is described concisely, I don't like lately "kilometre long stretches" of code and chewing up each line to "and now we will name the variable XY, where we will store ...".

Thank you!

So there are different readers, both sophisticated and beginners.
 
Alexander Fedosov:
So there are different kinds of readers, both experienced and novice.

Well, as if you are right, and not very - human attention is a very limited resource, and even an experienced reader to get into the essence of the article has to read "diagonally" otherwise by the end of the article simply will not digest the material.

If I'm not mistaken, a person can digest new material only for the first 15 minutes, then attention decreases and the effect of new knowledge will be null - as they say "less is better".

It is possible, of course, to stretch reading a large article, like reading a magazine or a book for several days, but this is not my option

 

socketclientEA (Si-6.19,M15) Connection localhost:9090 error 4014

https://www.mql5.com/en/docs/network/socketconnect

When called from the indicator,GetLastError() will return error 4014 - "System function not allowed to be called".

Solution:

The address for connection should be added to the list of allowed addresses on the client terminal side (section Tools \ Settings \ Expert Advisors).

Allow Webrequest for the following url:

http://localhost

 
MetaQuotes Software Corp.:

New article MetaTrader 5 and Python integration: receiving and sending data has been published:

Author: Maxim Dmitrievsky

Thank you finally.. :)

Please post more.. Lots More.. I love it..
 
MT5 finally interfaces with python!