data_to_csv

Download script
Author: Inovance
data_to_csv

This script is designed to act as an automated data extraction tool for MetaTrader users who need to export historical market information into a format compatible with external software, such as Microsoft Excel or Google Sheets.

Core Functionality

The primary purpose of this script is to capture specific price action data—specifically the "Open," "High," "Low," "Close," and "Volume" (OHLCV) values—and save them into a structured Comma Separated Values (.csv) file. By converting real-time or historical chart data into a CSV format, it allows traders to move their information from a trading platform into an analysis spreadsheet for auditing, backtesting, or personal record-keeping.

How the Logic Works

The script follows a systematic process to ensure every piece of data on your chart is captured accurately:

  • File Configuration: At the start, the script identifies the name of the file it should create (by default, "PriceData.csv"). It sets up a standard header row so that when you open the file in a spreadsheet, each column is clearly labeled (Timestamp, Open, High, Low, Close, and Volume).
  • Data Scanning: The logic automatically calculates how many historical bars (candles) are currently visible on your chart. Instead of just grabbing the current price, it looks back through the entire available history.
  • Sequential Extraction: The script loops through the historical data one candle at a time. For every single candle identified, it performs the following steps:
    1. Opens a connection to the destination file.
    2. Identifies the specific price points for that moment in time.
    3. Appends that specific row of information to the bottom of the document.
    4. Closes and refreshes the file connection to ensure the data is saved properly.
  • Continuous Updating: Because this script runs as an indicator, it stays active on your chart. As new price movements occur or as you scroll through history, the script ensures that the underlying data points are processed and moved into your external file systematically.

Key Benefits for the User

  • Seamless Exporting: You no longer have to manually copy and paste figures from your trading terminal.
  • Data Integrity: By capturing all five primary price metrics (OHLCV) plus the exact timestamp, you maintain a complete and accurate history of the asset's movement.
  • Compatibility: Since it exports to .csv, the data is immediately ready for use in any standard data analysis or accounting software.
Miscellaneous
Uses files from the file systemIt writes information to file
6 Views
0 Downloads
0 Favorites
data_to_csv
//+------------------------------------------------------------------+
//|                                                  Data_to_CSV.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Inovance"
#property link      "https://www.inovancetech.com/"
#property description "Save OHLCV data to a csv file."
#property version   "1.00"
#property strict
#property indicator_chart_window

   //Filename
input string   FileName = "PriceData.csv";



//+------------------------------------------------------------------+
//| Initialization function                                          |
//+------------------------------------------------------------------+
int OnInit()
  {
  
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
      //Define variables
      int limit,i;
      int counted_bars = IndicatorCounted();
      
      //Make sure on most recent bar
      if(counted_bars>0) counted_bars--;
   
      //Set limit
      limit = Bars - counted_bars - 1;
      
      
      //Main loop
      for(i = limit - 1; i>=0; i--) 
         { 
          
            //Create and Open file
            int handle=FileOpen(FileName,FILE_CSV|FILE_READ|FILE_WRITE,",");
            
            //Name column headers
            FileWrite(handle,"Open Timestamp","Open","High","Low","Close","Volume");
            
            //Go to end of file
            FileSeek(handle,0,SEEK_END);
            
            //Record data
            FileWrite(handle,Time[i],Open[i],High[i],Low[i],Close[i],Volume[i]);
            
            //Close file
            FileClose(handle);    
            
         }
         
      return(0);
  }

Comments