Best time source to use for checking current time?

Created at 28 Sep 2015, 19:26
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!
ST

stevie.c

Joined 28.09.2015

Best time source to use for checking current time?
28 Sep 2015, 19:26


Hi,

I'm gradually getting to grips with cAlgo and I'm coding an indicator that requires testing the current local time against a fixed (constant) time, but I'm having a problem in that when the indicator is running on any pair for any timeframe, there is always a delay or lag in the time refresh/update, which causes the time tests to fail because the time will jump several seconds, seemingly because the chart for the pair has not updated/refreshed. I've tried serverTime and DateTime.now, but get the same problem with both. Am I using the correct time source? As a workaround I'm having to make the test check a 10 second window around the constant.

Any help would be appreciated. Thank you.

 


@stevie.c
Replies

Spotware
28 Sep 2015, 20:14

Dear Trader,

The DateTime.Now method returns the current date and time on your computer. Unfortunately, we cannot fully understand what you are trying to do. Could you please provide us with a more detailed explanation?


@Spotware

stevie.c
28 Sep 2015, 21:36

Thanks for responding. To explain further, my indicator tests DateTime.Now and if it is later than a specified time, an alarm should sound (called by Notifications.Playsound) and a message will be displayed on the chart (ChartObjects.DrawText) that the indicator is active on. The code builds successfully and I can add an instance to a chart.

So, for example, when 17:00:00 < DateTime.Now < 17:00:02 is TRUE (i.e. DateTime.Now = 17:00:01) I would expect the alarm to sound and the message to be displayed. However, this does not always happen (sometimes it will, sometimes it won't), and when I have displayed DateTime.Now on the chart (using ChartObjects.DrawText...DateTime.Now.ToLongString()) to troubleshoot the problem, I have observed that the time will not always update regularly, and will sometimes 'stop' on a value and then catch up several seconds later, seemingly when the chart itself updates. It makes me think that the accuracy/update frequency of the time source I'm using at the moment is dependent upon the refresh rate for the chart it is instantiated on, but surely this cannot be the case?

I hope I've explained it better for you? Thanks again.


@stevie.c

ClickAlgo
28 Sep 2015, 22:05

Hi Stevie,

Just a quick response as I am busy.

Try and update the chart text using OnTick and not OnBar

Read more about it here http://help.spotware.com/calgo/cbots/onbar-vs-ontick

To compare dates; look at:

DateTime dt1 = DateTime.Parse("07/12/2011");
DateTime dt2 = DateTime.Now;

if(dt1.Date > dt2.Date)
{
     //It's a later date
}
else
{
     //It's an earlier or equal date
}

 


@ClickAlgo

stevie.c
29 Sep 2015, 00:50

Hi Paul,

Thanks for the reply - very much appreciated. To clarify, I don't use OnTick or OnBar in my indicator. The display updates from within: public override void Calculate, just using ChartObjects.DrawText..., in a similar way to your Market Trading Clock indicator (which has been a very useful reference thank you). Also, I've already tried the approach you've suggested for testing the time and it suffers from the same problem as my current method, i.e. it is very hit-and-miss because DateTime.Now does not seem to provide a reliable, constant feed of data that updates on a second-by-second basis, so the detection of a later date might only become TRUE, say, several seconds AFTER it should have actually triggered. Any further advice would be very welcome!

 


@stevie.c

ClickAlgo
29 Sep 2015, 08:53

Take a closer look at the Market Clock indicator for date time comparisons, you may be missing something, if you still stuck post a snippet of your code here, we need to see code to help you.


@ClickAlgo

stevie.c
29 Sep 2015, 18:44

Thanks Paul.

I'm not sure that TimeSpan will provide a solution to the problem I'm encountering - I've experimented with Market Trading Clock (MTC) and observed the same problem. To elaborate, in the MTC indicator I changed the clock format thus:

marketClocks.ClockFormat = "HH:mm:ss";

Now, when the MTC indicator clocks are observed on any chart, the seconds are displayed but will quite often pause and then jump 2 or 3 seconds to catch up. This seems to affect any accurate time check being performed, since the jump in time can mean that a test fails as the time might never equal the time being checked owing to the jump in seconds. I've tried DateTime.Compare but again the jumps affect the comparison. It seems like a simple thing such as testing the current time to an accuracy of 1 second and then triggering an event when a specified time is met is hampered by this problem.

Thanks again for your input.


@stevie.c

ClickAlgo
29 Sep 2015, 22:25

Hey no worries, I hope you get it sorted, if this doesn't help then maybe the OnCalculate() method doesn't get called every second..

http://stackoverflow.com/questions/16032451/get-datetime-now-with-milliseconds-precision

 

 


@ClickAlgo

stevie.c
01 Oct 2015, 22:20

Can Spotware confirm/clarify...?

At the moment I tend to agree with you Paul, in that for an Indicator, Calculate() does not get called every second. Perhaps a Spotware representative can clarify?

 

 


@stevie.c

cyfer
02 Oct 2015, 23:43

it's very hard to know the problem or the fix without code , you could try to split part of the code showing the issue and post it here 

however , because this is C# and you say it works some times and some times it lags .. it could be GC related 

but again it's hard to tell without sample code 

 


@cyfer

stevie.c
03 Oct 2015, 13:23

Thanks for your input. The code is a very simple 'if' statement incorporating 'Notifications.Playsound' and 'ChartObjectsDrawtext', thus:

public override void Calculate(int index)
if (DateTime.Now.Minute == 59 && DateTime.Now.Second < 10)
    {
     Notifications.PlaySound("c:\\windows\\media\\tada.wav");
     ChartObjects.DrawText("MESSAGETEXT_1", "Alarm triggered at: " + DateTime.Now.ToLongTimeString(), StaticPosition.Center, Colors.Yellow);
     }

Now, ideally the 'if' statement should work with...

DateTime.Now.Minute == 59 && DateTime.Now.Second == 0

...but it does not, owing to these jumps that I'm observing (and as I mentioned before, it is not just my indicator that suffers from this problem). I've had to put DateTime.Now.Second < 10 in there in an attempt to overcome the jumping, so at least this way there is a 10 second window of opportunity for the 'if' statement to be true.

From reading older posts I am of the opinion that Calculate() perhaps only runs on a per tick basis at best, which then causes these jumps in time. Of course, this indicator would then never run when the markets are closed as Calculate() is not being called independently of the market status. As an aside, I cannot see why I should not be able to run such an alarm indicator outside of market hours if it is only using my system's DateTime.

 

 


@stevie.c

simba
03 Oct 2015, 21:35

In your indicator or cbot code, call Server.Time and use it as a time source. you will be fine with that. Especially it won't make sense in backtesting instead of using DateTime,Now...


@simba

stevie.c
04 Oct 2015, 02:33

As I stated in my first post, I have tried Server.Time and I get the same problem. Thanks though.


@stevie.c

cyfer
04 Oct 2015, 04:43

well , eh .. I got some progress  .. always a delay around 3 sec !!

maybe its how long the text takes to display ? 

protected override void Initialize()
        {

            Timer.Start(1);


        }

        protected override void OnTimer()
        {
            ChartObjects.DrawText("time", Time.ToString("HH:mm:ss"), StaticPosition.TopLeft);
            if (DateTime.Now.Minute == 37 && DateTime.Now.Second == 0)
            {
                Notifications.PlaySound("C:\\windows\\media\\tada.wav");
                ChartObjects.DrawText("MESSAGETEXT_1", "Alarm triggered at: " + DateTime.Now.ToLongTimeString(), StaticPosition.Center, Colors.Yellow);
            }
        }

This lag between the exact time specified and when it shows is the problem ? 

BTW , I couldn't get the sound to play .. it must be something on my PC 

 


@cyfer

stevie.c
05 Oct 2015, 00:21

Thanks for having a crack at it Cyfer. However, the lag between the actual time and the time specified in the 'if' statement happens regardless of whether or not the time is displayed on screen, i.e. anything I put in the 'if' statement will occasionally be missed owing to the irregular value of DateTime.

Would anyone from Spotware care to comment or make any suggestions?

Thanks.


@stevie.c