Loading…

Hashing Toolkit

A hash table, a change-detection digest or a composite cache key all need the same underlying tool: turn some value into a well-spread integer. This header covers strings (FNV-1a 32 and 64-bit, and djb2 as a second independent algorithm), plain integers (an avalanche mix so sequential ticket numbers and magic numbers do not cluster into the same bucket under a naive modulo), and doubles. The double case is the one worth reading the source for. Hashing a double by formatting it to a string first is a common shortcut, and a fragile one - two doubles that compare exactly equal in code can still format differently depending on requested precision, and the terminal's default DoubleToString precision of 8 decimal places silently discards bits that can matter for an exact equality or change-detection check. HashDouble() reads the value's raw 8 bytes through a union instead, the same technique C and C++ hash-of-bytes code uses, so the hash reflects exactly what MetaTrader has stored rather than a rounded text rendering of it. The demo shows two doubles that differ far past the 8th decimal place hashing differently while their DoubleToString(x, 8) text representations are identical. HashCombine() folds several hashed fields - a symbol's hash, a timeframe, a shift - into one composite key without concatenating strings first, and HashDoubleArray() extends that into a cheap whole-array digest for "did this price or indicator buffer change since I last checked" without a full CRC. Notes and limitations - None of these are cryptographic hashes. They are fast and well-spread for hash tables, cache keys and change detection, not suitable for anything where an adversary might deliberately try to engineer a collision. - FNV-1a32 and djb2 both operate on character codes read via StringGetCharacter(), which covers ASCII and the Basic Multilingual Plane on both platforms; full Unicode surrogate pairs are not specially handled. - HashDouble() depends on the union reading the double's in-memory representation, which assumes the terminal is compiled for the standard IEEE 754 little-endian layout - true for every current MetaTrader build on either platform, not guaranteed to be portable to a hypothetical different platform. - HashUInt()'s constants are MurmurHash3's published finalizer constants, reused here rather than invented, since a well-tested avalanche mix is not something worth re-deriving from scratch.

Script · MT5 · v1.0 · Free

About Hashing Toolkit

Hashing Toolkit is a script for MetaTrader 5. MQL source code included.

Files in the download: Hashing.mq5.mqh, Hashing.txt, HashingDemo.ex5, HashingDemo.mq5.

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 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: MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria