Loading…

Fixed Width Record Codec

CSV is not universal - a number of legacy back-office exports, some broker statement formats, and interfaces descended from mainframe-era systems still use fixed-width columns instead: every field occupies an exact number of characters, padded with spaces or zeros, with nothing marking where one column ends and the next begins. Hand-writing the encode and decode sides separately for a format like this is exactly how the two drift apart the first time a column width changes and only one side gets updated. FwcFieldSpec describes one column's layout - name, width, alignment, and pad character - once. The same schema array then drives both directions: FwcEncodeRecord builds a fixed-width line from a values[] array, padding and truncating each field to its declared width; FwcDecodeRecord splits a line back into the same values[] shape, trimming each field's padding from whichever side it was added on (left for a right-aligned/numeric field, right for a left-aligned/text field), using the field's own declared pad character rather than generic whitespace - a field whose real content legitimately starts or ends with a space is not corrupted by the trim. FwcWriteRecord/FwcReadRecord are thin file-handle convenience wrappers, kept deliberately off FILE_CSV mode, since CSV's automatic delimiter and quoting behaviour would corrupt a fixed-width payload that can legitimately contain commas or quote characters as ordinary field content. Notes and limitations A value longer than its field's declared width is truncated from the right on encode rather than overflowing the record - callers that must never lose data should check StringLen(value) against the field width themselves before calling FwcEncodeField/FwcEncodeRecord. FwcDecodeRecord returns false without useful output if the input line is shorter than the schema's total width (a truncated or corrupt record), but does not otherwise validate field content - a numeric column containing non-digit text decodes as that literal text with no error, since the codec has no concept of a column's semantic type, only its width and alignment. FwcEncodeRecord/FwcDecodeRecord operate on a single line/record at a time; multi-record files are handled by calling them in a loop, as the demo's file-write/file-read sections do. No character-encoding conversion is performed beyond what FileOpen's own FILE_ANSI/FILE_UNICODE flags already provide - keep encode and decode on the same flag choice for a given file.

Script · MT4 · v1.0 · Free

About Fixed Width Record Codec

Fixed Width Record 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: FixedWidthRecordCodec.mqh, FixedWidthRecordCodec.txt, FixedWidthRecordCodecDemo.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: Dashboards & Scanners · Gold (XAUUSD) · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria