Current Time, Server Time

Created at 22 Mar 2021, 17:05
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
KE

kebbo

Joined 28.09.2020

Current Time, Server Time
22 Mar 2021, 17:05


Hi Community,

Is there any way to get the current time/date from the server?
-Server.Time.
Problem: In the backtest the current time is not returned, but the time of the respective bar.
-DateTime.Now
Problem: Returns the time of the executing computer.

But what I want is that the current time of the server (e.g. at the start of the backtest) is returned.

Is this possible?

Thank you very much! Have a nice day!

 

 


@kebbo
Replies

amusleh
22 Mar 2021, 17:12

Server.Time works very well on back test, if you use the server time on back test it will give you the time based on back test time not current time.

If you want to get the current time during back test you can use the DateTimeOffset.Now and then change its offset to server time offset, its still dependent to user system time.

If you need the current time but you don't want to use the user system time then you can use a web request, the current time will be on request header.

 


@amusleh

amusleh
22 Mar 2021, 17:20 ( Updated at: 22 Mar 2021, 20:11 )

You can use this class to get the current time:

        public static DateTimeOffset GetTime()
        {
            var currentTime = DateTimeOffset.Now;

            string[] hosts =
            {
                "http://www.google.com",
                "http://www.microsoft.com",
                "http://timezonedb.com/",
                "http://www.twitter.com",
                "http://spotware.com",
            };

            Parallel.ForEach(hosts, (host, loopState) =>
            {
                try
                {
                    if (loopState.ShouldExitCurrentIteration)
                    {
                        return;
                    }

                    var response = WebRequest.Create(host).GetResponse();

                    if (loopState.ShouldExitCurrentIteration)
                    {
                        return;
                    }

                    currentTime = DateTime.ParseExact(
                        response.Headers["date"],
                        "ddd, dd MMM yyyy HH:mm:ss 'GMT'",
                        CultureInfo.InvariantCulture.DateTimeFormat
                        , DateTimeStyles.AssumeUniversal).ToLocalTime();

                    if (loopState.ShouldExitCurrentIteration)
                    {
                        return;
                    }

                    loopState.Stop();
                }
                catch (Exception ex) when (ex is WebException || ex is FormatException)
                {
                }
            });

            return currentTime;
        }

If the system isn't connected to internet or there was any other issue in connection with listed hosts the method will return DateTimeOffset.Now which is the current time of user system, modify it based on your needs.


@amusleh

kebbo
22 Mar 2021, 19:21

Hi,

thank you very much for your detailed answer!

I will test this once, however, I do not have so much experience with web requests, possibly there are difficulties with the implementation.

Is it possible to access the time information that is displayed at the bottom right of the platform at "current time:"?

Thank you!


@kebbo

amusleh
22 Mar 2021, 20:12

RE:

kebbo said:

Hi,

thank you very much for your detailed answer!

I will test this once, however, I do not have so much experience with web requests, possibly there are difficulties with the implementation.

Is it possible to access the time information that is displayed at the bottom right of the platform at "current time:"?

Thank you!

I updated the code, please try again.


@amusleh

kebbo
24 Mar 2021, 08:37

RE: RE:

amusleh said:

kebbo said:

Hi,

thank you very much for your detailed answer!

I will test this once, however, I do not have so much experience with web requests, possibly there are difficulties with the implementation.

Is it possible to access the time information that is displayed at the bottom right of the platform at "current time:"?

Thank you!

I updated the code, please try again.

Thanks again for your effort!

I would like to get along without WebRequests.
Do you have any other ideas, or are you convinced that there is no other way?

Is it possible to access the time displayed at the bottom right of cTrader? This time is retrieved directly from the server, isn't it?

When backtesting, I need the current time and not the times of the respective candle, and also not the system time of the executing PC, but the current server time...

It would be great if you could help me.

Many greetings and many thanks

 


@kebbo

amusleh
24 Mar 2021, 09:19

RE: RE: RE:

kebbo said:

amusleh said:

kebbo said:

Hi,

thank you very much for your detailed answer!

I will test this once, however, I do not have so much experience with web requests, possibly there are difficulties with the implementation.

Is it possible to access the time information that is displayed at the bottom right of the platform at "current time:"?

Thank you!

I updated the code, please try again.

Thanks again for your effort!

I would like to get along without WebRequests.
Do you have any other ideas, or are you convinced that there is no other way?

Is it possible to access the time displayed at the bottom right of cTrader? This time is retrieved directly from the server, isn't it?

When backtesting, I need the current time and not the times of the respective candle, and also not the system time of the executing PC, but the current server time...

It would be great if you could help me.

Many greetings and many thanks

 

Not sure but I don't think its possible to get the current server time during back test, getting time from web request is one option, if you Google you might find other options.


@amusleh

kebbo
26 Mar 2021, 18:12

RE: RE: RE: RE:

Hi again,

do you have more precise information on which time value "DateTimeOffset" refers to? Where are the times taken from which the offset value is calculated?

When I use DateTimeOffset.Now.Offset.Hours, I always have the same value output, regardless of the platform time set, as well as regardless of the computer system time set.
This could help me, but for my understanding I would like to know how this offset value is calculated.

Best regards
kebbo


@kebbo

amusleh
26 Mar 2021, 18:57

RE: RE: RE: RE: RE:

kebbo said:

Hi again,

do you have more precise information on which time value "DateTimeOffset" refers to? Where are the times taken from which the offset value is calculated?

When I use DateTimeOffset.Now.Offset.Hours, I always have the same value output, regardless of the platform time set, as well as regardless of the computer system time set.
This could help me, but for my understanding I would like to know how this offset value is calculated.

Best regards
kebbo

Read this: 

Both DateTime.Now and DateTimeOffset.Now gives you the system time, you can get time from somewhere else like an API if you have one or if you don't have any you can use the web requests time, you can find other options if you google.


@amusleh

kebbo
27 Mar 2021, 09:29

RE: RE: RE: RE: RE: RE:

amusleh said:

kebbo said:

Hi again,

do you have more precise information on which time value "DateTimeOffset" refers to? Where are the times taken from which the offset value is calculated?

When I use DateTimeOffset.Now.Offset.Hours, I always have the same value output, regardless of the platform time set, as well as regardless of the computer system time set.
This could help me, but for my understanding I would like to know how this offset value is calculated.

Best regards
kebbo

Read this: 

Both DateTime.Now and DateTimeOffset.Now gives you the system time, you can get time from somewhere else like an API if you have one or if you don't have any you can use the web requests time, you can find other options if you google.

Hi and thank you again!

I did some experiments with the DateTimeOffset class and got the "Offset hours" output as described in my previous post.
Additionally the server time and the system time via the DateTime.
I then set the system time one hour earlier or later and found that the offset value always remained the same. I therefore asked myself where the time for DateTimeOffset is taken from.
With DateTime, the new time is output when the system time is changed.

Do you understand what I mean?

DateTimeOffset.Now.Offset.Hours gives me the difference in hours between my system time and the server time, right? But why do they stay the same when I change the system time...?

best regards


@kebbo

amusleh
27 Mar 2021, 11:25

RE: RE: RE: RE: RE: RE: RE:

kebbo said:

amusleh said:

kebbo said:

Hi again,

do you have more precise information on which time value "DateTimeOffset" refers to? Where are the times taken from which the offset value is calculated?

When I use DateTimeOffset.Now.Offset.Hours, I always have the same value output, regardless of the platform time set, as well as regardless of the computer system time set.
This could help me, but for my understanding I would like to know how this offset value is calculated.

Best regards
kebbo

Read this: 

Both DateTime.Now and DateTimeOffset.Now gives you the system time, you can get time from somewhere else like an API if you have one or if you don't have any you can use the web requests time, you can find other options if you google.

Hi and thank you again!

I did some experiments with the DateTimeOffset class and got the "Offset hours" output as described in my previous post.
Additionally the server time and the system time via the DateTime.
I then set the system time one hour earlier or later and found that the offset value always remained the same. I therefore asked myself where the time for DateTimeOffset is taken from.
With DateTime, the new time is output when the system time is changed.

Do you understand what I mean?

DateTimeOffset.Now.Offset.Hours gives me the difference in hours between my system time and the server time, right? But why do they stay the same when I change the system time...?

best regards

Hi,

Your question is not related to the cTrader automate API, its related to .NET.

A DateTimeOffset Offset property returns the offset or difference of that DateTimeOffset from UTC, it doesn't change if you change the system time settings.

Please read the link I posted on my previous post and you will find all detail related to DateTimeOffset.


@amusleh

kebbo
27 Mar 2021, 22:03

RE: RE: RE: RE: RE: RE: RE: RE:

hi,

I followed the link and read it through, thank you.

amusleh said:

A DateTimeOffset Offset property returns the offset or difference of that DateTimeOffset from UTC, it doesn't change if you change the system time settings.

 

 

Here we are exactly at the point where I have difficulties in understanding.

You say an offset value from UTC time, but what is the reference time? If the reference point (from which the offset is calculated) is my system time, then the DateTimeOffset value should also adjust when the system time changes, shouldn't it?

 

best regards


@kebbo

amusleh
28 Mar 2021, 11:18

RE: RE: RE: RE: RE: RE: RE: RE: RE:

kebbo said:

hi,

I followed the link and read it through, thank you.

amusleh said:

A DateTimeOffset Offset property returns the offset or difference of that DateTimeOffset from UTC, it doesn't change if you change the system time settings.

 

 

Here we are exactly at the point where I have difficulties in understanding.

You say an offset value from UTC time, but what is the reference time? If the reference point (from which the offset is calculated) is my system time, then the DateTimeOffset value should also adjust when the system time changes, shouldn't it?

 

best regards

Hi,

Offset is the time zone offset not time of day, if you want to get the time of day you can use DateTimeOffset.Now.TimeOfDay.


@amusleh