Why am I getting .dat file instead of .csv when exporting?

 

I’m trying to export my strategy signal data into a .csv file. The file is created successfully in MQL5/ , but instead of agreement_export.csv I always get a file with .dat extension. Could someone explain why this happens, and what is the correct way to force MT5 to generate a proper .csv instead of .dat ?

Here is my current code(shortened for clarity):

input string OutFileName = "agreement_export.csv"; // saved in MQL5/Files/

int fileHandle = INVALID_HANDLE;
datetime lastLoggedCandleTime = 0;

//---------------------------------------------------------------
bool OpenCsv()
{
   if(fileHandle != INVALID_HANDLE) return true;

   fileHandle = FileOpen(OutFileName, FILE_WRITE|FILE_CSV|FILE_ANSI, ',');
   if(fileHandle == INVALID_HANDLE)
   {
      Print("Failed to open file! Error: ", GetLastError());
   }
   FileSeek(fileHandle, 0, SEEK_END);
   
   if(FileTell(fileHandle) == 0 && WriteHeaderIfMissing)
   {
      FileWrite(fileHandle,
      "Time","Bullish Agr.","Bearish Agr.",
      "1M Open Bid","1M Open Ask","1M High Bid","1M High Ask",
      "1M Low Bid","1M Low Ask","1M Close Bid","1M Close Ask",
      "1M Status","5M Status","15M Status","30M Status","1H Status","4H Status","1D Status"
   );
      FileFlush(fileHandle);
   }
   return true;
}

//---------------------------------------------------------------
int OnInit()
{
   Print("agreement_exporter_v2 started. Output: ", OutFileName);
   OpenCsv();
   return INIT_SUCCEEDED;
}

void OnDeinit(const int reason)
{
   if(fileHandle != INVALID_HANDLE)
   {
      FileClose(fileHandle);
      Print("closed file");
   }
}

//---------------------------------------------------------------
void OnTick()
{
   
// --- Code to get values to be written in csv 

   // --- Build row
   FileWrite(fileHandle,
         TimeToString(lastClosedM1, TIME_DATE|TIME_MINUTES),
         bullAgr,
         bearAgr,
         oBid, oAsk,
         hBid, hAsk,
         lBid, lAsk,
         cBid, cAsk,
         statuses[0],
         statuses[1],
         statuses[2],
         statuses[3],
         statuses[4],
         statuses[5],
         statuses[6]      // 1D Status
      );
   FileFlush(fileHandle);
}
 
  1. Make sure that you are not using cached inputs when you place the program on the chart. You may have set the input in the past with a ".dat" and it is using the cached parameter. So, reset the inputs or verify that it is correctly set.
  2. Make sure you are looking in the correct output folder.
The file is opened in the folder of the client terminal in the subfolder MQL5\files (or testing_agent_directory\MQL5\files in case of testing). If FILE_COMMON is specified among flags, the file is opened in a shared folder for all MetaTrader 5 client terminals.
 
Fernando Carreiro #:
  1. Make sure that you are not using cached inputs when you place the program on the chart. You may have set the input in the past with a ".dat" and it is using the cached parameter. So, reset the inputs or verify that it is correctly set.
  2. Make sure you are looking in the correct output folder.
I have never used .dat extension but have still tried resetting the inputs but I am still getting .dat file instead of .csv
 
vaishali2304 #I have never used .dat extension but have still tried resetting the inputs but I am still getting .dat file instead of .csv

Since your code is not self-contained for us to test, I reduced it as follows and ran a live test. It functioned without issue ...

input string OutFileName = "agreement_export.csv"; // saved in MQL5/Files/

int fileHandle = INVALID_HANDLE;
datetime lastLoggedCandleTime = 0;

//---------------------------------------------------------------
bool OpenCsv()
{
   if(fileHandle != INVALID_HANDLE) return true;

   fileHandle = FileOpen(OutFileName, FILE_WRITE|FILE_CSV|FILE_ANSI, ',');
   if(fileHandle == INVALID_HANDLE)
   {
      Print("Failed to open file! Error: ", GetLastError());
   }
   FileSeek(fileHandle, 0, SEEK_END);
   
   // if(FileTell(fileHandle) == 0 && WriteHeaderIfMissing)
   {
      FileWrite(fileHandle,
      "Time","Bullish Agr.","Bearish Agr.",
      "1M Open Bid","1M Open Ask","1M High Bid","1M High Ask",
      "1M Low Bid","1M Low Ask","1M Close Bid","1M Close Ask",
      "1M Status","5M Status","15M Status","30M Status","1H Status","4H Status","1D Status"
   );
      FileFlush(fileHandle);
   }
   return true;
}

//---------------------------------------------------------------
int OnInit()
{
   Print("agreement_exporter_v2 started. Output: ", OutFileName);
   OpenCsv();
   return INIT_SUCCEEDED;
}

void OnDeinit(const int reason)
{
   if(fileHandle != INVALID_HANDLE)
   {
      FileClose(fileHandle);
      Print("closed file");
   }
}

//---------------------------------------------------------------
void OnTick() {
}

Time,Bullish Agr.,Bearish Agr.,1M Open Bid,1M Open Ask,1M High Bid,1M High Ask,1M Low Bid,1M Low Ask,1M Close Bid,1M Close Ask,1M Status,5M Status,15M Status,30M Status,1H Status,4H Status,1D Status
 
vaishali2304 #I have never used .dat extension but have still tried resetting the inputs but I am still getting .dat file instead of .csv

Please provide more details of how you are running it. On a live chart or in the tester?

Show the log output and screenshot of the folder where the file is located, etc.

Also tell us what build of MT5 you are using.

 
Fernando Carreiro #:

Since your code is not self-contained for us to test, I reduced it as follows and ran a live test. It functioned without issue ...

Thank you for the now the code is working after a few changes.