Open Files during backtest

 

Hello,

when i place a file into folder files i can open this file when running or debug my expert.

But when i run the backtest, the file is not found.

Ok, after some research, i found the property tester_file.


When i add this to my code:

#property  tester_file "my_file_name_with_relative_path_to_files"

The backtest can open the file. 

But when i run now the expert in debug mode or in chart, it works not.

When disable #property, still not working.



How to make file referencing easy ?

Why not simply allow to store or load files from Files section or everywhere (with admin rights.)


This is an ugly stumblestone.

 
chinaski: when i place a file into folder files
  1. When you open files outside the tester they go in <data folder>\files\...
  2. When you open files inside the tester they go in <data folder>\tester\files\...
  3. Do not add a path unless "..." contains one.
Copy them to the tester\files folder to use the tester.
          Data Structure in MetaTrader 4 Build 600 and Higher - MQL4 Articles 17.02.2014
 
whroeder1:
  1. When you open files outside the tester they go in <data folder>\files\...
  2. When you open files inside the tester they go in <data folder>\tester\files\...
  3. Do not add a path unless "..." contains one.
Copy them to the tester\files folder to use the tester.
          Data Structure in MetaTrader 4 Build 600 and Higher - MQL4 Articles 17.02.2014

Sorry, can't follow you. I have no cryptic data folder as i work with /portable runtime flag.


If have a folder A:

mql5/files

and folders B,..:

        root/tester/agent_some_name_rule/MQL5/files (there are multiple of them)


When i do no backtest, the files are picked from A.

If i do backtest, they are picked from elsewhere but i don't know which of the various agent folder is important.

It seems the mentioned property can help but the it seems the expert is messed up when running not in backtest.


The link provided explains the folder structure in MetaTrader but not really why files are loaded

in different ways when done from backtest or not.



I just want to use some files for configuration. I need the same files in backtest as well as in live run.

Is this possible ? Thank you.

 
chinaski:

Sorry, can't follow you. I have no cryptic data folder as i work with /portable runtime flag.


If have a folder A:

mql5/files

and folders B,..:

        root/tester/agent_some_name_rule/MQL5/files (there are multiple of them)


When i do no backtest, the files are picked from A.

If i do backtest, they are picked from elsewhere but i don't know which of the various agent folder is important.

It seems the mentioned property can help but the it seems the expert is messed up when running not in backtest.


The link provided explains the folder structure in MetaTrader but not really why files are loaded

in different ways when done from backtest or not.



I just want to use some files for configuration. I need the same files in backtest as well as in live run.

Is this possible ? Thank you.

#property tester_file has nothing to do with live chart. Must be something else as you said it continues to be a problem even if you remove the directive.

Please show the relevant code.

 
Alain Verleyen:

#property tester_file has nothing to do with live chart. Must be something else as you said it continues to be a problem even if you remove the directive.

Please show the relevant code.

Hi,

this is my code in OnInit

string read_text_file(string xfn)
{
        ResetLastError(); 
        string fn="subfolder\\";
        fn += xfn;
        int file_handle=FileOpen(fn,FILE_READ|FILE_ANSI); 
        if(file_handle!=INVALID_HANDLE) 
        { 
                //--- additional variables 
                int    str_size; 
                string str; 
                string content="";
                //--- read data from the file 
                while(!FileIsEnding(file_handle)) 
                { 
                        //--- find out how many symbols are used for writing the time 
                        str_size=FileReadInteger(file_handle,INT_VALUE); 

                        //--- read the string 
                        str=FileReadString(file_handle,str_size); 
                        //--- print the string 
                        content += str;
                } 
        
                //--- close the file 
                FileClose(file_handle); 
                return content;
        } 
        else 
        {
                PrintFormat("Failed to open %s file, Error code = %d",fn,GetLastError()); 
                return "";
        }
}

When running/debugging expert, not problem. File is loaded.

When running in backtest, it gives error 5004


2018.05.17 03:05:08.333 Core 1  2000.01.01 00:00:00   Failed to open subfolder\setup.txt file, Error code = 5004

 
Don't know if it plays a role: I start MetaTrader with /portable flag. So all my folders are below install folder.
 
chinaski:

Hi,

this is my code in OnInit

When running/debugging expert, not problem. File is loaded.

When running in backtest, it gives error 5004



Of course you need to tester_file directive.
 
Alain Verleyen:
Of course you need to tester_file directive.

Can you give some information please. How to use it exactly: my file is: 

mql5/files/subfolder/setup.txt

How to use the directive exactly ? Thank you

 
chinaski:

Can you give some information please. How to use it exactly: my file is: 

How to use the directive exactly ? Thank you

#property tester_file "subfolder\\setup.txt"
This directive will copy your setup.txt file in the testing environment. So you don't need to change anything else in your code.
 
Alain Verleyen:
This directive will copy your setup.txt file in the testing environment. So you don't need to change anything else in your code.

Thank you but i did this and it is was working. Now restarting or next day and it works. Whats going on.

 

Hi, I found an easy way to avoid copying or searching the folder in which the file is. Just need to save the file in the directory: "...AppData\Roaming\MetaQuotes\Terminal\Common\Files", and in your code add "FILE_COMMON" flag. 

   int      file_handle=FileOpen(InpFileName,FILE_ANSI|FILE_READ|FILE_CSV|FILE_COMMON,',');


Hope it helps

Reason: