Author: KLeTcHATyI
csv_to_hst

This script is designed to act as a data converter for the MetaTrader 4 platform. Its primary purpose is to take historical price information from a standard spreadsheet format (CSV) and transform it into a specialized binary format (.hst) that the trading software can read natively as historical chart data.

How the Process Works

The logic follows a linear, step-by-step conversion process:

1. Configuration and Initialization The script begins by defining specific parameters for the conversion. It identifies which source file to read (the CSV), what to name the new output file, and specific market details such as the currency pair (e.g., USDCHF), the timeframe (e.g., 1440 minutes), and the number of decimal digits.

2. Establishing Connections The tool then attempts to "handshake" with your computer's files:

  • It opens the Source File in a read-only mode to look at the existing data.
  • It creates a new Destination File in a binary write mode, where the converted data will be saved.

3. Creating the Header Before moving any price data, the script must "introduce" the new file to MetaTrader. It writes a mandatory header of information. This tells the software essential metadata, including:

  • The copyright information and software version.
  • The specific currency symbol and timeframe.
  • Technical placeholders required by the platform's architecture to recognize the file as valid history.

4. The Conversion Loop This is the core engine of the script. It reads the CSV file line-by-line from top to bottom:

  • It extracts six specific pieces of information for every entry: the Date, Time, Opening Price, Highest Price, Lowest Price, Closing Price, and Trading Volume.
  • It cleans the data by skipping any empty lines it encounters.
  • It combines the Date and Time into a single timestamp that the software can understand.
  • It then "writes" these values into the new binary file in the exact order and format required for a historical chart.

5. Completion and Cleanup Once the script reaches the end of the CSV file, it performs a final cleanup by closing both files. This ensures that no data is corrupted and the memory is freed up. If successful, it provides a confirmation message in the system log to let you know the conversion is complete.

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
8 Views
1 Downloads
0 Favorites
csv_to_hst
//+------------------------------------------------------------------+
//|                                                   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