Does socket work in tester?

 

I've been trying to test my socket connection using tester, as I will need it to communicate back and forth with Python. But it keep returning 4014. What I found in the documentation ( https://www.mql5.com/en/docs/network/socketconnect), it cannot be called from an indicator, I've made it as an expert advisor, it shown itself in Expert Advisor tab. Is it can't be used in socket? If it not, is there any other alternative to established communication between Python and MetaTrader other than using file? I've been using file, but it so slow as I need to give it delay for each iteration so they do not try to read a file at the same time as the other write them.


//+------------------------------------------------------------------+
//|                                              socketclienttry.mq5 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

string host = "127.0.0.1";
int port = 37474;
int hour;
datetime bardate;
MqlDateTime date;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   bardate = iTime(Symbol(), PERIOD_H1, 0);
   TimeToStruct(bardate, date);
   hour = date.hour;
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   bardate = iTime(Symbol(), PERIOD_H1, 0);
   TimeToStruct(bardate, date);
   
   if(hour != date.hour)
     {
      hour = date.hour;
      
      int socket = SocketCreate();
      if(socket != INVALID_HANDLE)
        {
         if(SocketConnect(socket, host, port, 1000))
           {
            Print("Established connection to ", host, ": ", port);
           } 
         else
           {
            Print(GetLastError());
           } SocketClose(socket);
        }
        else
          {
           Print(GetLastError());
          }
     }
  }
//+------------------------------------------------------------------+
Documentation on MQL5: Network Functions / SocketConnect
Documentation on MQL5: Network Functions / SocketConnect
  • www.mql5.com
//|                                                SocketExample.mq5 | //|                        Copyright 2018, MetaQuotes Software Corp. | //|                                             https://www.mql5.com | "Add Address to the list of allowed ones in the terminal settings to let the example work...
 
Erland Devona:

I've been trying to test my socket connection using tester, as I will need it to communicate back and forth with Python. But it keep returning 4014. What I found in the documentation ( https://www.mql5.com/en/docs/network/socketconnect), it cannot be called from an indicator, I've made it as an expert advisor, it shown itself in Expert Advisor tab. Is it can't be used in socket? If it not, is there any other alternative to established communication between Python and MetaTrader other than using file? I've been using file, but it so slow as I need to give it delay for each iteration so they do not try to read a file at the same time as the other write them.

I chanced upon ZeroMQ recently - you might want to check it out.

 
Erland Devona:

I've been trying to test my socket connection using tester, as I will need it to communicate back and forth with Python. But it keep returning 4014. What I found in the documentation ( https://www.mql5.com/en/docs/network/socketconnect), it cannot be called from an indicator, I've made it as an expert advisor, it shown itself in Expert Advisor tab. Is it can't be used in socket? If it not, is there any other alternative to established communication between Python and MetaTrader other than using file? I've been using file, but it so slow as I need to give it delay for each iteration so they do not try to read a file at the same time as the other write them.


You can't use SocketXXX function from the tester. See below article.

mql5 has now possibilities to integrate Python and MT5 : https://www.mql5.com/en/docs/integration/python_metatrader5

An other time please do some researches before posting.

MetaTrader 5 and Python integration: receiving and sending data
MetaTrader 5 and Python integration: receiving and sending data
  • www.mql5.com
A network socket is the endpoint of interprocess communication over a computer network. The MQL5 Standard Library includes a group of Socket functions, which provide a low-level interface for working on the Internet. This is a common interface for different programming languages, as it uses system calls at the operating system level. Data...
 
Alain Verleyen:

You can't use SocketXXX function from the tester. See below article.

mql5 has now possibilities to integrate Python and MT5 : https://www.mql5.com/en/docs/integration/python_metatrader5

An other time please do some researches before posting.

I've known the link before posting this, but it does not provide what I need. That's why I'm trying to use socket rather than the library. I need for Python to be able to reply with their own data.

But, I really appreciate your reply. Thank you for your help.

 
Erland Devona:

I've known the link before posting this, but it does not provide what I need. That's why I'm trying to use socket rather than the library. I need for Python to be able to reply with their own data.

But, I really appreciate your reply. Thank you for your help.

I provided you 2 links, the second one is exactly what you asked for.
 
It does not work for me too. I tried to execute the example, but seem that the SocketConnect in MT5 does not able to connect to python. I have tested by using other python socket. :(
 

Sockets are not supported by the MT4 Tester.  But there is a simple workaround for this as 

shown in the link: 

 http://www.fxdesignlab.com/wp-content/uploads/2020/08/EA-Comm-by-Sockets-with-the-MT4-Tester.pdf

Reason: