Skip to content

perf: cache GetMarketTrend() results per-bar in isAllGood() #67

Description

@cfournel

Problem

GetMarketTrend() is called inside isAllGood() for W1, D1, and H4 timeframes.
Since isAllGood() evaluates ~50 OBs per bar, this results in 150 CopyRates() system calls per bar
(3 timeframes × 50 OBs). Each CopyRates() allocates memory and copies rate arrays.

Solution

  • Added RefreshTrendCache() in helpers.mqh — called once per CTOB bar in ExecuteTimer
  • Cache variables in globals.mqh: g_cachedMacroTrend, g_cachedD1Trend, g_cachedH4Trend
  • isAllGood() in cOrderBlock.mqh now reads cached values instead of calling GetMarketTrend()

Impact

  • 50× reduction in CopyRates() calls (150 → 3 per bar)
  • Estimated 20-30% CPU savings in the OB evaluation loop
  • Zero behavioral change — trend values don't change within the same candle

Files Changed

  • OBInclude/globals.mqh — added cache variables
  • OBInclude/helpers.mqh — added RefreshTrendCache(), call site in ExecuteTimer
  • OBInclude/cOrderBlock.mqh — replaced 3 GetMarketTrend() calls with cached reads

Verification

Needs a compile test + before/after backtest to confirm identical PF/trades/DD.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions