Discussing the article: "Lazy-Loading Indicator Handles in MQL5: A Resource Manager Pattern for Multi-Timeframe EAs"

 

Check out the new article: Lazy-Loading Indicator Handles in MQL5: A Resource Manager Pattern for Multi-Timeframe EAs.

Multi‑timeframe EAs that initialize every indicator handle in OnInit() pay a fixed startup cost even when most handles are never used. CIndicatorCache applies lazy loading with composite‑key lookup, reference‑counted Acquire/Release, and a deterministic FlushAll() for cleanup. Handles are created on first request and reused across ticks, reducing startup latency, avoiding repeated heap allocation, and preventing terminal resource leaks through centralized ownership.

A multi-timeframe Expert Advisor (EA) trading 10 symbols across four timeframes with five indicators per combination requires 200 indicator handles for full coverage. Conventionally, developers initialize all handles inside OnInit(). Before OnInit() returns, the terminal connects to each price feed, verifies data availability, allocates indicator buffers, and registers the handles. This process introduces initialization latency. This latency increases during terminal cold starts or when synchronizing stale historical data.

Furthermore, many of these handles are rarely used simultaneously. A strategy may only generate active signals on a small fraction of its watched symbols at any given time. The remaining inactive handles consume memory and handle slots without contributing to the current session.

Static initialization also increases code maintenance overhead. If indicator requirements change, developers must manually update the handle instantiations in OnInit(). Omitted or forgotten handles waste runtime resources while remaining undetected by the compiler.

The lazy-loading resource manager pattern resolves these inefficiencies through three core mechanisms:

  • On-Demand Initialization: Indicator handles are created only upon their first runtime request. This shifts initialization costs from theoretical use cases to active ones.
  • Reference Counting: Identical indicator configurations are shared. For example, if two separate trading modules request the same Average True Range (ATR) parameters on the same symbol and timeframe, the manager creates the handle once and tracks its active consumers.
  • Centralized Ownership: Resource cleanup is consolidated. Instead of maintaining an extensive list of manual IndicatorRelease() calls within OnDeinit(), the EA triggers a single cleanup method from the resource cache.

Cache Decision Path

Author: Ushana Kevin Iorkumbul

 
You don't need all this: the platform supports reference counting and resource cleanup (for indicator handles left unreferenced after deinit) right from the shelf, and behind the scenes.