Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Last Half Bar Duration

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

    Last Half Bar Duration

    In my strategy, I’m trying to determine the duration (minutes & seconds) of the last half of the previous bar, and am using a bar period value of 466 ticks. The code below generates the error “Cannot implicitly convert type ‘System.DateTime’ to ‘int’”. Any suggestions on what I need to do to find out the duration of the last half of the previous bar?

    private int BarEndTime = 0;

    if (Bars.TickCount >= 233 && BarEndTime == 0)
    {
    BarEndTime = Time[0];
    }

    #2
    DenMan, Time[0] would be a # DateTime object, to convert this to an integer just the ToTime() method -

    Comment


      #3
      Way to easy!!! Should have been able to figure that one out myself. Thanks!

      Comment


        #4
        No worries, good luck for the work ahead.

        Comment


          #5
          I now have another issue. The int time value I created appears to be in the following format "hhmmss". Is there a Math method to subtract that variable from the current time to get a duration?

          Comment


            #6
            You can direcrlt substract the ints if you express DateTime.Now (current time) or Time[0] as integer as well using the ToTime() method - as an alternative record the difference in a C# timespan variable - http://www.dotnetperls.com/timespan

            Comment


              #7
              To help me better understand your response, I would like to propose the following example:
              Let's say the current time is 22:30:03 and my current int Variable created by ToTime() is 223003. The previous bar's time was 22:28:50 and it's ToTime() int Variable 222850. What method would I use to get 00:01:13 or better yet 73 Seconds?

              Comment


                #8
                Hi DenMan,

                ToTime() converts into standard integers, so you would need some extra steps to then convert this back into time (base 60) for valid comparisons.

                Another way to work with this is with a C# TimeSpan object. TimeSpan can be assigned to the difference of two DateTime objects.

                TimeSpan myTimeSpan = Time[0] - Time[1];
                Ryan M.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                647 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                369 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                108 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                572 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                573 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X