Attrib_to_File2_GBPUSD

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

This script is designed to automate the collection and export of specific financial market data into an organized spreadsheet format (CSV). It acts as a data mining tool that scans historical price information across multiple timeframes simultaneously to create a unified dataset for later analysis.

Core Functionality

The primary purpose of this script is to take raw price data from different trading periods—specifically 1-minute, 5-minute, 60-minute (hourly), and 240-minute (4-hour) charts—and save them into a single file for every currency pair defined in its settings.

How the Logic Works

The script follows a structured process to ensure data accuracy:

  • Multi-Symbol Processing: The tool is configured to loop through a specific list of financial instruments (for example, GBPUSD). It performs the same operations on each instrument sequentially.
  • Timeframe Synchronization: One of the more complex aspects of this script is how it handles different timeframes. Because a 1-minute chart has many more data points than a 4-hour chart, the script identifies the exact "matching" moment in history for each timeframe. It looks at every 1-minute candle and then "looks back" to find the corresponding prices and averages on the longer timeframes (5m, 60m, and 240m) that occurred at that specific time.
  • Moving Average Calculation: For each of the longer timeframes, the script calculates three different types of Simple Moving Averages (SMA). Specifically, it extracts values for periods of 5, 10, and 15 units. This allows a user to see how trends are behaving on short, medium, and long scales at the exact same moment in time.
  • Automated Data Export: Instead of displaying this information on a chart, the script writes all gathered values—including the date/time, the closing price, and all the calculated moving averages—directly into a CSV file.

Summary of Output

By running this script, a user generates a comprehensive historical log where every row represents a specific point in time. Each row contains a "snapshot" of the market's health, showing not just what the price was at that minute, but also what the smoothed trends (Moving Averages) were looking like across four different time horizons simultaneously. This is highly useful for traders who want to perform deep-dive statistical analysis or backtesting using external software like Excel or specialized data tools.

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
9 Views
0 Downloads
0 Favorites
Attrib_to_File2_GBPUSD
//+------------------------------------------------------------------+
//|                                                  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]={"","GBPUSD"};

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