Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error on Implementing the Strategy out of Range Bars

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

    Error on Implementing the Strategy out of Range Bars

    Hello,

    I hope my message finds you well. I am trying to write a strategy which uses the swing indicator and the RSI. I provide the code below:

    Code:
    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;
    
    if (CurrentBars[0] < 150) // I even incremented the bars here, but still does the error.
    return;
    
    Print (Time[0]);
    
    int SwingLow = swing1.SwingLowBar(0, 1, 20);
    Print("swingLow :" + SwingLow);
    double SwingLowPrice = Low[Math.Max(0,SwingLow)];
    Print("PriceLow :" + SwingLowPrice);
    int SwingLow1 = swing1.SwingLowBar(0,2,40);
    Print("swingLow1 :" + SwingLow1);
    double SwingLowPrice1 = Low[Math.Max(0,SwingLow1)];
    Print ("SwingLowPrice1 " + SwingLowPrice1);
    
    Entrycondition();
    
    
    }
    
    private bool _RSIDivergence;
    private void Entrycondition()
    {
    int SwingLow = swing1.SwingLowBar(0, 1, 20);
    double SwingLowPrice = Low[Math.Max(0, SwingLow)];
    _RSIDivergence = Close[1] < SwingLowPrice;
    _RSIDivergence = DavideRSI[1] > DavideRSI[SwingLow];
    _RSIDivergence = Close[0] > High[1];
    
    if (_RSIDivergence)
    BackBrush = Brushes.Blue;
    
    
    }
    However, I get the following error: Error on calling 'OnBarUpdate' method on bar 426: 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.

    I don't understand because until I wrote the conditions _RSIDivergence the strategy was working fine with Printing the different values for the Swing variables.

    I really thank you for your help.
    Best Regards
    David​

    #2
    Hello NorwegianCat92,

    Do you know which specific line of code is having the error? That would really be the first step here or to identify which line of code it is, once we know that it will be more apparent on what direction to take for debugging the situation.

    Comment


      #3
      Hello Jesse,

      Thank you for your reply. No unfortunately I don't know what is the line of code which has the error, because the NinjaTrader output window just says:

      Error on calling 'OnBarUpdate' method on bar 454: 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.

      So, I don't know where it might be this error, given the few lines of codes that I wrote as per the previous message.

      If you could help, I would really appreciate it.

      Thanks again
      David

      Comment


        #4
        Hi Jesse,

        I was able to understand, the line of code which causes the error is:

        _RSIDivergence = DavideRSI[1] > DavideRSI[SwingLow];

        What do you think could be the error?

        Thank you

        Comment


          #5
          Hello NorwegianCat92,

          Thank you for the update.

          With that line you are using a variable bars ago so that is likely going to be the issue. I would suggest to use a Print and see what the value is when you get the error.

          Print("SwingLow " + SwingLow);
          _RSIDivergence = DavideRSI[1] > DavideRSI[SwingLow];

          You should see the value printed right before the error. If you are using the swing indicators methods to find swings that can return -1 when no swing instance is found which would cause the error you are seeing, -1 is not a valid bars ago.

          Comment


            #6


            Hello Jesse,

            Thank you for your reply. Yes the error says that the Swing gives a value of -1. What could be a workaround to avoid this error?
            Thanks
            David

            Comment


              #7
              Hello NorwegianCat92,

              That means no swing was found, you can add a condition like the following to avoid that.


              if(SwingLow>= 0)
              {
              _RSIDivergence = DavideRSI[1] > DavideRSI[SwingLow];

              }

              Comment


                #8
                Hello Jesse,

                Thank you very much for your help and support. It worked.
                Thanks
                David

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NullPointStrategies, Yesterday, 05:17 AM
                0 responses
                58 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                133 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                73 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                45 views
                0 likes
                Last Post Deep42
                by Deep42
                 
                Started by TheRealMorford, 03-05-2026, 06:15 PM
                0 responses
                50 views
                0 likes
                Last Post TheRealMorford  
                Working...
                X