FileOpen works in script but not EA Strategy Tester

 

Hi there,

I have code that opens and reads a file when run in a script.  When I copy the function and put it in an EA and test it in ST an error returns as below.

5004

ERR_FILE_CANNOT_OPEN

Cannot open file


The script:

input string fileName="eventdates.csv";
string token,year,month,day,time;
datetime newsTime;
datetime newsTimes[1];
int newsItemsCount;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   getNewsTime();
  }
//+------------------------------------------------------------------+
void getNewsTime()
  {
   ResetLastError();
   int fileHandle=FileOpen(fileName,FILE_READ|FILE_CSV);
   if(fileHandle!=INVALID_HANDLE)
     {
      PrintFormat("%s file is open for reading",fileName);
      // Count how many items are in the file
      
      while(!FileIsEnding(fileHandle))
        {
         //Print(FileReadString(fileHandle));

         token=FileReadString(fileHandle);
         year=StringSubstr(token,0,4);
         month=StringSubstr(token,4,2);
         day=StringSubstr(token,6,2);
         time=StringSubstr(token,9,5);
         newsTime=StrToTime(StringConcatenate(year,".",month,".",day," ",time));
         newsTimes[newsItemsCount]=newsTime;

         newsItemsCount++;
         ArrayResize(newsTimes,newsItemsCount+1);
        }// End While
        
        for(int i = 0; i < newsItemsCount+1;i++)
         {
         Print("newsTimes: ", newsTimes[i]);
         }

      //--- close the file
      FileClose(fileHandle);
     }
   else
     {
      PrintFormat("Failed to open %s file, Error code = %d",fileName,GetLastError());
     }
  }// end getNewsTime

Displays the following:



The EA:

input string fileName="eventdates.csv";
string token,year,month,day,time;
datetime newsTime;
datetime newsTimes[1];
int newsItemsCount;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   getNewsTime();
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void getNewsTime()
  {
   ResetLastError();
   int fileHandle=FileOpen(fileName,FILE_READ|FILE_CSV);
   if(fileHandle!=INVALID_HANDLE)
     {
      PrintFormat("%s file is open for reading",fileName);
      // Count how many items are in the file
      
      while(!FileIsEnding(fileHandle))
        {
         //Print(FileReadString(fileHandle));

         token=FileReadString(fileHandle);
         year=StringSubstr(token,0,4);
         month=StringSubstr(token,4,2);
         day=StringSubstr(token,6,2);
         time=StringSubstr(token,9,5);
         newsTime=StrToTime(StringConcatenate(year,".",month,".",day," ",time));
         newsTimes[newsItemsCount]=newsTime;

         newsItemsCount++;
         ArrayResize(newsTimes,newsItemsCount+1);
        }// End While
        
        for(int i = 0; i < newsItemsCount+1;i++)
         {
         Print("newsTimes: ", newsTimes[i]);
         }

      //--- close the file
      FileClose(fileHandle);
     }
   else
     {
      PrintFormat("Failed to open %s file, Error code = %d",fileName,GetLastError());
     }
  }// end getNewsTime

Displays:


Can you please assist?

Thanks.

 
Have you put eventdates.csv into Tester\Files?
 
Thank you!
Reason: