Error 5004 when trying to open file while backtesting and not in COMMONS

 

Hi!

I'm trying to open a .txt file using CFileTxt class while executing backtesting. The idea is that it gets a list of datetime values which the EA is supposed to skip.

When trying to open the file, I allways get error 5004, so apparently the FileOpen function is not finding my .txt file. The problem is that I've tried to put it everywhere and it doesn't matter where I put it, it doesn't find it:

  • Inside the "Files" folder in the data directory (MQL5/Files)
  • Inside the "Tester" folder (installation id/Tester)
    • Inside a "Files" folder inside there (installation id/Tester/Files)
  • Getting the info of TerminalInfoString(TERMINAL_DATA_PATH), where the backtest agent is located
    • Inside a "MQL5" folder created there
    • Inside a "MQL5/Files" folder created there
    • Inside a "Files" folder created there
  • Inside the "Tester" folder where there is the list of tester agents
    • Inside a "Files" folder created there
I found this link where mr. Alain suggested using the 

#property tester_file

in the EA (and the helper files do indeed say files that are going to be used in backtesting must be informed with this property: "Input files to be tested, if needed, should always be specified."), but nothing changed.

So why exactly is this happening? I remember facing the exact same problem before and ended giving up and using the COMMONS file path. I'd like, though, to finally solve this issue. The only "appearence of solution" I found was this post , but using Windows functions to copy a file into a folder seems akward to say the least. 

Finally, the "nothing special code":

string path = TerminalInfoString(TERMINAL_DATA_PATH);
   Print("DEBUG: Data Folder: ", path);

   const int handle = m_file.Open(m_fileName,FILE_READ);
   
   if (handle == INVALID_HANDLE)
   {
      Print("MW DTF: No filter file named \"",m_fileName,"\" found (",GetLastError(),")");
      
      return false;
   }
Read TXT or CSV
Read TXT or CSV
  • 2024.12.22
  • www.mql5.com
Hi, I don't know why, but can't manage to read CSV or TXT I have tried all the possible combinations for reading path, and always get the same resu...
 
It should be in MQL5/Files folder with the #property tester_file set to the filename.
 
Alain Verleyen #:
It should be in MQL5/Files folder with the #property tester_file set to the filename.

Hi!

Thanks, but it didn't work; same 5004 error. Note: using version 5430.

 
Martin Bittencourt #:

Hi!

Thanks, but it didn't work; same 5004 error. Note: using version 5430.

Fix your code or check that your file isn't open with an other application.
#property tester_file "access.log"
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
 {
//--- create timer
  EventSetTimer(1);

//---
  return(INIT_SUCCEEDED);
 }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
 {
//--- destroy timer
  EventKillTimer();

 }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
 {
//---

 }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
 {
//---
  int fhandle = FileOpen("access.log",FILE_READ,"");
  if(fhandle!=INVALID_HANDLE)
   {
    Print("File opened !");
    FileClose(fhandle);
   }
  else
   {
    printf("File open error #%i",_LastError);
   }
  EventKillTimer();
 }


 
Alain Verleyen #:
Fix your code or check that your file isn't open with an other application.


Thanks for the test code!

Result: the same :T

It only worked when I attached to a chart as trying to run it. But in backtesting, failure again.