csv_to_hst_v1

Download script
Author: KLeTcHATyI
csv_to_hst_v1

This script is designed to automate the conversion of financial data from a standard spreadsheet format (CSV) into a format that the MetaTrader trading platform can natively recognize as historical price data (HST). It acts as a bridge for traders who have external historical price data and need to import it into their charting software.

How the Process Works

1. Input Selection The script begins by identifying two specific files:

  • Source File: A CSV file containing raw price information (e.g., USDCHF prices).
  • Destination File: A new history file that the script will create.

It also automatically sets the necessary technical parameters for this specific conversion, such as the time frame of the data (e.g., hourly or daily intervals) and the number of decimal places used in the pricing.

2. Establishing the Connection The script attempts to open both files simultaneously. It verifies that the source CSV is readable and prepares the destination file for writing. If it cannot access either file, the process will stop immediately to prevent data errors.

3. Formatting the Header MetaTrader requires a specific "header" or "identity card" at the beginning of every history file so it knows how to read the data correctly. The script automatically generates this metadata, including:

  • Software copyright information.
  • The currency symbol (e.g., USDCHF).
  • The timeframe and digit precision.
  • Required technical placeholders that ensure the platform doesn't reject the file as "corrupt."

4. Data Migration Loop This is the core of the script. It reads the source CSV file line by line. For every entry, it captures:

  • The Date and Time.
  • The Opening Price.
  • The Highest Price reached.
  • The Lowest Price reached.
  • The Closing Price.
  • The Trading Volume.

For each line found in the spreadsheet, the script translates these values into the specific binary structure required by MetaTrader and writes them into the new history file in the correct order.

5. Completion and Cleanup Once the script reaches the end of the CSV file, it performs a final check to ensure all data was processed successfully. It then saves the new history file and safely closes both connections. A success message is then printed to the system log, indicating that your custom historical data is now ready for use in the platform.

Miscellaneous
Uses files from the file systemIt writes information to fileIt writes information to fileIt writes information to fileIt reads information from a fileIt reads information from a fileIt writes information to file
18 Views
0 Downloads
0 Favorites
csv_to_hst_v1
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                                   csv_to_hst.mq4 |
//|                                                       KLeTcHATyI |
//|                                                  nowindows@nm.ru |
//+------------------------------------------------------------------+
#property copyright "KLeTcHATyI"
#property link      "nowindows@nm.ru"
#property show_inputs

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
//---- TODO: Add your code.
   // input
   // files must be in expert/files directory,
   string csv_filename = "USDCHF1440.csv";
   string hst_filename = "TEST.hst";
   int i_period = 1440;
   int i_digits = 4;
   string c_symbol = "USDCHF";
   
   //open csv
   int csv_file = FileOpen(csv_filename, FILE_CSV|FILE_READ, ',');
   if(csv_file < 0) return(-1);
   
   //save hst
   int hst_file = FileOpen(hst_filename, FILE_BIN|FILE_WRITE);
   if(hst_file < 0) return(-1);
   //header
   string c_copyright="(C)opyright 2003, MetaQuotes Software Corp.";
   int    version=400;
   int    i_unused[13];
   FileWriteInteger(hst_file, version, LONG_VALUE);
   FileWriteString(hst_file, c_copyright, 64);
   FileWriteString(hst_file, c_symbol, 12);
   FileWriteInteger(hst_file, i_period, LONG_VALUE);
   FileWriteInteger(hst_file, i_digits, LONG_VALUE);
   FileWriteInteger(hst_file, 0, LONG_VALUE);       //timesign
   FileWriteInteger(hst_file, 0, LONG_VALUE);       //last_sync
   FileWriteArray(hst_file, i_unused, 0, 13);
  
   
   string yyyymmdd, hhmm;
   double d_open, d_high, d_low, d_close, d_volume;
   int t_time;
   //1971.01.04,00:00,4.3180,4.3180,4.3180,4.3180,0
   while (FileIsEnding(csv_file) == false)
   {
     yyyymmdd = FileReadString(csv_file);
     if (yyyymmdd != "") // as a rule, metatrader .csv has empty line
     {
       hhmm = FileReadString(csv_file);
       d_open = FileReadNumber(csv_file);
       d_high = FileReadNumber(csv_file);
       d_low = FileReadNumber(csv_file);
       d_close = FileReadNumber(csv_file);
       d_volume = FileReadNumber(csv_file);
       
       t_time = StrToTime(yyyymmdd+' '+hhmm);
       FileWriteInteger(hst_file, t_time, LONG_VALUE);
       FileWriteDouble(hst_file, d_open, DOUBLE_VALUE);
       FileWriteDouble(hst_file, d_low, DOUBLE_VALUE);
       FileWriteDouble(hst_file, d_high, DOUBLE_VALUE);
       FileWriteDouble(hst_file, d_close, DOUBLE_VALUE);
       FileWriteDouble(hst_file, d_volume, DOUBLE_VALUE);
     }
   }  
   
   Print("csv_to_hst OK");
   
   // close
   FileClose(hst_file);
   FileClose(csv_file);
   
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments