socket-library-mt4-mt5.mqh – an unknown

 
Hello, I have a problem with this library, socket-library-mt4-mt5.mqh. Basically, I built an EA
//+------------------------------------------------------------------+
//|                                              TERMINAL_IN_OUT.mq5 |
//|                                  Copyright 2025, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#define SOCKET_LIBRARY_USE_EVENTS
#include "socket-library-mt4-mt5.mqh"

// --------------------------------------------------------------------
// EA user inputs
// --------------------------------------------------------------------
input string   Hostname = "127.0.0.1";    // Server hostname or IP address
input ushort   ServerPort = 5002;         // Server port

// --------------------------------------------------------------------
// Global variables
// --------------------------------------------------------------------
ClientSocket *glbClientSocket = NULL;

// --------------------------------------------------------------------
// Initialization
// --------------------------------------------------------------------
int OnInit()
{
   // Crea e connette il client socket
   glbClientSocket = new ClientSocket(Hostname, ServerPort);

   if (glbClientSocket.IsSocketConnected())
      Print("Client connection succeeded");
   else
      Print("Client connection failed");

   return(INIT_SUCCEEDED);
}

// --------------------------------------------------------------------
// Deinitialization
// --------------------------------------------------------------------
void OnDeinit(const int reason)
{
   if (glbClientSocket)
   {
      delete glbClientSocket;
      glbClientSocket = NULL;
   }
}

// --------------------------------------------------------------------
// Event-driven communication (no OnTick / no OnTimer)
// --------------------------------------------------------------------
void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam)
{
    // Controlla se l'evento proviene dal socket
    if (glbClientSocket && lparam == glbClientSocket.GetSocketHandle())
    {
        if (glbClientSocket.IsSocketConnected())
        {
            // Invia posizioni aperte appena richieste dal server
            int totalPositions = PositionsTotal();
            string msg = "";
            for (int i = 0; i < totalPositions; i++)
            {
                ulong ticket = PositionGetTicket(i);
                if (PositionSelectByTicket(ticket))
                {
                    string symbol = PositionGetString(POSITION_SYMBOL);
                    double profit = PositionGetDouble(POSITION_PROFIT);
                    msg += symbol + "," + IntegerToString(ticket) + "," + DoubleToString(profit,2) + "\n";
                }
            }

            glbClientSocket.Send(msg);
        }
        else
        {
            // Se il socket è disconnesso, lo ricreiamo
            delete glbClientSocket;
            glbClientSocket = new ClientSocket(Hostname, ServerPort);
        }
    }
    else
    {
        // Evento reale da tastiera (non dal socket)
        // Puoi ignorarlo o gestirlo separatamente
    }
}
that is supposed to send me the open and closed trades in real-time. Not wanting to rely on OnTick or OnTimer for performance reasons, I chose to use this library, socket-library-mt4-mt5.mqh, (https://www.mql5.com/en/blogs/post/706665) which should, let's say, bypass the limitations of OnTick and OnTimer.

If I open a server with Netcat and run my EA, I can see the 3 trades I already have open, but if I open additional trades afterward, it doesn’t send anything.

If anyone has experience with this library and could help me, I would be infinitely grateful.
Socket library for MT4 and MT5
Socket library for MT4 and MT5
  • 2017.09.06
  • www.mql5.com
[Published as a blog entry because submission to the Codebase stalled on the fact that this socket library works with both MT4 and MT5, whereas the Codebase is divided into separate sections for MT4
 
Stefano Cerbioni:
Hello, I have a problem with this library, socket-library-mt4-mt5.mqh. Basically, I built an EA that is supposed to send me the open and closed trades in real-time. Not wanting to rely on OnTick or OnTimer for performance reasons, I chose to use this library, socket-library-mt4-mt5.mqh, (https://www.mql5.com/en/blogs/post/706665) which should, let's say, bypass the limitations of OnTick and OnTimer.

If I open a server with Netcat and run my EA, I can see the 3 trades I already have open, but if I open additional trades afterward, it doesn’t send anything.

If anyone has experience with this library and could help me, I would be infinitely grateful.

The code you posted sends the positions on demand. When the server sends a message the EA replies with the open positions. 

You would need to put the sending code into OnTrade or OnTradeTransaction. 

 
Laszlo Tormasi #:

The code you posted sends the positions on demand. When the server sends a message the EA replies with the open positions. 

You would need to put the sending code into OnTrade or OnTradeTransaction. 

thanks 4 rply