• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Working with DateTime

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

    Working with DateTime

    Ok I am working with the DateTime function.

    in my example I am capturing the DateTime detail of a specific bar into a variable. myBarTime

    Code:
    private Series<DateTime> myBarTime;
    
    myBarTime[0] = Time[0]
    I am then trying to check if the current bar is a specific distance in time from myBar based upon the hour and date values.

    Code:
    if (Time[0].Hour > myBarTime[0].Hour + 5)
    {
        Do Something
    }
    In the above example if myBarTime is a bar that formed at 06:00 the Hour item is 6

    I was expecting to be able to add 5 to that and it should return 11

    However when I print out the hour value of myBarTime I get 65

    clearly the + is not adding the numbers but rather concatenating them.

    Can you explain how I achieve my desired result which should have myBarTime.Hour[0] +5 to = 11

    Kind regards

    Duncan

    #2
    Hello EastLondonKiwi,

    DateTime.AddHours(int hours)
    to add minutes use
    DateTime.AddMinutes(int minutes)
    https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx

    That said, I am not able to reproduce this behavior.
    Attached is a test indicator and a screenshot of how this looks on my end.
    Are you seeing that the print is not adding 5 to the .Hour int object on your end.
    Attached Files
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks Chelsea I think I had a bracket issue that was causing the problem. the time check seems to work now.

      however I can not get the Date check to work properly:

      Code:
      Time[0].Date > myBarTime[0].Date
      appears to work.

      but

      Code:
      Time[0].Date == myBarTime[0].Date
      does not?

      can you advise

      Comment


        #4
        Do some prints to the output window to see what is being compared.

        Print ( Time[0].Date.ToString() );

        Wouldn't you need in your example : Time[0].Date.CompareTo ( myBarTime[0].Date )

        Are you looking for generic code time entry? I picked this up here a long time ago:

        Code:
        if (      ToTime(Time[0]) >= ToTime( 10, 30, 00)
             && ToTime(Time[0]) <= ToTime( 15, 01, 00) )
         {
        
        Print ( ToTime(Time[0]));
        
         }
        You might also be interested in the TimeSpan which has come in handy.

        http://ninjatrader.com/support/forum...d.php?p=441061

        Also you might never see a == condition - trades aren't guaranteed to happen down to any second...

        Comment


          #5
          Originally posted by sledge View Post
          Do some prints to the output window to see what is being compared.

          Print ( Time[0].Date.ToString() );

          Wouldn't you need in your example : Time[0].Date.CompareTo ( myBarTime[0].Date )

          Are you looking for generic code time entry? I picked this up here a long time ago:

          Code:
          if (      ToTime(Time[0]) >= ToTime( 10, 30, 00)
               && ToTime(Time[0]) <= ToTime( 15, 01, 00) )
           {
          
          Print ( ToTime(Time[0]));
          
           }
          You might also be interested in the TimeSpan which has come in handy.

          http://ninjatrader.com/support/forum...d.php?p=441061

          Also you might never see a == condition - trades aren't guaranteed to happen down to any second...
          Thanks Sledge, but ToTime(Time[0]) is a value in hour format like 12:00:00 and not a date format.

          I found the answer in the function 'Time[0].DayOfYear' which is an int value that is the number of days a bar is from Jan 01 of each year.

          That gave me the opportunity to simply test the current bars DayOfYear against the specific bar I was checking how far away I was.

          Like this:

          Code:
          Time[0].DayOfYear >= myBarDayOfYear

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by GTBrooks, Today, 02:44 PM
          0 responses
          4 views
          0 likes
          Last Post GTBrooks  
          Started by GLFX005, Today, 12:22 PM
          3 responses
          39 views
          0 likes
          Last Post GLFX005
          by GLFX005
           
          Started by AspenHiker, Today, 08:37 AM
          1 response
          12 views
          0 likes
          Last Post NinjaTrader_Kate  
          Started by MartinT, Yesterday, 01:12 PM
          3 responses
          16 views
          0 likes
          Last Post MartinT
          by MartinT
           
          Started by caveat_lector, Today, 11:58 AM
          1 response
          7 views
          0 likes
          Last Post NinjaTrader_Heath  
          Working...
          X