GMT and broker shift help (confirm my code)

 

Hi. 

I have been working with this news indicator and its working great for me but when someone far away uses it. The news don't comes in the right place.

Can someone please have a look at the code and see if there is something that is not right. 

So here it makes the array with the news only including the relevant code 

AdjustNewsTime(date, time, AutoAdjustTimeDiff);
                  NewsArray[j][DATE] = date;
                  NewsArray[j][TIME] = time;

 Here is the auto adjust function that should fix the broker GMT issue eaven if it has to shift day

void AdjustNewsTime(string &date, string &time, bool auto)
{
   string stime = date+" "+time;
   datetime dtime;

   if(auto)
   {
      dtime = StrToTime(stime) + GMTOffset();
   }
   else
   {
      dtime = StrToTime(stime) + NewsTimeDiff * 3600;
   }
   string adjusted = TimeToStr(dtime, TIME_DATE | TIME_MINUTES);
   date = StringSubstr(adjusted, 0, 10);
   time = StringSubstr(adjusted, 11, 0);
}

 And the GMT shift that I think has some issue

int GMTOffset()
{
   int srv = (int)TimeCurrent();
   int GMT = (int)TimeGMT();
   int Offset = 0, srvOffset = 0;

   if (TimeHour(srv) > TimeHour(GMT))
   {
      Offset = TimeHour(srv) - TimeHour(GMT);
      srvOffset = (Offset * 60 * 60);
   }
   else if (TimeHour(srv) < TimeHour(GMT))
   {
      Offset = TimeHour(GMT) - TimeHour(srv);
      srvOffset = (Offset * 60 * 60);
   }
   else
   {
      srvOffset = 0;
   }
   GMT = (int)TimeCurrent() - srvOffset;
   return(srvOffset);
}

 Ok TThat should be it. 

Please let me know if you can see something that would make it not to work if the broker time diff is to huge or something else.

 

Thank you 

 

Your GMTOffset is not reliable. TimeCurrent() is server time, TimeGMT() is local time, there is little chance they are synchronized.

Suppose server is at GMT+3, you function should return 3, right ?

But TimeCurrent() can be 23:59:48 and TimeGMT() 21:00:16, what will return your function ?

 

Isn't TimeGMT() the GMT time and TimeLocal() the local time? 

However I don't want anything to do with local time cause that's not what I'm looking for.

I  just want to make a function that automaticlu can calculate the GMT and ServerTime diffrence so that you don't have to manually set the value so the news drops in on the right place. 

Help anyone?  

 

The build in function in metatader gmtoffset () display the local and time GMT offset I know that but the function in my first post should calculate server and gmt offset in hours. Shouldn't it? 

But I don't think it works properly  

 

All broker times as I have tested so far has the minutes synced perfectly so there is no meaning including the minutes. 

But if you would work with timelocal() then yes the minutes would be nesseseary to include. Or I'm I wrong? 

Anyhow I only want the hourly diff calculated correctly so if it's 01:00 server time and 21:00 GMT time it should be able to pull it off and say it's -4 

 
slicken:

All broker times as I have tested so far has the minutes synced perfectly so there is no meaning including the minutes. 

But if you would work with timelocal() then yes the minutes would be nesseseary to include. Or I'm I wrong? 

Anyhow I only want the hourly diff calculated correctly so if it's 01:00 server time and 21:00 GMT time it should be able to pull it off and say it's -4 

Your function is not reliable, and will sometimes provide a wrong value.

But it's as you wish.

 

Its resolved! No need for more help , thx!  

 
Daniel Sic #:

Its resolved! No need for more help , thx!  

I am also facing the same problem, can you share your solution?

 
Pham Quoc Khanh #:

I am also facing the same problem, can you share your solution?

In Mt5 

int secs_offset=(int)TimeTradeServer()-(int)TimeGMT();
//so if your news are in gmt seconds timestamp ,to plot them on the chart you :
datetime news_time_on_chart=(datetime)(_news_time_gmt+secs_offset);

in Mt4 you need to know your brokers gmt offset , or , make sure you are calling the following when the broker is active (and TimeCurrent is not frozen at 23:59)

int secs_offset=(int)TimeCurrent()-(int)TimeGMT();
//so if your news are in gmt seconds timestamp ,to plot them on the chart you :
datetime news_time_on_chart=(datetime)(_news_time_gmt+secs_offset);
Reason: