Why does my script still exports YEAR 1970

 

I have put a condition on year

int year = TimeYear(Time[i]);
       if (year > 1970)

still it doesn't take into account, do you see why ?

Full source below:

//+------------------------------------------------------------------+
//|                                                       Export.mq4 |
//|                                                     Forexgenuine |
//|                                          http://forexgenuine.com |
//+------------------------------------------------------------------+
#property copyright "Forexgenuine"
#property link      "http://forexgenuine.com"

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
//----
   string symbols[]={"EURUSDiam","EURJPYiam","GBPUSDiam","EURCHFiam","USDCHFiam","USDJPYiam","USDCADiam","EURGBPiam","EURJPYiam","GBPJPYiam","AUDUSDiam"};
   int periods[] = {1,5,15,60,240,1440,10080,43200};
   int count = ArraySize(symbols);
   int counttf = ArraySize(periods); 
     
   string symbol;
   int period;
   for (int ii=0; ii<count; ii++){   
      symbol = symbols[ii];
      for (int jj=0; jj<counttf; jj++){ 
         period = periods[jj];
         export(symbol, period);
      }
   }   

//----
   Alert("the end");
   return(0);
  }
//+------------------------------------------------------------------+

int export(string symbol, int theperiod)
  {

   string filename = StringSubstr(symbol,0,6) + theperiod + ".csv";
   int handle = FileOpen(filename, FILE_CSV|FILE_WRITE, ";");
   if(handle>0)
   {
      // table column headers recording
      FileWrite(handle, "Time;Open;High;Low;Close;Volume");
      // data recording
      MessageBox(Bars);
      
      for(int i=Bars; i>0; i--) {

       int year = TimeYear(Time[i]);
       if (year > 1970) {

         datetime t = iTime(symbol,theperiod,i);
         string strdatetime = TimeToStr(t);
         strdatetime = StringSetChar(strdatetime,4,'-');
         strdatetime = StringSetChar(strdatetime,7,'-');
                  
         double Open_i = iOpen(symbol,theperiod,i);
         double High_i = iHigh(symbol,theperiod,i);
         double Low_i = iLow(symbol,theperiod,i);
         double Close_i = iClose(symbol,theperiod,i);
         double Volume_i = iVolume(symbol,theperiod,i);
         
         //FileWrite(handle, year + "-" + month + "-" + month + " " + hour + ":" + minute + ":" + seconds, Open[i], High[i], Low[i], Close[i], Volume[i]);
         FileWrite(handle, strdatetime, Open_i, High_i, Low_i, Close_i, Volume_i);
         
       }
      }
      FileClose(handle);
   }
   return(0);
  }
 
wrong:
int year = TimeYear(Time[i]);
       if (year > 1970) {

because Time[i] will always use the times of the current symbol and timeframe, the one of the chart where the script is running on.


You want to export many different symbols and for each one you can only access them with iTime() instead, like you already do it one line below that.


Also you should use iBars() instead of Bars for your loop for getting the numbers of bars in the other symbols because Bars will always only give you the number of bars in the current chart and this is absolutely not relevant when exporting a different symbol/timeframe, each of them have a completely different number of bars available in the downloaded history.


You can also use iBarShift() to directly find the starting point for your loop for any given date. It will return the bar index number corresponding to a certain date in a certain symbol and period. This way you do not need to loop over all the older irrelevant bars, check their date and throw them away, you can directly start the loop at 1.1.1970 00:00 or the oldest available bar, nearest to that date.

 
  1. for(int i=Bars; i>0; i--) {
           int year = TimeYear(Time[i]);
           if (year > 1970) {
    

    indexes are zero based: 0, 1, ... Bars-1. You are referring one past the end


  2. If you are not referencing the current symbols/period then you must use iTime, iBars etc.
 
7bit:
wrong:

because Time[i] will always use the times of the current symbol and timeframe, the one of the chart where the script is running on.


You want to export many different symbols and for each one you can only access them with iTime() instead, like you already do it one line below that.


Also you should use iBars() instead of Bars for your loop for getting the numbers of bars in the other symbols because Bars will always only give you the number of bars in the current chart and this is absolutely not relevant when exporting a different symbol/timeframe, each of them have a completely different number of bars available in the downloaded history.


You can also use iBarShift() to directly find the starting point for your loop for any given date. It will return the bar index number corresponding to a certain date in a certain symbol and period. This way you do not need to loop over all the older irrelevant bars, check their date and throw them away, you can directly start the loop at 1.1.1970 00:00 or the oldest available bar, nearest to that date.


Thank you very much 7bit I'm very beginner at mql4 and not accustomed to programming :)
 
WHRoeder:
  1. indexes are zero based: 0, 1, ... Bars-1. You are referring one past the end


  2. If you are not referencing the current symbols/period then you must use iTime, iBars etc.

This code snippet will be usefull for me thanks.