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

BarsArray out of bounds

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

    BarsArray out of bounds

    Hi,

    I need some help with the error "Index was outside the bounds of the array"

    Code:
    double ATRday = ATR(BarsArray[1],14)[0];
    
    if(CurrentBars[0] <= BarsRequired || CurrentBars[1] <= 14) 
    return;
    Thanks

    #2
    Hello,
    Thanks for your post.

    It is impossible to tell without looking at your entire script. But, if this is the only code that you have so far it looks like you are accessing a second bars object when you only have one. Unless you have added another data series or indicator using the Add() method than your BarsArray will only have its first index filled (BarsArray[0]). For a single Data series you will want to do something more like the following snippet:

    Code:
    double ATRday;
    protected override void OnBarUpdate()
            {
    			if(CurrentBars[0] <= BarsRequired ) 
    			return;
    			ATRday = ATR(BarsArray[0],14)[0];
            }
    I am including a link to our help guide documentation on BarsArray, for your convenience. I believe that if you check out the comments on the example script, it will clear some things up about the index value inside BarsArray.
    https://ninjatrader.com/support/help...?barsarray.htm

    I also suggest checking out this forum post on making sure that you have enough bars for accessing a Data Series.
    https://ninjatrader.com/support/foru...ead.php?t=3170

    Please let me know if you have any further questions.
    Josh G.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the links and as I'm getting to understand BarsArray a bit more, I'm now getting the following error after making some changes:
      " You must use the overload that has a 'BarsInProgress' parameter when calling the BarsSinceExit() method in the context of a multi-time frame and instrument strategy."
      Code:
      protected override void Initialize()
              {            
                  Add(PeriodType.Day,1);
              }
      protected override void OnBarUpdate()
              {
                  double ATRday       =    ATR(BarsArray[1],14)[0]; values
      
                  if(CurrentBars[0] <= BarsRequired || CurrentBars[1] <= 14) 
                      return;
                  
                  if (BarsInProgress != 0)          
                      return;
         
                  if (BarsSinceExit()!=0 && Close[0]>SMA(200)[0] && SMA(12)[0]>SMA(25)[0] &&  ATRday>=15 )
                          {                        
                              EnterLong();
                          }

      Comment


        #4
        Hello,

        That error means that you are using the wrong overload for BarsSinceExit().
        From the help guide:
        The following method signature should be used when working with multi-time frame and instrument strategies:

        BarsSinceExit(int barsInProgressIndex, string signalName, int exitsAgo)
        I am including a link to our help guide documentation for BarsSinceExit. Also, please take a look at the help guide page for 'Working with multiple time frames and instruments'. You will see that there can be several differences when writing scripts with multiple bar objects.

        BarsSinceExit()
        https://ninjatrader.com/support/help...ssinceexit.htm

        Multi-Time Frame & Instruments
        https://ninjatrader.com/support/help...nstruments.htm

        Please let me know if you have any further questions.
        Josh G.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by DJ888, 04-16-2024, 06:09 PM
        3 responses
        10 views
        0 likes
        Last Post NinjaTrader_Erick  
        Started by RookieTrader, Today, 07:41 AM
        0 responses
        3 views
        0 likes
        Last Post RookieTrader  
        Started by maybeimnotrader, Yesterday, 05:46 PM
        1 response
        18 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by Perr0Grande, Yesterday, 08:16 PM
        1 response
        7 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by f.saeidi, Yesterday, 08:12 AM
        3 responses
        27 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Working...
        X