Bug report: Wrong TimeTradeServer() in indicators.

 

Forum on trading, automated trading systems and testing trading strategies

Possible bug in TimeTradeServer

amrali, 2024.04.05 02:10

I am sorry for my previous responses. I re-checked and it seems to be a bug that affects TimeTraderServer() only inside indicators. Specifically, the bug occurs when the terminal is launched with an indicator ( that calls TimeTradeServer() ) is attached to the chart.

The returned value of TimeTradeServer() is wrong. It becomes equal to TimeGMT() inside OnInit() and also during the first 2 or 3 ticks until the terminal connects to the trade server (connected sound).

This is an obvoius bug in TimeTradeServer() in indicators.  I file this bug report for MT5 developers team.

NOTICE: I checked experts advisors, luckily the bug DOES NOT happen there.

//+------------------------------------------------------------------+
//|                                         TimeTradeServer_indi.mq5 |
//|                                        Copyright © 2018, Amr Ali |
//|                             https://www.mql5.com/en/users/amrali |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 0
#property indicator_plots   0

int msg=0;

int OnInit()
  {
   Print(__FUNCTION__+": is called.");
   ShowTimes();
   return (INIT_SUCCEEDED);
  }

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[])
  {
   if(++msg < 4)
     {
      Print(__FUNCTION__+": is called.");
      ShowTimes();
     }
   return(0);
  }

void ShowTimes()
  {
   datetime tts = TimeTradeServer();
   datetime gmt = TimeGMT();
   int BrokerOffset = (int)(tts - gmt);
   PrintFormat("  Server            = %s", AccountInfoString(ACCOUNT_SERVER));
   PrintFormat("  TimeTradeServer() = %s", string(tts));
   PrintFormat("  TimeGMT()         = %s", string(gmt));
   PrintFormat("  BrokerOffset      = %d (GMT%+g)", BrokerOffset, BrokerOffset/3600.0);
  }

output:

// 2024.04.05 02:04:58.685      TimeTradeServer_indi (EURUSD,M1)        OnInit: is called.
// 2024.04.05 02:04:58.685      TimeTradeServer_indi (EURUSD,M1)          Server            = ICMarketsSC-Demo
// 2024.04.05 02:04:58.685      TimeTradeServer_indi (EURUSD,M1)          TimeTradeServer() = 2024.04.05 00:04:58
// 2024.04.05 02:04:58.685      TimeTradeServer_indi (EURUSD,M1)          TimeGMT()         = 2024.04.05 00:04:58
// 2024.04.05 02:04:58.685      TimeTradeServer_indi (EURUSD,M1)          BrokerOffset      = 0 (GMT+0)
// 2024.04.05 02:04:58.685      TimeTradeServer_indi (EURUSD,M1)        OnCalculate: is called.
// 2024.04.05 02:04:58.685      TimeTradeServer_indi (EURUSD,M1)          Server            = ICMarketsSC-Demo
// 2024.04.05 02:04:58.685      TimeTradeServer_indi (EURUSD,M1)          TimeTradeServer() = 2024.04.05 00:04:58
// 2024.04.05 02:04:58.685      TimeTradeServer_indi (EURUSD,M1)          TimeGMT()         = 2024.04.05 00:04:58
// 2024.04.05 02:04:58.685      TimeTradeServer_indi (EURUSD,M1)          BrokerOffset      = 0 (GMT+0)
// 2024.04.05 02:05:00.282      TimeTradeServer_indi (EURUSD,M1)        OnCalculate: is called.
// 2024.04.05 02:05:00.282      TimeTradeServer_indi (EURUSD,M1)          Server            = ICMarketsSC-Demo
// 2024.04.05 02:05:00.282      TimeTradeServer_indi (EURUSD,M1)          TimeTradeServer() = 2024.04.05 03:05:00
// 2024.04.05 02:05:00.282      TimeTradeServer_indi (EURUSD,M1)          TimeGMT()         = 2024.04.05 00:05:00
// 2024.04.05 02:05:00.282      TimeTradeServer_indi (EURUSD,M1)          BrokerOffset      = 10800 (GMT+3)
// 2024.04.05 02:05:00.931      TimeTradeServer_indi (EURUSD,M1)        OnCalculate: is called.
// 2024.04.05 02:05:00.931      TimeTradeServer_indi (EURUSD,M1)          Server            = ICMarketsSC-Demo
// 2024.04.05 02:05:00.931      TimeTradeServer_indi (EURUSD,M1)          TimeTradeServer() = 2024.04.05 03:05:00
// 2024.04.05 02:05:00.931      TimeTradeServer_indi (EURUSD,M1)          TimeGMT()         = 2024.04.05 00:05:00
// 2024.04.05 02:05:00.931      TimeTradeServer_indi (EURUSD,M1)          BrokerOffset      = 10800 (GMT+3)

 
amrali:

Interesting bug....

A workaround for now maybe:

Get both, TimeCurrent and TimeTradeServer, return TimeCurrent if it's greater than TimeTradeServer, else return TimeTradeServer.


 
Dominik Egert #:
Get both, TimeCurrent and TimeTradeServer, return TimeCurrent if it's greater than TimeTradeServer, else return TimeTradeServer.

TimeTradeServerExact.

MQL5 Book: Common APIs / Functions for working with time / Local and server time
MQL5 Book: Common APIs / Functions for working with time / Local and server time
  • www.mql5.com
There are always two types of time on the MetaTrader 5 platform: local (client) and server (broker). Local time corresponds to the time of the...
 
Ok, but it does not work in the weekend on brokers that not provide crypto currency trading. There are many other ways to fix for example:
if(TerminalInfoInteger(TERMINAL_CONNECTED) { datetime t = TimeTradeServer(); }

I think either the docs should state this unexpected behavior or the function is fixed some way.
 
amrali #:
Ok, but it does not work in the weekend on brokers that not provide crypto currency trading. There are many other ways to fix for example:
if(TerminalInfoInteger(TERMINAL_CONNECTED) { datetime t = TimeTradeServer(); }

I think either the docs should state this unexpected behavior or the function is fixed some way.
Yes, I agree. That is not expected behavior.
 

Forum on trading, automated trading systems and testing trading strategies

Possible bug in TimeTradeServer

amrali, 2024.04.07 18:56

For a better approach: check the terminal is connected to the trade server before using TimeTradeServer in indicators/experts

if(!TerminalInfoInteger(TERMINAL_CONNECTED))
  {
   Print("Caution: Terminal is not connected, TimeTradeServer will be unreliable.");  
  }
  
//--- now it is safe to use the function
datetime t = TimeTradeServer();

I suggest that TimeTradeServer() should be fixed to return 0 instead of TimeGMT, if the connection to the trade server is not yet established.

This will warn users that something wrong happened. Also setting the _LastError variable would be a good idea.


 

The result was the following code:

datetime TimeServer()
{  datetime tts=0;
   datetime tc=TimeCurrent();
   if (TerminalInfoInteger(TERMINAL_CONNECTED)) tts = TimeTradeServer();
   if (tc>tts) return(tc);
   else return(tts);
 }

Reason: