Skip to content
All library documents

Designing a Type-Safe Publish-Subscribe Event Bus for Trading Systems

Article MQL5 articles

Summary

This article explains how to coordinate components in an MQL5 Expert Advisor without shared global variables. It proposes a publish-subscribe event bus: components publish typed events, and registered listeners receive the events they need through a central dispatcher. An enumeration routes event types to subscriber lists, while a common payload carries fields such as timestamps, order identifiers, prices, volumes, and diagnostic messages. The design uses an abstract listener class and wrapper structs to work within MQL5’s array constraints.

The article argues that this structure makes dependencies more visible and supports component-level testing and refactoring. It explains trade-offs as well: dispatch is synchronous, so slow listeners add tick-handler latency; the bus does not own listener objects, so callers must manage their lifetimes; and the event taxonomy is fixed at compile time. The performance discussion is architectural rather than a reported benchmark, and the approach requires careful registration and cleanup.

Key ideas

  • A central event bus can replace implicit communication through shared mutable global variables.
  • Enum-based event routing enables direct indexing and compile-time checking of event types.
  • Listeners receive a shared payload by constant reference and must implement a common dispatch method.
  • Synchronous dispatch can affect handler latency, and callers are responsible for listener lifetimes.
  • The fixed event enumeration does not support runtime addition of event types.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.