Download MetaTrader 5
To add comments, please log in or register
Do you know that MQL5 has event handling?
JPS1
239
JPS1 2010.01.12 13:49 

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
239
JPS1 2010.01.13 13:22  

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
1574
cloudbreaker 2010.01.13 14:59  

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
1389
JC 2010.01.14 03:01  
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
1574
cloudbreaker 2010.01.14 09:19  

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
239
JPS1 2010.01.14 09:53  
cloudbreaker:

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

CB



This helps. Thank you so much!

pc-wiz
12
pc-wiz 2010.01.30 07:49  

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
2322
gordon 2010.01.30 09:49  
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 2014.10.25 17:47  

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
5927
Carl Schreiber 2014.10.26 07:48  
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