FileOpen, FileWrite not working in onTester()

 

Hi,

This is driving me crazy but I have come to conclusion that you just cant use file access in the OnTester() function. But I hope I am wrong and someone can help?

I am wanting to write data out as part of the Strategy Tester Optimisation.

I can open and write to a file in both OnTesterInit() and OnTesterDeinit() functions. But putting exactly the same code in OnTester() and it just never does anything.

Below is the example code to prove it - 3 examples files opened and written too, but the OnTester() one never works ....  the other two work fine as expected.

Ideas anyone ?  Am I missing something obvious or is this just not permitted to do File I/O in OnTester() ????

void OnTesterInit() {

     string row = "INIT File\n";
      int fH = FileOpen("TestFile1.csv",FILE_READ|FILE_WRITE);
      FileSeek(fH,0,SEEK_END); 
      FileWriteString(fH, row, StringLen(row) );
      FileClose(fH);
        
  }
  
//+------------------------------------------------------------------+ 
//| Tester function                                                  | 
//+------------------------------------------------------------------+ 
double OnTester() 
  { 

   double ret=0.0; 

      string row = "Some data\n";
      int fH = FileOpen("TestFile2.csv",FILE_READ|FILE_WRITE);
      FileSeek(fH,0,SEEK_END); 
      FileWriteString(fH, row, StringLen(row) );
      FileClose(fH);


   return(ret); 
  } 
  
  //+------------------------------------------------------------------+
//| End of optimization                                              |
//+------------------------------------------------------------------+
void OnTesterDeinit()
  {

     string row = "END of Data\n";
      int fH = FileOpen("TestFile3.csv",FILE_READ|FILE_WRITE);
      FileSeek(fH,0,SEEK_END); 
      FileWriteString(fH, row, StringLen(row) );
      FileClose(fH);

  
  }
The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
The idea of ​​automated trading is appealing by the fact that the trading robot can work non-stop for 24 hours a day, seven days a week. The robot does not get tired, doubtful or scared, it's is totally free from any psychological problems. It is sufficient enough to clearly formalize the trading rules and implement them in the algorithms, and...
 
Freefox:

Hi,

This is driving me crazy but I have come to conclusion that you just cant use file access in the OnTester() function. But I hope I am wrong and someone can help?

I am wanting to write data out as part of the Strategy Tester Optimisation.

I can open and write to a file in both OnTesterInit() and OnTesterDeinit() functions. But putting exactly the same code in OnTester() and it just never does anything.

Below is the example code to prove it - 3 examples files opened and written too, but the OnTester() one never works ....  the other two work fine as expected.

Ideas anyone ?  Am I missing something obvious or is this just not permitted to do File I/O in OnTester() ????

There is no problem to do file I/O in OnTester(). You probably just don't look in the right folder for your file. It's not the same place as with OnTesterInit(), which is standard MQL5/Files.

Use the FILE_COMMON flag or check in the right folder, mine is "C:\Users\alain\AppData\Roaming\MetaQuotes\Tester\7F9D16CEFDFF2D39EA4EF8A208B4F896\Agent-127.0.0.1-3001\MQL5\Files"

 

Thanks for the reply.

Misread it originally as I do have two files already created correctly in the folder.

Would seem obvious to expect the 3rd one from the onTester() function to be placed in the same location.

Bizarre it isn't and written elsewhere. Not surprising people get confused as very inconsistent but sorted now.

Thanks once again for helping.

 
Alain Verleyen:

There is no problem to do file I/O in OnTester(). You probably just don't look in the right folder for your file. It's not the same place as with OnTesterInit(), which is standard MQL5/Files.

Use the FILE_COMMON flag or check in the right folder, mine is "C:\Users\alain\AppData\Roaming\MetaQuotes\Tester\7F9D16CEFDFF2D39EA4EF8A208B4F896\Agent-127.0.0.1-3001\MQL5\Files"

If use FILE_COMMON flag, I want to output some self-defined test result to common folder, but it may has confict when mult test pass write to the same file.

Can I get the test passid in ea so that I can output to a special filename?

 
Xiaowei Yan:

If use FILE_COMMON flag, I want to output some self-defined test result to common folder, but it may has confict when mult test pass write to the same file.

Did you use FILE_SHARE_READ|FILE_SHARE_WRITE ?

Can I get the test passid in ea so that I can output to a special filename?

You need to use Frames().

FrameAdd using data file
FrameAdd using data file
  • 2017.01.03
  • www.mql5.com
Hello all, According to the MQL5 Official Documentation, the FrameAdd function can be called using two different sets of parameters...
 
Alain Verleyen #:

There is no problem to do file I/O in OnTester(). You probably just don't look in the right folder for your file. It's not the same place as with OnTesterInit(), which is standard MQL5/Files.

Use the FILE_COMMON flag or check in the right folder, mine is "C:\Users\alain\AppData\Roaming\MetaQuotes\Tester\7F9D16CEFDFF2D39EA4EF8A208B4F896\Agent-127.0.0.1-3001\MQL5\Files"

how do you know which folder to look at? I cannot also see the file. I debuggined handle is not -1 so it creates ok but I just do not see the file.


//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
      static double lastHigh;
      static double lastLow;   
 
      MqlRates priceInfo[];
 
      ArraySetAsSeries(priceInfo, true);  
 
      int priceData = CopyRates(_Symbol, _Period, 0, 3, priceInfo);
 
      if ((lastHigh != priceInfo[1].high) && (lastLow != priceInfo[1].low)) {
 
         int handle = FileOpen("aaa.csv", FILE_READ|FILE_WRITE|FILE_CSV|FILE_ANSI);
 
        FileSeek(handle, 0, SEEK_END);
 
        FileWrite(handle, "Time", priceInfo[1].time);
        FileClose(handle);
        lastHigh = priceInfo[1].high;
        lastLow = priceInfo[1].low;
 
        Comment(priceInfo[1].high);
      }
  }
//+------------------------------------------------------------------+


I also executed search on all computer by the file name , and it did not find it. How to understand?


Tried adding a breakpoint after file is created and searched whole computer and then found it. I do not get how earlier I could not find by searching all computer.

C:\Users\Darius\AppData\Roaming\MetaQuotes\Tester\9EB2973C469D24060397BB5158EA73A5\Agent-127.0.0.1-3000\MQL5\Files

 

I also have some problems writing files...

I use the COMMON option.

I've run my EA hundreds of times (in optimization mode), with my file correctly written each time. But suddenly it stopped working for some unknown reason (with no error message), and no way to get my EA to write another file with this MT


On other PCs it works fine.

I re-installed, and it works again, but I have to re-import all my symbols...


Any idea why? I would like to avoid having to completely re-import my symbols again.