JustPaste.it

To send a variable value from one MT4 platform to another remote MT4 platform

Sure, I can help you with that. To send a variable value from one MT4 platform to another remote MT4 platform, we can use the TCP/IP protocol to establish a network connection between the two platforms. One platform will act as a client, and the other will act as a server.

Here's an example of how you can achieve this using MQL4 programming language:

 

  1. Server-Side (Remote MT4 Platform):
// Server Parameters
input int ServerPort = 8888; // Choose a free port for communication
 
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
    // Start the server
    int server = NetworkCreateServer();
    if (server < 0)
    {
        Print("Error creating the server: ", ErrorDescription(server));
        return INIT_FAILED;
    }
 
    Print("Server is listening on port ", ServerPort);
    return INIT_SUCCEEDED;
}
 
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    // Wait for a client to connect
    int client = NetworkWaitForClient();
    if (client < 0)
    {
        Print("Error waiting for a client: ", ErrorDescription(client));
        return;
    }
 
    Print("Client connected.");
 
    // Receive the variable value from the client
    double receivedValue;
    int result = NetworkReceiveDouble(client, receivedValue);
    if (result != SOCKET_ERROR)
    {
        Print("Received value from client: ", receivedValue);
    }
 
    // Close the client connection
    NetworkClose(client);
}

 

2. Client-Side (Your MT4 Platform):

 

// Client Parameters
input string ServerIP = "127.0.0.1"; // Change this to the remote server IP
input int ServerPort = 8888; // Make sure it matches the server port
 
//+------------------------------------------------------------------+
//| Function to send a double value to the server                    |
//+------------------------------------------------------------------+
bool SendDoubleToServer(double value)
{
    int client = NetworkCreateConnection();
    if (client < 0)
    {
        Print("Error creating a connection: ", ErrorDescription(client));
        return false;
    }
 
    int result = NetworkConnect(client, ServerIP, ServerPort);
    if (result != SOCKET_ERROR)
    {
        result = NetworkSendDouble(client, value);
        if (result != SOCKET_ERROR)
        {
            Print("Value sent to server: ", value);
        }
    }
 
    NetworkClose(client);
    return result != SOCKET_ERROR;
}
 
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    double myVariableValue = 123.45; // Replace this with your variable value
    SendDoubleToServer(myVariableValue);
}

 

Please make sure you've allowed DLL imports and have added the Winsock32.mqh library to both platforms' Include folder. Additionally, ensure that the server platform is running and accessible from the client platform.

Remember to modify the ServerIP variable in the client code to match the IP address of your remote MT4 server. Also, ensure that any firewalls or network configurations do not block the communication on the specified port.

Note: When working with network communication, ensure that you fully understand the security implications and potential risks of allowing external access to your MT4 platform. Consider implementing appropriate authentication and encryption mechanisms if needed. Always test the code thoroughly in a safe environment before deploying it in a live setting.