Discussing the article: "Building a Type-Safe Event Bus in MQL5: Decoupling EA Components Without Global Variables"

 

Check out the new article: Building a Type-Safe Event Bus in MQL5: Decoupling EA Components Without Global Variables.

A typed publish-subscribe event bus in MQL5 replaces global variables and direct cross-references. Using an abstract listener interface and an enum-indexed subscription table, a signal engine, order manager, and drawdown monitor communicate only through the bus, with no shared state. The article analyzes dispatch overhead, pointer validation, and recursive publish risks, helping you design decoupled, testable EAs.

When an Expert Advisor grows beyond a single trading-logic block, developers often reach for the most readily available coordination mechanism: global variables. A risk manager needs to know the current drawdown, so a double named g_current_drawdown appears at file scope. A signal engine fires, and the order manager is informed through a bool g_signal_active flag. Within weeks, a moderately complex EA accumulates dozens of these state bridges, each one representing a direct, invisible dependency between components that were designed to be independent.

The structural consequence of this pattern is not just messiness; it is behavioral coupling. When the risk manager reads g_current_drawdown, it assumes the right module wrote it at the right time within the correct tick cycle. If the execution engine is refactored to update that value asynchronously, or if a new component also writes to it, the risk manager's behavior changes silently. There is no contract enforced at the language level. The global variable is a shared mutable state channel with no access control, no type guarantee beyond the primitive, and no record of who produced the value or when.

Consider a realistic three-component architecture: a signal generator, an order manager, and a drawdown monitor. In the naïve global-variable design, their relationships look like this:

Figure 1: Architectural coupling of a three-component Expert Advisor

Author: Ushana Kevin Iorkumbul

 
I like the schemes but why the reasoning about global variables? Global variables are normally used for state persistency or communication between many different MQL5 programs - not inside the same standalone EA or standalone indicator or anything else single. If it's one of the latest cases - you use MQL5 variables/objects with native type-safety, which is obvious and demonstrated in the article. Indeed, a complex trading system can be composed of many programs, which provides multi-threaded execution and requires some synchronization mechanism - and the global variables can afford this.