How to reliably export historical data from MT4 using MQL4 from the iOpen, iHigh, etc arrays?

 

Currently, I am exporting all the historical data stored in MT4 into a script by reading off the data stored in the iOpen, iHigh, iLow, iClose, and iVolume arrays.

 The problem I have is that it may be the case that the current period's data (which is already available) is not written for some symbols. For example,  the candlestick for week 16 Jan 2015 is already available for USDJPY but somehow this is not written into the file. In such cases, I have to open the chart for USDJPY, switch to the weekly timeframe (to make sure I can see candle I want to export) and rerun the script.

 

I've tried to wait for a while and rerun the code that reads the data stored in the arrays but this does not work. Anyone have any suggestions or alternative methods to export data from MT4? 

 
annur:

Currently, I am exporting all the historical data stored in MT4 into a script by reading off the data stored in the iOpen, iHigh, iLow, iClose, and iVolume arrays.

 The problem I have is that it may be the case that the current period's data (which is already available) is not written for some symbols. For example,  the candlestick for week 16 Jan 2015 is already available for USDJPY but somehow this is not written into the file. In such cases, I have to open the chart for USDJPY, switch to the weekly timeframe (to make sure I can see candle I want to export) and rerun the script.

 

I've tried to wait for a while and rerun the code that reads the data stored in the arrays but this does not work. Anyone have any suggestions or alternative methods to export data from MT4? 

Please show the relevant code. You have a bug.
 
angevoyageur:
Please show the relevant code. You have a bug.

The part of the code that reads the data from the array into the given csv file (adapted from here):

 

 int myexport(int outfile,string symbol, int theperiod)  
  {  
   
      
    // data recording
    
    int totalbars=iBars(symbol,theperiod);
    
    if (totalbars==0)
      {
         Alert("No bars for " + symbol + "," + humanperiod(theperiod));
      }
    //Start from 1 because current bar i.e. bar 0 is not closed yet  
    for(int i=totalbars; i>=1; i--) {
      
     datetime t = iTime(symbol,theperiod,i);  
     // int year = TimeYear(Time[i]);  
     int year = TimeYear(t);  
     if (year > 1970) {  
      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(outfile, symbol + "_" + strdatetime + "_" + humanperiod(theperiod),symbol,humanperiod(theperiod),strdatetime, Open_i, High_i, Low_i, Close_i, Volume_i,AccountServer(),TimeToStr(TimeLocal()));  
     }  
    }  
         
   return(0);  
  }

 

In the code above, humanperiod() is a function that translates the integer representing the timeframe into a string, for example humanperiod(1) returns "M1".

 
annur:

The part of the code that reads the data from the array into the given csv file (adapted from here):

 

 

In the code above, humanperiod() is a function that translates the integer representing the timeframe into a string, for example humanperiod(1) returns "M1".

No error checking. Please read the documentation carefully for iTime, iOpen, and all iXXX function.

Returned value

Time value for the bar of specified symbol with timeframe and shift. If local history is empty (not loaded), function returns 0. To check errors, one has to call the GetLastError() function.

If there is no data available, your t variable is 0, so no data will be written to your file. Search the forum for error 4066.

 
angevoyageur:

No error checking. Please read the documentation carefully for iTime, iOpen, and all iXXX function.

If there is no data available, your t variable is 0, so no data will be written to your file. Search the forum for error 4066.

Thank you for your recommendations.