metastock_to_csv

Download script
Author: KLeTcHATyI
metastock_to_csv

This script is designed as a specialized data converter that bridges the gap between two different types of financial software. Its primary purpose is to take historical market data exported from MetaStock (in a .txt format) and convert it into a standardized .csv file that can be easily imported and read by the MetaTrader trading platform.

How the Process Works

The script follows a systematic "read, transform, and write" logic to ensure that data remains accurate during the transition between platforms:

1. File Identification and Access The tool looks for a specific input file (the MetaStock text file) and prepares a new destination file (the MetaTrader CSV). It establishes a secure connection to read the source information and creates a blank slate for the output, ensuring that no original data is overwritten or lost during the process.

2. Header Skipping Raw data files often contain a "header" row—a single line at the very top that labels what each column represents (e.g., Ticker, Price, Time). To prevent the trading platform from trying to read these labels as actual numbers, the script automatically identifies and skips over this first row of text before beginning its work.

3. Data Extraction and Cleaning The script then scans the remainder of the file line by line. For every row it finds, it isolates the specific pieces of information needed for trading:

  • Price Data: It captures the Opening, High, Low, and Closing prices.
  • Volume Data: It identifies the trading volume.
  • Time Formatting: This is a critical step. The script takes the raw date and time strings from the original file and reorganizes them into a standardized "Year.Month.Day Hour:Minute" format that MetaTrader recognizes perfectly.

4. Exporting the Result Once the data has been extracted and the time has been reformatted, the script writes these clean values into the new CSV file. By doing this, it strips away unnecessary information (like Ticker symbols or internal codes) and leaves only the essential price history required for chart analysis and backtesting.

Summary of Results

Once the script finishes its run, it closes both files to preserve data integrity and provides a confirmation message. The end result is a clean, organized CSV file where every price point is correctly aligned with a standardized timestamp, making it ready for immediate use in your trading environment.

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

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

/*
  This script transform metastock .txt file to metatrader .csv file
  metastock : <TICKER>,<PER>,<DTYYYYMMDD>,<TIME>,<OPEN>,<HIGH>,<LOW>,<CLOSE>,<VOL>,<OPENINT>
  see input
*/


//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
//---- TODO: Add your code.
   // input
   // files must be in expert/files directory,
   string metastock_filename = "chfd.txt";
   string csv_filename = "USDCHF1440.csv";
   //int i_digits = 4;
   //string c_symbol = "USDCHF";
   

   //open metastock txt
   int metastock_file = FileOpen(metastock_filename, FILE_CSV|FILE_READ, ',');
   if(metastock_file < 0) return(-1);
   
   //save metatrader csv
   int csv_file = FileOpen(csv_filename, FILE_CSV|FILE_WRITE,',');
   if(csv_file < 0) return(-1);
   
   string yyyymmdd, hhmmss;
   string s_open, s_high, s_low, s_close, s_volume;
   string s_ticker, s_period, s_time;
   
   // skip header of metastock file <TICKER>,<PER>,<DTYYYYMMDD>,<TIME>,<OPEN>,<HIGH>,<LOW>,<CLOSE>,<VOL>,<OPENINT>
   string tmp = FileReadString(metastock_file); //ticker
   tmp = FileReadString(metastock_file); //per
   tmp = FileReadString(metastock_file);
   tmp = FileReadString(metastock_file);
   tmp = FileReadString(metastock_file);
   tmp = FileReadString(metastock_file);
   tmp = FileReadString(metastock_file);
   tmp = FileReadString(metastock_file);
   tmp = FileReadString(metastock_file);
   tmp = FileReadString(metastock_file); //openint
   
   //CHF,D,19710104,000000,4.3180,4.3180,4.3180,4.3180,0,0
   while (FileIsEnding(metastock_file) == false)
   {
     s_ticker = FileReadString(metastock_file); // last line in file may be empty
     if (s_ticker != "")
     {
       s_period = FileReadString(metastock_file);
       yyyymmdd = FileReadString(metastock_file);
       hhmmss = FileReadString(metastock_file);
       s_open = FileReadString(metastock_file);
       s_high = FileReadString(metastock_file);
       s_low = FileReadString(metastock_file);
       s_close = FileReadString(metastock_file);
       s_volume = FileReadString(metastock_file);
       tmp = FileReadString(metastock_file); //open interest
       
       s_time = StringSubstr(yyyymmdd, 0, 4)+"."+StringSubstr(yyyymmdd, 4, 2)+"."+StringSubstr(yyyymmdd, 6, 2)+" "+StringSubstr(hhmmss, 0, 2)+":"+StringSubstr(hhmmss, 2, 2);
       
       FileWrite(csv_file, s_time, s_open, s_high, s_low, s_close, s_volume);
     }
   }  
   
   FileClose(metastock_file);
   FileClose(csv_file);
   
   Print("metastock_to_csv OK");
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments