Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

CSV file reader for MQL5 - MetaTrader 5 Library | MT5 EA Download - MetaTrader 5 Resources

author EAcpu | 3 reads | 0 comments |

1. Set the column description array in the MqlParam array.

  CCsvReader infile; 
Message parameter example_column_desc[] =
  {
    { TYPE_STRING , 0 , 0.0 , "Token" }, // 1. The field is a string { Type_Double , 0 , 0.0 , "Contract Size" }, // 2. The field is a double { Type_DateTime , 0 , 0.0 , "Expired" } // 3. The field is a datetime }; 
  infile.FieldDesc(example_column_desc);


2. Use the Open() function to open the file

  internal files. Open (Inp file name, file reading );

2a. When the file has a header and you are not interested in the contents of the header line, use Readline(void) to skip it.

 infile.ReadLine();


3. Using ReadLine(MqlParam arr), the MqlParam array will be filled with the values ​​of one text line of the CSV file.

 message parameter data line[];
Integer field reading;
  fieldsread = infile.ReadLine(data line);

Disadvantages of this class

  • This code is not very fast
  • Currently only Unicode files are processed. Single-byte files are not a development priority.
  • This code might read a line and notice that EOF is reached at the end of the line. If possible do not use IsEnding() for testing, instead use the return value of the ReadLine function. The return value will only return FILE_STATE_EOF after the next read. So all records will be processed.

Use samples

I've provided a LibreCalc spreadsheet containing 3 sheets of paper. Here is an example of calculating some orders of a pyramid system. The first worksheet is exported as a CSV file via "Save as...". Use the tab-delimited CSV file format and use Unicode. Place the CSV file into your \Files directory. The script Sample.mq5 will read the CSV file and print the values ​​of the CSV file.


Attachment download

📎 sample.mq5 (3.59 KB)

📎 csvreader.mqh (19.16 KB)

📎 enum_file_state.mqh (0.9 KB)

Source: MQL5 #24777

Verification code Refresh