Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

The simplest CSV file writer - MetaTrader 5 script | Trading script download - MT4/MT5 resources

author EAcpu | 6 reads | 0 comments |

Use this simplest class to write CSV files without any preparation and type conversion declarations.

Find the shortest usage example below.

 string filename = "filename_writer.csv" ;

  CDKSimplestCSVWriter CSVFile; // Create class object // Add first row and set value by column name CSVFile.AddRow();
  CSVFile.SetLastRowValue( "time" , string time ( time current (), TIME_DATE | TIME_MINUTES ));
  CSVFile.SetLastRowValue( "Open" , double to string ( symbol information double ( symbol (), SYMBOL_BID ), _number ));
  CSVFile.SetLastRowValue( "Close" , double to string ( symbol information double ( symbol (), SYMBOL_ASK ), _number ));

// Add a second row of units using the returned row index and setting the value by name r = CSVFile.AddRow();
  CSVFile.SetValue(r, "time" , string time ( time current () + 60 , TIME_DATE | TIME_MINUTES ));
  CSVFile.SetValue(r, "open" , double to string ( symbol information double ( symbol (), SYMBOL_BID ), _number ));
  CSVFile.SetValue(r, "off" , double to string ( symbol information double ( symbol (), SYMBOL_ASK ), _number ));

// Demonstrate SetValue by column index (index 0..n-1). Here we set the first column of the first row to a custom value if (CSVFile.RowCount() > 0 && CSVFile.ColumnCount() > 0 )
    CSVFile.SetValue( 0 , 0 , "CUSTOM_TIME" );

//Write CSV to file. If necessary, ANSI encoded files can be written via FILE_ANSI. if (CSVFile.WriteCSV(filename, true , ";" , file_ANSI )) {
Print format ( "Successfully written %d rows and %d columns: %s" , 
                CSVFile.RowCount(), CSVFile.ColumnCount(), file name);
  } other {
printFormat ( "Error writing CSV file: %s" , filename);
  }

// Optional: clear memory CSVFile.Clear();



Attachment download

📎DKSimplestCSVWriter.mqh (7.62 KB)

📎 DKSimplestCSVWriter_Example.mq5 (2.28 KB)

Source: MQL5 #63261

Verification code Refresh