How to get last sync time between MT terminal and servers?

 

Hi All,

 

Sorry if there is a topic discussing this, but I couldn't find such one. I am also waiting for an answer from Meta Quotes... I will paste the question here too.

For the purposes of our robots, we need to be able to work with latest rates at any given moment, otherwise we may take wrong decisions. 

 

Let's use the following code:

 

MqlRates rates[];
ArraySetAsSeries(rates, true);

int size = CopyRates('NZDUSD', PERIOD_M15, StringToTime("2016.11.01 13:00"), TimeCurrent(), rates);

And let's assume TimeCurrent() is 2016.11.01 13:56:15. And also let's assume that there is a connection problem from 13:55 to TimeCurrent (13:56:15). What will the returned rates be? 
 - Are they going to be synced up to 13:55? 
 - How can we check the time when these rates are synced, so that we do not allow using stale data if we are out of sync with more than a few secs?
 - Is it possible CopyRates to return just part of the data?

 - Is it possible CopyRates to return data with holes? (e.g. 13:00, 13:15, 13:45 - note 13:30 is missing)

Might be TimeLocal() is better for the example, given that TimeVurrent() returns the time of the last but you get what I mean.

What's the right way to make sure CopyRates returns exactly what is requested?

Or an alternative question might be... Is there a way to get the time when the rates were last synced with the server? We can compare that time with TimeLocal() and if the difference is big, just do not trade at that time.

That can be simulated by turning off the network for a while.

Functions in https://www.mql5.com/en/docs/marketinformation do not really help because no of them seems to return error if network is disconnected. 

SymbolIsSynchronized was promising, but I doesn't seem to work... SymbolInfoTick might be of some help but when it returns the last tick and it is not changed, we can't say if the tick was just not updated on the trading server or data is not synced and that's an important difference. 

 

Thanks,

Todor 

Documentation on MQL5: Market Info
Documentation on MQL5: Market Info
  • www.mql5.com
Market Info - Reference on algorithmic/automated trading language for MetaTrader 5
 
tpetrov - How can we check the time when these rates are synced, so that we do not allow using stale data if we are out of sync with more than a few secs?

You get whatever the terminal has.

What do you do if there has been no ticks for the last couple of minutes, such as during the Asian session? "Free-of-Holes" Charts - MQL4 Articles

You could remember TimeLocal when you receive a tick and can then check, but why bother. No tick, nothing has changed. You already processed that tick, what would you do now?

Free-of-Holes Charts
Free-of-Holes Charts
  • 2006.06.20
  • Andrey Khatimlianskii
  • www.mql5.com
The article deals with realization of charts without skipped bars.
 

@whroeder1, that's different.

 

If you don't have ticks that's just fine, you have the latest data, so it's up to your algorithm to decide what to do (as you say nothing changes, nothing to do).

 

Imagine you have ticks1 t1,.....,t100 on the trading server, but the terminal has lost connection for a while and you have just ticks t1,...,t5 on the terminal. Taking decisions based on t1,...,t5 might be completely wrong. Moreover the prices you are going to use for your orders may be completely irrelevant.  

 

Keeping time of the last tick does not help. And the reason is that you don't know if the last tick is the last one on the trading server or you just got disconnected.

 

"No tick, nothing has changed. You already processed that tick, what would you do now?"

 

That statement is not correct for what I ask. There are new ticks, the terminal just didn't receive them because of connection issues. In that case we should get an alert and understand why we have connection issues. Best brokers should almost never have connection issues, but if there are, then we may want to stop the robot or do some other mitigation. (we've had a similar problem recently - ~24 hours connection issues because of issues on the broker's side - which corrupts the strategy for that period) 

Hopefully this makes sense.

 

Regards,

Todor 

 
tpetrov: That statement is not correct for what I ask. There are new ticks, the terminal just didn't receive them because of connection issues. In that case we should get an alert and understand why we have connection issues.
  1. If the terminal hasn't received them, you can't know that and can't do anything about it. The terminal will timeout in about a minute (isConnected will then return false) and it will try to reconnect.
  2. If you enabled tools -> options -> events, the terminal has already made the disconnect sound. If you want to issue an additional Alert from OnTimer, when current TimeLocal exceeds last saved value, from OnTick by a minimum of 60.
 

Thanks @whroeder1 for spending the time, trying to help.

 

It's enough to know the last time when rates in terminal and the ones on the trading server are in sync. That's absolutely easy if MT5 keeps a timestamp of that and exposes it through an API. That's what I asked Meta Quotes and what I asked here.

That's a robot and moreover unattended, sounds do not help in these scenarios. We have a much more complicated monitoring system that we integrate with the robot. But its integration is not a problem, the lack of the right event is the problem.

What you suggest is a partially-working solution which I have mentioned in the end of my initial post... but this is not working good enough... It might be simply the case when you have no ticks ... in this case the difference might be X seconds from last tick but this does not mean disconnect. I want to distinguish failures. 

I can detect the message "lost connection" in the log, but I am not sure it's absolutely reliable. I was hoping that there might be an API for that.

 

Regards,

Todor 

 

After researching a bit... 

I found a property TERMINAL_CONNECTED (https://www.mql5.com/en/docs/constants/environment_state/terminalstatus) which becomes 0 ~15 secs after the network disconnection.

 

That can be used for handling if the connection is lost. If there is connect/reconnect for less than 15 sec, we won't be able to handle the connection problem this way. However,  we can use CopyTicks to find if ticks have been missed for a few sec. That would help evaluating a broker.

 

@whroeder1 thanks again for your help.

Documentation on MQL5: Standard Constants, Enumerations and Structures / Environment State / Client Terminal Properties
Documentation on MQL5: Standard Constants, Enumerations and Structures / Environment State / Client Terminal Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Environment State / Client Terminal Properties - Reference on algorithmic/automated trading language for MetaTrader 5