Can't open file in Tester

 

Hi everyone,


When I attach my EA to the chart it can create and read files no problem.

In the tester, I can create files, but when trying to read them I get ERR_CANNOT_OPEN_FILE.

   //function to save results to file
   void save_results()
   {
      for (int i=0;i<TDs;i++)
      {
         string InpFileName="data"+IntegerToString(i)+".bin";
         string path=InpFileName;      
         
         //delete any existing file
         FileDelete(path); 
         
         //--- open the file
         ResetLastError();
         int handle=FileOpen(path,FILE_READ|FILE_WRITE|FILE_BIN);
         if(handle!=INVALID_HANDLE)
         {
            //--- write array data to the beginning of the file
            FileSeek(handle,0,SEEK_SET);
            FileWriteArray(handle,S[i].result);
            //--- close the file
            FileClose(handle);
         }
         else
         {
            Print("Failed to open the file, error ",GetLastError());
         }
      }
      Print(TerminalInfoString(TERMINAL_DATA_PATH),"\\MQL5\\Files");
   }

   //function to load results from file
   void load_results()
   {
      for (int i=0;i<TDs;i++)
      {
         //--- file path
         string InpFileName="data"+IntegerToString(i)+".bin";     
         string path=InpFileName;
                 
         //--- open the file
         ResetLastError();
         int file_handle=FileOpen(path,FILE_READ|FILE_BIN);
         if(file_handle!=INVALID_HANDLE)
         {
            //--- read all data from the file to the array
            FileReadArray(file_handle,S[i].result);
            //--- close the file
            FileClose(file_handle);
         }
         else
         {
            Print("File open failed, error ",GetLastError());
         }
      }
      Print(TerminalInfoString(TERMINAL_DATA_PATH),"\\MQL5\\Files");
   }   
Documentation on MQL5: File Functions / FileOpen
Documentation on MQL5: File Functions / FileOpen
  • www.mql5.com
FileOpen - File Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

J Sky:

When I attach my EA to the chart it can create and read files no problem.
In the tester, I can create files, but when trying to read them I get ERR_CANNOT_OPEN_FILE.

  1. Files are at “«DataFolder»\MQLx\Files”.
  2. In the tester, files are at “«DataFolder»\MQLx\Tester\Files”.
 
William Roeder #:
  1. Files are at “«DataFolder»\MQLx\Files”.
  2. In the tester, files are at “«DataFolder»\MQLx\Tester\Files”.

Hi yes I know they are created there but then I can't open them. I can navigate to the tester files folder and see the created files, but then when trying to read the files, FileOpen deletes the files and returns error code ERR_CANNOT_OPEN_FILE. 

 
J Sky #:

Hi yes I know they are created there but then I can't open them. I can navigate to the tester files folder and see the created files, but then when trying to read the files, FileOpen deletes the files and returns error code ERR_CANNOT_OPEN_FILE. 

Files are deleted by the tester.

If you need to use persistent data files for multiple backtests, use the attribute FILE_COMMON.

 
Alain Verleyen #:

Files are deleted by the tester.

If you need to use persistent data files for multiple backtests, use the attribute FILE_COMMON.

Thank you! working now :D

Reason: