CSV producer

Download script
Author: MojoFX
CSV producer

This tool is designed to act as an automated data exporter for MetaTrader users who need to extract historical price information into a structured, readable file format. Instead of manually copying and pasting data from your trading platform, this script automates the gathering of specific market statistics over a designated period.

How the Logic Works

1. Scope Selection The script begins by determining how much historical data you wish to export. You can specify a fixed number of "bars" (which represent individual price candles on your chart). If no specific number is set, the tool is designed to automatically capture the entire available history for that specific asset.

2. File Preparation and Organization Before it begins gathering data, the script identifies where to save the information. It creates a unique filename based on the specific currency pair (Symbol) and the timeframe (Period) you are currently viewing. To ensure you always have the most recent data without duplicates, it clears any previous versions of this file before starting the new export.

3. Data Extraction Loop The script then moves through your trading history candle by candle. For every candle identified in your requested range, it performs the following actions:

  • Date Formatting: It extracts the year, month, and day from each candle and organizes them into a standardized date format (YYYY-MM-DD).
  • Price Normalization: It captures the Open, High, Low, and Close prices. Crucially, it converts these prices into whole numbers by adjusting for the decimal places used by the broker. This makes the data much easier to use in spreadsheets or external calculation tools.
  • Volume Recording: It also captures the trading volume associated with each candle.

4. Exporting the Results Each piece of information—the date, the asset name, the four price points, and the volume—is written into a comma-separated values format. This structure ensures that once the script finishes running, you are left with a clean, organized file ready to be opened in applications like Microsoft Excel or Google Sheets for further analysis.

Miscellaneous
Uses files from the file systemIt writes information to file
14 Views
0 Downloads
0 Favorites
CSV producer
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                                 CSV producer.mq4 |
//|                                                           MojoFX |
//|                                                fx.studiomojo.com |
//+------------------------------------------------------------------+
#property copyright "MojoFX"
#property link      "fx.studiomojo.com"
#property show_confirm
#property show_inputs

extern int barQty = 500;
extern string fileExtension = ".txt";
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
   {
   if (barQty == 0) barQty = Bars-1; 
   
//----
   FileDelete(Symbol()+Period()+fileExtension);
   for (int i=barQty; i>0; i--)
      {
      int handle;      
      handle = FileOpen(Symbol()+Period()+fileExtension, FILE_CSV|FILE_WRITE|FILE_READ,',');      
      if (handle>0)
         {
         string mmStr,ddStr;
         if (TimeDay(Time[i])<10) ddStr="0"+TimeDay(Time[i]); else ddStr=TimeDay(Time[i]);
         if (TimeMonth(Time[i])<10) mmStr="0"+TimeMonth(Time[i]); else mmStr=TimeMonth(Time[i]);
         string dateStr = TimeYear(Time[i])+"-"+mmStr+"-"+ddStr;
         FileSeek(handle,0,SEEK_END);
         FileWrite(handle,
            dateStr,
            Symbol(),
            Open[i]*MathPow(10,Digits),
            High[i]*MathPow(10,Digits),
            Low[i]*MathPow(10,Digits),
            Close[i]*MathPow(10,Digits),
            Volume[i]);
         FileClose(handle);
         }      
      }
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments