FileWrite in backtesting strategy mode problem

 

Hi,

logging output to a file using simple FileWrite. I works well in real mode. However it does not work in backtesting mode :-(

Has anyone any idea. Any help would be appreciated.

My code:

fh = FileOpen("mydata.csv", FILE_CSV|FILE_WRITE);
if (fh<0) {
Alert("File opening failed: "+GetLastError());

}

if(fh>0)
{
FileWrite(fh,TimeToStr(TimeCurrent()),"*","Teststring");

FileFlush(fh);

FileClose(fh);

}

 

You know you are over-writing the file every time a backtest is ran, yes?

You need to use FILE_READ|FILE_WRITE if you want to append data to the file as backtester is running.

 
1005phillip:

You know you are over-writing the file every time a backtest is ran, yes?

You need to use FILE_READ|FILE_WRITE if you want to append data to the file as backtester is running.


I get no output in backtesting at all :-( I was looking for it in all directories in /Tester/Files and no output file.

I only want the outputfile of the last back test run.

 

Alert() will not work in backtesting mode. Use Print() instead for debugging the error.

The file will be in tester/files if successful.

 

Just a thought, since you have no control over the actual value returned by the fileopen function in the event that a file is opened, implement the following:

fh = FileOpen("mydata.csv", FILE_CSV|FILE_WRITE);
if (fh==-1) // <- MQL4 code says a file handle of -1 will be returned if fileopen fails, don't make this a general case of <0
   {
   Alert("File opening failed: "+GetLastError());
   }

//if (fh>0) <- not guaranteed to have a >0 file handle value, can be any numer != -1
else
   {
   FileWrite(fh,TimeToStr(TimeCurrent()),"*","Teststring");
   // FileFlush(fh); <- not necessary, FileClose does FileFlush for you
   FileClose(fh);
   }
All you know is that if the fileopen fails then the returned value is uniquely -1, making wider generalities than that in your if/then logic constructs invites debugging issues precisely of the kind you are experiencing.
 
1005phillip:

Just a thought, since you have no control over the actual value returned by the fileopen function in the event that a file is opened, implement the following:

All you know is that if the fileopen fails then the returned value is uniquely -1, making wider generalities than that in your if/then logic constructs invites debugging issues precisely of the kind you are experiencing.


Thank you all a lot.

I've found the error. I opened the file in my init() function. However, just before the FileOpen call, a MessageBox was issued which returned NO in the testing mode and the init method was left. Consequence FileOpen was never executed.

 
7bit:

Alert() will not work in backtesting mode. Use Print() instead for debugging the error.

The file will be in tester/files if successful.

Alert becomes a Print in the tester, no changes needed.
Reason: