Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to reference the Nth bar of a session

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

    How to reference the Nth bar of a session

    I want to create a strategy for trading the breakout of the Nth bar of the session. Is there a simple, direct way of of referencing the High and Low of the Nth bar? If not, will the following indirect ways work when, for example, N = 10?

    if High[0] > High[Bars.BarsSinceSession – 10] + 1*TickSize
    EnterLong()

    OR do I need to do something like this:
    int barsAgo = Bars.BarsSinceSession – 10

    if High[0] > High[barsAgo] + 1*TickSize
    EnterLong()

    Thanks for your help.
    Last edited by raintree; 12-07-2007, 08:18 PM.

    #2
    Well Bars.BarsSinceSession will return the number of bars since the start of the session. This is not a static number so subtracting 10 from it will not return you the value of the 10th bar. What you want to do is mark the 10th bar in some fashion and then reference it with respect to that. Something like this might work. I haven't tested the code, but the theoretical logic is there.

    Code:
    int markerBar = 0;
    if (Bars.BarsSinceSession == 10)
         markerBar = CurrentBar;
    int tenthBar = CurrentBar - markerBar
    if (High[0] > High[tenthBar] + TickSize)
         EnterLong()
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Hi Josh. Your markerBar is a neat idea but if I use the suggested code and add a Draw diamond action to show the marked bar, every bar gets marked and I get WAY too many positions! LOL. Here’s the code I used.

      // Create a BarsAgo index for the markerBar so it can be compared with the current bar
      int markerBar = 0;
      if (Bars.BarsSinceSession == 10)
      markerBar = CurrentBar;
      {
      DrawDiamond("markerBar" + CurrentBar, 0, High[0] + 1 * TickSize, Color.Red);
      }
      int tenthBar = CurrentBar - markerBar;

      // Condition set 1 Long Entry
      if (Close[0] > High[tenthBar])
      {
      EnterLong(DefaultQuantity, "EnterLong");
      }

      Any further thoughts?
      Last edited by raintree; 12-08-2007, 08:20 AM.

      Comment


        #4
        You split up your if statement and its bracket. You will need to keep them together.


        // Create a BarsAgo index for the markerBar so it can be compared with the current bar
        int markerBar = 0;
        if (Bars.BarsSinceSession == 10)
        {
        markerBar = CurrentBar;
        DrawDiamond("markerBar" + CurrentBar, 0, High[0] + 1 * TickSize, Color.Red);
        }
        int tenthBar = CurrentBar - markerBar;

        // Condition set 1 Long Entry
        if (Close[0] > High[tenthBar])
        {
        EnterLong(DefaultQuantity, "EnterLong");
        }
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Thanks, Josh. That works. I also found out that this works too and produces the same result:

          // Condition set 1
          if (Close[0] > High[Bars.BarsSinceSession -10])
          {
          EnterLong(DefaultQuantity,
          "EnterLong");
          }

          Comment


            #6
            if (Close[0] > High[Bars.BarsSinceSession -10])
            {
            EnterLong(DefaultQuantity,
            "EnterLong");
            }


            That doesn't necessarily work all the time. Like I explained earlier, Bars.BarsSinceSession returns an int representing how many bars have passed since the session start time. If you are on a 1min chart this could return an int in the hundreds. Lets say you are on the 100th bar. Subtracting 10 from that does not return you the High of the 10th bar it returns you the High of the 90th bar. This is why I suggested the approach of marking the 10th bar and returning to that bar.

            Also please ensure that you don't actually have this line of code in the OnBarUpdate().
            Code:
            int markerBar = 0;
            If that is in the OnBarUpdate() that destroys our marker place as it keeps resetting to zero. You want to put this line inside the Variables region up top.
            Code:
            private int markerBar = 0;
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              I’m not sure whether I’ve got a basic misunderstanding or I haven’t yet communicated clearly what it is I want to do. My understanding of the meaning of the number inside the brackets in terms like Close[1], High[2], etc. is that it means “bars ago.” If the current bar is the 100th bar and the expression inside the brackets evaluates to 90 “bars ago”, then 100-90 refers back to bar 10, which is exactly what I want.
              Of course, this only works on time bars, and 1 minute bars would require a different number inside the brackets than 5 minute bars to get me back to the place in the session that I want to reference. But assuming I can count bars and from my chart studies know exactly which bar I want to reference, then the code I posted should work, should it not? It’s very simple and it doesn’t need markers or any other additional code that a novice programmer like me might put in the wrong place!
              Thanks for explaining how to use the markerBar trick which I may may also use in other contexts. In the meantime please do let me know if I’ve got some basic misunderstanding about the meaning of the number inside the brackets or in the logic of my simpler code.

              Comment


                #8
                Then your understanding is correct, but you would not want to use Bars.BarsSinceSession to do this calculation. You want to use CurrentBar. The reason is because BarsSinceSession is reset at every start of a session and you can have many sessions on the chart. CurrentBar returns the number of bars on the chart and is more inline with what you want to do which is simply to subtract some number away from it.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  Thanks, Josh. I've learned a lot from this exchange. Learning to program in NinjaScript is great fun and you're a good teacher. I have enough to work with for now but I have the feeling I'll be getting back to you eventually about how to access range data from within a group of time-referenced historical bars, if that is possible. Focusing on the bar number was for me really just a work around for what seemed a more difficult task of referencing data inside time blocks. I thought the simplest way would be just to encapsulate that data (the high and the low) inside a bar of the appropriate duration, which could then be referenced with a bar index number.

                  Comment


                    #10
                    My pleasure. Just drop a line on the forums when you have other questions and I'll try my best to help you out.
                    Josh P.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    598 views
                    0 likes
                    Last Post Geovanny Suaza  
                    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                    0 responses
                    343 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by Mindset, 02-09-2026, 11:44 AM
                    0 responses
                    103 views
                    0 likes
                    Last Post Mindset
                    by Mindset
                     
                    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                    0 responses
                    556 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by RFrosty, 01-28-2026, 06:49 PM
                    0 responses
                    555 views
                    1 like
                    Last Post RFrosty
                    by RFrosty
                     
                    Working...
                    X