indicator_to_csv

Download script
Author: Inovance
indicator_to_csv

This tool is designed to bridge the gap between your trading platform and external data analysis software by automatically exporting key market data into a structured spreadsheet format. Instead of manually copying prices or values, this script performs the heavy lifting of gathering technical data and organizing it into a persistent file.

How the Logic Works

The script operates by scanning through your historical price data and performing three primary actions simultaneously:

1. Technical Data Calculation The tool monitors two specific technical indicators: the Commodity Channel Index (CCI) and the Relative Strength Index (RSI). It calculates these values based on customizable periods, allowing you to see how the market's momentum and trend strength are behaving at any given point in time.

2. Comprehensive Market Recording For every historical "bar" or candle on your chart, the script captures a complete snapshot of the market environment. This includes:

  • Time and Price Action: The exact timestamp, along with the Open, High, Low, and Close prices.
  • Market Activity: The trading volume for that period.
  • Technical Metrics: The specific calculated values for both the CCI and RSI indicators.

3. Automated File Export The core functionality lies in its ability to "write" this information to an external CSV file. Rather than just displaying these numbers on your screen, the script:

  • Opens a dedicated spreadsheet-ready file (CSV).
  • Organizes the data into labeled columns (Headers) so it is immediately readable by programs like Excel or Google Sheets.
  • Appends every piece of data—from the oldest historical point to the most recent—into the file.
  • Automatically manages the opening and closing of the file during the process to ensure the data is saved correctly without corruption.

Summary for the User

Essentially, this script acts as a data logger. It watches your charts, performs technical calculations in the background, and builds a comprehensive database of market history in a CSV file. This allows you to move your trading data out of the platform and into your preferred spreadsheet software for deeper research, backtesting, or record-keeping.

Indicators Used
Commodity channel indexRelative strength index
Miscellaneous
Implements a curve of type %1Uses files from the file systemIt writes information to file
13 Views
0 Downloads
0 Favorites
indicator_to_csv
//+------------------------------------------------------------------+
//|                                             Indicator_to_CSV.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Inovance"
#property link      "https://www.inovancetech.com/"
#property description "Save indicator values and OHLC data to a csv file."
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Blue

   //Indicator periods
input int CCIPeriod = 14;
input int RSIPeriod = 14;

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


   //Indicator Buffers
double CCIBuffer[];
double RSIBuffer[];


//+------------------------------------------------------------------+
//| Initialization function                                          |
//+------------------------------------------------------------------+
int OnInit()
  {
      //Indicator buffers mapping
   SetIndexBuffer(0,CCIBuffer);
   SetIndexBuffer(1,RSIBuffer);
   
      //Indicator styling
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_LINE);
   
//---
   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--) 
         { 
         
            //Indicators
            CCIBuffer[i] = iCCI(NULL,0,CCIPeriod,PRICE_OPEN,i);
            RSIBuffer[i] = iRSI(NULL,0,RSIPeriod,PRICE_OPEN,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","CCI(14)","RSI(14)");
            
            //Go to end of file
            FileSeek(handle,0,SEEK_END);
            
            //Record Indicator values
            FileWrite(handle,Time[i],Open[i],High[i],Low[i],Close[i],Volume[i],CCIBuffer[i],RSIBuffer[i]);
            
            //Close file
            FileClose(handle);    
            
         }
         
      return(0);
  }

Comments