pythonsocketsmqlmql5

MetaTrader 5 and Python integration using socket


I am trying to established connection between MetaTrader 5 and Python using socket. Server part written in python and client part written in mql5. also client is Expert Advisor not a indicator. problem is when i run both server and client ,client gives me "error 4014". I'm very thankful if someone can help me to solve this problem.

enter image description here

 //this is python server code
 import socket

 serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 serv.bind(('127.0.0.1', 9090))
 serv.listen(1)

 while True:
     conn, addr = serv.accept()
     print('client connected to :', addr)
     conn.close()


//This is mql5 client code
//+------------------------------------------------------------------+
//|                                               test_client_EA.mq5 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

int socket;
int OnInit()
  {
   socket=SocketCreate();
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
   SocketClose(socket);  
  }

void OnTick()
{
  socket=SocketCreate();
  if(socket!=INVALID_HANDLE) 
  {
     if(SocketConnect(socket,"127.0.0.1",9090,1000)) 
       {
        Print("Connected to "," 127.0.0.1",":",9090);
       }  
     else
       {
        Print("Connection ","127.0.0.1",":",9090," error ",GetLastError());
       }
  SocketClose(socket); 
  }
 else Print("Socket creation error ",GetLastError());
}

Solution

  • Have you tried configure WebRequest setting in option?

    The path to the config is Tools -> Options -> Expert advisors -> WbRequest (checkbox). You want to add localhost, or 127.0.0.1 to allow connection.

    I hope this is still pending and it helps.

    Best