Features of the mql5 language, subtleties and tricks - page 75

 
Vitaly Muzichenko:

I put the EA on the chart on Saturday, but the EA can get the time of the last tick, it is for example in the Market Watch 23:58, and my local 04:45. The server time I do not know.

Question: What is the time from the last tick to the server time (terminal time)?

I thought about it. It appears that there is no difference when an EA is attached to a chart. As long as there is no real tick, we cannot run the part of the program algorithm that uses the server's real time.

If calculations are required before the arrival of the tick, then request from the user ServerGMTOffset.

Thus, the problem can be solved even without ServerTradeTime().

 
Ihor Herasko:

I have given it some thought. It turns out that there is no difference when the EA is attached to the chart. As long as there is no real tick, you cannot execute that part of the program algorithm which uses the real server time.

If calculations are required before the arrival of the tick, then request from the user ServerGMTOffset.

Thus, the problem is solvable even without ServerTradeTime().

The user doesn't know the Offset of the server, he doesn't know it.

Only two values are known, and this data is not enough to calculate:"how much time has passed from the last tick to the server's(terminal's) time".

This is a very peculiar problem and I don't see a solution, like you do.

 
Vitaly Muzichenko:

I put the EA on the chart on Saturday, but the EA can get the time of the last tick, it is for example in the Market Watch 23:58, and my local 04:45. The server time I do not know.

The question is how much time passed from the last tick to the server time (terminal time)

It's not just a time, it's a datetime. Ie both date and time. So, the arithmetic operation with these data will give the difference in seconds, which is not difficult to translate into days, hours, minutes and seconds.

In addition, you need to check on weekends what TimeTradeServer will show.

2018.03.29 15:00:03.640 00 (EURUSD,H1)  TimeCurrent() = 2018.03.29 14:59:54
2018.03.29 15:00:03.640 00 (EURUSD,H1)  TimeLocal() = 2018.03.29 15:00:03
2018.03.29 15:00:03.640 00 (EURUSD,H1)  TimeGMT() = 2018.03.29 12:00:03
2018.03.29 15:00:03.640 00 (EURUSD,H1)  TimeGMTOffset() = -10800
2018.03.29 15:00:03.640 00 (EURUSD,H1)  TimeTradeServer() = 2018.03.29 15:00:03
Pay attention to the discrepancy between the seconds of the server time and the local time, and then the exact match between local and estimated server time.
 
For forex, you can remake this code from quadruple

Forum on trading, automated trading systems and trading strategies testing

Peculiarities of mql4 language, subtleties and tricks

fxsaber, 2018.03.29 14:32

#property strict

#define  HOUR 3600
#define  DAY (24 * HOUR)
#define  WEEK 7

datetime GetBarTime( const datetime time, const bool NextBar = false, string Symb = NULL, const ENUM_TIMEFRAMES TimeFrame = PERIOD_M1 )
{
  if (Symb == NULL)
    Symb = _Symbol;
    
  return(iTime(Symb, TimeFrame, iBarShift(Symb, TimeFrame, time) - (NextBar ? 1 : 0)));
}

datetime GetTimeDayOfWeek( const int Shift = 0, const ENUM_DAY_OF_WEEK Day = SUNDAY )
{
  const datetime Res = TimeCurrent() / DAY * DAY;
  
  return(Res - (((WEEK + (TimeDayOfWeek(Res) - Day)) % WEEK) + Shift * WEEK) * DAY);
}

// Аналог по серверному времени - https://www.mql5.com/ru/docs/dateandtime/timegmtoffset
// Работает для FOREX-символов, когда M1-история доступна за ближайшую неделю
int TimeServerGMTOffset( void )
{
  const datetime Sunday = GetTimeDayOfWeek();
  
  return(((int)MathRound((double)MathMin(Sunday - DAY - GetBarTime(Sunday), Sunday + DAY - GetBarTime(Sunday, true)) / HOUR) - 3) * HOUR);
}

// Аналог по серверному времени - https://www.mql5.com/ru/docs/dateandtime/timegmt
// Работает для FOREX-символов, когда M1-история доступна за ближайшую неделю
datetime TimeServerGMT( void )
{
  return(TimeCurrent() + TimeServerGMTOffset());
}
 
fxsaber:
I can remake this code for forex

Thanks, I'll take a look. But then again, there are not forex symbols.

How is it not convenient that you can not simply and briefly get the current time of the server, at any point in time.

Alexey Viktorov:

Both are not just time, but datetime. That is, both date and time. So, the arithmetic operation with these data will give the difference in seconds, which is not difficult to translate into days, hours, minutes and seconds.

In addition, you need to check on weekends what TimeTradeServer will show, but so far it shows this way

Pay attention to the discrepancy between the seconds of the server time and the local time, and then the exact match between the local and the server's estimated time.

I have also printed it, but how it can be used on weekends. In general, I should try to use this information.

The task is minimal:"how much time has passed from the last tick to the server's (terminal's) time".

if( TimeCurrent()<TimeServer()-60 ) Alert( "нет котировок более 1-й минуты" );
 
Vitaly Muzichenko:

Thanks, I'll take a look. But then again, there are non-forex symbols.

If the broker has at least one forex symbol, it will work for others.

If the broker does not have one Forex symbol, the question of reasonability arises.

 
Vitaly Muzichenko:

Thanks, I'll take a look. But then again, there are not forex symbols.

How is it not convenient that you can not simply and briefly get the current server time, at any point in time.

I also have it printed, but how to use it at the weekend. In general, you need to rotate the information provided.

The task is minimal:"how much time has passed from last tick to server (terminal) time".

Of course, we should check it, but judging by the difference between TimeCurrent and TimeTradeServer, we can assume that the difference will grow.

Please tell me if my MQ-demo server time is the same as the time in Moscow.

 
Alexey Viktorov:

It is necessary to check, of course, but judging by the difference between TimeCurrent and TimeTradeServer, we can assume that the difference will grow.

Please tell me if my MQ-demo server time is the same as my Moscow time and what it shows if server time is not the same as my local time.

Robo

My local time was 17:17:13

 

Forum on trading, automated trading systems and trading strategies testing

Bugs, bugs, questions

fxsaber, 2017.05.19 22:35

TimeTradeServer can return all sorts of nonsense. For example, a value less than TimeCurrent. This completely nullifies its practical application.

If I understand the idea correctly, TimeTradeServer should calculate the elapsed time after receiving TimeCurrent and add this difference, sort of emulating the future TimeCurrent values.

Now, having started the Expert Advisor

#define  TOSTRING(A) #A + " = " + (string)(A) + "\n"

void OnInit()
{
  EventSetTimer(1);
}

void OnTimer()
{
  Comment(TOSTRING(TimeTradeServer()) + TOSTRING(TimeCurrent()));
}

you can see any nonsense

Note the dates.

 
fxsaber:

And who is to blame for this, the terminal, or the DC with crooked settings?

Reason: