HELP ME FileWrite!

Back to topics list To post a new topic, please log in or register
avatar
8
sgtrader 2015.08.17 16:25
 


I already created Trade.csv in folder ...\MQL4\Files\, then code following rows in my EA:

............... 

         int file_handle = FileOpen("Trade.csv",FILE_READ|FILE_WRITE|FILE_SHARE_READ|FILE_CSV);

         if(file_handle != INVALID_HANDLE){

            FileWrite(file_handle,OrderOpenTime(),OrderOpenPrice(),OrderSymbol(),OrderComment());

            FileClose(file_handle);

            Print("Open Successfully");

         }

         else Print("Open Unsuccessfully", GetLastError()); 

.....

I see in Experts Tab, notification that is "Open Successfully", but nothing is written in Trade.scv. What is wrong?

 Looking forwards your help. Thank you so much! 

avatar
12988
WHRoeder 2015.08.17 18:48 #
 
Your code ran fine. Are you looking in MQL4/files or tester/files? Are you looking in the data folder? Data Structure in MetaTrader 4 Build 600 and Higher - MQL4 Articles 17.02.2014
avatar
8
sgtrader 2015.08.17 19:06 #
 

I created file in C:\Program Files (x86)\serverXXX\MQL4\Files. And I have just opened data folder\MQL4\Files and see Trade.csv with right content. So the file is automatically created and written here, isnot it?

 Thanks  

avatar
8
sgtrader 2015.08.17 20:54 #
 
sgtrader:

I created file in C:\Program Files (x86)\serverXXX\MQL4\Files. And I have just opened data folder\MQL4\Files and see Trade.csv with right content. So the file is automatically created and written here, isnot it?

 Thanks  

By the way, I have just got an error 5004 - cannot open file. The fact that I open file by Excel Program when EA runs. I donot know if that is reason making error because in FileOpen function had FILE_SHARE_READ flag.

 "int file_handle = FileOpen("Trade.csv",FILE_READ|FILE_WRITE|FILE_SHARE_READ|FILE_CSV); "  

avatar
12988
WHRoeder 2015.08.18 14:03 #
 
sgtrader: I created file in C:\Program Files (x86)\serverXXX\MQL4\Files. And I have just opened data folder\MQL4\Files and see Trade.csv with right content. So the file is automatically created and written here, isnot it?
No. It is created under the data folder (see Data Structure in MetaTrader 4 Build 600 and Higher - MQL4 Articles 17.02.2014) either MQL4\files or tester\files.
avatar
8
sgtrader 2015.08.18 14:19 #
 
WHRoeder:
No. It is created under the data folder (see Data Structure in MetaTrader 4 Build 600 and Higher - MQL4 Articles 17.02.2014) either MQL4\files or tester\files.

Sure, it is created under data folder\MQL4\Files.

By the way, I have just got an error 5004 - cannot open file. The fact that I open file by Excel Program when EA runs. I donot know if that is reason making error because in FileOpen function had FILE_SHARE_READ flag.

 "int file_handle = FileOpen("Trade.csv",FILE_READ|FILE_WRITE|FILE_SHARE_READ|FILE_CSV); "  

Could you show me what reason is?

  Thanks 

avatar
145
aakcaagac 2015.08.18 14:22 #
 
sgtrader:

FileOpen function had FILE_SHARE_READ flag.

Could you show me what reason is?

Most likely your Excel program does not have FILE_SHARE_READ (or similar) flag set, therefore it sets a hard lock on the file and the operating system does not allow opening of the file from other programs.
avatar
8
sgtrader 2015.08.18 14:32 #
 

aakcaagac:
Most likely your Excel program does not have FILE_SHARE_READ (or similar) flag set, therefore it sets a hard lock on the file and the operating system does not allow opening of the file from other programs.

thanks 


avatar
8
sgtrader 2015.08.18 15:44 #
 

............... 

         int file_handle = FileOpen("Trade.csv",FILE_READ|FILE_WRITE|FILE_SHARE_READ|FILE_CSV);

         if(file_handle != INVALID_HANDLE){

            FileWrite(file_handle,OrderOpenTime(),OrderOpenPrice(),OrderSymbol(),OrderComment());

            FileClose(file_handle);

            Print("Open Successfully");

         }

         else Print("Open Unsuccessfully", GetLastError()); 

.....

 I got another problem that EA writes file only first row then old content was inserted and only newest content appears in first row. Could you show me my problem in the code?

 Thanks in advance!  

avatar
145
aakcaagac 2015.08.18 16:26 #
 

Not Tested!

int handle = FileOpen("trade.csv", FILE_READ | FILE_WRITE | FILE_TXT | FILE_ANSI);

FileSeek(handle, FileSize(handle));

string str = StringFormat("%s;%s;%s;%s\n", OrderOpenTime(),
                                           OrderOpenPrice(),
                                           OrderSymbol(),
                                           OrderComment());

FileWriteString(handle, str);
FileClose(handle);

As written above, I just nailed the lines into the SRC box here in the Forum. I have not tested this at all. What I figured from your request is, that you would like to have all orders placed line by line (e.g. keeping the old entries). This is what FileSeek(); seem to do. You open the file, you get the FileSize(); and then seek to the end of the file. You use StringFormat to concatenate the CSV line correctly and dump it with FileWriteString(); to the file. Then you FileClose(); the stuff again. The line written to the *.csv file is semicolon separated followed by a newline.

Test around with that code and adopt it to your needs. 

avatar
8
sgtrader 2015.08.18 19:28 #
 
aakcaagac:

Not Tested!

As written above, I just nailed the lines into the SRC box here in the Forum. I have not tested this at all. What I figured from your request is, that you would like to have all orders placed line by line (e.g. keeping the old entries). This is what FileSeek(); seem to do. You open the file, you get the FileSize(); and then seek to the end of the file. You use StringFormat to concatenate the CSV line correctly and dump it with FileWriteString(); to the file. Then you FileClose(); the stuff again. The line written to the *.csv file is semicolon separated followed by a newline.

Test around with that code and adopt it to your needs. 

 your " FileSeek(handle, FileSize(handle)); " seems to lack ENUM_FILE_POSITION SEEK_SET, right?

I think my code can be corrected (only add: FileSeek() function to move file pointer and "\n" to enter at the end of row), as follow:

  int file_handle = FileOpen("Trade.csv",FILE_READ|FILE_WRITE|FILE_SHARE_READ|FILE_CSV);

         if(file_handle != INVALID_HANDLE){

     FileSeek(file_handle,FileSize(file_handle),SEEK_SET); 

            FileWrite(file_handle,OrderOpenTime(),OrderOpenPrice(),OrderSymbol(),OrderComment(), "\n");

            FileClose(file_handle);

            Print("Open Successfully");

         }

         else Print("Open Unsuccessfully", GetLastError()); 

Back to topics list  

To add comments, please log in or register