Skip to main content

How to Communicate Between MetaTrader Terminals using Files


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.


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”.


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:



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



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



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.


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:








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




 string Line,Values[];





//do something with the values…





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






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:



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.


Popular posts from this blog

MetaTrader 4 Indicator to show Currency trend in Different Timeframes

Usage When trading, you probably have several windows open next to each other, different products, different time frames. Or, you have the same product but in different timeframes to see how it performs.
I wrote this indicator so I can see for a given product, how it behaves in the different timeframes available by MetaTradre 4. It has three different lines to express the following information: The trend for a given timeframe, blue arrow means bullish, red arrow means bearish.The pips change for a given timeframe, blue text means bullish, red text means bearish.The standard deviation for a given timeframe, red text means that the timeframe movement is higher thatn the volatility, otherwise black. Once attached, a panel will be drawn in the lower left corner as in the following screenshot for EURUSD:

You may see, that the daily trend for example is bullish. And that the hourly movement in pips is higher than the volatility, thus the STD value for hourly timeframe is painted in red.

To …

Mutual Exclusion (Mutex) Implementation in MQL4

Introduction In computer science, mutual exclusion is a property of concurrency control, which is instituted for the purpose of preventing race conditions; it is the requirement that one thread of execution never enters its critical section at the same time that another concurrent thread of execution enters its own critical section.

In Algotrading, you may encounter this situation while:
Running several expert advisers using a shared object which may worked on only once and not in parallel.Trading only in certain conditions, for example allowing only one expert adviser to open a position while the other ones are blocked. In my case, I am running several expert advisers on the same account trading different currencies. But, I wanted to have only one product open at a given moment while the others are blocked. So, if for example both EURUSD & EURJPY have the right conditions to open an order, only one order will be open. I wanted to avoid to be double exposed to products which are c…

How I make Money trading Forex

Did you know that on average 80% of retail traders lose money when trading Forex?

But, if you've been following my blog, you realized by now that I make my income from trading Forex, and to be specific from trading EURUSD only. This is the only product I trade and live from, which is quite counter intuitive, how is it possible to trade only one product with all it's ups and downs and live from it? In this post, I will explain exactly how I make a living from trading Forex and which traps to avoid.

Still, even that it worked for me and the setup is quite simple, there's no guarantee it will work for you the reader. We are all in a different situations in life, with more or less to risk, knowledge and so on. But still, I suggest to read this post to understand which traps are out there and which path you can follow to avoid losing your money.

The Traps

But first, let's start with the typical pitfalls out there, unfortunately, in some of them I fell myself and lost money: