The overall goal of this article is to see if there interest in releasing the MSMQWriter to the public. When dealing with interprocess communication, many developers take the approach of trying to exchange information between MQL4 and a programming language directly. For example, I have seen various solutions which allow an MQL4 dll to call a function in C# directly, and vice versa. But what if we take the approach of looking at communication as a type of data exchange. The way that I approached this problem is to include a middle man. The middle man being the powerful Windows MSMQ. The high level idea is outlined by the following:
Using this approach, we have the following process to exchange data:
1. MQL4 writes to the Private Queue
2. Your program of choice written in (.NET, Java, Python) to name a few, periodically checks the Message Queue for data written. If it finds a message, then it will read it and parse it.
3. Your program can now make a decision based on the data it got, and write back a signal to the Message Queue.
4. MQL4 will periodically read from the Private Queue and pick up the message. If the message is a type of signal, then the script can take action. For example, it reads a message from the queue that says “BUY NOW”. This tells the EA
to place a buy order.
The program that I am using this approach with is written in C#. I used the .NET framework, and specifically used the MessageQueue class to read from the Message queue. The MQL4 script has a couple of sections, and I list them below.
First, we need to include the DLL so MQL4 knows which functions to call:
Right now there are 3 functions:
1. sendMsg – This sends a message to the private queue
2. rcvMsg – Gets a message from the private queue
3. CreateQueue – Will create a queue if one doesn’t exist.
The other important part of getting data exchange to work properly is to of course set up your queues. For example, I set various private queues for each currency pair I was trading:
This allowed me to segregate my queues with my different EAs, and write different programs to take action on a particular currency pair.
Sending a Message
An example of how I do this on MQL4 code:
What i’m doing here is just writing price data to the Message queue. If you notice, the first parameter parameter is the actual message being sent to the message queue. bufmsg is of type char array. The third parameter is the queuename you’re writing to. You can write to any private queue you want. queuename is also of type char array.
Receiving a Message from the Queue
The great part about this is that the call to rcvMsg blocks. It’s similar to a cin statement in C++, it waits until there is a message on the queue, until then your EA Advisor will block. Here is an example of how I receive a message from the queue:
In MQL4 code, I receive a message from the queue referenced by the queuename variable, and place the data on bufresp which is a char array. I then cast it to a string and then do my necessary comparisons. The caveat here is that, since you don’t want your EA to block forever because it can cause it to crash, I constantly exchange messages between the EA and the .NET Program. When the .NET program receives price data, it will send back one of three messages to the queue:
When my MQL4 program receives a message, it will get one of these, and take action. Regardless, I always make sure that the blocking of the message is at most a minute, so that it can move ahead and not block forever. I’d like to improve on this by adding a 4th function which posts data to a web service, and can also get data from a web service. But before I move ahead, I am just looking for feedback if there is use for this, so that I can share.