Hash Map
Neither platform has templates or a built-in associative container. A "look up a value by symbol name" or "look up a value by magic number label" requirement is almost always solved by two parallel arrays and a linear search - fine at a handful of entries, and quietly the slowest part of OnTick() once a program tracks per-symbol state across a full watchlist, because every lookup costs O(n) and there are usually several lookups per tick. CHashMap is open addressing with linear probing over three parallel arrays - keys, values, and a per-slot state flag - rather than separate-chaining with linked lists, because neither platform gains anything from per-slot allocation and open addressing keeps everything in flat, cache-friendly arrays. Hashing is FNV-1a: a small, allocation-free, well-distributed hash with no library dependency, which matters because self-containment rules out pulling in a general hashing library for this one. Deletion is the part every from-scratch open-addressing implementation gets wrong on the first attempt: you cannot just mark a removed slot "empty", because a later lookup for a different key that collided past that slot during insertion would stop probing at the first empty slot it sees and report the key missing even though it is still there, two slots further along. Remove() marks the slot a tombstone instead - "was occupied, keep probing past me, but available for reuse on the next insert". The demo proves this directly: it removes a key from the middle of a probe chain and confirms the keys after it in that chain are still found. The table grows itself. Set() checks the load factor before every insert and doubles the bucket count once it would exceed 70%, rehashing every live key into the new table. Growth happens before the insert that would trigger it, which is what guarantees FindSlot always has room to place the new key during that same call. Values are plain doubles, not a generic type - neither platform has templates, and double is what covers the overwhelming majority of trading use: prices, volumes, running totals keyed by symbol or magic number. TryGet uses a found/not-found return value rather than a sentinel value for "missing", because a sentinel like -1 or 0 cannot be distinguished from a genuinely stored value of -1 or 0 - the demo stores a real 0.0 and confirms it still reads back as present. Notes and limitations - Keys are case-sensitive. "EURUSD" and "eurusd" are different keys; the demo shows both stored side by side. - GetAllKeys() returns keys in bucket order, which has no relationship to insertion order and can change after a resize. Do not rely on it for anything ordered. - The table never shrinks. Removing many keys frees slots for reuse by future inserts but does not reduce BucketCount() or reclaim memory - build a fresh CHashMap if a program needs to release capacity after a large, one-off burst of keys. - FNV-1a is a fast, well-distributed hash for short ASCII-ish keys, which covers symbol names and label strings, but it is not a cryptographic hash and must never be used for anything security-sensitive. - Not thread-safe, which matches both platforms' single-threaded-per-chart execution model; nothing here changes that.
Script · MT5 · v1.0 · Free
About Hash Map
Hash Map is a script for MetaTrader 5. It shows several symbols in one panel. It has 2 settings, such as Initial buckets and Growth inserts. MQL source code included.
Files in the download: HashMap.mq5.mqh, HashMap.txt, HashMapDemo.ex5, HashMapDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Initial buckets | 8 | Starting bucket count for the demo map |
| Growth inserts | 40 | Number of keys inserted to force at least one resize |
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: Dashboards & Scanners · MT5 Script · MQL5 Source Code