Loading…

CSV Reader And Writer

The terminal's built-in FILE_CSV mode looks like the obvious way to read and write CSV, and for the simplest cases it is fine. The trap is that its field tokenizing is tied to Windows regional settings - on a machine configured with a comma as the decimal separator, the terminal itself switches the CSV field delimiter to a semicolon, silently, with no code change. An EA that writes a trade log with FILE_CSV on the developer's machine can produce a differently-delimited file on a user's machine running the same binary, which then fails to parse anywhere it is opened with an explicit delimiter assumption. This header sidesteps that entirely by opening the file as plain text (FILE_TXT) and doing the quoting and delimiting itself, so behaviour is identical on every machine regardless of locale. Quoting follows RFC 4180: a field is wrapped in double quotes only when it actually contains the delimiter, a quote character, or a line break, and an embedded quote is doubled - the same convention Excel and every other spreadsheet tool both read and write, so files produced here open correctly outside MetaTrader too. CsvBuildLine() / CsvParseLine() are also exposed standalone, for anyone who wants to build or parse a line without opening a file at all - useful for a single-row log line inside Print(), or when the "file" is actually a string coming from WebRequest. Notes and limitations - Files are opened without FILE_SHARE_READ/WRITE flags, matching the rest of this catalogue's file-handling libraries - two programs writing the same file at the same time is not supported. - CsvCountRows() counts line reads, not validated rows; a file with an inconsistent number of fields per line will still report a row count, it will just be the caller's job to notice the mismatch when reading. - Quoting handles embedded delimiters, quotes and CR/LF correctly per RFC 4180. It does not attempt to interpret or convert numeric or date formats - every field is a string in and a string out, exactly as read from the file. - CsvOpenWrite() truncates any existing file with the same name; there is no append mode. Read the existing rows first with CsvOpenRead() if they need to be preserved.

Script · MT4 · v1.0 · Free

About CSV Reader And Writer

CSV Reader And Writer is a script for MetaTrader 4. It has 1 setting, such as Csv file name. MQL source code included.

Files in the download: CsvFile.mqh, CsvFile.txt, CsvFileDemo.mq4.

Settings

SettingDefaultWhat it does
Csv file nameCsvFileDemo_output.csvFile written under MQL4\Files\

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: MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria