This script is designed to automate the collection of specific market data and export it into a structured file format. Its primary purpose is to gather price information and technical moving average calculations across multiple timeframes simultaneously, allowing for easier analysis outside of the trading platform.
How the Logic Works
The script operates by systematically scanning through different financial symbols and time intervals to compile a comprehensive dataset. Here is the step-by-step breakdown of its logic:
1. Symbol Selection and Initialization The script identifies specific currency pairs or assets (for example, EURCHF) to monitor. It then initiates a process to cycle through these chosen assets one by one.
2. Multi-Timeframe Data Alignment For every asset identified, the script looks at the market history starting from the most recent data point and moves backward. The core logic involves "time alignment":
- It identifies a specific moment in time on a short-term chart (1-minute interval).
- It then automatically locates the corresponding price candles on much larger timeframes, such as 5-minute, 60-minute, and 240-minute (4-hour) intervals.
3. Technical Calculation Gathering As it moves through the history, the script extracts several specific data points for each point in time:
- Closing Prices: It records the price at which the market closed.
- Moving Averages: It calculates three different Simple Moving Averages (SMAs) for each of the larger timeframes (based on 5, 10, and 15-period windows).
4. Automated File Export Instead of just displaying this data on your screen, the script creates a dedicated CSV file for each asset. It writes the following information into rows:
- The specific timestamp.
- The closing price of the short-term interval.
- All the calculated Moving Average values from the various longer timeframes.
Summary of Output The end result is a clean, organized spreadsheet (CSV file) that aligns different market "speeds" into a single row. This allows you to see exactly what the moving averages looked like on 5-minute, hourly, and 4-hour charts at any specific minute in history, making it highly useful for backtesting or deep data analysis.
//+------------------------------------------------------------------+
//| RSI_to_File.mq4 |
//| Copyright © 2007, MetaQuotes Software Corp. |
//| http://www.metaquotes.ru/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link "http://www.metaquotes.ru/"
#define NR_SYMB 6
#property show_inputs
string SymbolsArray[NR_SYMB]={"","EURCHF"};
//+------------------------------------------------------------------+
//| string SymbolByNumber |
//+------------------------------------------------------------------+
string GetSymbolString(int Number)
{
//----
string res="";
res=SymbolsArray[Number];
//----
return(res);
}
//+------------------------------------------------------------------+
//| âîçâðàùàåò ïåðèîä |
//+------------------------------------------------------------------+
/*
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);
}
*/
//+------------------------------------------------------------------+
//| âûâîäèò â ôàéë êîòèðîâêè + çíà÷åíèÿ èíäèêàòîðà |
//+------------------------------------------------------------------+
double x0,x1,x2,x3,x4,x5,x6,x7,x8,x9;
void Ma_output(
string SymbolName,
int PeriodMinutes1,
int PeriodMinutes5,
int PeriodMinutes60,
int PeriodMinutes240
)
{
int size=iBars(SymbolName,PeriodMinutes1);
//----
if (size==0) return;
int handle=FileOpen(SymbolName+"1_.csv",FILE_WRITE|FILE_CSV,",");
if (handle<0) return;
//FileWrite(handle,"TIME,OPEN,LOW,HIGH,CLOSE,VOLUME");
//FileWrite(handle,"N10,N8.5,N8.5,N8.5,N8.5,N6");
for (int i=size-1;i>=0;i--)
{
datetime time1=iTime(SymbolName,PeriodMinutes1,i);
int b5=iBarShift(SymbolName,PeriodMinutes5,time1,true);
int b60=iBarShift(SymbolName,PeriodMinutes60,time1,true);
int b240=iBarShift(SymbolName,PeriodMinutes240,time1,true);
if(b5>=0)
{
x0=iClose(SymbolName,PeriodMinutes5,b5);
x1=iMA(SymbolName,PeriodMinutes5,5,0,MODE_SMA,PRICE_CLOSE,b5);
x2=iMA(SymbolName,PeriodMinutes5,10,0,MODE_SMA,PRICE_CLOSE,b5);
x3=iMA(SymbolName,PeriodMinutes5,15,0,MODE_SMA,PRICE_CLOSE,b5);
}
if(b60>=0)
{
x4=iMA(SymbolName,PeriodMinutes60,5,0,MODE_SMA,PRICE_CLOSE,b60);
x5=iMA(SymbolName,PeriodMinutes60,10,0,MODE_SMA,PRICE_CLOSE,b60);
x6=iMA(SymbolName,PeriodMinutes60,15,0,MODE_SMA,PRICE_CLOSE,b60);
}
if(b240>=0)
{
x7=iMA(SymbolName,PeriodMinutes240,5,0,MODE_SMA,PRICE_CLOSE,b240);
x8=iMA(SymbolName,PeriodMinutes240,10,0,MODE_SMA,PRICE_CLOSE,b240);
x9=iMA(SymbolName,PeriodMinutes240,15,0,MODE_SMA,PRICE_CLOSE,b240);
}
FileWrite(handle,
iTime(SymbolName,PeriodMinutes1,i),
iClose(SymbolName,PeriodMinutes1,i),
x0,
x1,
x2,
x3,
x4,
x5,
x6,
x7,
x8,
x9
);
}
FileClose(handle);
//----
return;
}
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
int start()
{
int SymbolCounter,PeriodCounter;
//----
for (SymbolCounter=1;SymbolCounter<NR_SYMB;SymbolCounter++)
{
Ma_output(GetSymbolString(SymbolCounter),PERIOD_M1,PERIOD_M5,PERIOD_H1,PERIOD_H4);
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments