Symbol Property Cache
Reading a symbol property is a call into the terminal, not a local variable read, on either platform. Code that reads several properties per tick per symbol - a basket loop across dozens of open trades is the usual case - pays that overhead on every pass, even though almost none of those properties change within a session. Digits does not change while the terminal is running. Tick value drifts with the account currency conversion rate, but slowly. Stop level and freeze level move only if the broker changes contract specifications, which is rare and mid-session rarer still. This header replaces the repeated property calls with a small per-symbol struct, populated once and read many times. The design choice that matters is that caching is opt-in on staleness, not blind. Every getter takes an optional maxAgeSeconds argument: zero means "cache once, trust it until ScRefresh is called by hand", the fastest option and the right one for properties that genuinely never change mid-session. A positive value makes the getter check the entry's age against GetTickCount() and refresh automatically once it is stale, which is the right choice for tick value or anything else that can legitimately drift. The caller picks the tolerance per call site rather than the library imposing one policy on everything. The age check itself has to survive GetTickCount() wrapping every 24.8 days. Subtracting two ordinary 32-bit ints wraps the same way the counter does, so no special-casing is needed - the comment in the header explains why the naive "is this larger than that" version would be wrong instead. A symbol that cannot be resolved - misspelled, or never added to Market Watch - reads the point value back as zero on both platforms. ScRefresh treats that as a hard failure and returns -1 rather than caching a row of zeros that would later look like a plausible broker value. Every getter propagates that failure as 0 / 0.0, which is documented as "not cached", not "confirmed zero". The cache holds any number of distinct symbols at once, keyed by name, so it works the same way whether the caller tracks one chart symbol or a basket across a dozen pairs. Notes and limitations - This is a read cache, not a change notifier. If a broker changes contract specifications mid-session, an unbounded getter (maxAgeSeconds = 0) will not see it until ScRefresh or ScClearCache is called explicitly. - The cache is a plain module-level array and is not thread-safe in the sense of being called from more than one program at once - each script, indicator or EA instance that includes this header gets its own independent cache, which is normally what you want. - Linear scan lookup by symbol name. Fine for the handful of symbols a typical script or EA tracks; not intended for scanning the entire Market Watch of a broker with several thousand instruments on every tick. - GetTickCount() has roughly 10-15ms resolution on most Windows builds, so ScAgeMs() on a very recently refreshed entry can read as 0ms even though a small amount of time has passed. This does not affect correctness of the staleness comparison, only its precision at very short intervals. All four notes apply identically on both platforms.
Script · MT5 · v1.0 · Free
About Symbol Property Cache
Symbol Property Cache is a script for MetaTrader 5. It has 2 settings, such as Lookup iterations and Stale seconds. MQL source code included.
Files in the download: SymbolCache.mq5.mqh, SymbolCache.txt, SymbolCacheDemo.ex5, SymbolCacheDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Lookup iterations | 200000 | Repeated lookups per timing pass |
| Stale seconds | 2 | Auto-refresh threshold for the staleness 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 · MT5 Script · MQL5 Source Code