Least Recently Used Cache
A cache with no eviction policy is a slow memory leak: every long-running EA or script that memoises results by key eventually accumulates keys it will never use again, and a multi-week live session notices. Least-recently-used eviction fixes this by always dropping the entry that has gone the longest without being touched, keeping frequently used results in place. A textbook LRU cache pairs a hash map with a doubly linked list for O(1) lookup and O(1) eviction. Neither MQL4 nor MQL5 offers pointers to build a real linked list from, and hand-rolling one with parallel index arrays is a well-documented source of off-by-one bugs that are hard to notice until the cache has been running for a while. This header takes a simpler, honestly- documented trade instead: every access stamps the entry with an incrementing logical clock, and eviction scans for the minimum stamp. That is O(capacity) per Put rather than O(1), but for the tens-to-low-hundreds of entries a terminal script or EA actually caches, the difference is not measurable - and the code is short enough to read in one sitting and trust. A miss always resets the caller's output value to 0.0 rather than leaving it untouched, so a stale local variable from a previous call can never be mistaken for a legitimately cached zero. The header is self-contained. No dependencies, no DLLs. Notes and limitations - Every lookup and insert is O(capacity), not O(1). This is the right trade for the small caches this header targets (tens to low hundreds of entries); for anything larger, a hash-map-backed design is the better choice at the cost of considerably more code. - Values are double only. Cache a string by hashing or indexing it yourself, or extend the header with a parallel string value array if you need one - neither platform's lack of generics means a second value type means a second array, not a type parameter. - Not thread-safe in any sense that matters differently from the rest of either platform: a single chart's single-threaded event model is all this assumes. - No money is at risk in the demo. It caches synthetic price snapshots only and never sends an order on either platform.
Script · MT5 · v1.0 · Free
About Least Recently Used Cache
Least Recently Used Cache is a script for MetaTrader 5. It shows several symbols in one panel. It has 1 setting, such as Capacity. MQL source code included.
Files in the download: LruCache.mq5.mqh, LruCache.txt, LruCacheDemo.ex5, LruCacheDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Capacity | 4 | Cache capacity used throughout the demo |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Trading Sessions · Dashboards & Scanners · MT5 Script · MQL5 Source Code