Author: Heaton Research
indi2csv

The Indi2csv script is a utility designed for traders who need to extract specific market data and technical indicator values from their trading platform into an external format. Instead of manually recording price movements or indicator readings, this tool automates the process of "harvesting" this information into a structured spreadsheet file (CSV) for later analysis.

How the Tool Functions

The script operates by following a three-step logical flow: Initialization, Data Collection, and File Management.

1. Setup and Preparation

When you first activate the script, it creates a new data file on your system. It automatically sets up the "header" or the top row of this file, labeling columns for different types of information. These include basic price data (Open, Close, and Time) as well as several popular technical indicators such as ATR, CCI, MACD, RSI, Stochastic Oscillator, and WPR.

2. Historical Data Processing

Once active, the script looks back at the historical price candles on your current chart. It systematically moves backward through time, visiting every completed candle one by one. For every single candle it identifies, it performs a "snapshot" of the market at that specific moment.

3. Automated Data Export

For every snapshot taken, the script gathers a comprehensive set of values including:

  • Time Stamps: A precise date and time for each entry.
  • Price Points: The exact opening and closing prices of the candle.
  • Technical Indicators: It calculates the specific numerical values for various indicators (like RSI or MACD) at that exact point in time based on standard settings.

All of this gathered information is then neatly organized and written into a row in your CSV file.

Key Benefits for Users

By transforming live chart data into a static file, this script allows you to move your trading analysis away from the platform and into software like Excel or Google Sheets. It eliminates the need for manual data entry, ensuring that the historical data used for backtesting or statistical research is accurate and consistent with what was actually displayed on your trading charts.

Indicators Used
Indicator of the average true rangeCommodity channel indexMACD HistogramRelative strength indexStochastic oscillatorLarry William percent range indicator
Miscellaneous
Uses files from the file systemIt writes information to file
8 Views
0 Downloads
0 Favorites
indi2csv
//+------------------------------------------------------------------+
//|                                                     Indi2csv.mq4 |
//|                                                  Heaton Research |
//|                              http://www.heatonresearch.com/encog |
//|                                simplified by Mustafa Doruk Basar |
//+------------------------------------------------------------------+
#property copyright "Heaton Research"
#property link      "http://www.heatonresearch.com/encog"
#property strict
#property indicator_separate_window

extern string file_name = "Indi2csv.csv";

int fileh =-1;
int lasterror;

//+------------------------------------------------------------------+

int init()
  {
 
   IndicatorShortName("Indicators2CSV");

   fileh = FileOpen(file_name,FILE_CSV|FILE_WRITE,',');
   if(fileh<1)
   {
      lasterror = GetLastError();
      Print("Error updating file: ",lasterror);
      return(false);
   }
   
   // file header - need to be the identifiers of the indicators to be exported   
   FileWrite(fileh,"time","close","open","atr","cci","macd","rsi","stoch","wpr");

   return(0);
   
  }

//+------------------------------------------------------------------+

int deinit()
  {
      if(fileh>0) 
      {
         FileClose(fileh);
      }
   
   return(0);
   
  }
  
//+------------------------------------------------------------------+
  
int start()
  {
   int barcount = IndicatorCounted();
   if (barcount<0) return(-1);
   if (barcount>0) barcount--;
   
   int barind=Bars-barcount-1;
   
      while(barind>1)
      {
         ExportIndiData(barind);
         barind--;
      }
      
   return(0);
   
  }
//+------------------------------------------------------------------+

void ExportIndiData(int barind) 
{
   datetime t = Time[barind];
   string inditime =  
      StringConcatenate(TimeYear(t)+"_"+
                        TimeMonth(t)+"_"+
                        TimeDay(t)+"_"+
                        TimeHour(t)+"_"+
                        TimeMinute(t)+"_"+
                        TimeSeconds(t));
                        
   // add indicators at will (do not forget to update line 31!
   FileWrite(fileh, 
         inditime,
			Close[barind],
			Open[barind],
			iATR(Symbol(),0,14,barind),
			iCCI(Symbol(),0,14,PRICE_CLOSE,barind),
			iMACD(Symbol(),0,12,26,9,PRICE_CLOSE,0,barind),
			iRSI(Symbol(),0,14,PRICE_CLOSE,barind),
			iStochastic(Symbol(),0,5,3,3,MODE_EMA,0,0,barind),
			iWPR(Symbol(),0,14,barind)
			);
			
}

//+------------------------------------------------------------------+ 

Comments