Loading…

Declarative Struct Codec

A custom binary file format - a compact trade log, a cache of computed values, an export another program will read - is normally written with one function doing the FileWriteInteger/FileWriteLong/FileWriteDouble/ FileWriteString calls in order, and read back with a second, separately hand-written function calling the matching FileReadInteger/FileReadLong/ FileReadDouble/FileReadString calls in the same order. The two inevitably drift the first time a field is added, removed, or reordered and only one side gets updated - a subtle, often silent corruption since the read side still succeeds, it just reads the wrong bytes into the wrong fields from that point in the record onward. This header describes a record's layout once, as an array of DscFieldSpec (name, type, and a byte size that only matters for the fixed-length string type). DscPackRecord and DscUnpackRecord both walk the same schema array in the same order, so there is exactly one place the layout is defined, and write/read can never see a different version of it. Every field value crosses the API as a plain string and is converted to/from its declared binary type at the point of packing or unpacking, so building a record does not need a different function call per field type. DscRecordByteSize and DscSeekToRecord support treating a file of same-schema records as a simple fixed-stride array on disk - jump straight to record N with one FileSeek rather than reading every earlier record first. Notes and limitations DscPackRecord refuses to write anything (returns false) if schema[] and values[] are different lengths, rather than writing a partial record that would desync every field read after the mismatch point - always keep the two arrays the same length and order. Field values travel through this API as strings; a numeric field given non-numeric text converts via StringToInteger/StringToDouble, which return 0 for unparseable text with no error raised - validate values against their declared type before packing if that matters. DSC_TYPE_STRING truncates a value longer than its declared byte size and zero-pads a shorter one, the same convention FileWriteString itself uses in FILE_BIN mode; the size is a fixed on-disk budget, not a variable-length field. DscSeekToRecord assumes every record in the file was written under the exact same schema - it computes a plain recordIndex * DscRecordByteSize(schema) offset with no per-record length prefix or validation, so seeking into a file that mixed schema versions (after a later version of a project changed the field layout) reads garbage rather than failing loudly. This header has no versioning or migration support of its own; add a fixed-size version field as the first schema entry if a project needs to detect a layout change later.

Script · MT4 · v1.0 · Free

About Declarative Struct Codec

Declarative Struct Codec is a script for MetaTrader 4. It shows several symbols in one panel and is set up for gold (XAUUSD). MQL source code included.

Files in the download: DeclarativeStructCodec.mqh, DeclarativeStructCodec.txt, DeclarativeStructCodecDemo.mq4.

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 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: Binary Options · Dashboards & Scanners · Gold (XAUUSD) · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria