MQL4/5 required functions: TimeTradeServerGMTOffset() or GetTradeServerTimeZoneInfo()

 
  • 88% (14)
  • 12% (2)
Total voters: 16
 
Mohammad Hossein Sadeghi:
  • 75% Yes
    (6)
  • 25% No
    (2)

ServerTimeGmtOffset()

 
Strategy Tester, Holidays, RealTime

Форум по трейдингу, автоматическим торговым системам и тестированию торговых стратегий

Особенности языка mql4, тонкости и приёмы работы

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());
}


Применение

#define PRINT(A) Print(#A + " = " + (string)(A))

void OnStart()
{  
  PRINT(TimeGMT());
  PRINT(TimeServerGMT());  
}
 
double MathNearest(  double v, double to){ return to * MathRound(v / to); }
double MathRoundDown(double v, double to){ return to * MathFloor(v / to); }
double MathRoundUp(  double v, double to){ return to * MathCeil( v / to); }
int TimeServerGMTOffset(void){
   return (int)MathNearest(TimeCurrent() - TimeGMT(), 1800);
}
datetime TimeServerGMT(void){ return TimeServerGMT(TimeCurrent() ); }
datetime TimeServerGMT(datetime when){
  return when + TimeServerGMTOffset();
}
 
whroeder1:
It's fine if you use it inside OnTick(), but if you need it in OnTimer() or on other code not related to a tick, it's not working.
 
And it's not working in the Strategy Tester.
 

Another thing to keep in mind. Strategy tester time works different vs terminal. It is based on tick time, so TimeCurrent() for example returns not the current timestamp, but time of last tick.

 
Alain Verleyen:
It's fine if you use it inside OnTick(), but if you need it in OnTimer() or on other code not related to a tick, it's not working.
double MathNearest(  double v, double to){ return to * MathRound(v / to); }
double MathRoundDown(double v, double to){ return to * MathFloor(v / to); }
double MathRoundUp(  double v, double to){ return to * MathCeil( v / to); }
int TimeServerGMTOffset(void){
   return (int)MathNearest(TimeCurrent() - TimeGMT(), 1800);
}
datetime TimeServerGMT(void){ return TimeServerGMT(TimeCurrent() ); }
datetime TimeServerGMT(datetime when){
  return when + TimeServerGMTOffset();
}

And should be minus.