3

I have a windows server 2016 and I am trying to send data from Meta Trader 5 using EA to the specific port (777) in localhost and I am using NodeJS, Socket.io on the other hand to capture that data.

I would like to pass data from MT5 to a localhost:some_port. I have never worked with sockets (client-server) for sending and receiving of data between two applications or codebase.

Here, Web(NodeJS application acting as a Server side) and MT5(as Client Side).

NodeJS application - Creates Server and looks for client connection

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var express = require('express');

io.on('connection', function(socket) {  
  console.log("new user connected");

  //sends test data on creating a connection
  socket.emit('live-data', 'test data');
});

app.use('/js', express.static(__dirname + '/node_modules/bootstrap/dist/js')); // redirect bootstrap JS
app.use('/js', express.static(__dirname + '/node_modules/jquery/dist')); // redirect JS jQuery
app.use('/css', express.static(__dirname + '/node_modules/bootstrap/dist/css')); // redirect CSS bootstrap

app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});

http.listen(777, function(){
  console.log('listening on *:777');
});

Client Side

/* ###################################################################

Example socket client.
Code can be used as both MQ4 and MQ5 (on both 32-bit and 64-bit MT5)

Simply sends each new tick to the server, as a CRLF-terminated 
message. The example server then writes these to its Experts log.

################################################################### */


#property strict

// --------------------------------------------------------------------
// Include socket library
// --------------------------------------------------------------------

#include <socket-library-mt4-mt5.mqh>


// --------------------------------------------------------------------
// EA user inputs
// --------------------------------------------------------------------

input string   Hostname = "localhost";    // Server hostname or IP address
input ushort   ServerPort = 777;        // Server port


// --------------------------------------------------------------------
// Global variables and constants
// --------------------------------------------------------------------

ClientSocket * glbClientSocket = NULL;

// --------------------------------------------------------------------
// Initialisation (no action required)
// --------------------------------------------------------------------

void OnInit() {}


// --------------------------------------------------------------------
// Termination - free the client socket, if created
// --------------------------------------------------------------------

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


// --------------------------------------------------------------------
// Tick handling - set up a connection, if none already active,
// and send the current price quote
// --------------------------------------------------------------------

void OnTick()
{
   if (!glbClientSocket) {
      glbClientSocket = new ClientSocket(Hostname, ServerPort);
      if (glbClientSocket.IsSocketConnected()) {
         Print("Client connection succeeded");
      } else {
         Print("Client connection failed");
      }
  }

   if (glbClientSocket.IsSocketConnected()) {
      // Send the current price as a CRLF-terminated message
      string strMsg = Symbol() + "," + DoubleToString(SymbolInfoDouble(Symbol(), SYMBOL_BID), 6) + "," + DoubleToString(SymbolInfoDouble(Symbol(), SYMBOL_ASK), 6) + "\r\n";
      glbClientSocket.Send(strMsg);

   } else {
      // Either the connection above failed, or the socket has been closed since an earlier
      // connection. We handle this in the next block of code...
   }

   // If the socket is closed, destroy it, and attempt a new connection
   // on the next call to OnTick()
   if (!glbClientSocket.IsSocketConnected()) {
      // Destroy the server socket. A new connection
      // will be attempted on the next tick
      Print("Client disconnected. Will retry.");
      delete glbClientSocket;
      glbClientSocket = NULL;
   }
}

Update

enter image description here

I keep getting above message. and I dont see any data sent to the Nodejs Socket.

Murlidhar Fichadia
  • 2,589
  • 6
  • 43
  • 93
  • You might want to edit your question a bit, since you're not actually asking anything. Aside from that, why socket.io ? WebSocket really are a standard now, socket.io isn't really easier to use and adds overhead. – Ealhad Apr 30 '19 at 08:06
  • @Ealhad I will edit my question, but I am looking to send data from mt5 to localhost:port. I am not coming from a background of mql5 or aspnet or c#. Can you tell me what technologies I should I use? – Murlidhar Fichadia Apr 30 '19 at 08:09
  • I don't know this tech stack either, but from what I understand these aren't WebSockets at all, just regular sockets. So in node you should use [net](https://nodejs.org/api/net.html). – Ealhad Apr 30 '19 at 08:14
  • @Ealhad ok this information you shared does help. I will try to use net.and see the outcome. – Murlidhar Fichadia Apr 30 '19 at 08:16
  • @Ealhad it works, net did the trick :) Thanks alot, I have been trying to figure out this for days. – Murlidhar Fichadia Apr 30 '19 at 08:31

1 Answers1

1

The problem is you are using sockets on one side, and WebSockets on the other, and the two are different things.

In node, you should use net, which is part of the standard library.

Ealhad
  • 1,912
  • 20
  • 27