简体   繁体   中英

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.

在此处输入图片说明

 //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());
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM