Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

not able to enable strategy in forward backtesting and live.

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

    not able to enable strategy in forward backtesting and live.

    Hello,

    I have a problem with a strategy I created. Every time I try to forward backtest it or run it live on a sim account, I can't get it enabled.

    I also received the following error message:
    "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."

    For clarity: my strategy uses multiple indicators on three different timeframes. Some indicators are duplicated, others are not, and there are also some minimum requirements before an entry can be placed.

    Thanks in advance for your help!

    #2
    You'll typically see that with arrays of some sort. You need to look through your code to find the []. For example, if you are trying to find the close of the previous bar you'll use Close[1], but it will give you an error on the first bar because there is no previous bar to access. You need to wait until you have the appropriate amount of candles first. It depends on how your code is structured. Put the code down and we might be able to give some insight.

    Comment


      #3
      Hello trimpy,

      Ensure the CurrentBar for all series is greater than any barsAgo index used.

      See the forum post below.


      If this does not correct the error, add prints above each line where an index is used, to identify the specific line causing the error.
      Then report back what the specific line of code is.

      Below is a link to a support article on adding debugging prints to understand behavior.
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        thank you all for the help, I will try it out and get back to you.
        thanks again!

        Comment


          #5
          hi again,

          as I mentioned I’ve been working on developing a strategy in NinjaScript that uses multiple data series (2-minute, 5-minute, and 15-minute charts). However, every time I try to run the strategy, I encounter an error related to BarsAgo being unreachable. I’m not sure if this is due to a limitation in the number of data series that can be added or if there’s something wrong with my code.

          Here’s a snippet of the relevant part of my code where I add the data series:

          csharp
          Copy
          protected override void OnStateChange()
          {
          if (State == State.SetDefaults)
          {
          // Strategy settings...
          }
          else if (State == State.Configure)
          {
          AddDataSeries(Data.BarsPeriodType.Minute, 2);
          AddDataSeries(Data.BarsPeriodType.Minute, 5);
          AddDataSeries(Data.BarsPeriodType.Minute, 15);
          }
          }
          And here’s where I’m using the data series in OnBarUpdate():

          csharp
          Copy
          protected override void OnBarUpdate()
          {
          if (BarsInProgress != 0)
          return;

          // Calculate ATR for 2-minute and 5-minute charts
          atr2 = ATR(BarsArray[1], AtrPeriod)[0]; // ATR for 2-minute chart
          atr5 = ATR(BarsArray[2], AtrPeriod)[0]; // ATR for 5-minute chart

          // Other calculations and logic...
          }
          The error occurs when trying to access historical data (e.g., Close[0], High[0], etc.) for the additional data series. I’m wondering if there’s a limit to the number of data series that can be added or if I’m missing something in my implementation.

          Could someone please help me understand:

          Is there a limit to the number of data series that can be added to a strategy?

          How can I ensure that BarsAgo is accessible for all data series?

          Are there any best practices for handling multiple data series in NinjaScript?

          Any guidance or suggestions would be greatly appreciated!​

          Comment


            #6
            Hello trimpy,

            As advised in post # 3, you need to make sure the indexes are valid.

            if (BarsInProgress != 0 || CurrentBars[0] < 1 || CurrentBars[1] < 1 || CurrentBars[2] < 1 || CurrentBars[3] < 1)
            return;​
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hello NinjaTrader_ChelseaB,

              I’ve been working with Trimpy on developing strategies in NinjaScript, and we’ve noticed that our strategies often stop performing correctly once we add multiple data series. We’re struggling to understand why this happens and would appreciate some guidance.

              We’ve already added checks for CurrentBars (as suggested in previous discussions), but this hasn’t resolved the issue. Essentially, the checks ensure that the strategy doesn’t trade if there aren’t enough bars loaded, but the core problem seems to be that the strategy isn’t properly accessing or synchronizing the bars from the additional data series.

              Key Questions:
              1. Why does adding multiple data series cause the strategy to stop working?
              2. How can we ensure that the strategy correctly accesses and processes bars from all data series?
              3. Are there specific best practices or common pitfalls we should be aware of when working with multiple data series in NinjaScript?

              Any insights, examples, or references to relevant documentation would be greatly appreciated!

              Thanks in advance for your help!

              Comment


                #8
                You would need to determine what error is causing the strategy to stop in order to figure what where the code is going wrong. If the strategy is disabling itself, and there are no compilation errors, then it is likely a runtime error, and you should find something in the output or logs to give you a clue what to fix next.

                Comment


                  #9
                  Hello Salahinho99,

                  "Why does adding multiple data series cause the strategy to stop working?"

                  With the error message you are getting, this is letting you know the strategy stops working because the script is encountering a run-time error, specifically an invalid index error.

                  You will need to determine what line of code is causing the error, and what the invalid index is.

                  "How can we ensure that the strategy correctly accesses and processes bars from all data series?"

                  Ensure the CurrentBar for all series is greater than any barsAgo index used.

                  "Are there specific best practices or common pitfalls we should be aware of when working with multiple data series in NinjaScript?"

                  All series will need to load data, and you should specify which BarsInProgress you want to evaluate the logic on.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Hello NinjaTrader_ChelseaB,​

                    I understand that the error message (index of x bars ago can't be loaded) occurs because the strategy is trying to access bars that haven’t been loaded yet. However, I’m struggling to figure out why the bars for these additional data series aren’t loading in the first place.

                    Here’s what I’ve observed:
                    • When I remove the additional data series and their associated logic, the strategy works perfectly fine.
                    • This suggests that the issue lies specifically in how the added data series are being loaded or accessed.
                    • Even with a check like the one below, the problem persists because the bars for the additional data series aren’t being loaded at all:

                    if (BarsInProgress != 0 || CurrentBars[0] < 1 || CurrentBars[1] < 1 || CurrentBars[2] < 1 || CurrentBars[3] < 1)
                    return;

                    This check ensures that the strategy doesn’t proceed unless the necessary bars are loaded, but it doesn’t address the core issue: why are 0 bars being loaded for the additional data series?

                    My Questions:
                    1. Why aren’t the bars for the additional data series loading?
                    2. Are there specific requirements or common mistakes when adding multiple data series to a strategy?
                    3. Is there a way to explicitly ensure that all data series are loaded and synchronized before the strategy begins processing?

                    Any insights, examples, or references to relevant documentation would be greatly appreciated. Thanks in advance for your help!
                    ​​

                    Comment


                      #11
                      Hello Salahinho99,

                      "why are 0 bars being loaded for the additional data series?"

                      I cannot say 0 bars are being loaded. Its more likely the bar has not yet processed.

                      What is the specific line of code causing the error?

                      "Any insights, examples, or references to relevant documentation would be greatly appreciated. Thanks in advance for your help!"

                      Below is a link to a forum post that discusses invalid indexes.
                      Chelsea B.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by samish18, Today, 09:30 PM
                      0 responses
                      5 views
                      0 likes
                      Last Post samish18  
                      Started by Super Bardock, Yesterday, 11:52 AM
                      2 responses
                      19 views
                      0 likes
                      Last Post Super Bardock  
                      Started by raysinred, 04-06-2025, 01:52 PM
                      16 responses
                      139 views
                      0 likes
                      Last Post raysinred  
                      Started by iantriestrading, Yesterday, 01:39 PM
                      5 responses
                      28 views
                      0 likes
                      Last Post iantriestrading  
                      Started by trendisyourfriend, 05-25-2023, 09:54 AM
                      10 responses
                      170 views
                      0 likes
                      Last Post Curerious  
                      Working...
                      X