astohastic_csv
Download scriptThis script is designed to bridge the gap between MetaTrader (a trading platform) and external data analysis tools by automatically exporting real-time market indicator data into a structured spreadsheet format.
Overview of Functionality
The primary purpose of this tool is to capture specific technical analysis values—specifically from a Stochastic Oscillator—and save them into a CSV file. This allows users to move beyond the visual charts provided by the platform and utilize the raw data for external auditing, advanced statistical modeling, or custom record-keeping in programs like Microsoft Excel or Google Sheets.
How the Logic Works
The script operates through three distinct phases:
1. Initialization and Setup When you first load the script, it performs a "handshake" with your computer's file system. It creates a new CSV file (by default named "Stochastic_CSV.csv") and sets up the framework for the data. During this stage, it also writes a header row, which acts as the labels for your columns (e.g., Timestamp, Main Value, and Signal Value), ensuring the resulting spreadsheet is organized and easy to read.
2. Continuous Data Monitoring Once active, the script constantly monitors the market history. It looks back through the price bars on your current chart. Rather than just looking at the present moment, it systematically scans the historical data points to ensure that the exported file contains a complete chronological record of the indicator's movements.
3. Data Extraction and Export For every historical bar it processes, the script performs two specific actions:
- Time Stamping: It converts the exact date and time of each price candle into a standardized text format (Year_Month_Day_Hour_Minute_Second). This ensures that every piece of data is tied to a specific moment in history.
- Indicator Calculation: It retrieves two specific values from the Stochastic Oscillator: the "Main" line and the "Signal" line.
The script then bundles these three pieces of information (Time, Main Value, and Signal Value) into a single row and writes it directly into the CSV file.
Key User Benefits
- Automation: You do not have to manually copy and paste numbers from your trading platform; the script handles the data collection in the background.
- External Compatibility: Because the output is a CSV file, the data is immediately ready for use in any standard spreadsheet software.
- Precision: By capturing both the Main and Signal lines of the Stochastic indicator, the tool provides a comprehensive look at momentum trends rather than just a single data point.
//+------------------------------------------------------------------+
//| Indi2csv.mq4 |
//| Heaton Research |
//| http://www.heatonresearch.com/encog |
//| simplified by Reza rahmad|
//+------------------------------------------------------------------+
#property copyright "Heaton Research"
#property link "http://www.heatonresearch.com/encog"
#property strict
#property indicator_separate_window
extern string file_name = "Stohastic_CSV.csv";
extern int Kperiod=14;
extern int Dperiod = 3;
extern int Slowing = 3;
int fileh =-1;
int lasterror;
extern
//+------------------------------------------------------------------+
int init()
{
IndicatorShortName("Indicators2CSV");
fileh = FileOpen(file_name,FILE_CSV|FILE_WRITE|FILE_SHARE_READ,',');;
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,"stoch_main","stoch_Signal");
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,
iStochastic(Symbol(),0,Kperiod,Dperiod,Slowing,MODE_SMMA,0,MODE_MAIN,barind),iStochastic(Symbol(),0,Kperiod,Dperiod,Slowing,MODE_SMMA,0,MODE_SIGNAL,barind)
);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
Comments