Introduction

Do you need different MetaTrader terminals to be able to communicate between themselves?

Some reasons to implement this logic could be:

  1. Trading with different accounts which need to know on each other status.
  2. Trading with different brokers which needs to have the same orders open.
  3. Using one terminal to notify another terminal upon a certain trading condition.

The use case which enforced me to communicate between different terminals, was a strategy running on one account that had to notify another terminal to open orders once certain conditions were met. The second strategy was a derivative of the first one, so it had a dependency on its behavior. But I didn’t want to run both strategies in the same terminal as I wanted the order book to be clean and not a mixture of these two strategies.

Important: this solution will only work if the terminals are running on the same PC, otherwise, you need to consider using other methods like reading remote URL or using FTP.

Solution

Using Common Files

MetaTrader has a feature to handle files located in the common data folder. A location accessible by using the option FILE_COMMON upon file functions call. This is where we will place our shared files which will be used to hold the information different terminals need to use.

Normally, there is a single producer who writes the file and N consumers who reads it. Otherwise, a solution needs to take file synchronization in consideration, not to get to a state where two processes are writing at the same time and corrupting the file.

There must be a shared name between all the participants, so all the terminals refer to the same file on the common folder.

So, high level the solution will be to open up a file in the common folder, update it with whatever information we want to share, close it. In parallel, the consumers check for changes every X minute, as soon as a change detected. They read it and act accordingly. Meaning that our code will have two sides, the producer and the consumer of the information.

To locate the common data folder location, click in MetaEditor File -> “Open Common Data Folder”.

Producer

This instance is responsible to open up the file as a write only file and update it with the relevant information for the consumers. It will do so in the common folders so other terminals will be able to access it.Farm

The first thing we need is a shared file name, one which will be used within all the MetaTrader terminals. I will declare it as follows:

const string FILENAME="Pipe.txt";

It may be any given name, I chose Pipe as it is behaving like an information pipe between the different terminals.

Next, once a new information is ready to be shared, we will do the following:

int FileHandle=FileOpen(FILENAME,FILE_TXT|FILE_WRITE|FILE_COMMON);

if(FileHandle==INVALID_HANDLE)

SendNotification(StringConcatenate("Operation FileOpen failed: ",IntegerToString(GetLastError())));

else
{

 if(FileWriteString(FileHandle,StringConcatenate("OP_SELL",";",DoubleToString(0.29,2)))==0)

SendNotification(StringConcatenate("Operation FileWrite failed: ",IntegerToString(GetLastError())));

FileClose(FileHandle);

}

We start by opening a file for writing in the common data folder, by using the flag FILE_COMMON flag. Then, if the file open was successful, we write into the file the information we want. In this case, I wrote an instruction to open a short order with a certain order lots value. If the write was successful, the return value will not be 0, rather, the number of the bytes written. We finish by closing the shared file.

Consumer

HorseOn the consumer side, we will use the same file name to refer to the same information pipe with the producer from the previous part:

const string FILENAME="Pipe.txt";

Then, we will check every X minute whether a pipe exists with new information. Once there, we will open for reading and get the new information to be used:

if(TheMinute!=Minute())

{

TheMinute=Minute();


if(FileIsExist(FILENAME,FILE_COMMON)==true)

{

 int FileHandle=FileOpen(FILENAME,FILE_TXT|FILE_READ|
FILE_COMMON);
if(FileHandle==INVALID_HANDLE)

{

 SendNotification(StringConcatenate("Operation FileOpen failed: ",IntegerToString(GetLastError())));

 ExpertRemove();

 return;

}


 string Line,Values[];

while(FileIsEnding(FileHandle)==false)

 {

 Line=FileReadString(FileHandle);

 StringSplit(Line,StringGetCharacter(";",0),Values);


//do something with the values…

}

FileClose(FileHandle);

 if(FileDelete(FILENAME,FILE_COMMON)==false)

{

SendNotification(StringConcatenate("Operation FileClose failed: ",IntegerToString(GetLastError())));

 ExpertRemove();

 return;

}

 }

}

As soon as we see the file exists, we will open it for reading, again in the common data folder by using the FILE_COMMON flag. Then we read line by line and split the line into the Values array using the “;” as the separator. Then the consumer has all the information he needs to use in the internal logic. Finally, we close and delete the file to make sure we will not use old information.

Using this piece of code, we ensured we are checking regularly for new input and once we have it, we are able to use it across multiple MetaTrader instances. In my case, I only need it in one instance of MetaTrader. But If I had to use it in more than one instance, I would use the file timestamp as the indicator if something changed instead of deleting the file as multiple instances would be using it. It is a simple check:

FileGetInteger(FileHandle,FILE_MODIFY_DATE);

Conclusion

I have introduced a method I am using with my expert advisers to communicate between different MetaTrader instances using files in the common data folder. It is based on a communication between a single producer to multiple consumers. With that in hand, you can create a multi strategy across several different accounts.


  • As always, for any information on my trading activity or any other information, don’t hesitate to contact me.
  • I launched the community page with different forums, please register and discuss with the community about FX trading and / or Expert Advisors programming.
This entry was posted by . Bookmark the . Titled

Leave a Reply

Your email address will not be published. Required fields are marked *