//+------------------------------------------------------------------+
//|                                                 OHLCV_script.mq4 |
//|                                                         hapalkos |
//|                                                   April 30, 2006 |
//+------------------------------------------------------------------+
#property copyright "hapalkos"
#property link      ""
#property show_inputs

extern int start_year = 2006;    //January 1, 2001 is on a Monday (1) - market holiday
extern int start_month = 10;
extern int start_day = 1;
extern int end_year = 2007;
extern int end_month = 1;
extern int end_day = 31;

                                 // Period   minutes   bars    length of time
                                 
extern int bar_Period =60;       // weekly   10080     1480    28 years     back to 1978
                                 // daily    1440      7380    28 years     back to 1978
                                 // 4 hour   240       7940     5 years     back to April, 2001
                                 // 1 hour   60       16370    32 months    back to September, 2003
                                 // 30 min   30       16420    16 months    back to January, 2005
                                 // 15 min   15       16460     8 months    back to September, 2005
                                 // 5  min   5        20940     6 months    back to November, 2005
                                 // 1  min   1        36640    30 days      back to March, 2006
                                 
//----   STRATEGYBUILDERFX4                                 
string FinancialSymbols[] = {"AUDJPY","AUDUSD","CHFJPY","EURAUD","EURCHF","EURGBP","EURJPY","EURUSD","GBPCHF",
                              "GBPJPY","GBPUSD","NZDJPY","NZDUSD","USDCAD","USDCHF","USDJPY"};                           
//                              "GOLD","_DJI","_NQ100","_NQCOMP","_SP500"};         
/* 
//----   ALPARI                             
string FinancialSymbols[] = {"USDSEK","USDNOK","USDSGD","NZDUSD","USDZAR","CHFJPY",
                              "_DXY","_EP","_ENQ","_S","_C",                             
                              "_W","_CL","_NG","_GOLD","_SLV"};                               
*/
                                 
                                 
double array_period[0][6];

datetime   barDateTime[40000];   // Arrays are sized for maximum number of one minute bars
double     barOpen[40000];       // maintained in the history files.
double     barHigh[40000];       // 36640 bars for approximately 30 days of data
double     barLow[40000];
double     barClose[40000];
double     barVolume[40000];

int   x;
int   i; 
int   j;
int   bar_date;
int   start_bar;
int   end_bar;
int   handle;
string FinancialSymbol;
string DataFileName;
string DataFilePrefix = "\OHLCV\OHLCV_";    // OHLCV_...   File to be located at ...\experts\files\OHLCV\

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+

int start()
  {
  
//--------------------------------------------------------------------  
//---- 
//----   Adjustment so that when Timeframes less than one day are used,
//----   the whole day is retrieved.
//----
//--------------------------------------------------------------------

if(bar_Period <= 240)
   {
   end_day = end_day + 1;
   } 
   
//--------------------------------------------------------------------   
//----
//----   Symbols of financial instruments for which data is retrieved
//----
//--------------------------------------------------------------------

      for (int ii=0; ii<ArraySize(FinancialSymbols); ii++)
      {
         FinancialSymbol = FinancialSymbols[ii];
         

//-------------------------------------------------------------------- 
//----
//----   Concatenation of Start and End Dates
//----
//-------------------------------------------------------------------- 

      string start_date = StringConcatenate(start_year,".",start_month,".",start_day," 00:00");
      datetime start_date_time = StrToTime(start_date);
      
      string end_date = StringConcatenate(end_year,".",end_month,".",end_day,"  00:00");
      datetime end_date_time = StrToTime(end_date);
       
//--------------------------------------------------------------------
//----   
//----   Data File Name
//----
//--------------------------------------------------------------------

DataFileName = StringConcatenate(DataFilePrefix, FinancialSymbol, ".csv");
    {
    ArrayCopyRates(array_period,FinancialSymbol,bar_Period);

//--------------------------------------------------------------------    
//----
//----   Determination of End Bar
//----    
//-------------------------------------------------------------------- 

   if(bar_Period == 43200)
   {
    for (j=0;j<=Bars;j++)
    {
      if(end_date_time <= array_period[j][0])
           {
           end_bar = j + 1;
           }}}    
    
   if(bar_Period == 10080)
   {
    for (j=0;j<=10000;j++)
    {
      bar_date = TimeDayOfWeek(array_period[j][0]);

      if(end_date_time <= array_period[j][0] && bar_date == 0)
           {
           end_bar = j + 1;
           }}}
           
   if(bar_Period == 1440)
   {
    for (j=0;j<=10000;j++)
    {
      bar_date = TimeDayOfWeek(array_period[j][0]);

      if(end_date_time <= array_period[j][0])
           {
           end_bar = j;
           }}}
           
   if(bar_Period <= 240)
   {
    for (j=0;j<=10000;j++)
    {
      bar_date = TimeDayOfWeek(array_period[j][0]);

      if(end_date_time <= array_period[j][0])
           {
           end_bar = j+1;
           }}}

//--------------------------------------------------------------------  
//----
//----  Determination of Start Bar
//----
//-------------------------------------------------------------------- 
  
   if(bar_Period == 43200)
   {
    for (j=0;j<=10000;j++)
    {       
     if(start_date_time <= array_period[j][0] && ( bar_Period == 43200))
           {
          start_bar = j;
           }}}
           
   if(bar_Period == 10080)
   {
    for (j=0;j<=10000;j++)
    {
      bar_date = TimeDayOfWeek(array_period[j][0]);
        
     if(start_date_time <= array_period[j][0] && ( bar_date == 0) && ( bar_Period == 10080))
           {
          start_bar = j;
           }}}
           
   if(bar_Period == 1440)
   {
    for (j=0;j<=10000;j++)
    {
      bar_date = TimeDayOfWeek(array_period[j][0]);
        
     if(start_date_time <= array_period[j][0] && ( bar_Period == 1440) && ( bar_date == 1 || bar_date == 2 || bar_date == 3 || bar_date == 4 || bar_date == 5))
           {
          start_bar = j;
           }}}
           
   if(bar_Period <= 240)
   {
    for (j=0;j<=10000;j++)
    {
      bar_date = TimeDayOfWeek(array_period[j][0]);
        
     if(start_date_time <= array_period[j][0] && ( bar_Period <= 240))
           {
          start_bar = j;
           }}}                      

//-------------------------------------------------------------------- 
//----
//----   Print Message to Experts tab in Terminal window  
//----
//-------------------------------------------------------------------- 
   
    Print(FinancialSymbol,"     Start Date: ",start_date,"  start_bar ",start_bar,"     End Date: ", end_date,"  end_bar ",end_bar);

//--------------------------------------------------------------------  
//----
//----   Retrieval of data from arrays 
//----
//-------------------------------------------------------------------- 
   
     for(x=end_bar;x<=start_bar;x++)
     {
         bar_date = TimeDayOfWeek(array_period[x][0]);
         
         if((bar_Period == 1440 || bar_Period == 240 || bar_Period == 60 || bar_Period == 30 || bar_Period == 15 || bar_Period == 5 || bar_Period == 1) && ( bar_date == 1 || bar_date == 2 || bar_date == 3 || bar_date == 4 || bar_date == 5))
            {
            barDateTime[x]= array_period[x][0];
            barOpen[x] = array_period[x][1];
            barHigh[x] = array_period[x][3];      
            barLow[x]  = array_period[x][2];      
            barClose[x] = array_period[x][4];
            barVolume[x] = array_period[x][5]; 
            }
         if((bar_Period == 10080) && ( bar_date == 0))
            {
            barDateTime[x]= array_period[x][0];
            barOpen[x] = array_period[x][1];
            barHigh[x] = array_period[x][3];      
            barLow[x]  = array_period[x][2];      
            barClose[x] = array_period[x][4];
            barVolume[x] = array_period[x][5];
            }     
         if(bar_Period == 43200)
            {
            barDateTime[x]= array_period[x][0];
            barOpen[x] = array_period[x][1];
            barHigh[x] = array_period[x][3];      
            barLow[x]  = array_period[x][2];      
            barClose[x] = array_period[x][4];
            barVolume[x] = array_period[x][5];
            }                                       
      }     

//-------------------------------------------------------------------- 
//----   
//----   Writing of data to .csv files 
//----
//-------------------------------------------------------------------- 
     
      handle = FileOpen(DataFileName, FILE_CSV|FILE_WRITE,",");
      if(handle>0)
        {
         // write table columns headers
         
         FileWrite(handle, "TimeStamp,Symbol,barDateTime,DateTime,Open,High,Low,Close,Volume");
         
         // write data
         for( i=start_bar; i>=end_bar; i--)
         {
          bar_date = TimeDayOfWeek(array_period[i][0]);
          
          if(("1970.01.01 00:00" != TimeToStr(array_period[i][0])) && (bar_Period == 1440) && ( bar_date == 1 || bar_date == 2 || bar_date == 3 || bar_date == 4 || bar_date == 5))
            {        
            FileWrite(handle, TimeToStr(CurTime(),TIME_DATE|TIME_MINUTES),FinancialSymbol,TimeToStr(barDateTime[i],TIME_DATE|TIME_MINUTES),
               StringConcatenate(TimeMonth(array_period[i][0]),"/",TimeDay(array_period[i][0]),"/",TimeYear(array_period[i][0]),
               " ",TimeHour(array_period[i][0]),":",TimeMinute(array_period[i][0])),
               barOpen[i], barHigh[i], barLow[i], barClose[i], barVolume[i]);
            }
          if(("1970.01.01 00:00" != TimeToStr(array_period[i][0])) && (bar_Period <= 240))
            {        
            FileWrite(handle, TimeToStr(CurTime(),TIME_DATE|TIME_MINUTES),FinancialSymbol,TimeToStr(barDateTime[i],TIME_DATE|TIME_MINUTES),
               StringConcatenate(TimeMonth(array_period[i][0]),"/",TimeDay(array_period[i][0]),"/",TimeYear(array_period[i][0]),
               " ",TimeHour(array_period[i][0]),":",TimeMinute(array_period[i][0])), 
               barOpen[i], barHigh[i], barLow[i], barClose[i], barVolume[i]);
            }            
          if(("1970.01.01 00:00" != TimeToStr(array_period[i][0])) && (bar_Period == 10080) && ( bar_date == 0))
            {        
            FileWrite(handle, TimeToStr(CurTime(),TIME_DATE|TIME_MINUTES),FinancialSymbol,TimeToStr(barDateTime[i],TIME_DATE|TIME_MINUTES),
               StringConcatenate(TimeMonth(array_period[i][0]),"/",TimeDay(array_period[i][0]),"/",TimeYear(array_period[i][0]),
               " ",TimeHour(array_period[i][0]),":",TimeMinute(array_period[i][0])), 
               barOpen[i], barHigh[i], barLow[i], barClose[i], barVolume[i]);
            }
          if(("1970.01.01 00:00" != TimeToStr(array_period[i][0])) && bar_Period == 43200)
            {        
            FileWrite(handle, TimeToStr(CurTime(),TIME_DATE|TIME_MINUTES),FinancialSymbol,TimeToStr(barDateTime[i],TIME_DATE|TIME_MINUTES),
               StringConcatenate(TimeMonth(array_period[i][0]),"/",TimeDay(array_period[i][0]),"/",TimeYear(array_period[i][0]),
               " ",TimeHour(array_period[i][0]),":",TimeMinute(array_period[i][0])), 
               barOpen[i], barHigh[i], barLow[i], barClose[i], barVolume[i]);
            }           
         }
         FileClose(handle);
       }
    }
}   
//*****

//----

   return(0);
  }
//+------------------------------------------------------------------+