Raw Socket Client for MT4???

To add comments, please log in or register
vanductai
34
vanductai  

Hello,

I currently have a (raw) socket server by python/php and I want to get Tick() of many pairs from MT4 to this server.

I search so many libs/dll for MT4 (WinSock, WinINET...) for this but all do not work.

I would like to make each chart a socket client to connect to socket server. Then, each chart would send bid/ask price every Tick() to the socket server.

I do hope someone can help on this. I just know about socket because i'm newbie of programming.

Thanks,

A MQL4 Newbie.

Farrukh Aleem
2140
Farrukh Aleem  
raw socket are not allowed on windows starting from xp sp2.
JC
1587
JC  
vanductai:

I search so many libs/dll for MT4 (WinSock, WinINET...) for this but all do not work.

If you just need to be able to send data, not to receive it, then the attached library should work.

It implements a ClientSocket class which wraps a TCP socket. You specify the server details in the class constructor, and the connection is attempted during instantiation of the class. If successful, you can then use the .Send() function to send textual messages. Example of usage:

#include <simple-client-socket-send-only.mqh>

void OnStart()
{
   ClientSocket s("localhost", 12345);
   if (!s.IsSocketConnected()) {
      Print("Failed to connect");
   } else {
      if (!s.Send("Hello")) {
         Print("Failed to send");
      } else {
         Print("Success!");
      }
   }
}
JC
1587
JC  
JC:

Example of usage:

... Slightly more realistic example, of an EA which creates a connection on start-up; drops the connection on unload; and sends a simple textual representation of each tick down the socket:

#include <simple-client-socket-send-only.mqh>

ClientSocket * glbSocket;

void OnInit()
{
   glbSocket = new ClientSocket("localhost", 12345);
   if (!glbSocket.IsSocketConnected()) {
      Print("Failed to connect");
   }
}

void OnDeinit(const int reason)
{
   delete glbSocket;
}


void OnTick()
{
   if (glbSocket.IsSocketConnected()) {
      string strTickMessage = StringConcatenate(Symbol(), "," , DoubleToString(Bid, 6) , ",", DoubleToString(Ask, 6), "\n");
      if (!glbSocket.Send(strTickMessage)) {
         Print("Failed to send");
      } else {
         // Okay
      }
   }
}
vanductai
34
vanductai  
JC:

... Slightly more realistic example, of an EA which creates a connection on start-up; drops the connection on unload; and sends a simple textual representation of each tick down the socket:


It works like a charm JC !!! Million thanks for this JC :)

In case the server got disconnected, how to make the client auto-reconnect instead of keep printing "Failed to sent"?

Once again, really thank you for your support JC. I'm current use your EA Socket Server in other topic as well :)

vanductai
34
vanductai  

ah, I slightly modified at: 

string strTickMessage = StringConcatenate(Symbol(), "," , DoubleToString(Bid, 6) , ",", DoubleToString(Ask, 6), "\n");

to

string strTickMessage = StringConcatenate(Symbol(), "," , DoubleToString(Bid, 6) , ",", DoubleToString(Ask, 6), "\r\n");

This could help the server can recognize the ending of message and do it job instead waiting to fulfill the buffer.

Best!

JC
1587
JC  
vanductai:

In case the server got disconnected, how to make the client auto-reconnect instead of keep printing "Failed to sent"?

If IsSocketConnected() returns false, or Send() fails, then you need to destroy the existing connection and create a new instance of the class.
JC
1587
JC  
JC:

If you just need to be able to send data [...]

Very substantially extended version of the library - albeit nowhere near fully tested. Handles both client and server; both send and receive; both MT4 and MT5 (32-bit and 64-bit).

As a demonstration of the extended functionality, the attached example implements a server which responds with a price quote if it receives the text "quote" (plus a CRLF). For example, you can telnet into the port, type "quote", and get back a price quote from the EA. (Although it's uploaded as "Example server.mq4", that code can also be used as MQ5 instead of MQ4.)

vanductai
34
vanductai  
JC:

Very substantially extended version of the library - albeit nowhere near fully tested. Handles both client and server; both send and receive; both MT4 and MT5 (32-bit and 64-bit).

As a demonstration of the extended functionality, the attached example implements a server which responds with a price quote if it receives the text "quote" (plus a CRLF). For example, you can telnet into the port, type "quote", and get back a price quote from the EA. (Although it's uploaded as "Example server.mq4", that code can also be used as MQ5 instead of MQ4.)


I strongly believe your posts in this thread will be the best working source for those who are looking for mt4 socket lib. Seems they have searched for years and tired totally, then some other solutions came stand for socket such as name pipes, zeromq, channel....

Best Wish for you JC :)

PS: Are you a trader or EA master, just wonder :) I have PM you before posting this thread, thanks again :)

JC
1587
JC  
vanductai:

I have PM you before posting this thread, thanks again :)

Thanks for the PM, which I have only now seen.

I had completely forgotten that I wrote the code at https://www.mql5.com/en/forum/160115/page3  <g>. That code is slightly more sophisticated in terms of having event-driven handling to minimise latency on server processing of client connections/messages, but the code here has the advantage of being simpler (*) and fully MT5-compatible.

(* simpler in terms of the interface for consumers of the code. Some of the internal handling of gethostbyname() on 32/64-bit platforms is unutterably horrible because of MQL5 not having an integer data type whose size depends on the platform.)

Carl Schreiber
Moderator
7658
Carl Schreiber  

Does anybody remembers those good old days? What about an old fashioned RAM-disk and the use of simple Files read/write by WinApi functions?

  1. Almost as fast as pipes or sockets.
  2. The WinApi functions are a bit faster than mt4's file functions.
  3. Linux-compatible solution (I guess).
  4. No server that might block everything on its side.
12
To add comments, please log in or register