expert advisor cannot open files but script can

 

Hi, 
I have a binary file containing an array and my expert advisor needs to read it. However, I can't get it to open the file. I can open it with a script though. I think that the expert advisor is not looking for my file in the same folder as the script. I have put it in data_folder/MQL5/Files/RiskTables/

Here is the code of the script that is working:

void OnStart()
  {
//---
   double myarray[];
   string path="RiskTables\\drawdown_6percent_focused_x.bin";
   ResetLastError();
   int handle=FileOpen(path,FILE_READ|FILE_BIN);
   if(handle!=INVALID_HANDLE)
   {
      FileReadArray(handle,myarray);
      int size=ArraySize(myarray);
      int step = size/10;
      for(int i=0;i<size;i=i+step)
         Alert(myarray[i]);
      //--- close the file
      FileClose(handle);
     }
   else
      Alert("Failed to open the file, error ",GetLastError());
   FileClose(handle);
  }

As expected I get numbers in the alert pop up window.


And the expert advisor code, which is not working is:

int OnInit()
  {
//---
   double myarray[];
   string path="RiskTables\\drawdown_6percent_focused_x.bin";
   ResetLastError();
   Print("we are going to open the file");
   int handle=FileOpen(path,FILE_READ|FILE_BIN);
   if(handle!=INVALID_HANDLE)
   {
      FileReadArray(handle,myarray);
      int size=ArraySize(myarray);
      int step = size/10;
      for(int i=0;i<size;i=i+step)
         Print(myarray[i]);
      //--- close the file
      FileClose(handle);
     }
   else
      Print("Failed to open the file, error ",GetLastError());
   FileClose(handle);
 
//---
   return(INIT_SUCCEEDED);
  }

Here I am getting this output:

2021.06.26 23:23:22.557 Core 1  2019.01.01 00:00:00   Failed to open the file, error 5004


Any piece of advise will be highly appreciated.

Benoit

 
If you are doing this inside the tester, the file location is a different one. There are special folders for the testers "personal" files. Try placing it inside the common folder.

Another hint, Resetlastterror () should be after Print().

Print() requires you to have more than just a static input, else it will set an error code.


 
Dominik Egert:
If you are doing this inside the tester, the file location is a different one. There are special folders for the testers "personal" files. Try placing it inside the common folder.

Another hint, Resetlastterror () should be after Print().

Print() requires you to have more than just a static input, else it will set an error code.


Thanks for the tips! 

I moved the file to my MetaQuotes\Terminal\Common\Files\Riskarray. I am getting error 5004. Is there a way to specify that the EA needs to look for files in the common folder?

 

Forum on trading, automated trading systems and testing trading strategies

expert advisor cannot open files but script can

aseedb, 2021.06.27 11:41

Thanks for the tips! 

I moved the file to my MetaQuotes\Terminal\Common\Files\Riskarray. I am getting error 5004. Is there a way to specify that the EA needs to look for files in the common folder?


Problem solved! I needed to add the FILE_COMMON flag.

int OnInit()
  {
//---
   double myarray[];
   string path="RiskTables\\drawdown_6percent_focused_x.bin";
   Print("we are going to open the file");
   ResetLastError();
   int handle=FileOpen(path,FILE_READ|FILE_BIN|FILE_COMMON);
   if(handle!=INVALID_HANDLE)
   {
      FileReadArray(handle,myarray);
      int size=ArraySize(myarray);
      int step = size/10;
      for(int i=0;i<size;i=i+step)
         Print(myarray[i]);
      //--- close the file
      FileClose(handle);
     }
   else
      Print("Failed to open the file, error ",GetLastError());
   FileClose(handle);
 
//---
   return(INIT_SUCCEEDED);
  }
aseedb
aseedb
  • 2021.06.26
  • www.mql5.com
Trader's profile