Download MetaTrader 5

Sorting CSV to Array.

To add comments, please log in or register
GrumpyDuckMan
590
GrumpyDuckMan  
void ReadFile_Array()

  {
   FileHandle=FileOpen(subfolder+"\\TestCSV.csv",FILE_READ|FILE_WRITE|FILE_CSV);
   if(FileHandle!=INVALID_HANDLE)
     {
      while(!FileIsEnding(FileHandle))
        {
         str=FileReadString(FileHandle,FILE_READ|FILE_CSV);

        }
     }
   for(Separator=",";str!=Separator;++c)
     {
      a_str=str;
     }

   FileClose(FileHandle);
  }

Hello everyone,

I have been trying to work with CSV files. I have some of the very basics working, such as read and write. I would like to add data from a CSV file into a dynamic array.

My aim is to sort data looking something like this.

"2017.05.19,2:15,1247.55,1248.42,1247.44,1248.26,761"

to look like this..

DATE:2017.5.19

TIME:2:15

How should I create a program flow that removes  "," between the data I wish to send to array elements?

Carl Schreiber
Moderator
7953
Carl Schreiber  

I don't use mql's csv-file-reading/writing. I use this to manage csv-files as this is easier to manage both: lines and cells:

   string fName = "MyFile.csv",  //my line Formate: "str1;str2;str3;..strLast;\n" even last element ends with ";" to catch both EOL: \r\n and \n
          all,lines[],cell[];
   ushort SepLine = StringGetCharacter("\n",0),
          SepCell = StringGetCharacter(";",0);
   
   int nL,i,nC,c,hdl = FileOpen(fName,FILE_READ|FILE_WRITE|FILE_BIN|FILE_COMMON); // |FILE_COMMON: a csv-file in the common folder
   if (hdl<0) {  
         Alert("Can't open ",fName,", err: ",_LastError);
         return(..);
   }
   sz = (int)FileSize(hdl); 
   all = FileReadString(hdl,sz); // read complete file
   nL = i = StringSplit(all,SepLine,lines); // split into lines => \n is eliminated!!
   while (i-->0) { // read from last to first line
      // manage one line
      int nC = c = StringSplit(lines[i],SepCell,cell);
      if (nI<3) continue; // skip empty lines
      while(c-->0) {
         // manage each one cell[c]
         // here you add each cell to your array that you want to sort
         // have in mind you can even sort string-arrays
         ...
      }      
   }
   // now re-write file completely
   FileSeek(hdl,0,SEEK_SET); // re-write the file, set the point at the beginning 
   for(i=0;i<ArraySize(lines);i++) {
      sz = StringLen(lines[i]+"\n");
      if (sz<5) continue; // skip empty lines
      FileWriteString(hdl,lines[i]+"\n",sz);
   }
   FileClose(hdl);

(not tested as it taken and modified from longer function)!

whroeder1
17084
whroeder1  
GrumpyDuckMan: How should I create a program flow that removes  "," between the data I wish to send to array elements?
  1. FileHandle=FileOpen(subfolder+"\\TestCSV.csv",FILE_READ|FILE_WRITE|FILE_CSV);
    
    What is the default separator for FileOpen and what is in the file.
  2. str=FileReadString(FileHandle,FILE_READ|FILE_CSV);
    
    What is the second argument to FileReadString and what are you passing?
  3. Just write it
    while(!FileIsEnding(FileHandle){
       do{
          str=FileReadString(FileHandle);
          // Do something with str
       while(!FileIsLineEnding(FileHandle) );
       // Do something at EOL
    }
    // EOF
    

  4. "2017.05.19,2:15,1247.55,1248.42,1247.44,1248.26,761"

    to look like this..

    DATE:2017.5.19

    TIME:2:15
    I would create a structure to do everything and have an array of it
    struct Data{
       datetime mTime;
       double   mPrice1;
       double   mPrice2;
       double   mPrice3;
       int      mCount;
       void read(int h){
          string date=FileReadString(h); 
          string time=FileReadString(h); 
          mTime = StrToTime(date+" "+time);
          mPrice1 = FileReadDouble(h);
          :
       }
    );

  5. As for sorting, see Sort multiple arrays - MQL4 and MetaTrader 4 - MQL4 programming forum
GrumpyDuckMan
590
GrumpyDuckMan  

Hello everyone,

I would like to thank you both of you of your time,assistance and advise. Unfortunately I am now totally confused about which approach to take. I however like the approach whroeder1 put forward.

Currently I started a new EA to deal with storing data from my CSV file... I'm doing more research on using arrays atm.

GrumpyDuckMan
590
GrumpyDuckMan  

Hello everyone,

I have started working on the approach whroeder1 posted. I haven't had much time over the weekend to do much with the program. The code is based around StringSplit example..


struct CSV_Data
  {
   string   TimeStamp,
            Open_Price,
            Close_Price,
            Highest_Price,
            Lowest_Price,
            Vol;
  }
Detail;

void Test_Function_A()
  {
    
   name1=StringGetCharacter(Separator,0);
    name2=StringSplit(name,name1,result);
   if(name2>0)
     {
      for(int name3=0;name3<name2;name3++)
        {
       Detail.TimeStamp=result[name3]; 
        }
     }
  }
To add comments, please log in or register