Attrib_to_File3_EURUSD

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

This script is designed to automate the collection and export of specific financial market data from MetaTrader 4 into an external CSV file. It acts as a data extraction tool, gathering multi-timeframe information for various currency pairs or assets.

Core Functionality

The primary purpose of the logic is to iterate through a list of predefined trading symbols (such as EURUSD) and record specific price points and moving average calculations. Instead of just looking at the current price on one chart, the script synchronizes data across multiple different timeframes simultaneously.

How the Logic Works

1. Multi-Symbol Processing The script starts by identifying which assets it needs to analyze. It loops through a set list of symbols, ensuring that the data collection happens for each asset defined in the system's configuration.

2. Timeframe Synchronization For every symbol, the script performs a "time-shift" analysis. Because different timeframes (like 1-minute, 5-minute, 1-hour, and 4-hour charts) do not align perfectly, the logic calculates exactly which candle on a larger timeframe corresponds to a specific point in time on a smaller timeframe. This ensures that the data saved to the file is chronologically accurate across all scales.

3. Data Points Collected During the process, the script captures several types of information:

  • Base Price: It records the closing price of the current candle on the primary timeframe.
  • Moving Averages: It calculates three different Simple Moving Averages (SMAs) for three different timeframes (5-minute, 60-minute, and 240-minute).
  • Historical Offset: The moving averages are specifically pulled from the "previous" candle of those respective timeframes to ensure the data is finalized and not subject to current-candle fluctuations.

4. Automated Export Once the calculations for a specific point in time are complete, the script organizes all these values into a single row. It then automatically writes this data into a CSV file named after the symbol. This includes:

  • The exact timestamp.
  • The closing price.
  • The calculated moving average values for all three major timeframes.

Summary of Workflow

The script behaves like an automated data logger: it looks at a specific asset, calculates how various long-term trends (moving averages) interact with short-term prices at any given moment in history, and saves that entire snapshot into a spreadsheet-ready file for later use.

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
13 Views
0 Downloads
0 Favorites
Attrib_to_File3_EURUSD
//+------------------------------------------------------------------+
//|                                                  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]={"","EURUSD"};

//+------------------------------------------------------------------+
//| 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;
double y0,y1,y2,y3,y4,y5,y6,y7,y8,y9;
double z0,z1,z2,z3,z4,z5,z6,z7,z8,z9;
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_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);

        y1=iMA(SymbolName,PeriodMinutes5,5,0,MODE_SMA,PRICE_CLOSE,b5+1);
        y2=iMA(SymbolName,PeriodMinutes5,10,0,MODE_SMA,PRICE_CLOSE,b5+1);
        y3=iMA(SymbolName,PeriodMinutes5,15,0,MODE_SMA,PRICE_CLOSE,b5+1);

        //z1=iMA(SymbolName,PeriodMinutes5,5,0,MODE_SMA,PRICE_CLOSE,b5+2);
        //z2=iMA(SymbolName,PeriodMinutes5,10,0,MODE_SMA,PRICE_CLOSE,b5+2);
        //z3=iMA(SymbolName,PeriodMinutes5,15,0,MODE_SMA,PRICE_CLOSE,b5+2);                
      }
      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);

        y4=iMA(SymbolName,PeriodMinutes60,5,0,MODE_SMA,PRICE_CLOSE,b60+1);
        y5=iMA(SymbolName,PeriodMinutes60,10,0,MODE_SMA,PRICE_CLOSE,b60+1);
        y6=iMA(SymbolName,PeriodMinutes60,15,0,MODE_SMA,PRICE_CLOSE,b60+1);

        //z4=iMA(SymbolName,PeriodMinutes60,5,0,MODE_SMA,PRICE_CLOSE,b60+2);
        //z5=iMA(SymbolName,PeriodMinutes60,10,0,MODE_SMA,PRICE_CLOSE,b60+2);
        //z6=iMA(SymbolName,PeriodMinutes60,15,0,MODE_SMA,PRICE_CLOSE,b60+2);
        
      }       
      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);

        y7=iMA(SymbolName,PeriodMinutes240,5,0,MODE_SMA,PRICE_CLOSE,b240+1);
        y8=iMA(SymbolName,PeriodMinutes240,10,0,MODE_SMA,PRICE_CLOSE,b240+1);
        y9=iMA(SymbolName,PeriodMinutes240,15,0,MODE_SMA,PRICE_CLOSE,b240+1);

        //z7=iMA(SymbolName,PeriodMinutes240,5,0,MODE_SMA,PRICE_CLOSE,b240+2);
        //z8=iMA(SymbolName,PeriodMinutes240,10,0,MODE_SMA,PRICE_CLOSE,b240+2);
        //z9=iMA(SymbolName,PeriodMinutes240,15,0,MODE_SMA,PRICE_CLOSE,b240+2);
        
      }
        
      FileWrite(handle,
                  iTime(SymbolName,PeriodMinutes1,i),
                  iClose(SymbolName,PeriodMinutes1,i),
                  x0,                                   
                  y1,
                  y2,
                  y3,  
                  y4,
                  y5,
                  y6,  
                  y7,
                  y8,
                  y9                  
                  );
      }
   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