Download MetaTrader 5
To add comments, please log in or register
Do you have idle PCs? Connect them to MQL5 Cloud Network and earn money!
Peter Bočan
89
Peter Bočan 2016.09.23 23:51 

Hello guys, I've tried to do some statistics/testing with EA and write it out into file but file always end up empty. 

Here is the opening and writing the info into it - it opens the file but does not write anything into it, what's the problem?

int OnInit()
{
   ZeroMemory(TouchCount);
   lastCandleTime = Time[0];
   CandleCounter = 1;
   ResetLastError();
   FileHandle = FileOpen("swingtest_" + TimeToString(TimeCurrent()) + ".txt", 
                                        FILE_WRITE|FILE_TXT|FILE_READ);
   if (FileHandle < 0) {
       Alert("Could not open file for write.");
       Alert(GetLastError());
       return(INIT_FAILED);
   }  

    FileWrite(FileHandle, "symbol: " + Symbol() + "\n"); 
    FileWrite(FileHandle, "timeframe: " + Timeframe() + "\n");
    FileWrite(FileHandle, "time: " + TimeToStr(TimeCurrent()) + "\n");
    FileWrite(FileHandle, "----------------------------------------------\n");
    FileFlush(FileHandle);
   return(INIT_SUCCEEDED);
}
Keith Watford
Moderator
7873
Keith Watford 2016.09.24 01:24  
TimeToString(TimeCurrent())

returns something like 2016.09.23

I think that the "." s in the filename confuses the system as it is expecting a file type to follow the "."

Try it with a different file name.

Fernando Carreiro
1429
Fernando Carreiro 2016.09.24 02:10  
GumRai:

returns something like 2016.09.23

I think that the "." s in the filename confuses the system as it is expecting a file type to follow the "."

Try it with a different file name.

@GumRai: Yes, you are correct, but it is not the "." in the date that is the problem - it is the ":" in the time that is invalid for filenames. Having multiple "." in the filename is allowed, but the ":" is reserved for the device/disk separator.

@PeterBocan. The FileWrite() function is usually used for CSV output which includes outputting the the CR+LF ("\r\n") at the end automatically, so you don't have to add the "\n" at the end. Also, I don't see a FileClose() in your code. Is there one, like maybe in the OnDeinit()?

PS! You are using the flags "FILE_WRITE|FILE_TXT|FILE_READ" but unless you intend to read it back within the EA, you should just use "FILE_WRITE|FILE_TXT". However, if you intend to look at the contents from an outside source at the same time while the EA runs, use the following "FILE_WRITE|FILE_TXT|FILE_SHARE_READ".

whroeder1
13593
whroeder1 2016.09.24 14:53  
    FileWrite(FileHandle, "----------------------------------------------\n");
    FileFlush(FileHandle);
   return(INIT_SUCCEEDED);
}
You don't close the file. You can't read from it until you do.
Peter Bočan
89
Peter Bočan 2016.09.24 19:19  

@GumRal @FMIC is right. 

@FMIC you got the cookie, you are right!

@WHRieder I do, in OnDeinit ;)

/
To add comments, please log in or register