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