Close_Norm_to_File
Download scriptThis script is designed to automate the process of exporting specific financial data from your trading platform into an external CSV file. It essentially acts as a bridge, taking processed market information and saving it into a format that can be easily opened in spreadsheet software like Microsoft Excel or Google Sheets.
How the Logic Works
1. Data Identification and Selection When the script runs, it first identifies which financial instrument (the "Symbol") you are currently viewing and determines the specific timeframe required for the export. It is programmed to look at the Daily timeframe by default.
2. Calculating the Data Scope The tool automatically calculates how many historical bars (completed price candles) exist for that specific symbol and timeframe. This ensures that the script knows exactly how much information it needs to process without requiring the user to manually input a limit.
3. File Creation Once the scope is established, the script creates a new CSV file on your computer. The filename is automatically generated using the name of the asset and the timeframe, making it easy to organize multiple exports for different markets (e.g., Gold, EURUSD, etc.).
4. Data Extraction and Normalization This is the core functionality of the script:
- It iterates backward through every historical candle from the most recent one back to the oldest available data point.
- For each candle, it retrieves a specific value provided by a "Normalized" calculation.
- Rather than just grabbing the raw closing price, it pulls a specialized value (based on "MTF_Normalize") which aligns data across different timeframes. This ensures that the data being saved is consistent and formatted correctly for further analysis.
5. Saving and Closing Each piece of processed data is written line-by-line into the CSV file. Once every historical candle has been processed, the script securely closes the file to prevent data corruption, leaving you with a ready-to-use report on your hard drive.
//+------------------------------------------------------------------+
//| Close_to_File.mq4 |
//| Copyright © 2008, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
void Close_output(string SymbolName,int PeriodMinutes)
{
int size=iBars(SymbolName,PeriodMinutes);
//----
if (size==0) return;
int handle=FileOpen(SymbolName+PeriodMinutes+"_Close_Norm.csv",FILE_WRITE|FILE_CSV);
if (handle<0) return;
// FileWrite(handle,"Time seconds;Time;Open;Low;High;Close;Volume;RSI");
for (int i=size-1;i>=0;i--)
{
// FileWrite(handle,iTime(SymbolName,PeriodMinutes,i),TimeToStr(iTime(SymbolName,PeriodMinutes,i))
// ,iOpen(SymbolName,PeriodMinutes,i),iLow(SymbolName,PeriodMinutes,i),iHigh(SymbolName,PeriodMinutes,i)
// ,iClose(SymbolName,PeriodMinutes,i),iVolume(SymbolName,PeriodMinutes,i),iCustom(SymbolName,PeriodMinutes,"RSI",0,i));
// FileWrite(handle,iCustom(SymbolName,PeriodMinutes,"RSI",0,i));
/*
extern string Symbol1 = "";
extern int ClosePeriod = 8;
extern int NrBars = 500;
extern int Shift1=0;
extern int price=PRICE_CLOSE; //PRICE_HIGH,PRICE_LOW,PRICE_OPEN
extern int Period1=PERIOD_W1;
*/
FileWrite(handle,iCustom(SymbolName,PeriodMinutes,"MTF_Normalize",SymbolName,8,2000,0,PRICE_CLOSE,PERIOD_D1,0,i));
}
FileClose(handle);
//----
return;
}
int PeriodNumber(int number)
{
int per_min;
switch (number)
{
case 0: per_min=PERIOD_M1;break;
case 1: per_min=PERIOD_M5;break;
case 2: per_min=PERIOD_M15;break;
case 3: per_min=PERIOD_M30;break;
case 4: per_min=PERIOD_H1;break;
case 5: per_min=PERIOD_H4;break;
default: per_min=PERIOD_D1;break;
}
return(per_min);
}
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
int start()
{
//----
Close_output(Symbol(),PeriodNumber(6));
//----
return(0);
}
//+------------------------------------------------------------------+
Comments