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

how to make a forward loop ?

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

    how to make a forward loop ?

    Hello
    I'd like some clarification please on a loop forward care I end up with the all-too-familiar error "Error on calling 'OnBarUpdate' method on bar 16: You are accessing an index with a value that is invalid since it is out-of-range. accessing a series [barsAgo] with a value."
    Even if I understand it, I don't understand why it's triggered in my specific case, since I've already looped over all the bars and I'm waiting to get to the "CurrentBar" before proceeding.
    I'm trying to test the value of the indicator I've calculated and what I'm doing seems logical, but obviously it's not...
    I searched but couldn't find anything specific to loops that run in this direction and the end...
    Any advice would be appreciated...
    Thank you in advance.
    Code:
    if (CurrentBar == 0)
        {
            for ( index = Count; index > 0; index--)
                {
                    Print(index.ToString());
    
                    if (MyIndic[index]>0)
                        {
                            Print(index.ToString()+"     "+   MyIndic[index].ToString());
                        }
                 }
           return;
        }
    Last edited by Nememoris; 10-07-2023, 12:51 PM.

    #2
    Hello Nememoris,

    Thanks for your post.

    "Error on calling 'OnBarUpdate' method on bar 16: You are accessing an index with a value that is invalid since it is out-of-range"

    This error will be displayed if you try to use a BarsAgo when it is not valid. A more simple example using one series would be on bar 5 you check for 6 BarsAgo. There are not yet 6 bars so the CurrentBar minus 6 would be a negative number or a non-existent bar.

    ​One item that may come up that you can test before doing anything else is to check for enough data for all data series in the script.

    See the help guide documentation below regarding using a CurrentBar or CurrentBars check to make sure you have enough data for all data series in the script.

    Make sure you have enough data in the series you are accessing: https://ninjatrader.com/support/help...nough_bars.htm
    CurrentBar: https://ninjatrader.com/support/help...currentbar.htm
    CurrentBars: https://ninjatrader.com/support/help...urrentbars.htm

    You could consider using index = CurrentBar instead of index = Count in your loop.

    Alternatively, you would need to locate where in the script this is failing to do more targeted debugging with Prints. Locating the logic that is causing the error would allow you to place a Print before it. This would help understand what CurrentBar is processing and the other variables you may be using.

    Below is a link to a forum post that demonstrates how to use prints to understand behavior.
    https://ninjatrader.com/support/foru...121#post791121

    Note that using C# Loops would fall under C# education which goes outside of the support we provide in the Support Department at NinjaTrader as we do not provide C# education services. You could do a quick Google search for something like 'Forward Loop C#' to find more information about C# loops.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Thank you for your reply, but I obviously didn't explain myself well.
      What's more, like a donkey, I translated the code in deepL...

      I completely understood the origin of the error message, but when I say that I don't understand it, it's because I'm using If ( CurrentBar = = 0 ), which means that I've already gone through all the bars and calculated my indicator on the previous bars, so MyIndic{Index] should exist.
      So I've already looped from the oldest to the most recent and I'm trying to do it again and get access to the calculated MyIndic.
      I don't understand why I'm replacing Count with CurrentBar since I only loop when CurrentBar == 0, so an index-- will inevitably raise an error.

      I hope I've made myself clearer.

      ​

      Comment


        #4
        >If ( CurrentBar = = 0 ), which means that I've already gone through all the bars
        Wrong, it's your first bar. Count starts from 0 and goes forward. If you wanna do something at last bar use Bars.Count - 2 instead of CurrentBar
        I would do something like this:

        if (CurrentBar == Bars.Count - 2)
        {
        for (index = 0; index < Count; index++)
        {
        Print(index.ToString());

        if (MyIndic[index]>0)
        {
        Print(index.ToString()+" "+ MyIndic[index].ToString());
        }
        }
        return;
        }​

        Comment


          #5
          Thank you for your reply.
          I'm going to test all this...​

          Comment


            #6
            Hello Nememoris,

            Thanks for your notes.

            Leeroy_Jenkins is correct in the information they shared. If CurrentBar == 0 then this means that the CurrentBar is the first bar on the chart. Note that bar indexes on the chart start at 0 and increment forward by 1 as new bars form.

            If you would like to have your loop occur on the last bar then you could consider checking if (CurrentBar == Bars.Count - 2) as Leeroy_Jenkins noted.

            Please let us know if we may assist further.
            Brandon H.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by rbeckmann05, Yesterday, 06:48 PM
            1 response
            12 views
            0 likes
            Last Post bltdavid  
            Started by llanqui, Today, 03:53 AM
            0 responses
            6 views
            0 likes
            Last Post llanqui
            by llanqui
             
            Started by burtoninlondon, Today, 12:38 AM
            0 responses
            10 views
            0 likes
            Last Post burtoninlondon  
            Started by AaronKoRn, Yesterday, 09:49 PM
            0 responses
            15 views
            0 likes
            Last Post AaronKoRn  
            Started by carnitron, Yesterday, 08:42 PM
            0 responses
            11 views
            0 likes
            Last Post carnitron  
            Working...
            X