Download MetaTrader 5
To add comments, please log in or register
Launch agents on an idle PC. It is profitable!
brettosm8
241
brettosm8 2014.07.26 10:03 

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.

honest_knave
954
honest_knave 2014.07.26 10:58  
Have you put eventdates.csv into Tester\Files?
brettosm8
241
brettosm8 2014.07.26 12:35  
Thank you!
/
To add comments, please log in or register