Skip to main content

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:
  1. Running several expert advisers using a shared object which may worked on only once and not in parallel.
  2. 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 correlated between them, but there could be many other reasons to prevent it.

Implementation

MQL4 language supports global variables which may use as the lock mechanism to prevent parallel execution of the same code and / or accessing a shared object. 
We start by declaring the Mutex variable as:
#define MUTEX "Mutex"

Also, we declare two constants which hold the different Mutex status, locked / unlocked:
#define LOCKED 1 #define UNLOCKED 0

We will use this variable  and the different status throughout the code to make sure certain code parts are not accessed in parallel, but first, we need to initialize it in the OnInit() function:
if(GlobalVariableSet(MUTEX,UNLOCKED)==0) { Log("OnInit Mutex GlobalVariableSet failed",true,GetLastError()); return INIT_FAILED; }
This code initializes the Mutex to unlocked status.

Then, throughout the execution, whenever we have a certain code we want to lock, we will add the following condition:
&& GlobalVariableSetOnCondition(MUTEX,LOCKED,UNLOCKED) &&
As per the function definition, the unction provides atomic access to the global variable, so it can be used for providing of a Mutex at interaction of several Expert Advisors working simultaneously within one client terminal.

As soon as the critical code is over, add the following piece of code to release the Mutex:
GlobalVariableSetOnCondition(MUTEX,UNLOCKED,LOCKED);

With this simple utility, you can ensure that the critical part is not executed in parallel, as the first thread / expert adviser will get the lock while the others will not be able to enter this block of code until the mutex is locked.

Concrete Example

As part of my trading, I am interested to know whenever the balance changed due to a profit / loss. As I run several expert advisers on the same account, so I don't want to get notified X times about balance change, rather once as this is a shared value. 

Mutual exclusion solves exactly that problem as the balance value is a shared value among the different expert advisers. To implement that, I created a global variable to hold the balance value:
const string BALANCE="Balance";

I initialize it with the current value in the OnInit() function:
if(GlobalVariableSetOnCondition(MUTEX,LOCKED,UNLOCKED)==true) { if(GlobalVariableSet(BALANCE,AccountBalance())==0) { SendNotification("OnInit BALANCE GlobalVariableSet failed, please update manually"); return INIT_FAILED; } GlobalVariableSetOnCondition(MUTEX,UNLOCKED,LOCKED); }

And then, whenever the OrdersTotal() number changes, I send a notification about the new balance value, but I use the Mutex object to make sure that only one Expert adviser sends the notification while the other are locked:
if(GlobalVariableSetOnCondition(MUTEX,LOCKED,UNLOCKED)==true) { if(GlobalVariableGet(BALANCE)!=AccountBalance()) { double DailyProfit=AccountBalance()-GlobalVariableGet(BALANCE); if(DailyProfit!=0) SendNotification(StringConcatenate(SYMBOL," daily Profit = ",DoubleToString(DailyProfit,2))); if(GlobalVariableSet(BALANCE,AccountBalance())==0) SendNotification("BALANCE GlobalVariableSet update failed, please update manually"); } GlobalVariableSetOnCondition(MUTEX,UNLOCKED,LOCKED); }

This way, the critical path of checking the balance value and sending notification will not happen in parallel. By the time the next expert adviser gets the balance value, it is already updated by the previous expert adviser, so, the notification will be send once.


Join the Telegram channel to stay updated with trading ideas, articles and signals t.me/ForexAlgotrading

Comments

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 …

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:

Buyi…