MetaComm communications library for MT4/MQL4 v1.0

MCSoft

Trader
Jun 14, 2014
2
0
12
Hi friends, my name is Marcos Correas. First of all, sorry for my bad english.
I am a programmer. I love programming and I enjoy forex. Currently I am writing software in C/C++, Python, PHP and MQL4, but also did some work in Java, C# and others.
I am launching now my first commercial product for Metatrader 4: The MetaComm communications library for MQL4.

First of all, a message to the forum moderators/admins: If I am posting in the wrong place, please tell to me. I am not trying to make spam, I am just offering a solution in which I have worked and I see many traders and programmers need.

The story of MetaComm begins a while ago, when I wrote my firsts programs in MQL:
I love programming in Python, I believe that python is a very powerful, yet very simple and natural, that allow to translate ideas into code very quickly, and it has plenty of tools and libraries for all kinds of tasks such as numerical analysis, statistics, machine learning, databases, etc.
In contrast, MQL is very similar to C, but does not allow the use of the features that really give power to C, as pointers and structures.
Compared with python and C, I think MQL is a very limited language, not only by the language itself, also for lack of libraries available for many other languages (databases, threading, file manipulation, etc.).
MQL allows the use of functions located in dynamic libraries (dlls), which could fill these gaps, but unfortunately most interesting libraries for use with MQL use pointers, structures, or both. They can be used, but employing a lot of artifacts to convert native types in structures, pointers to integers, etc.
I had the intention of implement my strategies and trading ideas in python, but I did not find any native python way to do. Then, I happened to program an expert advisor in MQL to send real-time data from metatrader to my python program, process the data according to the trading strategy and return to the advisor orders (purchase, sale, lots, etc) to execute in terminal.
And how I could do that: How about using TCP / IP!!. That would allow me to integrate one or more terminals with my python software on the same machine, on the same network or even over the Internet without changing the code at all. I could write my analysis and trading software in python, and could even implement trading strategies such as arbitrage between various brokers, hedging, etc..
Thus began my search for a communications library for MQL4, but without very luck.
The only alternative I did found to provide communication to my expert advisor was using Winsock, the library itself using by windows to communicate over TCP/IP.

Winsock can be used from MQL4 using this library of codebase:
http://www.mql5.com/es/code/9296

But winsock is very difficult to use, simply look at the example of the author to implement a server:
http://www.mql5.com/es/code/viewcode/9296/47344/wsockserv.mq4
After a long search with no luck, and noting that many people needed a similar solution to communicate between expert advisors in the same terminal, or between terminals, I started in the task of creating my own library. And this is how MetaComm born.

Some of the most important features of MetaComm:

- Is based on ZeroMQ, a powerful and fast message queueing library. MQL can not use ZeroMQ directly because MQL do not support pointers nor structs. MetaComm is a wrapper that add a "simplification" layer over ZeroMQ. MetaComm manages pointers,
context initialization, context deinitialization, sockets and communication patterns
of ZeroMQ for you, in a very easy way

- This library allows to interchange messages (strings) over TCP/IP:
- between EAs/scripts/indicators in the same MT4 terminal.
- between EAs/scripts/indicators in different MT4 terminals in the same machine.
- bettween EAs/scripts/indicators running in different machines across the internet.

- This library allows to implement:
- Arbitrage strategies between different brokers or accounts
- Hedging between different brokers or accounts
- Communicate EAs with software written in other languages (c/c++, python, visual basic, etc). This will allow to code robots in any language of your choice. NOTE: for now, I am only supplying examples written in MQL, but soon, I will add examples and libraries in another languages.

- This library allows to connect many clients to the same server without modify server code
- The library implements message queueing transparently
- Connection lost is managed automatically

- And the most important: THIS LIBRARY IS VERY, VERY EASY to use!!!!!!

This is an example of client implemented as Expert Advisor:

//-------------------------------------------

// include MetaComm library and helper functions
#include <MetaComm.mqh>

// global variable to store the client ID
int client1

//-------------------------------------------
// Expert initialization function
//-------------------------------------------
int OnInit() {
int result;

Print("-- MetaComm client --");
// create client, and connect to server localhost:5555
// if this is the first client or server created, the library is
// atomatically init
client1 = MC_client_create("localhost", 5555);
// verify result
if client1 == -1) {
// init error
Print("Init Error!!! " + MC_errdesc());
return -1;
}
// client created OK
Print("Client STARTED");
return(INIT_SUCCEEDED);
}
//-------------------------------------------
// Expert deinitialization function
//-------------------------------------------
void OnDeinit(const int reason)
{
// destroy client (and deinit library if there is no more clients or
// servers running)
int result = MC_client_destroy(client1);
Print("Client TERMINATED");
}
//-------------------------------------------
// Expert tick function
//-------------------------------------------
void OnTick()
{
// string to receive response
string response;

int result;
// timeout for response
int timeout = 1500;

// create request string
string request = Symbol() + ": " + "BID=" + DoubleToString(Bid, Digits) +
" ASK=" + DoubleToString(Ask, Digits);
Print("Client sending: " + request);
// send request to server, and receive response
result = MC_client_request(client1, request, response, timeout );
// verify if there is a response from server
if (result == -1)
Print("Error!!! " + MC_errdesc());
else
Print("Response received from server: " + response);

}
//-------------------------------------------

this EA, on init create a MetaComm Client, and in each tick will send to server a message with the current quote of symbol, like the following:
"EURUSD: BID=1.9834 ASK=1.9831"

And....., this is the code for server written as MQL4 Script:

//-------------------------------------------

// include MetaComm library and helper functions
#include <MetaComm.mqh>

// global variable to store the server ID
int server1

//-------------------------------------------
// Script program start function
//-------------------------------------------
void OnStart()
{
// string to receive request from clients
string request;
int result;
Print("--- MetaComm server ---");

// create server listening in server_port
server1 = MC_server_create(server_port);
// verify
if (server1 == -1)
{
// error creating server
Print("Server create Error!!! " + MC_errdesc());
return;
}
// server created OK.
Print("Server STARTED");
// Wait messages (requests) from clients
while(!IsStopped())
{
// verify if there is a request available
if (MC_server_request_available(server1) == 1)
{
// retrieve client request and store in request string
result = MC_server_get_request(server1, request, 1500);
// verify
if (result == -1)
{
// error
Print("Server get request error");
}
else
{
Print("Request: " + request);
// resend the same message to client as response
MC_server_send_response(server1, request);
}
}
}

// destroy server. If there is no more clients or servers running,
// the library is also deinit automatically
MC_server_destroy(server1);
Print("MetaComm server TERMINATED");
}
//-------------------------------------------

The server receives the request from clients, and send back the same message as response.
In the examples is demonstrated the ease of use of the library.

How to wiev the library in action?
You can download a compiled demo of the library from the next link:
https://drive.google.com/file/d/0B1smCleOwqTNMEVFNk9pTUwzc1k/edit?usp=sharing

Demo do not include source code. EAs and library are compiled.
Demo includes one client EA, and one Script Server written in MQL4 (.ex4). You can attach multiple clients to multiple graphs, then will talk to te same server. The library routes each response from server to the correct client who made the request.
The library is limited to 10 client and 10 servers simultaneous per loaded instance (each Metatrader Terminal load one instance of library). This limit can be changed by request.

Interested in buy the library can contact me by email: mmcorreas@gmail.com
the price of the library is U$D 25, this include:
- library dll
- Header file MetaComm.mqh
- Source files for client and server (mql)

If you need source code for clients and servers in C/C++/Python, you are interested in the source code of the library, or you need a personalized solution, please contact me.

Regards!!!!

Marcos M. Correas
MCSoft.