Download MetaTrader 5

Help Writing to .CSV file.

To add comments, please log in or register
JPS1
244
JPS1  

I can write files to say a .dat file, but I'm looking to write to .csv, and to sort data into different columns and cells.

I'm also looking to be able to retrieve "individual cell" data through my EA.


Can anyone help, point me in a direction, or provide a sample for me?


Thank you!

JPS1
244
JPS1  

Just checking in for replies and needing to bump the post.


I've searched for tips on reading and writing to .csv files, but can find the info related to my question.

cloudbreaker
1577
cloudbreaker  

Here are a couple of functions I've written for you.
CB


//+------------------------------------------------------------------+ 
bool fnReadFile()
 {
  iHandle = FileOpen(sFileName,FILE_CSV|FILE_READ);
  if(iHandle < 1)
   {
    iErrorCode = GetLastError();
    if (iErrorCode == 4103)
     Print("File not found");
    else
     Print("Error reading file: ",iErrorCode);
    return(false);
   }
  iInteger = StrToInteger(FileReadString(iHandle));
  sString = FileReadString(iHandle);
  dDouble = StrToDouble(FileReadString(iHandle)); 
  FileClose(iHandle);
  return(true);  
 }
//+------------------------------------------------------------------+  
bool fnWriteFile()
 {
  iHandle = FileOpen(sFileName,FILE_CSV|FILE_WRITE);
  if(iHandle < 1)
   {
    iErrorCode = GetLastError();
    Print("Error updating file: ",iErrorCode);
    return(false);
   }
  FileWrite(iHandle,iInteger,sString,DoubleToStr(dDouble,iDecimalPlaces));
  FileClose(iHandle);
  return(true);
 }
//+------------------------------------------------------------------+
JC
1456
JC  
cloudbreaker:

if(iHandle < 1)

A piece of almost unbelievable pedantry...


The documentation doesn't say this (https://docs.mql4.com/files/FileOpen). It says that the return value is -1 if the function fails. According to the documentation, -25356 could be a valid file handle.


Moving from pedantry to real life, I would actually be a tiny bit cautious about this. FileOpen() in MQ4 is not a simple wrapper around the CreateFile() function in the Win32 API. However, the return value from CreateFile() is INVALID_HANDLE_VALUE if the operation fails. INVALID_HANDLE_VALUE is -1; all other values such as -25356 can be potentially valid file handles - and very occasionally are. Therefore, depending on what MT4 is doing internally, negative signed integers could potentially represent valid file handles.

cloudbreaker
1577
cloudbreaker  

Ha ha. I like it jic.

I've been using that handle check in various EAs for over a year with not a single file access failure. I'm confident it'll work just fine.

We could start a race to see who's first to get a -ve file handle? :-)


CB

JPS1
244
JPS1  
cloudbreaker:

Here are a couple of functions I've written for you.

CB



This helps. Thank you so much!

pc-wiz
33
pc-wiz  

I want to append to an existing .CSV file. I tried FILE_CSV|FILE_WRITE|FILE_READ but it didn't work. anyone done that yet?

gordon
2326
gordon  
BillR:

I want to append to an existing .CSV file. I tried FILE_CSV|FILE_WRITE|FILE_READ but it didn't work. anyone done that yet?

Yes, that's exactly how to do it. It might not work for u if that file is already opened by another process in a mode that does not allow file sharing. So please check that the file is not opened by another process (either in metatrader or any other windows program). You can also check experts/journal log for any specific errors...

Luis
226
Luis  

Hello I have using this code:


void WriteCSV() {
   int handle = FileOpen( StringConcatenate( Symbol(), ".csv" ),
                          FILE_CSV|FILE_READ|FILE_WRITE, ','
                          );

   if ( handle > 0 ) {
        FileSeek( handle, 0, SEEK_END );

        FileWrite( handle,
                   TimeToStr( TimeCurrent(), TIME_DATE )
                   );
        FileWrite( handle,
                   "hello" );

        FileClose( handle );
      }


The second FileWrite writes in the next row. How could I do it to make it write in the next row? In excel would be B1 instead of A2?


THank you


BeLikeWater

Carl Schreiber
6692
Carl Schreiber  
BeLikeWater:

Hello I have using this code:


void WriteCSV() {
   int handle = FileOpen( StringConcatenate( Symbol(), ".csv" ),
                          FILE_CSV|FILE_READ|FILE_WRITE, ','
                          );

   if ( handle > 0 ) {
        FileSeek( handle, 0, SEEK_END );

        FileWrite( handle,
                   TimeToStr( TimeCurrent(), TIME_DATE )
                   );
        FileWrite( handle,
                   "hello" );

        FileClose( handle );
      }


The second FileWrite writes in the next row. How could I do it to make it write in the next row? In excel would be B1 instead of A2?


THank you


BeLikeWater

please read the doc of FileWrite - just the first line. So what do you have to do to put a bunch of numbers in one line?
To add comments, please log in or register