//+------------------------------------------------------------------+
//|                                                           Dd.mq5 |
//|                                                  ParsinaTejaratt |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "ParsinaTejaratt"
#property link      "http://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   //---- 
   int handle;
   int cnt;
   int _year = 2002;
   int _month = 1;
   int _day = 1;
   string strline;
   string name_file;
   int DailyLength = 16408;
   int idxCounter = 1000;
   
   //-- define the variable "rates"
   MqlRates rates [];
   //-- elements will be indexed like in timeseries.
   ArraySetAsSeries(rates,true);
   //-- Get the  history data of current symbol. (first point is present)
   int copied=CopyRates(Symbol(),Period(),0,idxCounter,rates);
   if(copied>0)
   {
   name_file = StringSubstr(Symbol(), 0, 1) + StringSubstr(Symbol(), 1, 1) + StringSubstr(Symbol(), 2, 1)+StringSubstr(Symbol(), 3, 1)+StringSubstr(Symbol(), 4, 1)+ StringSubstr(Symbol(), 5, 1)+ "-" + PeriodConvertor(Period())+ ".prn";
   //Print("name_file=", name_file);
   //FILE_CSV|FILE_WRITE
   handle = FileOpen(name_file , FILE_WRITE|FILE_ANSI , "\t");
   if(handle<1)
      {
      Print("can't open file error-",GetLastError());
      return(0);
      }
   strline = "<DTYYYYMMDD>,<TIME>,<OPEN>,<HIGH>,<LOW>,<CLOSE>,<VOL>";
   FileWrite(handle, strline);
   
   int size=fmin(copied,idxCounter);
   for (cnt=size-1;cnt>=0;cnt--)
      {
      if (Period() < DailyLength)
         {
         
         strline = DoubleToString(_year,0);
         if (_month < 10)
            strline = strline + "0" + DoubleToString(_month,0);
         else
            strline = strline + DoubleToString(_month,0);
      
         if (_day < 10)
            strline = strline + "0" + DoubleToString(_day,0);
         else
            strline = strline + DoubleToString(_day,0);          
         }
      else
         {
         strline = TimeToString(rates[cnt].time,TIME_DATE|TIME_SECONDS);
         strline = StringSubstr(strline,0,4) + StringSubstr(strline,5,2) + StringSubstr(strline,8,2);
         //Print("strline=", strline);
         }
      strline = strline + ",0," + DoubleToString(rates[cnt].open,4) + "," + DoubleToString(rates[cnt].high,4) + "," + DoubleToString(rates[cnt].low,4) + "," + DoubleToString(rates[cnt].close,4) + "," + DoubleToString(rates[cnt].tick_volume,0);
      FileWrite(handle, strline);
      // Start of build virtual date.
      if (Period() < DailyLength)
         {
         if (_day < 28) 
            _day++; 
         else 
            {
            _day = 1;
            _month++;
            if (_month > 12)
               {
               _month = 1;
               _year++;
               }
            }
         }
      // End of build virtual date.
      }
   FileClose(handle);
   }//end if copied
//----
   return(0);
  }
  
  string PeriodConvertor(ENUM_TIMEFRAMES tf)
  {
     switch(tf)
        {
         case 1: return "M1";
         case 2: return "M2";
         case 3: return "M3";
         case 4: return "M4";
         case 5: return "M5";
         case 6: return "M6";
         case 10: return "M10";
         case 12: return "M12";
         case 15: return "M15";
         case 30: return "M30";
         case 16385: return "H1";
         case 16386: return "H2";
         case 16387: return "H3";
         case 16388: return "H4";
         case 16390: return "H6";
         case 16392: return "H8";
         case 16396: return "H12";
         case 16408: return "D1";
         case 32769: return "W1";
         case 49153: return "MN1";
         }
     return (string)tf;
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
  }
//+------------------------------------------------------------------+
