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 sjsj2732, Today, 04:31 AM
            0 responses
            20 views
            0 likes
            Last Post sjsj2732  
            Started by NullPointStrategies, 03-13-2026, 05:17 AM
            0 responses
            280 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            279 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            130 views
            1 like
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            90 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Working...
            X