Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Duration between two distinct (volume) bars

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

    Duration between two distinct (volume) bars

    Hello

    I'm looking to get the duration (in seconds) between the bar with the lowest low within a predefined range of bars and the current bar.

    In order to achieve this, I'm using the MIN command and a boolean called "Duration".

    The lowest low (of the last 5 bars) is defined by the following snippet:
    double lowestlow = MIN(Low, 5)[0];

    So far so good.

    The duration between two bars is defined by the following snippet:

    TimeSpan time1 = Time[0] - new DateTime(1970,1,1,0,0,0);
    TimeSpan time2 = Time[1] - new DateTime(1970,1,1,0,0,0);
    int diff=(int)time1.TotalSeconds-(int)time2.TotalSeconds;

    if(diff > 60)
    {
    Duration = true;
    }
    else
    {
    Duration = false;
    }

    This works as well, but the issue I'm encountering here is that I obviously don't know in advance WHICH of the last 5 bars has produced the lowest low, so I cannot call the bar in the "TimeSpan time2" command, not knowing whether it is Time[1], Time[2], Time[3], Time[4], or Time[5]

    (Time[0] is always the current bar so this part of the equation obviously works.)

    Thanks for helping me out here.

    #2
    laocoon, as an idea : couldn't you save the barnumber of the low that would be the Min Low? Then you would know how many bars to reference back fro the CurrentBar on the chart.

    Comment


      #3
      Bertrand, that's an interesting idea but I'm afraid I don't know how to do that.
      Could you give me a hint on how to go about this?

      Thanks a lot.

      Comment


        #4
        lacoon, you could use a check like this I believe -

        Code:
        if (Low[0] == MIN(Low,5)[1])
        {
        DrawDot("tag" + CurrentBar, true, 0, Low[0] - 2 * TickSize, Color.Goldenrod);
        keepTrack = CurrentBar;
        Print(keepTrack);
        }

        Comment


          #5
          Hello Bertrand

          Thanks a lot for your help. I have a further question however:
          I tested your code, but the keepTrack function only returns a bar number like"2866" for example. What I'm looking for is the number of the bar that produces the lowest low of the last 5 bars. This means that the bar number can only be either [1], [2], [3], [4] or [5].

          I then want this number to replace the number "[1]" in the line of code below, in order to be able to calculate the duration in seconds between the current bar [0] and the bar with the lowest low ([1], [2], [3], [4] or [5]).

          TimeSpan time2 = Time[1] - new DateTime(1970,1,1,0,0,0);

          Thanks

          Comment


            #6
            This snippet was not meant to be the full code needed, it will give you the barnumber of the min low 5, you then want to calc the difference still to arrive at how many bars you would need to reference back from CurrentBar to end there.

            Comment


              #7
              Number of bars calculation

              Given two datetime values how would I calculate the number of bars between the two time values on say a Renko chart

              So for example

              DateTime dt1 = GetDate1();
              DateTime dt2 = GetDate2();

              int numberbars = CalculateNumBars(dt1,dt2);

              Is there a way of getting the CurrentBar value for a given datetime and then subtracting the two values?

              Comment


                #8
                Hello mballagan,

                Thanks for your post.

                You have the right idea and you would use the GetBar() method to get the bar number according to the date/time specification.
                Please see: http://ninjatrader.com/support/helpG...t7/?getbar.htm

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                557 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                324 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                101 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                545 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                547 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X