🚀 AI One-Click Generation of mql4 Strategy Code

How to Create a Shared Symbol Blacklist for Multiple EAs Using Mql4 Global Variables

mql4 | Published on: 2025/12/12
The following content is generated by EasyQuant .

Problem Description

I'm running multiple trading bots. How can I make them share information, like a list of currency pairs to avoid trading?

Solution

To allow multiple Expert Advisors (EAs) to share information like a "Do Not Trade" list in MetaTrader 4, the most efficient and robust method is using Global Variables of the Client Terminal.

Global Variables (GV) are stored in the terminal's memory, are accessible by all EAs running on any chart within that terminal, and persist even if the terminal is restarted (until 4 weeks of inactivity).

Solution: Global Variable Blocklist

Here is a complete, modular solution. You can copy the helper functions into your EAs or save them as an include file (.mqh).

1. The Shared Logic

Add these functions to the bottom of your EA code, or include them. This logic uses a specific prefix (e.g., BLOCK_) to identify variables related to your blocklist.

//+------------------------------------------------------------------+
//| Shared Blocklist Logic                                           |
//+------------------------------------------------------------------+
#define BLOCK_PREFIX "BLOCK_"

// Function to check if a specific symbol is in the blocklist
bool IsPairBlocked(string symbol)
{
   // Check if a Global Variable exists with the name "BLOCK_EURUSD" (for example)
   if(GlobalVariableCheck(BLOCK_PREFIX + symbol))
   {
      return(true);
   }
   return(false);
}

// Function to add a symbol to the blocklist
void BlockPair(string symbol)
{
   // Set value to 1.0 (value doesn't matter much, existence is the key)
   GlobalVariableSet(BLOCK_PREFIX + symbol, 1.0);
   Print("Communication: ", symbol, " has been added to the blocklist.");
}

// Function to remove a symbol from the blocklist
void UnblockPair(string symbol)
{
   if(GlobalVariableCheck(BLOCK_PREFIX + symbol))
   {
      GlobalVariableDel(BLOCK_PREFIX + symbol);
      Print("Communication: ", symbol, " has been removed from the blocklist.");
   }
}

// Function to clear the entire blocklist (delete all BLOCK_ variables)
void ClearBlocklist()
{
   int total = GlobalVariablesTotal();
   // Iterate backwards because we are deleting items
   for(int i = total - 1; i >= 0; i--)
   {
      string name = GlobalVariableName(i);
      if(StringFind(name, BLOCK_PREFIX) == 0) // If name starts with BLOCK_
      {
         GlobalVariableDel(name);
      }
   }
   Print("Communication: Blocklist cleared.");
}

2. Implementation in the Trading Bot

Here is how you integrate the logic into your EA's OnTick function.

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   // 1. Check if the current symbol is blocked by another bot
   if(IsPairBlocked(Symbol()))
   {
      // Optional: Display comment on chart
      Comment("Trading Paused: Symbol is in shared blocklist.");
      return; // Exit OnTick immediately, do not trade
   }
   else
   {
      Comment("Trading Active.");
   }

   // 2. Your Strategy Logic Here
   // ...
   
   // 3. Example: Logic to block THIS pair for everyone else
   // If we have too much drawdown, tell other bots to avoid this pair
   if(AccountEquity() < AccountBalance() * 0.90) // 10% Drawdown example
   {
      BlockPair(Symbol());
   }
}

Alternative: Shared File (CSV)

If you need to edit the list manually using Notepad or share data between different MT4 terminals, use the File system with the FILE_COMMON flag.

//+------------------------------------------------------------------+
//| Check Blocklist from Common CSV                                  |
//+------------------------------------------------------------------+
bool IsPairBlockedByFile(string symbol)
{
   string filename = "SharedBlocklist.csv";
   // FILE_COMMON looks in the Common Data Folder (accessible by all terminals)
   // FILE_SHARE_READ allows multiple bots to read simultaneously
   int handle = FileOpen(filename, FILE_READ|FILE_CSV|FILE_ANSI|FILE_COMMON|FILE_SHARE_READ);
   
   if(handle == INVALID_HANDLE) return(false); // File doesn't exist, assume safe
   
   bool isBlocked = false;
   
   while(!FileIsEnding(handle))
   {
      string line = FileReadString(handle);
      // Remove whitespace and compare
      if(StringFind(line, symbol) >= 0) 
      {
         isBlocked = true;
         break;
      }
   }
   
   FileClose(handle);
   return(isBlocked);
}

Summary of Methods

Feature Global Variables (Method 1) Shared File (Method 2)
Speed Extremely Fast (Memory) Slower (Disk I/O)
Scope Single MT4 Terminal Multiple MT4 Terminals (if using FILE_COMMON)
Ease of Use High (Native MQL4 functions) Medium (Requires parsing)
Manual Editing Press F3 in Terminal Edit text file in Notepad

Recommendation: Use Method 1 (Global Variables) for bots running within the same terminal. It is faster and prevents file-locking errors that occur when multiple bots try to read/write a file at the exact same millisecond.

Q&A

Q: How do I manually view or edit the Global Variables?
A: In MetaTrader 4, press F3. This opens the Global Variables window where you can see, create, or delete variables (e.g., BLOCK_EURUSD) manually to control your bots in real-time.

Q: Will these variables survive if I restart my computer?
A: Yes. MetaTrader 4 saves Global Variables to the hard drive upon shutdown and restores them on startup. They only expire if they are not accessed (read or written) for 4 weeks.

Q: Can I store text (string) in Global Variables?
A: No, Global Variables in MT4 only store double (numbers). That is why the code uses the name of the variable (BLOCK_EURUSD) to store the information (the currency pair), and the value is just a dummy number (1.0).