Writing datas in a file

 

Hi,

 

I want to write data to a CSV file for analyzing. It writes effectively but only one valye, and not each value at each tick or bar. There is something i don't understand. Anyone can help me?

 

Here is my code, copied and adapted from mql5 reference. The write code is in a function, called in OnTick().


Thank you for advance.  :) 

//--- Writing datas
   ResetLastError();
   string terminal_data_path=TerminalInfoString(TERMINAL_DATA_PATH);
   int filehandle=FileOpen("datas.csv",FILE_WRITE|FILE_CSV);
      if(filehandle!=INVALID_HANDLE)
     {
      FileWrite(filehandle,Eur);
      FileClose(filehandle);
      Print("The file is created in the folder "+terminal_data_path);
     }
   else Print("File open failed, error ",GetLastError());
 
fxseedtrading:

Hi,

I want to write data to a CSV file for analyzing. It writes effectively but only one valye, and not each value at each tick or bar. There is something i don't understand. Anyone can help me?

Here is my code, copied and adapted from mql5 reference. The write code is in a function, called in OnTick().

Thank you for advance.  :) 

Hi fxseedtrading,

Try this. Read the example of file's combination of flags https://www.mql5.com/en/docs/constants/io_constants/fileflags.

:D

//+------------------------------------------------------------------+
void OnTick()
  {
//---
   //--- Writing datas
   MqlTick Tick_tock;
   ResetLastError();
   int file_handle = FileOpen("datas.csv",FILE_READ|FILE_WRITE|FILE_CSV);
   if (file_handle != INVALID_HANDLE)
     {
     if (SymbolInfoTick(Symbol(),Tick_tock))
        {
        FileSeek(file_handle, 0, SEEK_END);
        FileWrite(file_handle,Tick_tock.time,DoubleToString(Tick_tock.ask),DoubleToString(Tick_tock.bid));   
        }
        else Print("SymbolInfoTick() failed, error = ",GetLastError());
     FileClose(file_handle);   
     }
   else Print("File open failed, error ",GetLastError());
   //---
  }
//+------------------------------------------------------------------+
Documentation on MQL5: Standard Constants, Enumerations and Structures / Input/Output Constants / File Opening Flags
Documentation on MQL5: Standard Constants, Enumerations and Structures / Input/Output Constants / File Opening Flags
  • www.mql5.com
Standard Constants, Enumerations and Structures / Input/Output Constants / File Opening Flags - Documentation on MQL5
Reason: