Returning Error 5020 from FileIsExist on an existing file.

 

I have a file writing function which has been rewritten, specifying the data from the OrderHistory log.

It now checks to see if the file exists before deciding to either create a new file and write the relevant data into it, or find the existing file, go to the end of the file and write data. 

For some reason it's not finding the file through the FileIsExist function, and so - as is programmed to do so - it creates a new file. 

Yes, I have read the documentation on the FileIsExist function, I am aware of the 5019 error which confirms if the file is an actual file or a subdirectory, & which is not an issue in this case, as I have - as you will see in the text below - specified the FILE_COMMON flag.

I have also checked the Write to File Syntax  documentation as well.

I don't see why it's not finding the file despite clearly specifying which directory to look for the file in, under FileIsExist.

void filewrite_function_WD(){
      
   //Create File
   
   string   yourAccountNumber = StringFormat("%d", AccountNumber());
   string   mySpreadSheet     = yourAccountNumber+"_trading_log.csv";
   
      int orderHist = OrdersHistoryTotal()-1;   
      
         if(!OrderSelect(orderHist,SELECT_BY_POS,MODE_HISTORY)){
         
	 int orderSelectError = GetLastError();           
  
	  PrintFormat("No order was selected, therefore, unable to write file %d", orderSelectError);
         
         
         } else {

            int file_handle = 0;
            
               //string strRSquared   =  StringFormat("%0.0f%%", rSquared * 100);
               
                  if(!FileIsExist(mySpreadSheet,FILE_COMMON)){ //If the file does not exist, create file and add the date. The problem is here.
                  
                     int fileIsExistError = GetLastError();
                     
                        PrintFormat("File does not exist. Error %d", fileIsExistError); //Returing 5020 Error
                  
                           file_handle = FileOpen(mySpreadSheet,FILE_CSV|FILE_READ|FILE_WRITE,',');
                             
                              MessageBox("The file does not exist, a new file will be created.","New File Created", MB_OK | MB_ICONINFORMATION);
                              
                                 FileSeek(file_handle,0,SEEK_SET);
                                 
                                    FileWrite(file_handle,"AccountNumber","Ticket Number","Symbol",StringFormat("Profit/Loss (%s)", AccountCurrency()),"Open Price","Open Time"); //Write on top line
                  
                                       FileWrite(file_handle,yourAccountNumber,OrderTicket(),OrderSymbol(),OrderProfit(),OrderOpenPrice(),OrderOpenTime()); //Write on line below
                  
                                          FileClose(file_handle);
                     
                  } else { //else if the file does exist, go to the end of the file, and write the data into the existing file. 
                      
                  file_handle = FileOpen(mySpreadSheet,FILE_CSV|FILE_READ|FILE_WRITE,',');
                     
                     FileSeek(file_handle,0,SEEK_END);
                  
                        FileWrite(file_handle,yourAccountNumber,OrderTicket(),OrderSymbol(),OrderProfit(),OrderOpenPrice(),OrderOpenTime()); 
   
                           FileClose(file_handle);
                     
                  }
               
                  PrintFormat("File Entry Written for %d", OrderTicket());
                                 
                     return;
         
         }
               
}
Documentation on MQL5: File Functions / FileIsExist
Documentation on MQL5: File Functions / FileIsExist
  • www.mql5.com
FileIsExist - File Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
TheHonestPrussian: For some reason it's not finding the file through the FileIsExist function,
                  if(!FileIsExist(mySpreadSheet,FILE_COMMON)){ //If the file does not exist, create file and add the date. The problem is here.
                  
                     int fileIsExistError = GetLastError();
                     
                        PrintFormat("File does not exist. Error %d", fileIsExistError); //Returing 5020 Error
                  
                           file_handle = FileOpen(mySpreadSheet,FILE_CSV|FILE_READ|FILE_WRITE,',');
For an obvious reason. You are looking in the common directory, but creating/updating it in the FILE directory.
 
William Roeder #:
For an obvious reason. You are looking in the common directory, but creating/updating it in the FILE directory.

Thanks William, I have rewritten both FILEOPEN handles this to 

file_handle = FileOpen(mySpreadSheet,FILE_COMMON | FILE_READ | FILE_WRITE,',');

which has done the trick. 

 
      int orderHist = OrdersHistoryTotal()-1;   
      
         if(!OrderSelect(orderHist,SELECT_BY_POS,MODE_HISTORY)){
  1. Do not assume history has only closed orders.
              OrderType() == 6, 7 in the history pool? - MQL4 programming forum (2017)

  2. Do not assume history is ordered by date, it's not.
              Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum (2012)
              Taking the last profit and storing it in a variable | MQL4 - MQL4 programming forum #3 (2020)

  3. Total Profit is OrderProfit() + OrderSwap() + OrderCommission(). Some brokers don't use the Commission/Swap fields. Instead, they add balance entries. (Maybe related to Government required accounting/tax laws.)
              "balance" orders in account history - Day Trading Techniques - MQL4 programming forum (2017)

    Broker History
    FXCM
    Commission - <TICKET>
    Rollover - <TICKET>

    >R/O - 1,000 EUR/USD @0.52

    #<ticket>  N/A
    OANDA
    Balance update
    Financing (Swap: One entry for all open orders.)

 
William Roeder #:
  1. Do not assume history has only closed orders.
              OrderType() == 6, 7 in the history pool? - MQL4 programming forum (2017)

  2. Do not assume history is ordered by date, it's not.
              Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum (2012)
              Taking the last profit and storing it in a variable | MQL4 - MQL4 programming forum #3 (2020)

  3. Total Profit is OrderProfit() + OrderSwap() + OrderCommission(). Some brokers don't use the Commission/Swap fields. Instead, they add balance entries. (Maybe related to Government required accounting/tax laws.)
              "balance" orders in account history - Day Trading Techniques - MQL4 programming forum (2017)

    Broker History
    FXCM
    Commission - <TICKET>
    Rollover - <TICKET>

    >R/O - 1,000 EUR/USD @0.52

    #<ticket>  N/A
    OANDA
    Balance update
    Financing (Swap: One entry for all open orders.)

No worries, thanks :)