Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

error when using swing indicator in a strategy

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

    error when using swing indicator in a strategy

    Here is the code that is used in the strategy

    int BarsAgo = Swing(SwingStrength).SwingHighBar(0, 1, Bars.BarsSinceNewTradingDay);
    if(BarsAgo < 0 || CurrentBar <= BarsAgo) return;
    double swingHigh = Swing(SwingStrength).SwingHigh[BarsAgo];

    The strategy works properly for some date ranges. For some date ranges, It would pop up an error as follows

    2024/10/24 15:16:26,Default,Strategy 'SwingTest': Error on calling 'OnBarUpdate' method on bar 34790: 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.,

    My question is how to trap this error so the strategy could continue to run.


    #2
    Hello Playdc,

    Any index used must be less than the size of the collection. For Series objects, the barsAgo index must be less than or equal to CurrentBar (and equal to or greater than 0).


    Your current code requires BarsAgo to be equal or greater than CurrentBar not less than CurrentBar.

    if(BarsAgo < 0 || CurrentBar <= BarsAgo) return;

    This states to return if CurrentBar is less than or equal to BarsAgo. However, CurrentBar must be greater or equal to the BarsAgo index.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea,

      Sorry, I am a little bit confused. I think the statement in my code as follows to catch the cases when BarAgos>=CurrentBar then it would just return and don't do any calculations.

      if (BarsAgo < 0 || CurrentBar <= BarsAgo) return;

      Comment


        #4
        Hello Playdc,

        My apologies, I think you've got that correct and I was not reading that correctly.

        Add prints to see what is causing the issue.

        int BarsAgo = Swing(SwingStrength).SwingHighBar(0, 1, Bars.BarsSinceNewTradingDay);
        if(BarsAgo < 0 || CurrentBar <= BarsAgo) return;
        Print(string.Format("{0} | CurrentBar: {1}, BarsAgo: {2}", Time[0], CurrentBar, BarsAgo));
        double swingHigh = Swing(SwingStrength).SwingHigh[BarsAgo];​
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hi, I followed your instructions to add the print statements for debugging. However, I didn't get any output from ninjascript output windows. What I got is still the error message in log tab as follows
          2024/11/12 15:16:26,Default,Strategy 'SwingTest': Error on calling 'OnBarUpdate' method on bar 34790: 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.,

          Comment


            #6
            Hello Playdc,

            Does the print output show in the output window before the error appears?

            If not, move the print higher in the code.
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by NullPointStrategies, Today, 05:17 AM
            0 responses
            50 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            126 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            69 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            42 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            46 views
            0 likes
            Last Post TheRealMorford  
            Working...
            X