Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Comparing Time[0] to variable

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

    Comparing Time[0] to variable

    Code:
    if (DateTime.TryParse(trade21Datetime, out myDateTime21))
         Print("Parsed successfully:\t" + myDateTime21);
    else
         Print("Unable to understand DateTime input.");
    
    
    if(Time[0] == myDateTime21)
    {
         Print("Current time equals myDateTime21");
    }
    Print("==========");
    Print("myDateTime21: " + myDateTime21);
    Print("Time[0]: " + Time[0]);
    This is the output:
    Parsed successfully: 1/20/2022 9:07:06 AM
    ==========
    myDateTime21: 1/20/2022 9:07:06 AM
    Time[0]: 1/20/2022 9:07:06 AM
    Can someone please help me understand why "Current time equals myDateTime21" doesn't print? When I print out Time[0] and myDateTime21 they appear to match, but when I compare them in the if statement they don't seem to be equal.


    #2
    Then they are not equal
    Try to out them with milliseconds.

    Comment


      #3
      Originally posted by Leeroy_Jenkins View Post
      Then they are not equal
      Try to out them with milliseconds.
      Thanks for your reply.

      The variable comes from a spreadsheet and the granularity is seconds, are you aware of a way to chop off the milliseconds?

      Or perhaps there's another way?

      Comment


        #4
        Here's a workaround that might suffice for now:

        Code:
        if (DateTime.TryParse(trade21Datetime, out myDateTime21))
             Print("Parsed successfully:\t" + myDateTime21);
        else
             Print("Unable to understand DateTime input.");
        
        
        if(Time[0] <= myDateTime21.AddSeconds(1) && Time[0] >= myDateTime21.AddSeconds(-1))
        {
             Print("Current time equals myDateTime21");
        }
        Print("==========");
        Print("myDateTime21: " + myDateTime21);
        Print("Time[0]: " + Time[0]);
        Output:
        Parsed successfully: 1/20/2022 9:07:06 AM
        Current time equals myDateTime21
        ==========
        myDateTime21: 1/20/2022 9:07:06 AM
        Time[0]: 1/20/2022 9:07:06 AM

        Comment


          #5
          Or like this:

          Code:
          DateTime time0 = Time[0].AddTicks(Time[0].Ticks % TimeSpan.TicksPerSecond);
          
          if(time0 == myDateTime21)
          {
          Print("Current time equals myDateTime21");
          }

          Comment


            #6
            Hello WalterSkinner,

            You would need to create a new DateTime object if you wanted to chop the seconds off.

            DateTime myDateTime = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, Time[0].Hour, Time[0].Minute)

            Or you could subtract the seconds as you suggested.
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by DJ888, 04-16-2024, 06:09 PM
            6 responses
            18 views
            0 likes
            Last Post DJ888
            by DJ888
             
            Started by Jon17, Today, 04:33 PM
            0 responses
            1 view
            0 likes
            Last Post Jon17
            by Jon17
             
            Started by Javierw.ok, Today, 04:12 PM
            0 responses
            6 views
            0 likes
            Last Post Javierw.ok  
            Started by timmbbo, Today, 08:59 AM
            2 responses
            10 views
            0 likes
            Last Post bltdavid  
            Started by alifarahani, Today, 09:40 AM
            6 responses
            41 views
            0 likes
            Last Post alifarahani  
            Working...
            X