hst2csv
Download scriptThis script is designed to serve as a data conversion utility for MetaTrader 4 users. Its primary purpose is to take historical price data stored in the platform's internal format and export it into a standard CSV (Comma-Separated Values) file, which can then be easily opened in spreadsheet software like Microsoft Excel or Google Sheets.
Core Logic Overview
The script follows a structured four-step process to move data from the trading platform to an external readable format:
1. File Validation and Access
Before performing any actions, the script verifies that the user has provided a valid historical data file (with a .hst extension). It checks whether the file is located in the standard "History" folder or another directory. If the file is invalid or cannot be accessed, the script will display an error message and stop immediately to prevent data corruption.
2. Metadata Extraction Instead of simply copying raw numbers, the script intelligently reads the "header" of the source file. It identifies specific information such as the currency pair symbol (e.g., EURUSD) and the timeframe (e.g., 5-minute charts). It uses this information to automatically name the output CSV file, ensuring that your exported data is organized correctly.
3. Systematic Data Parsing The script then performs a systematic "read and write" operation:
- Reading: It opens the source file and scans through every single price candle one by one. It extracts the specific data points for each candle: time, opening price, high price, low price, closing price, and trading volume.
- Formatting: It converts the raw timestamp into a human-readable date and time format.
- Writing: It places this organized information into a new CSV file. Each candle becomes a new row in the spreadsheet, with the different values (Price, Volume, Time) separated by semi-colons for clean formatting.
4. Finalization and Reporting Once the script reaches the end of the historical data, it systematically closes both the source and destination files to ensure no data is lost or locked. Finally, it provides a pop-up notification informing the user that the conversion is complete and specifies exactly where the new CSV file was saved within the MetaTrader "Experts\files" folder.
Key Features for the User:
- Automation: Handles the tedious task of manual data entry by extracting all price points automatically.
- Error Handling: Includes built-in checks to alert you if a file is missing, corrupted, or formatted incorrectly.
- Seamless Integration: Automatically generates an organized CSV filename based on the content of the source file.
//+------------------------------------------------------------------+
//| hst2csv.mq4 |
//| Copyright © 2007, Kiriyenko Dmitriy |
//| http://kiriyenko.moikrug.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, Kiriyenko Dmitriy"
#property link "http://kiriyenko.moikrug.ru"
//----
#property show_inputs
//----
#include <WinUser32.mqh>
#include <stdlib.mqh>
//----
#define FILE_NAME "hst2csv.ex4"
#define WRONG_FILE_EXT "Íåâåðíûé òèï ôàéëà: ìîæíî çàäàâàòü òîëüêî *.hst ôàéëû"
#define WRONG_FILE_EXT_HDR "Îøèáêà âõîäíûõ äàííûõ"
//----
extern string input_file_name = "EURUSD5.hst";
extern bool input_file_in_history = true;
//----
int hst_handle;
int err;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
// îïðåäåëåíèå òèïà âõîäíîãî ôàéëà
if(file_ext(input_file_name) != "hst")
{
MessageBox(WRONG_FILE_EXT,WRONG_FILE_EXT_HDR, MB_OK | MB_ICONSTOP);
return(-3);
}
// îòêðûòèå âõîäíîãî ôàéëà
if(input_file_in_history)
hst_handle = FileOpenHistory(input_file_name, FILE_BIN | FILE_READ);
else
hst_handle = FileOpen(input_file_name, FILE_BIN | FILE_READ);
err = GetLastError();
if(hst_handle < 0 || err > 0)
return(error_out(err, "Îòêðûòèå âõîäíîãî ôàéëà"));
// ÷òåíèå çàãîëîâêà
FileSeek(hst_handle, 68, SEEK_SET);
err = GetLastError();
if(err > 0)
return(error_out(err, "Ñìåùåíèå âî âõîäíîì ôàéëå ê ñèìâîëó"));
string hst_symbol = FileReadString(hst_handle, 12);
int hst_period = FileReadInteger(hst_handle, LONG_VALUE);
err = GetLastError();
if(err > 0)
return (error_out(err, "×òåíèå çàãîëîâêà *.hst ôàéëà"));
FileSeek(hst_handle, 64, SEEK_CUR);
err = GetLastError();
if(err > 0)
return(error_out(err, "Ñìåùåíèå âî âõîäíîì ôàéëå ê ïåðâîé êîòèðîâêå"));
// ïîïûòêà îòêðûòü âûõîäíîé ôàéë
string csv_file_name = StringConcatenate(hst_symbol, hst_period, ".csv");
int csv_handle = FileOpen(csv_file_name, FILE_CSV | FILE_WRITE);
err = GetLastError();
if(csv_handle < 0 || err > 0)
return(error_out(err,"Îòêðûòèå âûõîäíîãî ôàéëà"));
// ïîêà âõîäíîé ôàéë íå çàêîí÷èòñÿ
while(!FileIsEnding(hst_handle))
{
err = GetLastError();
if(err > 0)
return(error_out(err, "Ïðîâåðêà EOF äëÿ îñíîâíîãî öèêëà"));
// ïîáàéòîâîå ÷òåíèå èç âõîäíîãî ôàéëà
int bar_time = FileReadInteger(hst_handle, LONG_VALUE);
double bar_open = FileReadDouble(hst_handle, DOUBLE_VALUE);
double bar_low = FileReadDouble(hst_handle, DOUBLE_VALUE);
double bar_high = FileReadDouble(hst_handle, DOUBLE_VALUE);
double bar_close = FileReadDouble(hst_handle, DOUBLE_VALUE);
double bar_volume = FileReadDouble(hst_handle, DOUBLE_VALUE);
err = GetLastError();
if(err == 4099)
break; // åñëè ôàéë çàêîí÷èëñÿ, çàâåðøàåì öèêë
if(err > 0)
return(error_out(err,"×òåíèå èç âõîäíîãî ôàéëà"));
// ïðåîáðàçîâàíèå âðåìåíè â ñòðîêó
string bar_time_str = StringConcatenate(TimeToStr(bar_time, TIME_DATE), ";",
TimeToStr(bar_time, TIME_MINUTES));
// ïîñòðî÷íàÿ çàïèñü â âûõîäíîé ôàéë
FileWrite(csv_handle, bar_time_str, bar_open, bar_high, bar_low,
bar_close, bar_volume);
err = GetLastError();
if(err > 0)
return(error_out(err,"Çàïèñü â èñõîäÿùèé ôàéë"));
}
err = GetLastError();
if(err > 0)
return(error_out(err, "Ïðîâåðêà EOF äëÿ îñíîâíîãî öèêëà"));
// çàêðûòèå ôàéëîâ
FileClose(hst_handle);
FileClose(csv_handle);
err = GetLastError();
if(err > 0)
return(error_out(err, "Çàêðûòèå ôàéëîâ"));
MessageBox("Â ïàïêå òåðìèíàëà MT4: \Experts\files\ ñîçäàí ôàéë:\n\n" + csv_file_name,
"Ïðåîáðàçîâàíèå ôàéëà óñïåøíî çàâåðøåíî", MB_OK | MB_DEFBUTTON1);
err = GetLastError();
if(err > 0)
return(error_out(err, "Âûâîä ñîîáùåíèÿ î çàâåðøåíèè ðàáîòû"));
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int error_out(int err, string where)
{
if(err == 0)
return;
string message = StringConcatenate("Îøèáêà ¹", err, ":\"",
ErrorDescription(err), "\"\n",
"âîçíèêëà ïðè âûïîëíåíèè îïåðàöèè \"",
where,"\"");
string caption = StringConcatenate("Âîçíèêëà îøèáêà â ìîäóëå: \"",
FILE_NAME, "\"!");
MessageBox(message,caption,MB_OK | MB_ICONSTOP);
return(err);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
string file_ext(string file_name)
{
string result = StringSubstr(file_name,
StringLen(input_file_name) - 3, 3);
int err = GetLastError();
if(err > 0)
return(error_out(err, "Îáðàáîòêà çàäàííîé ñòðîêè-èìåíè ôàéëà"));
return (result);
}
//+------------------------------------------------------------------+
Comments