Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

index out of range - where?

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

    index out of range - where?

    In an earlier thread, I was testing & debugging multi-bar indicators.

    Well, I had to stop for a while because NT (64-bit v7) starting crashing.
    Went back to look again today, and now on the simplest test, I am getting an index out of bounds error -- I understand these, but cannot figure where this testcase is failing. It is the same test that was working before. The code is patterned off the code in WMA which explicitly handles CurrentBar == 0.

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

    This is the entire code in Initialize & OnBarUpdate:

    Code:
    protected override void Initialize()
            {
                Add(BarsPeriod.Id, BarsPeriod.Value*_basePrime);  // guaranteed to be BarsInProgress == 1
                Add(new Plot(Color.Orange, Name));
                Overlay                = false;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                if (CurrentBars[0] < 0 || CurrentBars[1] < 0) return; // patch for NT design flaw
                if ( Bars[0] == null || Bars[1] == null) return; 
                if (CurrentBars[x] < _basePrime) return;    // Check 2nd data stream for sufficient bars
                if (BarsInProgress == 0)   // NT can only plot from BIP=0 !!!
                {
                    Value.Set( WMA( Inputs[x], 10 )[0] );
                }
            }
    Attached Files

    #2
    LostTrader, it works ok without your if ( Bars[0] == null || Bars[1] == null) return; check - I'm not exactly sure why you put this in.

    Thanks,

    Comment


      #3
      The reason for putting it in is buried somewhere in the forum. Yep, that fixed the error. Thanks.

      Can you explain the relationship between the two? The error message seems off...

      Comment


        #4
        You're welcome, your CurrentBars check was not high enough to cover Bars[1] access.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by cmoran13, 04-16-2026, 01:02 PM
        0 responses
        43 views
        0 likes
        Last Post cmoran13  
        Started by PaulMohn, 04-10-2026, 11:11 AM
        0 responses
        25 views
        0 likes
        Last Post PaulMohn  
        Started by CarlTrading, 03-31-2026, 09:41 PM
        1 response
        162 views
        1 like
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 04-01-2026, 02:41 AM
        0 responses
        98 views
        1 like
        Last Post CarlTrading  
        Started by CaptainJack, 03-31-2026, 11:44 PM
        0 responses
        158 views
        2 likes
        Last Post CaptainJack  
        Working...
        X