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!

Bart 

0.59119,0.91302,-0.24845,-1.4449,-1.0474,-0.19321,0.86771,1.2237,2.7086,1.909,-0.05177,0.44011
0.31486,3.2801,-2.7768,1.334,-0.62165,-1.5147,-0.3493,2.4592,4.3133,0.1713,-3.2866,0.48743
2.0176,-1.7724,0.1237,0.10214,-0.41129,3.3639,-0.19308,1.7814,1.0445,0.47436,-1.1098,-1.2311
2.4973,-1.2728,0.68445,1.8011,-1.893,-3.49,-1.6895,0.49105,-0.22979,-0.11506,-3.0139,2.653
-1.422,0.20207,-1.7585,-0.00018628,-0.28835,0.24825,-4.237,0.45852,-1.9619,1.279,2.7651,0.30175
-0.23272,2.3705,0.06,-0.31937,-0.45936,-0.16604,-0.34425,-0.61509,-0.14743,-0.78805,0.90661,0.091302
1.2191,0.92969,3.3985,-0.37366,1.9945,-0.41439,-0.94896,-1.5174,-0.99342,-1.3067,1.3398,0.78294
1.1661,-0.4661,0.084859,3.0469,0.59137,-4.3791,-1.2947,1.873,-2.4296,-1.4597,-1.7482,-2.1998
0.64769,-0.23214,0.79859,-1.4505,1.3397,-2.8848,-0.94848,0.15517,0.28185,-0.18656,0.90189,-0.41948
-1.4782,-3.8424,0.025689,0.48499,-1.354,0.64871,0.42047,-0.26407,0.42064,0.00014723,-0.29586,-0.088966
 
Bartlomiej Winter:

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!

Bart 

What have you tried ?
 
Bartlomiej Winter:

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!

Bart 

Reading a csv file into an array is simple.

https://www.mql5.com/en/docs/files

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
  • www.mql5.com
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);
   if(file_handle!=INVALID_HANDLE)
     {
      //--- read all data from the file to the array
      FileReadArray(file_handle,arr);
      //--- receive the array size
      int size=ArraySize(arr);
      //--- print data from the array
     }
Print("Array size = "+arr[2][3]);
  }
//+------------------------------------------------------------------+

 
 
Bartlomiej Winter:
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.
 

Try adding error handling to your code like the example on this page.
https://www.mql5.com/en/docs/files/filereadarray

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

You're storing "buy_GBPvsAUD.csv" in  \Terminal\Common\Files\ right?

 
Anthony Garot:

Try adding error handling to your code like the example on this page.
https://www.mql5.com/en/docs/files/filereadarray

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 :)

 
Anthony Garot:

Try adding error handling to your code like the example on this page.
https://www.mql5.com/en/docs/files/filereadarray

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

You're storing "buy_GBPvsAUD.csv" in  \Terminal\Common\Files\ right?

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

 
Bartlomiej Winter:
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.
 

FileReadArray

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)

 
Mladen Rakic:

FileReadArray

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. :)

 
Bartlomiej Winter:

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. :)

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 : https://en.wikipedia.org/wiki/String_(computer_science)

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

 
Mladen Rakic:

CSV file is a string 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 : https://en.wikipedia.org/wiki/String_(computer_science)

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?