Read log file from strategy tester

 

Hello, I am trying to read in the log file from the strategy tester by


Print("PATH: ", TerminalInfoString(TERMINAL_DATA_PATH)+"\\logs\\20221022.log");
 
if(MQLInfoInteger(MQL_TESTER))
   {  int h=FileOpen(TerminalInfoString(TERMINAL_DATA_PATH)+"\\logs\\20221022.log",FILE_READ|FILE_TXT);
      if(h==INVALID_HANDLE)
      {  if(GetLastError()!=0) Print("Error in function GetStrategyTesterSettings() | INVALID_HANDLE | Ecode="+(string)GetLastError());
         return;
      }
   }
2022.10.22 12:57:26.326 2016.10.12 13:30:19   PATH: C:\Users\*****\AppData\Roaming\MetaQuotes\Tester\1A55FAA85B60C737304934B2D831858E\Agent-127.0.0.1-3001\logs\20221022.log
2022.10.22 12:57:26.326 2016.10.12 13:30:19   Error in function GetStrategyTesterSettings() | INVALID_HANDLE | Ecode=5002

The documentation of FileOpen says that the way I try it is incorrect but I don't know how to access the path otherwise? Is it actually allowed to read from this folder as writing is restricted to the Sandbox files folder

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- incorrect file opening method
   string terminal_data_path=TerminalInfoString(TERMINAL_DATA_PATH);
   string filename=terminal_data_path+"\\MQL5\\Files\\"+"fractals.csv";
   int filehandle=FileOpen(filename,FILE_WRITE|FILE_CSV);
   if(filehandle<0)
     {
      Print("Failed to open the file by the absolute path ");
      Print("Error code ",GetLastError());
     }
 
//--- correct way of working in the "file sandbox"
   ResetLastError();
   filehandle=FileOpen("fractals.csv",FILE_WRITE|FILE_CSV);
   if(filehandle!=INVALID_HANDLE)
     {
      FileWrite(filehandle,TimeCurrent(),Symbol(), EnumToString(_Period));
      FileClose(filehandle);
      Print("FileOpen OK");
     }
   else Print("Operation FileOpen failed, error ",GetLastError());
//--- another example with the creation of an enclosed directory in MQL5\Files\
   string subfolder="Research";
   filehandle=FileOpen(subfolder+"\\fractals.txt",FILE_WRITE|FILE_CSV);
      if(filehandle!=INVALID_HANDLE)
     {
      FileWrite(filehandle,TimeCurrent(),Symbol(), EnumToString(_Period));
      FileClose(filehandle);
      Print("The file must be created in the folder "+terminal_data_path+"\\"+subfolder);
     }
   else Print("File open failed, error ",GetLastError());
  }
 
  1. eatrader1231231231231233r5235134: I am trying to read in the log file from the strategy tester by
    eatrader1231231231231233r5235134: Is it actually allowed to read from this folder as writing is restricted to the Sandbox files folder

    You can't read (or write) outside the sandbox with normal code.

    For security reasons, work with files is strictly controlled in the MQL5 language. Files with which file operations are conducted using MQL5 means, cannot be outside the file sandbox.
              FileOpen - File Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

    File Write Problem (Error: 5002) - Expert Advisors and Automated Trading - MQL5 programming forum #1-2 (2020)
    and FolderDelete using TERMINAL_DATA_PATH - General - MQL5 programming forum (2017)


  2. There are two ways around this, but you still can't read the logs correctly because of the caching.
              Can a file that exists outside of the MetaTrader folders be opened? - MQL4 programming forum #1-#3 (2020)
              File Path for saving .csv files in MT5 - MQL5 programming forum (2019)
              File Operations via WinAPI - MQL4 Articles (2008)

    Stop trying. Have your code write its own file with whatever data you want.

 
William Roeder #:
  1. You can't read (or write) outside the sandbox with normal code.

    File Write Problem (Error: 5002) - Expert Advisors and Automated Trading - MQL5 programming forum #1-2 (2020)
    and FolderDelete using TERMINAL_DATA_PATH - General - MQL5 programming forum (2017)


  2. There are two ways around this, but you still can't read the logs correctly because of the caching.
              Can a file that exists outside of the MetaTrader folders be opened? - MQL4 programming forum #1-#3 (2020)
              File Path for saving .csv files in MT5 - MQL5 programming forum (2019)
              File Operations via WinAPI - MQL4 Articles (2008)

    Stop trying. Have your code write its own file with whatever data you want.

Thank you William, for pointing out the alternative ways... I will try it!
Reason: