Structured GV Shared State
GlobalVariables are the one channel every program on a terminal shares without files or a DLL, but used directly they are just a flat pile of loose double-valued names with no structure and no way to tell "did anything change" short of re-reading every field you care about on every tick. This library adds three things on top of the raw mechanism. First, namespacing: Init() with a prefix (the same GV_PREFIX + "Name" convention already used elsewhere in this catalogue), and every Set/Get call operates on "<prefix><field>" without the caller ever building the string by hand. Second, types: Double/Int/Bool/Time accessor pairs, all genuinely doubles under the hood since that is all a GlobalVariable can hold, but typed at the call site instead of every caller casting by hand and risking a mismatch. Third, change signalling: every Set/Delete bumps one "__EPOCH" counter GlobalVariable per namespace, and HasChanged() lets a subscriber compare a remembered epoch against the current one with a single GlobalVariableGet() - cheap enough to call every tick, unlike re-reading a whole field set to notice one changed value. Because GlobalVariables have no native string type, this library does not attempt to fake one - there is no SetString/GetString, and the notes below say so plainly rather than shipping a fragile per-character encoding on top of a mechanism that was never meant to carry text. Notes and limitations - No string field type exists, by design - GlobalVariables cannot hold one. Encode small enumerations as Int fields instead. - EnumerateFields() and ClearNamespace() scan every GlobalVariable on the terminal filtering by prefix - fine for an operator "dump state" call or test teardown, not meant for a hot per-tick path. - GlobalVariables are per-terminal. Two terminals, even on the same machine, do not share a namespace - this is not a cross-terminal mechanism. - HasChanged() reports "something in the namespace changed", not which field. A caller that needs to know exactly what changed still has to re-read the specific fields it cares about after a positive result.
Script · MT4 · v1.0 · Free
About Structured GV Shared State
Structured GV Shared State is a script for MetaTrader 4. It has 1 setting, such as Namespace. MQL source code included.
Files in the download: StructuredGvSharedState.txt, StructuredGvSharedStateDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Namespace | SGVDEMO_ | Namespace prefix shared by publisher and subscriber |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Channels & Envelopes · MT4 Script · MQL4 Source Code