Attrib_to_File2_AUDUSD
Download scriptThis script is designed to automatically export historical market data and specific moving average calculations from your trading platform into an external CSV file. Instead of manually tracking different timeframes, this tool synchronizes multiple price points into a single organized report.
Core Functionality
The primary purpose of the script is to scan through a specific set of financial symbols (such as AUDUSD) and collect data across four different time intervals simultaneously: 1-minute, 5-minute, 1-hour, and 4-hour periods.
How the Logic Works
- Multi-Timeframe Synchronization: The script looks at each individual candle on a 1-minute chart. For every single 1-minute candle, it "looks back" to find the corresponding price data for the larger timeframes (5-minute, 1-hour, and 4-hour). This ensures that the final report aligns all different time perspectives into one chronological row.
- Moving Average Calculations: For the larger timeframes (5m, 60m, and 240m), the script specifically calculates three different Simple Moving Averages (SMA) based on periods of 5, 10, and 15 units. These are captured for each timeframe to provide a comprehensive view of trend momentum at various scales.
- Data Compilation: For every point in history, the script gathers the following information:
- The specific timestamp.
- The closing price of the 1-minute candle.
- The closing price of the corresponding 5-minute candle.
- The three moving averages for the 5-minute timeframe.
- The three moving averages for the 1-hour timeframe.
- The three moving averages for the 4-hour timeframe.
- Automated File Export: Once the calculations are complete, the script automatically creates a CSV file named after the symbol (e.g.,
AUDUSD1_.csv). It writes all the compiled data into this file in a structured format, making it ready for use in external spreadsheets like Microsoft Excel or Google Sheets.
Summary of Output
The end result is a comprehensive dataset where each row represents a single minute of trading activity, populated with synchronized price points and moving average indicators across four different time horizons. This allows for advanced analysis without the need to manually switch between different charts.
//+------------------------------------------------------------------+
//| 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]={"","AUDUSD"};
//+------------------------------------------------------------------+
//| 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