Loading…

Memoisation Helpers

Any calculation that gets called repeatedly with the same inputs within a session - a correlation between two symbols, a multi-bar statistic, anything that is genuinely expensive but produces the same answer for the same input every time - is a candidate for memoisation: compute it once, remember the answer, and return the remembered answer on every subsequent call with the same input instead of recomputing. CMemoCache is a small, general-purpose building block for exactly that pattern. It stores string-keyed double values with a fixed capacity; once full, the next brand-new key evicts the least-recently- used existing entry rather than growing without bound or refusing new entries outright. TryGet() returns a genuine boolean hit/miss rather than a sentinel value, so a legitimately-cached 0.0 result is never mistaken for "not cached". The LRU eviction mechanics (a monotonic access clock plus a linear scan for the oldest-accessed slot) are the same technique this catalogue's TwoTierCache.mqh library uses, but written out again from scratch in this header - per house style, no project includes another project's file, so shared logic like this is reimplemented locally rather than pulled in. Unlike TwoTierCache, there is deliberately no file-based spill tier here: a memoised result is by definition cheap to recompute on a miss, so persisting evicted entries to disk would add cost and complexity for no real benefit - a plain miss-and- recompute is the correct, honest behaviour for this use case. Invalidate()/InvalidateAll() cover the two common cases for dropping stale results: one specific input whose underlying data just changed, or everything at once (e.g. on a new bar, or after a parameter change that invalidates the whole cache). Notes and limitations - Values are a plain double. There is no string-valued or struct- valued variant in this header - encode a composite result as a formatted string key/value pair yourself if you need to memoise something richer than a single number (this catalogue's TtlResultStore.mqh and ReferenceDataRegistry.mqh cover string-keyed string values instead, for different use cases). - Lookup and eviction are both a linear scan over the live entries. Deliberately simple and fine for the tens-to-low-hundreds of distinct memoised inputs a typical indicator/EA calculation produces in one session; a much larger key space would want a hash-indexed lookup instead. - Not thread-safe and not persisted - this is a pure in-memory, single-terminal-instance cache that is empty again on every restart, by design (see the TTL variant elsewhere in this catalogue if persistence or time-based expiry, rather than pure LRU capacity pressure, is what you actually need).

Script · MT4 · v1.0 · Free

About Memoisation Helpers

Memoisation Helpers is a script for MetaTrader 4. MQL source code included.

Files in the download: MemoisationHelpers.mqh, MemoisationHelpers.txt, MemoisationHelpersDemo.mq4.

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: Trading Sessions · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria