Reading a CSV file into an EA

Hi all!

Thank you for looking at this thread. I’m having an issue at the moment of trying to import a matrix from a CSV file into a MetaEditor script. The file below is what the CSV file looks like in MetaEditor. What piece of code could I write to efficiently import these numbers into an array which has 10 rows and 12 columns? Thank you so much!


What have you tried ?

Reading a csv file into an array is simple.

However, you need to write the code to do it.

Show us what you have tried, and you’d probably get a little more help.

Documentation on MQL5: File Functions Documentation on MQL5: File Functions
For security reasons, work with files is strictly controlled in the MQL5 language. Files with which file operations are conducted using MQL5 means cannot be outside the file sandbox. the common folder for all the terminals installed on a computer - usually located in the directory C:\Documents and Settings\All Users\Application...

Thank you guys for the speedy reply. I’m quite new to MQL5 so I’m a bit confused with the syntax and functions. This is what I wrote so far but unfortunately I cannot seem to store the matrix into an array. The EA won’t print out the individual element when I ask it too.

void OnTick()
  double arr[];
int file_handle=FileOpen("buy_GBPvsAUD.csv",FILE_READ|FILE_CSV);
      //--- read all data from the file to the array
      //--- receive the array size
      int size=ArraySize(arr);
      //--- print data from the array
Print("Array size = "+arr[2][3]);

Try adding error handling to your code like the example on this page.

It may be as simple as it cannot find the file.

You’re storing “buy_GBPvsAUD.csv” in \Terminal\Common\Files\ right?

Thank you so much for the reply! It does print an error 5004. I’ll look into it but I did store the file in the write folder :slight_smile:

I forgot to mention, I’m using MQL5, I don’t know if that makes any difference or not.


Reads from a file of BIN type arrays of any type except string (may be an array of structures, not containing strings, and dynamic arrays).

CSV files are not BIN files and you are trying to read strings in array(s)

Sorry I don’t follow. My csv file only contains numbers and not strings. I don’t understand what a BIN type of array is. Would you be able to explicitly write the code to perform this operation, if that is not too much to ask? Thank you. :slight_smile:

CSV file is a string (simple textual) file - does not matter if you use “only” numbers - some more information on what string is (and what it is not) you can find here :

In the hep description of FileReadArray() you can follow what “BIN” files are and how you can use them

Save the data using bin file format and FileSaveArray() and then use again that same bin file and use FileReadArray() and ll will work

Hi again,

So I saved the file as a BIN file and then used the same code as before however when trying to print the value of the first element to the screen I got the following,

2018.06.07 11:11:42.517	temp_EA (EURNZD,M15)	1.426707300392364e-71

where this tiny number came from I don’t know but on the next tick I got the array is out of range error.

Sorry to keep asking you for help I just can’t seem to be able to get my head around it! Now I have a bin file, how would you write a script which prints out an element from the array?