Looking for a way to allow MQL4 EA to write to file while file is being viewed.

To add comments, please log in or register
Connorc1231
14
Connorc1231  

Hello MQL4 community,

I am developing an Expert Adviser to gather bar data from a chart (such as Open, High, Low, etc.) in order to export the data into a csv file. A snippet of the code below performs the functions to open the file, seek the end, append data, and close the file when it's complete. 

   // Open file
   fileHandlerH1 = FileOpen(H1FileName, FILE_CSV|FILE_READ|FILE_WRITE|FILE_SHARE_READ|FILE_SHARE_WRITE, ",");
   
   // Find end of file to append
   if(FileSeek(fileHandlerH1, 0, SEEK_END)) {
      // Append data to file
      FileWrite(fileHandlerH1, H1Time, H1Open, H1High, H1Low, H1Close, H1Volume, H1EMA13, H1EMA21);
   }
   
   // Close file
   FileClose(fileHandlerH1);

The above code works fine assuming the csv file is left alone while the EA is running, or while I perform a backtest. However, when I have the csv file open in Excel, for example, the program fails to open the file and no data is appended. From my understanding of the "FILE_SHARE_READ" and "FILE_SHARE_WRITE" flags, this should allow multiple programs to share read and write capabilities with the program but in such a case, the "fileHandlerH1" returns a value of -1 and the above snippet of code doesn't execute properly. 

I am wondering if my understanding of the "FILE_SHARE_READ" and "FILE_SHARE_WRITE" is wrong? Is there a different way to allow an MQL4 EA to open a file for writing while a different program is viewing it?

My end goal with this is to have a separate Python script read the data from the csv to perform it's own calculations and algorithms. My concern is that Python will not be able to keep the file open to detect changes and preform calculations based on those changes. I have a slight workaround in mind at a slight inconvenience but if there is a way to get this sharing of files to work, I would like to use that. 

Keith Watford
Moderator
14428
Keith Watford  

It is not often that I use files in my codes, so I always have to check the documentation so I may not be much help.

 but in such a case, the "fileHandlerH1" returns a value of -1 and the above snippet of code doesn't execute properly. 

How do you know? I don't see where you check it and what was the last error?

If you have the csv file open, it won't show any updated data until it is closed and re-opened in Excel (if I remember correctly)

Connorc1231
14
Connorc1231  

Hi Keith,

Thanks for replying. The part where I noticed "fileHanderH1" returns a value of -1 is if I Print(fileHandlerH1). That line isn't in the above code but I tested it and that's what I received. I went ahead to GetLastError() at the point where it attempts to open the file and I get error code 5004, which per the runtime errors table means it "cannot open file". 

Closing the csv file afterwards regardless of the error and opening it up again doesn't work, as expected since there is an error in opening the file. For some reason when I am viewing the csv in excel, MQL4 cannot open the file even if I added the "FILE_SHARE_READ" and "FILE_SHARE_WRITE". 

Keith Watford
Moderator
14428
Keith Watford  
Hopefully somebody who uses files more often will be able to help you.
nicholi shen
2147
nicholi shen  
Excel locks the file when you open it. In order to prevent this you need to open it in read-only mode. A better open is to use vscode and install the "excel viewer" extension, and when you open the csv file in vscode you can watch it automatically update. 
Connorc1231
14
Connorc1231  

That would make sense why Excel would cause such a conflict then. I went ahead and installed the excel viewer extension for vscode and can confirm the MQL4 can write to the csv file while it is open, and can see it updating in real time. 


Thank you for your suggestion!

Feng Guozheng
65
Feng Guozheng  
ea write to the file with open-write-close operation.
run a simple http server on same pc with mt4/5,root set to folder mql4/file/

then use your ie to view your file
To add comments, please log in or register