Download MetaTrader 5

GMT to Server

To add comments, please log in or register
PieroNetto
295
PieroNetto  

HI there!

Im trying to get the difference from GMT to Server, but is the server shows midnight as 0 and GMT shows as 21, so of curse 21 - 0 = 21.

21 would be the wrong different between GMT and Server in my case.

How do i calculate it the right way.

Thanks a lot, Piero

int start()
{
      datetime gt          = TimeGMT();
      datetime GMT_Time    = TimeHour(gt);
      datetime st          = TimeCurrent();
      datetime Server_Time = TimeHour(st);
      datetime lt          = TimeLocal();
      datetime Local_Time  =TimeHour(lt);
      
      
      int   GMT_to_Local    = TimeGMTOffset()/3600;
      int   GMT_to_Server   = (GMT_Time - Server_Time);
      
      Comment("GMT to Local       = ",GMT_to_Local, "\n",
              "GMT to Server      = ",GMT_to_Server,"\n",
              "GMT Time           = ",GMT_Time,     "\n",
              "Local Time         = ",Local_Time,   "\n",
              "Server Time        = ",Server_Time);
              
      return(0);
      }
Carl Schreiber
7467
Carl Schreiber  

1.) This: 

int   GMT_to_Server   = (GMT_Time - Server_Time);

might cause an issue if Server_Time > GMT_Time!

2.) Your code does not work in the StrategyTester! To catch this I suggest you to find a weekend, as the normal forex session starts at 17:00 NY-Time. Regarding the USA DST you can calc the GMT.

PieroNetto
295
PieroNetto  
Carl Schreiber:

1.) This: 

might cause an issue if Server_Time > GMT_Time!

2.) Your code does not work in the StrategyTester! To catch this I suggest you to find a weekend, as the normal forex session starts at 17:00 NY-Time. Regarding the USA DST you can calc the GMT.


My Server_Time is > GMT_Time and returns -3, so this is correct, Server_Time - 3 = GMT_Time. 

The problem is when i'm crossing midnight and Server_Time returns 0 or 1 or 2.

(GMT_Time "22") - (Server_Time "0") = (GMT_to_Server "22"). So my difference is 22 not correct.

Please help what can i do?

During testing in the Strategy Tester, TimeGMT() is always equal to TimeCurrent() simulated server time.

I know that, Thanks anyway for your time.

Carl Schreiber
7467
Carl Schreiber  

Not tested but you can try:

if (GMT_Time    < 12) GMT_Time += 24;
if (Server_Time < 12) Server_Time += 24;
diff = (GMT_Time-Server_Time)%24;
whroeder1
16287
whroeder1  
  1. int   GMT_to_Server   = (GMT_Time - Server_Time);
    This is what you have to add to Server_Time to get to GMT_Time. The variable name is backwards.

  2. Because of timing differences, it needs to be rounded to timezone.
    datetime gt           = TimeGMT();
    datetime st           = TimeCurrent();
    int      sTZ          = (st-gt + 1800)/3600;
    int   GMT_to_Server_Correct = 3600*sTZ;
  3. PieroNetto:

    My Server_Time is > GMT_Time and returns -3, so this is correct, Server_Time - 3 = GMT_Time. 

    The problem is when i'm crossing midnight and Server_Time returns 0 or 1 or 2.

    (GMT_Time "22") - (Server_Time "0") = (GMT_to_Server "22"). So my difference is 22 not correct.

    Please help what can i do?

    Don't look at hours. Hours rolls over, time do not. GMT_Time = ServerTime + GMT_to_Server_Correct

  4. As Carl Schreiber said, the above doesn't work in the tester. Ignoring DST
    input int ServerTZ = +3;
    int sTZ; // Initialized in OnTick
    :
    datetime ServerToGMT(datetime when=0){
       if(when == 0) when = TimeCurrent();
       return when + 3600 * (isTesting() ? ServerTz : sTz);
    }
  5. If you are testing over 6 months, you'll have to find out what DST is for your broker and handle that as well.


PieroNetto
295
PieroNetto  
Carl Schreiber:

Not tested but you can try:


Thanks a lot, will play with it tomorrow when market is open and i can test it.

Was trying a few things with +=24, don't know how to.

Hear from me later, thanks again.

nicholishen
964
nicholishen  
whroeder1:
  1. This is what you have to add to Server_Time to get to GMT_Time. The variable name is backwards.

  2. Because of timing differences, it needs to be rounded to timezone.
  3. Don't look at hours. Hours rolls over, time do not. GMT_Time = ServerTime + GMT_to_Server_Correct

What is rounding to time-zone? Why are you adding 30 minutes to the time difference? Your equation is not simplified.  Simplified it's 

int   GMT_to_Server_Correct = (int)TimeCurrent() - (int)TimeGMT() + 1800;

Your time calculation will always be off by 30 minutes. 

whroeder1
16287
whroeder1  
nicholishen:

What is rounding to time-zone? Why are you adding 30 minutes to the time difference? Your equation is not simplified.  Simplified it's 

Your time calculation will always be off by 30 minutes.

I didn't add 30 minutes. There will be jitter on reading both timestamps, but you know that the time zone is an exact 3600 difference. What if your server time read is 23:59:59 and GMT is read 21:00:01. The difference is not 3*3600; it is two seconds less and 7198/3600 is 2 not 3. I rounded to the nearest hour, (3600 seconds.)
int      sTZ          = (st-gt + 1800)/3600;

How do you round a double to the nearest int? int r = int(d+0.5)
How do you round to the nearest hour? int tz = int(t/3600.0 + 0.5) or the equivalent using only integer arithmetic: tz = int(t + 1800)/3600.

nicholishen
964
nicholishen  
whroeder1:
I didn't add 30 minutes. There will be jitter on reading both timestamps, but you know that the time zone is an exact 3600 difference. What if your server time read is 23:59:59 and GMT is read 21:00:01. The difference is not 3*3600; it is two seconds less and 7198/3600 is 2 not 3. I rounded to the nearest hour, (3600 seconds.)

How do you round a double to the nearest int? int r = int(d+0.5)
How do you round to the nearest hour? int tz = int(t/3600.0 + 0.5) or the equivalent using only integer arithmetic: tz = int(t + 1800)/3600.


Time is measured in seconds. You added 1800 seconds to the delta. 1800 / 60 = 30 minutes.

whroeder1
16287
whroeder1  

Yes and then divided by 3600 to get rounded Timezone (hours.) tz = int(st-gt + 1800)/3600. You're not thinking about the next part.

I could have done int tz = (int) MathRound( (st-gt)/3600.0) and got the same result (except slower because of the four floating point operations and a function call.)

nicholishen
964
nicholishen  
whroeder1:

Yes and then divided by 3600 to get rounded Timezone (hours.) tz = int(st-gt + 1800)/3600. You're not thinking about the next part.

I could have done int tz = (int) MathRound( (st-gt)/3600.0) and got the same result (except slower because of the four floating point operations and a function call.)

I still don't see where you're going there by adding thirty minutes to the delta then dividing by 60 min... ???

Why not just keep it simple and let int division do its thing? There's no need to over-complicate it. 

int gmt_offset = ((int)TimeCurrent()-(int)TimeGMT()) / 3600;
To add comments, please log in or register