Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Why does this not work?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Why does this not work?

    NO errors are produced but it fails.

    Code:
    DateTime currentTime = DateTime.Now;
    DateTime setTime = new DateTime(2024,06,25,9, 46, 00);// time ahead of Now
    if(currentTime == setTime)
    {
    Print("Current time equals mySetTime");
    }​​


    Code:
    if(currentTime.Equals(setTime))
    Also does not work.
    Last edited by Mindset; 06-24-2024, 11:29 PM.

    #2
    This is a C# question.

    The DateTime values of currentTime and setTime are equal
    only if their Ticks property values are equal.​

    Have you tried printing the value of Ticks to see why your
    DateTime values are not equal?

    Comment


      #3
      Hi

      I tried this as well
      DateTime.Compare(date1,date2);

      So it doesn't appear to be a ticks issue as from MSDN

      "the Compare method compares the Ticks property"
      I am guessing there is something going on to do with Bars that I am missing.​
      Edit - essentially I only need to compare to the second level
      Last edited by Mindset; 06-25-2024, 12:51 AM.

      Comment


        #4
        I really feel this should not be a complicated thing at all.
        So far I can get my strategy to work using a workaround (ToTime.Time[0] == SetTime)
        Edit - I should add setTime is an int here - not a DateTime object.
        which oddly requires second precision to work but if I want to look 1 second before the bar closes I can't seem to work it out
        Last edited by Mindset; 06-25-2024, 03:26 AM.

        Comment


          #5
          Originally posted by bltdavid View Post
          This is a C# question.

          The DateTime values of currentTime and setTime are equal
          only if their Ticks property values are equal.​

          Have you tried printing the value of Ticks to see why your
          DateTime values are not equal?

          So using OnBarUpdate will rarely get the same ticks value if I understand that correctly - so how can i compare just seconds and not Ticks?

          Comment


            #6
            Hello Mindset,

            If you are trying to make a time condition that uses a DateTime as input the input DateTime needs to be exactly equal to the bar time. If you were using 1 minute bars the time you make needs to be a time that has 0 seconds, if you were using a 30 second bar the time would need to be in intervals of 30 seconds and so on.

            In your original sample you are using the PC time and not bar times so that would be hard to compare against, if you are trying to make a print when the bar time equals your input time it would be best to use the Time[0] value. The easiest way to compare times in NinjaScript is to use the ToTime method to convert your DateTimes to ints which is much easier to print and compare.



            Code:
            Print(ToTime(Time[0]) + " " + ToTime(setTime));
            
            if (ToTime(Time[0]) == ToTime(setTime))
            Alternatively you can use the TimeOfDay property like the strategy builder:

            Code:
            if (Time[0].TimeOfDay == setTime.TimeOfDay)
            {
            }

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            558 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            324 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            101 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            545 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            547 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X