Discussion of article "MQL5 Programming Basics: Time" - page 4

 
Dennis Kirichenko:

Need an analogue of TimeCurrent() with millisecond accuracy, something like TimeCurrentMsс().

Forum on trading, automated trading systems and testing trading strategies

Market closed

fxsaber, 2017.09.22 09:45 AM

// Time of the last character tick
long GetSymbolTime( const string Symb )
{
  MqlTick Tick;
  
  return(SymbolInfoTick(Symb, Tick) ? Tick.time_msc : 0);
}

// Time of the last tick of the Market Watch
long GetMarketWatchTime( void )
{
  long Res = 0;
  
  for (int i = SymbolsTotal(true) - 1; i >= 0; i--)
  {
    const long TmpTime = GetSymbolTime(SymbolName(i, true));
    
    if (TmpTime > Res)
      Res = TmpTime;
  }
  
  return(Res);
}

// Current time on the trade server without taking ping into account
long GetCurrenTime( void )
{
  static ulong StartTime = GetMicrosecondCount();
  static long PrevTime = 0;
  
  const long TmpTime = GetMarketWatchTime();
  
  if (TmpTime > PrevTime)
  {
    PrevTime = TmpTime;
    
    StartTime = GetMicrosecondCount();
  }
  
  return(PrevTime + (long)((GetMicrosecondCount() - StartTime) / 1000));
}

void OnInit()
{
  MarketBookAdd(_Symbol);
}

void OnDeinit( const int )
{
  MarketBookRelease(_Symbol);
}

string TimeToString( const long Value )
{
  return((string)(datetime)(Value / 1000) + "." + (string)IntegerToString(Value % 1000, 3, '0'));
}

void OnBookEvent( const string& )
{
  Comment(TimeToString(GetCurrenTime()));
}

Not perfect, of course.

 

fxsaber, thank you! You are on point as always. Respect

 
Enjoyed the article and found it useful, thanks!
 
where i can get time for candle incators to use mt5? please help
 
Good Day -- Where can I fine these indicators for MT$?
 

Any way to determine more simply if I want an EA to run between 8am and 2pm, optimisable or at a certain time e.g. 9hrs 15 min optimisable?

 
As someone new to both MQL5 and programming this gave me some ideas for techniques I can use when I need to manage anything related to time in my first Expert Advisor.