iTime not returning the current bar open time - page 4

 

Look again here.

The first request is your loop that you decide not to talk about:

2023.08.18 05:27:50.319 Test2 EURUSD,M1: First request USDCHF M1. Error: 4066

The first request generates an error, but you don't check for it.

The second request is your attempt to see the error after the loop:

2023.08.18 05:27:50.321 Test2 EURUSD,M1: Second same request USDCHF M1. Error: 0

No more error!

 
Vladislav Boyko #:

Look again here.

The first request is your loop that you decide not to talk about:

The first request generates an error, but you don't check for it.

The second request is your attempt to see the error after the loop:

No more error!

Instead of making stuff up, here's the real code:

ResetLastError();
iTime(SYMBOLS[i],PERIOD_M15,0);
Print(" last error :",ErrorDescription(GetLastError()));

There is never any error.

 
dc3463456 #:

Why is the error not generated?

Because that's how the terminal works.

Possibly due to calling iTime() with different parameters between repeated first and second requests:

datetime notUsed = iTime(Symbol(),PERIOD_M5,0);

But most likely, even without this, the error can also stop being generated.


The error will be generated at the moment the history starts to load. But you cannot be sure that the error will be generated during the whole history loading time.

 

How can one symbol have a different server time than all the others?

 
I think I get it now. Once a candle starts late, all the next ones are equally offset. I don't know when it stops. It even gets repeated from one week to the other. 
 
dc3463456 #:

Instead of making stuff up, here's the real code:

There is never any error.

If you are accessing non native (the operating chart) price + time data you have to anticipate errors .

If you are not working with the native chart , as @Vladislav Boyko indicates , the first call usually tells the terminal to go fetch , so you must be running checks

for whether or not there is a new bar formation based on server time and that symbols server time . 

If its in the market watch its last tick time is logged so if you pick up a distance in iTime(...,...,0) to its last tick bigger than PeriodSeconds() you know 

it is not providing you with the latest bar.

I have never encountered it from this side (the right side) but in several scanners (in MT4) to make sure data was loaded properly i would open charts and 

navigate in the past to expedite the download of data .

It helps if you think about it from the perspective of the terminal . 

Edit : observe this EA screen comments for a while :

#property copyright "Forum Discussion"
#property link      "https://www.mql5.com/en/forum/452518"
#property version   "1.00"
#property strict
input ENUM_TIMEFRAMES timeframe=PERIOD_M15;//timeframe
ENUM_TIMEFRAMES TF=PERIOD_CURRENT;
int OnInit()
  {
//--- create timer
   while(!EventSetTimer(1)){
   Sleep(144);
   }
  return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
  EventKillTimer();
  }

void OnTick(){}

void OnTimer()
  {
   TF=timeframe;
   if(TF==PERIOD_CURRENT){TF=tfFromPeriod(_Period);}
  //scan the market watch
    string found="";
    int t=SymbolsTotal(true);
    for(int i=0;i<t;i++){
    if(t!=SymbolsTotal(true)){return;}
    //get the symbol name
      string target_symbol=SymbolName(i,true);
    //get the latest iTime of the symbol 
      ResetLastError();
      datetime last_formed_bar_time=iTime(target_symbol,TF,0);
      if(GetLastError()==0){
        //get the latest tick time of the symbol
          ResetLastError();//this may be redundant
          datetime last_tick_time=(datetime)SymbolInfoInteger(target_symbol,SYMBOL_TIME);
          if(GetLastError()==0){
            int periodSecs=PeriodSeconds(TF);
            //get the delta 
              int delta=((int)last_tick_time)-((int)last_formed_bar_time);
              if(delta>periodSecs){
                found+="\n"+target_symbol+" LAST BAR NOT AVAILABLE YET";
                }
            }else{found+="\n"+target_symbol+" Cannot read lat tick time";}
        }else{found+="\n"+target_symbol+" Cannot read last bar time";}
    }
    if(found==""){found="All Synced and readable";}
  Comment(found);
  }

ENUM_TIMEFRAMES tfFromPeriod(int period){
if(period==1){return(PERIOD_M1);}
else if(period==5){return(PERIOD_M5);}
else if(period==15){return(PERIOD_M15);}
else if(period==30){return(PERIOD_M30);}
else if(period==60){return(PERIOD_H1);}
else if(period==240){return(PERIOD_H4);}
else if(period==1440){return(PERIOD_D1);}
else if(period==10080){return(PERIOD_W1);}
else if(period==43200){return(PERIOD_MN1);}
return(PERIOD_CURRENT);
}
 
  1. Bars are not late.

    You can't know when a candle closes. Only when a new tick arrives that starts a new bar is the old bar closed, and that tick could arrive almost at the end of a bar's duration.

    Do not assume every bar every exists — they don't. What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific), requires knowledge of when your broker stops and starts (not necessary the same as the market.)
              "Free-of-Holes" Charts - MQL4 Articles (2006)
              No candle if open = close ? - MQL4 programming forum (2010)

  2. On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - MQL4 programming forum - Page 3 #26.4 (2019)

 
William Roeder #
  1. On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - MQL4 programming forum - Page 3 #26.4 (2019)

Suppose still 4066/4073 is not resolved. What do you do? Just wait in a loop until it is resolved? Or exit the indicator with a error message?

 
Yashar Seyyedin #:

Suppose still 4066/4073 is not resolved. What do you do? Just wait in a loop until it is resolved? Or exit the indicator with a error message?

In MT4 you exit , if a chart has never been opened -in MT4- it may require a manual open before it can receive data .

 
Yashar Seyyedin #:

Suppose still 4066/4073 is not resolved. What do you do? Just wait in a loop until it is resolved? Or exit the indicator with a error message?

This means that you cannot use historical data until it is loaded by the terminal.

Just wait.

Yashar Seyyedin #:
in a loop

Not in a loop. Your program should be designed to accommodate situations where historical data is not available. Historical data is needed for some kind of action. While this data is in the process of loading, the program should not perform actions that depend on historical data.

For example, the program has an entry signal generation block that uses historical data, and a trailing stop that does not require historical data. As long as historical data is not available, the entry signal generation block generates "no signal", and the trailing stop continues to work normally.

The issue of determining the moment when the loading of historical data in MT4 is completed does not have a simple solution. Personally, I wait 30 seconds and 10 ticks from the moment I receive a history access error. That is, if a history error occurred within the last 30 seconds or within the last 10 ticks, my code does not use the history. But I request all used charts with each tick, in order for the terminal to keep them up to date. Thus, my MTF EAs often start work 30 seconds late (from the moment of attachment), but thanks to the request of all charts with each tick, history errors no longer occur.