iTime not returning the current bar open time

 
iTime(_Symbol,_Period,0);

Should return the current bar open time, but I have an issue where it sometimes returns the previous bar time.

I didn't think it was possible.


Maybe if I RefreshRates() ? Edit: Nope. Still happenning. 

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
  • www.mql5.com
Position Properties - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
dc3463456:

Should return the current bar open time, but I have an issue where it sometimes returns the previous bar time.

I didn't think it was possible.


Maybe if I RefreshRates() ? Edit: Nope. Still happenning. 

Maybe this is the case. Sometimes there is no new tick arrived and it has to return the previous bar value.

https://www.mql5.com/en/forum/452137

Candle closed not no new tick available yet
Candle closed not no new tick available yet
  • 2023.08.08
  • www.mql5.com
Hello everyone. Suppose a candle is closed and there has been no new tick in the new candle for a few seconds...
 
Yashar Seyyedin #:

Maybe this is the case. Sometimes there is no new tick arrived and it has to return the previous bar value.

https://www.mql5.com/en/forum/452137

iTime(_Symbol,_Period,0);

just returned the fourth newest bar open time, so a no new tick situation is clearly not the case.

 
I will keep testing the with the live account. Unreliable demo data might be the cause. Don't ask me how.
 
No issue with the live account so far. *Cross fingers*
 
dc3463456:

Should return the current bar open time, but I have an issue where it sometimes returns the previous bar time.

I didn't think it was possible.


Maybe if I RefreshRates() ? Edit: Nope. Still happenning. 

Yashar is right , the bar you want may not have formed yet because there are no ticks .

It should not affect operations however unless you are deploying a multi pair expert advisor . In that case prefer OnTimer() , store each symbol's latest formed bar time and that way whenever a new bar opens for one of the symbols you will know.

 
dc3463456:

Should return the current bar open time, but I have an issue where it sometimes returns the previous bar time.

I didn't think it was possible.


Maybe if I RefreshRates() ? Edit: Nope. Still happenning. 

  1. What is the value of Time[0]?
  2. From which event handling function are you calling iTime()?
  3. What does GetLastError() return after calling iTime()?

If GetLastError() returns an error, then you should not use the data that iTime() returned and you need to wait some time while the data is loading.

What are these difficulties for? You are calling this for the current symbol and timeframe.

From OnCalculate() use time[]

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[])

In other cases (not from OnCalculate) use Time[].

Using time[] and Time[] does not require error checking

 
Lorentzos Roussos #:

Yashar is right , the bar you want may not have formed yet because there are no ticks .

It should not affect operations however unless you are deploying a multi pair expert advisor . In that case prefer OnTimer() , store each symbol's latest formed bar time and that way whenever a new bar opens for one of the symbols you will know.

You quoted an older message. Things have changed since then. Updating at each symbol's first tick is an improvement I intend to implement but like I said it was not always the cause of my issue:

dc3463456 #:

iTime(_Symbol,_Period,0); just returned the fourth newest bar open time, so a no new tick situation is clearly not the case.

Vladislav Boyko #:

  1. What is the value of Time[0]?
  2. From which event handling function are you calling iTime()?
  3. What does GetLastError() return after calling iTime()?

If GetLastError() returns an error, then you should not use the data that iTime() returned and you need to wait some time while the data is loading.

What are these difficulties for? You are calling this for the current symbol and timeframe.

From OnCalculate() use time[]

In other cases (not from OnCalculate) use Time[].

Using time[] and Time[] does not require error checking

The problem didn't happen again, but if it does I will try GetLastError().

I'm using iTime because I'm not only working with the current chart.

 
dc3463456 #:

You quoted an older message. Things have changed since then. Updating at each symbol's first tick is an improvement I intend to implement but like I said it was not always the cause of my issue:

The problem didn't happen again, but if it does I will try GetLastError().

I'm using iTime because I'm not only working with the current chart.

Don't forget also with iTime and OnTick() it will only update the other symbol data when the host chart receives a tick....
 
dc3463456 #:
The problem didn't happen again, but if it does I will try GetLastError().

It will definitely occur when requesting data of another symbol/timeframe if it has not been used for some time.

"has not been used" - means that the chart window of this symbol/timeframe is not open and its data (rates) were not requested from MQL programs. In this case, the terminal stops updating this chart, and when requested (using iTime(), for example), it may take some time to update the chart. During this time (while the requested historical data is being updated), you will receive incorrect values.

Therefore, when working with iXXX() functions, it is always necessary to check GetLastError()

 
Can also check the distance of the time received on iTime(...,...,0) against the SymbolInfoInteger(...,SYMBOL_TIME) which has the last quote time for that symbol  , if that's available in mt4