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

 
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..
 

when i trying to run the example of socketclientEA.mq5.The MT5 terminal come out with 'Connection localhost:9090 error 4014' ,

official statement document said that:

'The function can be called only from Expert Advisors and scripts, as they run in their own execution threads. If calling from an indicator, GetLastError() returns the error 4014 – "Function is not allowed for call".'

I do calling the example as a Expert Advisors,but it just come out with the same error 4014 as i mentioned above.

My terminal version is 5.00 build 2009 15 Mar 2019.

Plz help.

 
Bohao Zhang:

when i trying to run the example of socketclientEA.mq5.The MT5 terminal come out with 'Connection localhost:9090 error 4014' ,

official statement document said that:

'The function can be called only from Expert Advisors and scripts, as they run in their own execution threads. If calling from an indicator, GetLastError() returns the error 4014 – "Function is not allowed for call".'

I do calling the example as a Expert Advisors,but it just come out with the same error 4014 as i mentioned above.

My terminal version is 5.00 build 2009 15 Mar 2019.

Plz help.

Hi, pls add and allow 'localhost' in tools->options->EA's


 
MetaQuotes Software Corp.:

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

Author: Maxim Dmitrievsky


Hello, please help, how do I stream live bar data into a Pandas data frame. I'd like to use this for machine learning to predict binary options.

 
Sakhile Mamba:

Its is very clear how to get historical data the integration part of documentation. what about live streaming the data?

 
Sakhile Mamba:

Its is very clear how to get historical data the integration part of documentation. what about live streaming the data?

Hi, yo can do this same way. Just don't call shutdown() func and then receive data every new bar or tick, by timer etc. 

 
Maxim Dmitrievsky:

Hi, yo can do this same way. Just don't call shutdown() func and then receive data every new bar or tick, by timer etc. 

True, but then i have to rerun the script. Isn't the a shortcut of adding new data to an existing dataframe and labelling green candles 1 and red candles 0 in a newly added label column for this existing dataframe

 
Maxim Dmitrievsky:

Hi, pls add and allow 'localhost' in tools->options->EA's


Hi Maxim,

I'm getting error 4014 already in SocketCreate phase. What can be wrong?

 
Andres Kull:

Hi Maxim,

I'm getting error 4014 already in SocketCreate phase. What can be wrong?

Hi, try this https://www.mql5.com/en/forum/308679#comment_11299587

Discussion of article "MetaTrader 5 and Python integration: receiving and sending data"
Discussion of article "MetaTrader 5 and Python integration: receiving and sending data"
  • 2019.03.28
  • www.mql5.com
New article MetaTrader 5 and Python integration: receiving and sending data has been published: Author: Maxim Dmitrievsky...
 
Maxim Dmitrievsky:

Hi, try this https://www.mql5.com/en/forum/308679#comment_11299587

I'm running EA, localhost is enabled. 


Reason: