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

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)
    Represents an instant in time, typically expressed as a date and time of day.


    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.



        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.



          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


            #6
            I think this is the same topic. How can I use the time function to exit or prevent reentry 2 min before session close for example?

            Comment


              #7
              Code:
              // field variable outside of OnBarUpdate
              DateTime MyTime;
              
              // later, in OnBarUpdate
              if (SomethingIsTrue) MyTime = Time[0];
              
              // further down, in OnBarUpdate
              if ((Time[0] - MyTime).TotalHours >= 5)
              {
                // here, it is at least 5 hours after SomethingIsTrue
              }
              ​
              Bruce DeVault
              QuantKey Trading Vendor Services
              NinjaTrader Ecosystem Vendor - QuantKey

              Comment


                #8
                Hello ronaldgreene828,

                Thanks for your note.

                You could create a time filter to limit trades being placed between a certain time range.

                If you want to exit 2 minutes before the session close, you could use a Time condition that checks if the current time is two minutes before the session close time and call your Exit method within that condition.

                See this 'How to create time conditions' and 'How to create Time FIlters' sections of the help guide page below.

                https://ninjatrader.com/support/helpGuides/nt8/strategybuilder_condition_builder.htm
                Last edited by NinjaTrader_BrandonH; 06-15-2023, 07:30 AM.
                Brandon H.NinjaTrader Customer Service

                Comment


                  #9
                  Thanks. Sometimes it's hard to find that stuff without the correct wording.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by warreng86, 11-10-2020, 02:04 PM
                  5 responses
                  1,356 views
                  0 likes
                  Last Post NinjaTrader_Manfred  
                  Started by Perr0Grande, Today, 08:16 PM
                  0 responses
                  3 views
                  0 likes
                  Last Post Perr0Grande  
                  Started by elderan, Today, 08:03 PM
                  0 responses
                  5 views
                  0 likes
                  Last Post elderan
                  by elderan
                   
                  Started by algospoke, Today, 06:40 PM
                  0 responses
                  10 views
                  0 likes
                  Last Post algospoke  
                  Started by maybeimnotrader, Today, 05:46 PM
                  0 responses
                  12 views
                  0 likes
                  Last Post maybeimnotrader  
                  Working...
                  X