Streaming CSV Parser
The obvious way to read a CSV file in MQL4 is FileReadString() once per physical line, then StringSplit() on the comma. That breaks the moment a field legitimately contains a comma, a double quote, or a newline, which RFC 4180 explicitly allows inside a double-quoted field - an address field like "123 Main St, Apt 4" or a multi-line note field silently shreds the rest of that row and every row after it, since the reader no longer agrees with the writer about where rows begin and end. This library reads the file in FILE_BIN mode, one byte at a time, through a proper quoting-aware state machine: outside quotes a comma ends a field and CR/LF ends a row; inside quotes both are literal field content; a doubled quote ("") inside a quoted field collapses to one literal quote and stays in quoted mode; a single quote ends quoted mode. FILE_BIN is not optional - FILE_TXT mode lets the platform normalise line endings before the parser ever sees them, which would defeat the embedded-newline handling this library exists to provide. "Streaming" means ReadRow() returns exactly one logical row at a time and never buffers more than the row currently being assembled - a file with a million rows parses in constant memory, not by loading the whole file into one giant string first. The one documented edge case: MQL4 file handles have no ungetc, so a bare CR not immediately followed by LF (a legacy classic-Mac line ending, not CRLF or plain LF) loses the one byte that followed the CR while resolving whether it was part of a CRLF pair. Ordinary CRLF, plain LF, and CRLF inside quoted fields are all unaffected - only a genuinely bare, isolated CR line ending is impacted, which real-world CSV exports essentially never produce. Notes and limitations - A quote character is only recognised as opening a quoted field when it is the very first byte of that field; a quote appearing mid-field after ordinary text is treated as a literal character, matching common real-world CSV writer behaviour rather than strict RFC pedantry. - A bare CR not followed by LF loses the one byte immediately after it (see FULL DESCRIPTION) - a genuine limitation of MQL4's handle-based file I/O, which has no way to push a read-ahead byte back onto the stream. - This is a reader only. Writing CSV correctly (quoting fields that need it) is a much smaller problem and is not in scope here. - Delimiter is a single character, configurable via Open()'s third argument; multi-character delimiters are not supported.
Script · MT5 · v1.0 · Free
About Streaming CSV Parser
Streaming CSV Parser is a script for MetaTrader 5. It has 1 setting, such as File name. MQL source code included.
Files in the download: StreamingCsvParser.mq5.mqh, StreamingCsvParser.txt, StreamingCsvParserDemo.ex5, StreamingCsvParserDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| File name | StreamingCsvParserDemo.csv | Sandbox filename used for the round trip |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT5 Script · MQL5 Source Code