Download MetaTrader 5

Download History of one currency pair from MetaTrader 5?

To add comments, please log in or register
disthedat
18
disthedat  

Hi,

Any smart guy here, I want to write a script that can download the history data of a currency pair in different time frames, so I can do analysis on it outside metatrader 5 software. How can I do this? and how reliable is the data that I download?

Snelle Moda
614
Snelle Moda  

Hi


Try this custom indicator. It does what you describe.

Make a new "custom indicator" file and call it Data.

put this code in it and compile.

Attach this custom indicator to your desired chart with timeframe.

All the BARS on the chart will be copied to a CSV file, located in the "FILES\DATA" map in you main MQL5 directory.



//+------------------------------------------------------------------+
//|                                                         Data.mq5 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Moda"
#property link      "LOL.nl"
#property version   "2.00"
#property indicator_chart_window

#property indicator_buffers 1
#property indicator_plots   1

// Parameters for writing data to the file.
input string InpDirectoryName = "Data"; // Directory name.
input bool   InpStringValue   = true;   // Use string values for the Date/Time output in Excel.

string FileName;     // File name 
string PeriodName;   // Name for the Chart time Period.
int    nfile_handle; // Handle


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
   {

   if(Period() == PERIOD_M1)       {PeriodName = "M1";}
   else if(Period() == PERIOD_M2)  {PeriodName = "M2";}  
   else if(Period() == PERIOD_M3)  {PeriodName = "M3";} 
   else if(Period() == PERIOD_M4)  {PeriodName = "M4";}
   else if(Period() == PERIOD_M5)  {PeriodName = "M5";}
   else if(Period() == PERIOD_M6)  {PeriodName = "M6";}
   else if(Period() == PERIOD_M10) {PeriodName = "M10";}
   else if(Period() == PERIOD_M12) {PeriodName = "M12";}
   else if(Period() == PERIOD_M15) {PeriodName = "M15";} 
   else if(Period() == PERIOD_M20) {PeriodName = "M20";} 
   else if(Period() == PERIOD_M30) {PeriodName = "M30";} 
   else if(Period() == PERIOD_H1)  {PeriodName = "H1";}  
   else if(Period() == PERIOD_H2)  {PeriodName = "H2";}  
   else if(Period() == PERIOD_H3)  {PeriodName = "H3";} 
   else if(Period() == PERIOD_H4)  {PeriodName = "H4";} 
   else if(Period() == PERIOD_H6)  {PeriodName = "H6";}  
   else if(Period() == PERIOD_H8)  {PeriodName = "H8";} 
   else if(Period() == PERIOD_H12) {PeriodName = "H12";}  
   else if(Period() == PERIOD_D1)  {PeriodName = "D1";}  
   else if(Period() == PERIOD_W1)  {PeriodName = "W1";} 
   else if(Period() == PERIOD_MN1) {PeriodName = "MN1";} 

// Use current symbol name as file name.  
   FileName = Symbol() + "_" + PeriodName + ".csv"; 
  
   return(INIT_SUCCEEDED);
   }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
   {

// CHECK FOR A NEW BAR.
//---------------------

// Rates structure array for last two bars 
   MqlRates mrate[2];                 
   CopyRates(Symbol(), Period(), 0, 2, mrate);

// NEW BAR CHECK.
//---------------
   static double   dBar_Open;     
   static double   dBar_High;
   static double   dBar_Low;
   static double   dBar_Close;
   static long     lBar_Volume;
   static datetime nBar_Time;

// Boolean for confirmation. 
   bool bStart_NewBar = false;

// Check if the price data has changed tov the previous bar.   
   if(mrate[0].open != dBar_Open || mrate[0].high != dBar_High || mrate[0].low != dBar_Low || mrate[0].close != dBar_Close || mrate[0].tick_volume != lBar_Volume || mrate[0].time != nBar_Time || prev_calculated == 0)
         {
         bStart_NewBar = true;         
     
         dBar_Open   = mrate[0].open;      
         dBar_High   = mrate[0].high;
         dBar_Low    = mrate[0].low;
         dBar_Close  = mrate[0].close;                 
         lBar_Volume = mrate[0].tick_volume;
         nBar_Time   = mrate[0].time;
         }

// Check if the indicator has not yet been calculated or if a new bar has formed.   
   if(bStart_NewBar == true || prev_calculated == 0)
         {

// Number of BARS to copy   
         static int nHistoryPeriod = 1;
         nHistoryPeriod = Bars(Symbol(), Period()) - 1;

// Data array for storage.         
         datetime aData_Time[];
         double   aData_Open[];
         double   aData_High[];
         double   aData_Low[];
         double   aData_Close[]; 
         long     aData_Volume[]; 

// Copy data to array.              
         CopyTime(Symbol(),       Period(), 1, nHistoryPeriod, aData_Time);
         CopyOpen(Symbol(),       Period(), 1, nHistoryPeriod, aData_Open);
         CopyHigh(Symbol(),       Period(), 1, nHistoryPeriod, aData_High);
         CopyLow(Symbol(),        Period(), 1, nHistoryPeriod, aData_Low);
         CopyClose(Symbol(),      Period(), 1, nHistoryPeriod, aData_Close);
         CopyTickVolume(Symbol(), Period(), 1, nHistoryPeriod, aData_Volume);

//------------------------------------------------------------------------------------
      
// Check/create CSV file      
         nfile_handle = FileOpen(InpDirectoryName + "//" + FileName, FILE_READ|FILE_WRITE|FILE_CSV);   

// Check if file is valid.         
         if(nfile_handle != INVALID_HANDLE)
               {
        
// Save the description header in Excel 
               FileWrite(nfile_handle, "Time/Date", "Open", "HIGH", "LOW", "CLOSE", "VOLUME");
               
// Check array size.
               int nArray_Size = ArraySize(aData_Close);
                        
// Loop calculation.
               for(int i = 0; i < nArray_Size; i++)
                     {

// Use string values for the date/time in Excel.
                     if(InpStringValue == true) 
                           {

// Write the values to the Excel file.                                  
                           FileWrite(nfile_handle, TimeToString(aData_Time[i], TIME_DATE|TIME_SECONDS), aData_Open[i], aData_High[i], aData_Low[i], aData_Close[i], aData_Volume[i]);                                               
                           }
                           else // Use numerical values for the date/time in Excel.
                           {
                           
// Write the values to the Excel file.                                  
                           FileWrite(nfile_handle, aData_Time[i], aData_Open[i], aData_High[i], aData_Low[i], aData_Close[i], aData_Volume[i]);                                                                                                   
                           }
                     }

// Alert for success.
               Alert("Copying successful for symbol: ", Symbol(), "  Timeframe: ", PeriodName, "  ", nArray_Size, " Bars written to excel file."); // "The data is written to a new file!"
      
// Close the file.
               FileClose(nfile_handle);          
               }
         }
   


//--- return value of prev_calculated for next call
   return(rates_total);
   }
//+------------------------------------------------------------------+
disthedat
18
disthedat  
Snelle Moda:

Hi


Try this custom indicator. It does what you describe.

Make a new "custom indicator" file and call it Data.

put this code in it and compile.

Attach this custom indicator to your desired chart with timeframe.

All the BARS on the chart will be copied to a CSV file, located in the "FILES\DATA" map in you main MQL5 directory.



Hi, I did that and it worked.

 

However, it gave only a cvs file with 100361 records, (rows in the csv), I did this on M1 time frame. The file is only till 

2015.12.09 19:35:00

How can I download it from the start of the time (like year 2000) ?

 

Also how one goes adding the spread data into this CSV? 

Snelle Moda
614
Snelle Moda  

The total amount of BARS copied is determined by the history available on the server of the broker.

The history start date in your terminal is probably 2015.12.09.

The script does copy the complete history in your terminal to the CSV file.

If you want to make a database, you have to build one yourself or take a look at sites like:

http://www.tickdatamarket.com/

You can also search a different broker with a longer history.

disthedat
18
disthedat  
Snelle Moda:

The total amount of BARS copied is determined by the history available on the server of the broker.

The history start date in your terminal is probably 2015.12.09.

The script does copy the complete history in your terminal to the CSV file.

If you want to make a database, you have to build one yourself or take a look at sites like:

http://www.tickdatamarket.com/

You can also search a different broker with a longer history.

Thanks buddy,

But it seems changing the broker didn't fix the problem. So I guess, I'll need to figure this out. That website you mentioned is not a viable option, their database is pricey,  and I don't trust them.

To add comments, please log in or register