Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Please help with ''barsAgo' needed to be between 0 and -1, but was 1' error, again

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

    Please help with ''barsAgo' needed to be between 0 and -1, but was 1' error, again

    Before I get into the details of this ubiquitous error, I think NT would do its customers a great service by not hiding support in e-mail. How many people have asked this same question in the forum? Invariably, the threads end with, "Send me XYZ files in e-mail." Then silence. No updates are given to the forum readers to inform them of the solution. Please share the solutions.

    On to the error:
    Code:
    ...OnBarUpdate()
    {[INDENT]if (CurrentBar < 1)[INDENT]return;[/INDENT]
     
    ...
    if (IsFirstTickOfBar)
    {[INDENT]if (CurrentBar < Period)
    {[INDENT](collection building stuff)
    return;[/INDENT]
     
    }
    ...
    double priceChange = Close[1] - Open[Period];  ----Out of range exception: ''barsAgo' needed to be...'
    ...[/INDENT]
     
    }
    ...[/INDENT]
     
    }
    In the last iteration through the indicator, debugging, CurrentBar was 4235. I hit F5, and now it's -1, and the code was run clean past my
    Code:
    if (CurrentBar < nnn)
    queries, which would have skipped the rest of OnBarUpdate.

    Does anybody know how this CurrentBar thing is happening? I notice, also, the built-in indicator, BuySellVolume, uses a "trick" for approximating whether OnBarUpdate is on the first tick:
    Code:
    if (CurrentBar != activeBar)
    {[INDENT]...
    activeBar = CurrentBar;[/INDENT]
     }
    Is there a reason to do it this way instead of simply using IsFirstTickOfBar?

    Thanks in advance,
    Caleb

    #2
    Hi,

    Does it work if you change it to

    if (CurrentBar < Period +1)

    In the case of "CurrentBar == Period" and Period = 14, then Close[0] would equal the 14th bar when IsFirstTickOfBar is true. So, Close[Period] or Close[14], would be the 15th bar back which doesn't exist in the zero based index.

    CurrentBar counts up and price index is zero based with increasing number going back in history.

    I hope that makes sense.

    Steven

    Comment


      #3
      Hello benJephunneh,

      The buy sell volume uses that specific logic as part of its accumulation logic, thats so it can get the last tick and also support bars types with remove last bar support. You can use similar logic in your script if you are trying to do a similar accumulation task.

      Any errors relating to a BarsAgo or index would generally relate to checking if enough data is present. As StevenL mentioned changing the CurrentBar check is likely a requirement here if you use greater than 1 for BarsAgo:

      if (CurrentBar < Period +1)

      -1 is also a normal state which is seen on series, checking that the CurrentBar < 0 to return is a standard item to add when using series.

      Comment


        #4
        Originally posted by StevenL View Post
        Hi,

        Does it work if you change it to

        if (CurrentBar < Period +1)

        In the case of "CurrentBar == Period" and Period = 14, then Close[0] would equal the 14th bar when IsFirstTickOfBar is true. So, Close[Period] or Close[14], would be the 15th bar back which doesn't exist in the zero based index.

        CurrentBar counts up and price index is zero based with increasing number going back in history.

        I hope that makes sense.

        Steven
        Thanks for the reply, Steven. I gave that a go, but I'm getting the same behavior. It's always on the very last bar of the chart, it throws the error. I can print out values, to make sure it's doing the calculations, OK, and I get values from bar 61 into the thousands. Last bar...bam!

        I also changed all bar indexing to use GetValueAt. Same.

        Comment


          #5
          Originally posted by NinjaTrader_Jesse View Post
          Hello benJephunneh,

          The buy sell volume uses that specific logic as part of its accumulation logic, thats so it can get the last tick and also support bars types with remove last bar support. You can use similar logic in your script if you are trying to do a similar accumulation task.

          Any errors relating to a BarsAgo or index would generally relate to checking if enough data is present. As StevenL mentioned changing the CurrentBar check is likely a requirement here if you use greater than 1 for BarsAgo:

          if (CurrentBar < Period +1)

          -1 is also a normal state which is seen on series, checking that the CurrentBar < 0 to return is a standard item to add when using series.
          Thanks for the reply, Jesse.

          My check for if (CurrentBar < 1) I would presume covers the < 0 check.

          And this is always happening on the last bar. If I let the chart build another bar and hit F5, it errors on the new, last bar.
          Last edited by benJephunneh; 02-10-2022, 09:00 AM.

          Comment


            #6
            Hello benJephunneh,

            From the given details there is not enough information to see why it would be having an error. You will likely need to reduce the script at this point to locate the specific syntax required to see the error. After doing that if its not apparent why please attach that sample along with steps to see the error.



            Comment


              #7
              I seem to have moved past the error, finally. As shown, above, the error originally occurred in a Close[1] - Open[Period] operation. I have no control over either of these series, and I've never seen in any documentation that this kind of indexing/operation would pose a problem.

              This is what I have, now:
              Code:
              double close1 = Close.GetValueAt(CurrentBar - 1);
              double openP = Open.GetValueAt(CurrentBar - Period - 1);
              double priceChange = close1 - openP;
              And the problem disappeared. It also did not work when I had the difference calculation in one line:
              Code:
              double priceChange = Close.GetValueAt(CurrentBar - 1) - Open.GetValueAt(CurrentBar - Period - 1);
              I've noticed that there are little "tricks" like this required for getting access to series, sometimes. On a couple occasions, I've had to access series with a dummy query before it'll give me the actual value. I might expect this with something I've hosed up and coded badly, but not the Close or Open series.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              608 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              355 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              105 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              560 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              561 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X