Download MetaTrader 5

Download history in MQL4 EA

To add comments, please log in or register
Ingvar Engelbrecht
Ingvar Engelbrecht  

I need 15M 1H 4H 1D 1W  for all 28 pairs for 30 periods back in my EA.

I have used a very good tool in MQL5 published  here but is there a way of doing this in MQl4?

Just wait for the download to complete
In an EA
void OnTick(){
   while(!download_history(PERIOD_M15) ){ Sleep(1000); RefreshRates(); }
In an indicator
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[]){
   if(!download_history(PERIOD_M15) ) return prev_calculated;
#define HR2400 PERIOD_D1 * 60    // 86400 = 24 * 3600
int      TimeOfDay(datetime when=0){      if(when == 0)  when = TimeCurrent();
                                          return( when % HR2400 );            }
datetime DateOfDay(datetime when=0){      if(when == 0)  when = TimeCurrent();
                                          return( when - TimeOfDay(when) );   }
#define SYMBOL string
#define THIS_SYMBOL ""
bool  download_history(ENUM_TIMEFRAMES period=PERIOD_CURRENT){
   return download_history(_Symbol, period); 
bool  download_history(
      SYMBOL            symbol=THIS_SYMBOL,     ///< The symbol required.
      ENUM_TIMEFRAMES   period=PERIOD_CURRENT   /**< The standard timeframe.*/){
   if(symbol == THIS_SYMBOL)     symbol = _Symbol;
   if(period == PERIOD_CURRENT)  period = _Period;
   datetime today = DateOfDay();
   datetime other = iTime(symbol, period, 0);
   if(_LastError == 0 
   && today == DateOfDay(other)) return true;   
   && _LastError != ERR_NO_HISTORY_DATA
      Print(StringFormat("iTime(%s,%i) Failed: %i", symbol, period,_LastError));
   return false;
Carl Schreiber
Carl Schreiber  

You wrote:

 if(!download_history(PERIOD_M15) ) return prev_calculated;

But shouldn't it be:

 if(!download_history(PERIOD_M15) ) return 0;

As this way the indicator starts to paint itself on the history of the chart as soon as the history has arrived?

Carl Schreiber: But shouldn't it be:
Just because you are now missing some history (on some pair,) doesn't mean all previously completed work (if any) is invalid. For example, lose of connection for a minute, why would you want to recalculate everything? Wait for the update and continue from where previously left off.
To add comments, please log in or register