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

Error on calling 'OnBarUpdate' method on bar 0??

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

    Error on calling 'OnBarUpdate' method on bar 0??

    Hi there,

    Pretty confused!

    The code below compiles, works and paints the bars:

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    candle = Close[0] > Open[0];

    if (candle) {
    BarBrush = Brushes.Blue;
    }

    }

    ================================================== ======

    This almost IDENTICAL code below fails:

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    candle = Open[0] >= Low[1] && Close[0] <= High[1];

    if (candle) {
    BarBrush = Brushes.Blue;
    }
    }


    ERROR: Strategy 'st1': Error on calling 'OnBarUpdate' method on bar 0: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

    Why would it fail when trying to access the previous bar [1]?

    Right now it's Saturday and the market is closed by I still don't get it, the chart has the data from the previous candle, no? Also confused why the error says bar 0 when clearly if I only use bar 0 there's no problems, only when I attempt bar 1 (Low[1] or High[1])

    Why does this happen and how can I fix it? Thank you kindly for taking the time to read and reply.


    UPDATE: Any bars except for 0 throw the error! If I use Low[1], Low[2], Low[3], etc.. all error out!
    Last edited by focus333; 10-11-2020, 12:15 AM.

    #2
    Trying to get help on a similar issue:

    I need help with custom indicator. No matter what I do, i get this error: &quot;Indicator 'Custom201010': Error on calling 'OnBarUpdate' method on bar 0: Index was outside the bounds of the array.&quot; Here's a code part from OnBarUpdate: protected override void OnBarUpdate() { double h0 = High[0]; double l0 = Low[0];

    Comment


      #3
      So, I think I sorta figure it out just now UltraNIX. Don't really fully understand but time is of the essence so I am going with this:


      protected override void OnBarUpdate()
      {
      if (BarsInProgress != 0 || CurrentBar < 1)
      return;

      // YOUR CODE STARTS HERE

      Comment


        #4
        For me it doesn't solve the issue, but I'm happy that it solved issue for you.

        Comment


          #5
          Originally posted by focus333 View Post
          So, I think I sorta figure it out just now UltraNIX. Don't really fully understand but time is of the essence so I am going with this:


          protected override void OnBarUpdate()
          {
          if (BarsInProgress != 0 || CurrentBar < 1)
          return;
          Ah, the young -- they are not fully grown members of the herd, and
          they are easy prey to bugs and misunderstandings ...

          I'm talking about the first few bars -- the very young bars, such as the
          very first bar -- what is so special about it?

          It's the first one!
          Meaning -- the first bar has no previous bar!
          It's the only bar, the only bar ever, that has no previous bar.

          So, on the very first bar, code like this fails,

          candle = Open[0] >= Low[1] && Close[0] <= High[1];

          Why? Because Low[1] is accessing the previous bar, which does not exist
          on the very first bar -- so specifying '1' as the index produces an out of range
          error -- this is called a runtime error, because it only happens when this code
          actually runs on a chart, ya know, when it actually processes each bar and
          adds it to the chart.

          That very first bar on the chart is a *****, er, I mean, is a gotcha.

          NinjaScript does nothing to compensate for the lack of a previous bar.
          This error is a human error -- the C# code has no way of knowing that
          your code works on 2,999 bars of a 3,000 bar chart, but fails on the
          very first bar.

          The index out of range makes perfect sense. What if you tried to go back
          and look at the 4,000th bar ago, using code like,

          candle = Close[4000] > Open[4000];

          Obviously, no one would do this. But this code fails for the first 4000 bars
          on the chart -- because until you have 4,001 bars, using index 4000 is out
          of range. Indexes start at 0 -- ya gotta remember that, too.

          Close[0] is looking at the close price of the current bar, but on the very
          first bar on the chart, this is bar number 0.

          Close[1] is looking at the close price of the previous bar, but on the very
          first bar on the chart, this doesn't exist yet -- so if your code needs to look
          at two consecutive bars, you must wait for 2 bars to arrive, otherwise your
          code will produce a runtime out-of-range error.

          The '[0]' index is not related to the bar number '0' -- they just happen to be
          the same value on the very first bar.

          The next bar on the chart is bar number 1, but Close[0] still means the close
          price of the current bar -- but the current bar has changed -- it is now advanced
          to the next bar, which is the new most recently closed bar, which is bar number 1.

          This makes sense -- as bars are added to the chart, the concept of the current
          bar means it must change -- generally speaking, 'current bar' is defined(*) as the
          the most recently closed bar, whatever bar number that happens to be. It is a
          moving concept, because accessing a Series via index '[n]' shifts towards the
          new bar each time a new bar arrives.

          Don't confuse accessing a Series with accessing an Array or List -- these are not
          the same data structure -- their indexing schemes are the reverse of each other.
          Array and List indexes '[n]' never change which physical element they point to,
          but Series indexes are always adjusting every time a new element, er, I mean,
          a new bar, is added to the chart. In a sense, they grow in opposite directions.

          One more thing. Your comment,

          UPDATE: Any bars except for 0 throw the error! If I use Low[1], Low[2], Low[3], etc.. all error out!

          If you were to use Low[3] anywhere in your OnBarUpdate, it means your code
          needs 4 bars to arrive before it can run. Why? Because Low[3] is accessing
          the oldest bar in a 4 bar sequence -- if your formula needs to look at that bar,
          well, you need to wait until you have at least 4 bars before you let that code
          execute. How do you do that?

          The easiest way is to note the highest BarsAgo index of all your Series accesses
          and simply use that value in a 'guard' at the top of your OnBarUpdate, like this,

          Code:
          protected override void OnBarUpdate()
          {
              // this guard protects the code below until enough bars arrive
          ​    if (CurrentBar < 3)
                 return;
          
              // YOUR CODE STARTS HERE
          }
          Since bar numbers start at 0, this 'guard' code returns on the first 3 bars,
          allowing full processing only for the 4th bar.

          bar number 0 - return
          bar number 1 - return
          bar number 2 - return
          bar number 3 <-- this is the 4th bar, full processing can begin

          Make sense?

          (*) In NT7, the 'current bar' is a definition that changed depending upon the
          CalculateOnBarClose setting. When True, the 'current bar' was the most
          recently closed bar. When False, it was the active bar under construction.
          In NT8, the Calculate replaces CalculateOnBarClose, but the realization
          that 'current bar' changes meaning depending upon the value of Calculate
          could still be helpful to a fuller understanding.
          Last edited by bltdavid; 11-19-2022, 08:47 PM. Reason: Fixed CODE section -- HTML coloring of '3' no longer recognized

          Comment


            #6
            bltdavid


            Hahaha!! David, you are AWESOME! Not only I really appreciate you taking the time to explain this is detail like you did but I loved your humorous writing style, I cracked up!

            Also, additional bonus thank you for the pro tips towards the bottom! I could not have asked for more, hope to one day return the favor when I am a better ninja

            Comment


              #7
              Hello focus333,

              bltdavid has provided the right information.

              I wanted to provide some additional resources.
              Hello, I want to create an indicator that show data in a chart but calculate in other charttime different to the time of the chart where is showed. For example:



              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                You're welcome!

                After I re-read it a few times, I thought to myself,
                "Wait, that 'youngest bar' metaphor was kinda dumb, because when ya think
                about, ya dumb doofus David, the very first bar is the oldest bar of all".

                Oh well, I decided not to change it, hoping no one would notice ...

                I'm glad you enjoyed my answer!

                Comment


                  #9
                  Originally posted by bltdavid View Post


                  That very first bar on the chart is a *****, er, I mean, is a gotcha.

                  .
                  1,000,000 upvotes good sir, detailed answer

                  I just quoted the sarcasm part because it was funny, but seriously, thank you for your answer

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by samish18, 02-27-2024, 06:46 PM
                  20 responses
                  144 views
                  0 likes
                  Last Post NinjaTrader_Gaby  
                  Started by Ray12345, Today, 03:33 AM
                  4 responses
                  19 views
                  0 likes
                  Last Post Ray12345  
                  Started by biegea, Yesterday, 08:54 PM
                  3 responses
                  27 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  
                  Working...
                  X