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

array indexing and current bar indexing confusion.

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

    array indexing and current bar indexing confusion.

    Hi,

    I am trying to understand the way ninjatrader uses their bar numbering systems. Methods i.e. High[0] start from the right and move to the left increasing the array count as the bars move to left. However, the current 0 bar is all the way to the left and the current bar numbers increase as the bars move to the right. Please correct me if I have misstated anything so far.

    How does that work?

    I trade from right to left but the strategies seem to want you to trade from left to right. Let me see if I can explain that better. Trading decisions are made on the hard right side of the chart where new bars are forming or a new bar has just closed. Depending on what your strategy consists of, you may want to "look-back" at some previous time, price, or event to help make a decision on what is happening or has happened as the new bar has formed. I would program from the event that has occurred and look back to a previous price, time, or event and use the array numbering system to identify the event and have plenty of bars on the chart to find that event.

    However, (and I may be misunderstanding how the strategies work) it appears to me that the strategies start out at the 0 current bar and there is no way to look past that 0 bar and that is why they require the test for current bars, i.e. there are enough previous current bars closed that your look-back logic doesn't cause an error and stop the script.

    1. Is this correct and if not, how do these two numbering systems work and work together?

    2. Is there a way to know the total number of bars on the chart so that you don't need to use an arbitrary number of bars in order to condition your look-back logic to not have an index out of range error and to not waste having an exaggerated number of bars just to insure you are safe from the index out of range error.


    3. How would you identify the total number of bars on the chart.





    Looking forward to your help in understanding these concepts.

    Thanks,

    Brendan

    #2
    I actually don't want to start trading with this strategy until all of the bars have been populated in the chart. That is how a chart would look like when I am actually trading.



    So if there is a way to wait until the chart is populated that would be great.



    Thanks again.



    Brendan

    Comment


      #3
      Hi Brendan,

      You are correct, the DataSeries object indexes are reversed to the actual bar number. This represents a barsAgo value.

      This is for ease when making simple strategies.

      For example if I wanted to know if the current bar's (0 barsAgo) high is greater than the high from 5 bars ago I would use:
      if (High[0] > High[5])

      CurrentBar is incremented each time OnBarUpdate is called and starts with 0. This will be the absolute index of the currently processing bar.

      While it is not documented for NinjaTrader 7, it is possible to use .GetOpen(int absoluteBarIndex), .GetHigh(), .GetClose(), .GetLow(), .GetVolume(), and .GetTime() with BarsArray objects.

      For example:
      Print(BarsArray[0].GetTime(CurrentBar));

      This is documented for NinjaTrader 8.


      To trigger an action on the last historically processed bar use
      if (Historical && CurrentBar == Count - 2)

      To trigger an action only in realtime use
      if (!Historical)
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Thanks Chelsea,



        I will experiment with that to get a better understanding. Enjoy your weekend.



        Brendan

        Comment


          #5
          Originally posted by Dazed&Confused View Post
          1. Is this correct and if not, how do these two numbering systems work and work together?

          2. Is there a way to know the total number of bars on the chart so that you don't need to use an arbitrary number of bars in order to condition your look-back logic to not have an index out of range error and to not waste having an exaggerated number of bars just to insure you are safe from the index out of range error.


          3. How would you identify the total number of bars on the chart.
          1. I think you're on the right track. There is a big difference between the BarsAgo numbering scheme and the BarIndex numbering scheme.

          BarsAgo numbering scheme starts with 0 on the most recently closed bar (on the far right side of the chart) and increases in number as you go backwards to the left.

          BarIndex numbering scheme starts at 0 with the very first bar of the chart (scroll all the way to the left, yes, all the way) and increases in number as you move forwards to the right, aka, numbers increase as new bars close.

          2. CurrentBar always contains the BarIndex number of the last closed bar.

          Even though CurrentBar reflects the BarIndex numbering scheme, you will use this value in a very simple gateway formula to convert to the BarsAgo numbering scheme.

          How?
          Inside your code, for example, when you want to remember an event, you need to save the value of CurrentBar, let's use a private variable called EventBar. Then 3, 5, or 30 bars later (or whenever) you wish to access pieces of this event (which is now in the past) the formula CurrentBar - EventBar will always produce the correct BarsAgo number for use as a DataSeries index.

          Code:
              // save the event 'location', perhaps it was a crossover
              EventBar = CurrentBar;
          
              // what was the high price of the event?
              int BarsAgo = CurrentBar - EventBar;
              Print("High of event was "+High[BarsAgo]+" from "+BarsAgo+" bars ago");
          It is very important that you realize the DataSeries class requires you to use the BarsAgo numbering scheme, not the (standard array-like 0-based) BarIndex numbering scheme.

          This 'backwardness' is possible because the DataSeries class utilizes indexed properties.

          3. See #2 about CurrentBar. Obviously the total number of bars on the chart increases in real-time, so CurrentBar is updated "as necessary" to reflect this.

          [where "as necessary" means "each time a real-time bar closes"]

          --

          You might benefit from studying this thread,
          Last edited by bltdavid; 06-04-2018, 12:34 AM.

          Comment


            #6
            Thanks David. I appreciate the detailed answer. Back to school to study the thread you referenced. Enjoy your day and your week.



            Brendan

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Ray12345, Today, 03:33 AM
            4 responses
            15 views
            0 likes
            Last Post Ray12345  
            Started by biegea, Yesterday, 08:54 PM
            3 responses
            21 views
            1 like
            Last Post TraderTate  
            Started by j0ch3n, Today, 03:38 AM
            0 responses
            1 view
            0 likes
            Last Post j0ch3n
            by j0ch3n
             
            Started by Matt Skinner, Today, 01:55 AM
            0 responses
            12 views
            0 likes
            Last Post Matt Skinner  
            Started by visasimple, Today, 01:40 AM
            0 responses
            7 views
            0 likes
            Last Post visasimple  
            Working...
            X