Attrib_to_File2_EURJPY

Download script
Author: Copyright � 2007, MetaQuotes Software Corp.
Attrib_to_File2_EURJPY

This script is designed to automate the extraction of specific market data from MetaTrader and save it into a structured CSV file. It essentially acts as a data collector that gathers historical price points and technical indicators across different timeframes simultaneously.

Core Functionality

The primary purpose of this script is to take historical market information for a designated currency pair (such as EURJPY) and export it into a spreadsheet-friendly format. Instead of just looking at a single moment in time, the script scans through the history of the "1-minute" chart and pulls related data from much larger timeframes (5-minute, 1-hour, and 4-hour).

How It Works Logically

To achieve this, the script follows a specific three-step process:

1. Data Identification The script begins by identifying which currency pair to analyze. Once selected, it looks at every individual "bar" (the smallest unit of time movement) on a 1-minute chart.

2. Multi-Timeframe Synchronization This is the most critical part of the logic. For every single 1-minute price point it finds, the script "looks ahead" to see what was happening on larger timeframes at that exact same moment in history. It calculates three specific Moving Averages (calculated over 5, 10, and 15 units) for each of the following periods:

  • The 5-minute chart
  • The 1-hour chart
  • The 4-hour chart

By doing this, it creates a comprehensive snapshot of how the market was behaving on a micro level (minutes) versus a macro level (hours) at any given second.

3. Automated Exporting Finally, the script organizes all this gathered information—including the timestamp, the closing price of the 1-minute bar, and all nine calculated Moving Averages from the various timeframes—and writes them into a single row in a CSV file.

Summary for the User

Think of this tool as a "Data Historian." Instead of you having to manually check different charts and write down prices or moving average values into an Excel sheet, this script does it automatically. It captures a massive amount of multi-layered market data and saves it into a file that you can then use for independent analysis, reporting, or external research.

Price Data Components
Series array that contains close prices for each bar
Indicators Used
Moving average indicator
Miscellaneous
Uses files from the file systemIt writes information to file
10 Views
0 Downloads
0 Favorites
Attrib_to_File2_EURJPY
//+------------------------------------------------------------------+
//|                                                  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]={"","EURJPY"};

//+------------------------------------------------------------------+
//| 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