Order Comment Codec
MT4's comment field looks like free text but is not reliable free text in practice. Some broker servers truncate it below the terminal's own limit, some append their own suffix (an account tag, a dealing-desk marker), and a few strip specific characters. Code that stuffs a raw "signal=42;gen=7" string into the comment and trusts it back byte-for-byte eventually reads a corrupted value as if it were genuine data - a signal id silently becomes a different signal id, a generation counter silently becomes a different generation, and nothing in the code notices. OccEncode() packs a tag plus up to eight key=value pairs into one compact string ("tag|k1=v1,k2=v2*C") with a checksum character appended after a "*" marker. If the encoded result would not fit within the configured length limit (31 characters by default, MT4's practical comment ceiling across brokers), whole fields are dropped from the end rather than cutting a field in half - a half-written field would decode into plausible-looking garbage instead of failing visibly, which defeats the point of having a checksum at all. OccDecode() reverses the process and verifies the checksum. A mismatch means the comment was altered somewhere between encoding and reading it back - by truncation, by corruption, or by anything else outside this code's control - and the caller gets a clear false rather than a silently wrong value. When the checksum fails, outTruncated gives a best-effort signal for whether the string looks like it was simply cut short (no "*" marker present, or the string exactly fills the length limit with no room the marker could have occupied), useful for logging which failure mode was more likely. OccEncodeSingle() and OccGetField() cover the common case of one tag and one field - tagging a ticket with a single signal id - without the caller having to build one-element arrays for it. Notes and limitations - The checksum defends against accidental truncation or corruption, not against a hostile actor deliberately forging a comment - it is a simple reduced Fletcher-style sum, not a cryptographic hash. - The default 31-character limit is a practical, commonly-cited MT4 ceiling, not a value read from the terminal - some brokers allow more, a few allow less. Pass a smaller maxLen explicitly if a specific broker is known to truncate earlier. - OccDecode()'s outTruncated flag is a heuristic, not a certainty. A broker that strips exactly the "*" marker character (rather than truncating the string's length) produces a checksum failure with outTruncated left false, since the string is not short - this is called out rather than silently misclassified as "not truncated, so probably fine". - Field values themselves must not contain "|", "," , "=" or "*" - those are the codec's own delimiters. Encode numeric or short alphanumeric payloads; do not attempt to pack arbitrary free text through this codec.
Script · MT4 · v1.0 · Free
About Order Comment Codec
Order Comment Codec is a script for MetaTrader 4. MQL source code included.
Files in the download: OrderCommentCodec.mqh, OrderCommentCodec.txt, OrderCommentCodecDemo.mq4.
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: Buy/Sell Arrows · MT4 Script · MQL4 Source Code