Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error on calling 'OnStateChange' method: Index was outside the bounds of the array.

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

    Error on calling 'OnStateChange' method: Index was outside the bounds of the array.

    Hi NinjaTrader Support,

    I'm getting a Error on calling 'OnStateChange' method: Index was outside the bounds of the array.

    the issue is because of this code.

    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 1)
    return;

    longEMA = ((Lows[1][2]) < Low[3]) && ((Highs[1][2]) > High[3]);
    shortEMA = ((Highs[1][2]) < High[3]) && ((Lows[1][2]) > Low[3]);

    it seems to me that there is an issue with "Bar[1] [2] and [3]".

    Maybe I'm wrong. What is the solution?

    Thanks and Best regards

    Tradeer


    #2
    Hi Tradeer,
    If (CurrentBars[0] is < 1) return, implies the code only stops as long as you don't yet have one bar. Otherwise, your code tries to continue.
    However, with one bar only, it is not yet possible to determine the High[3], Low[3], i.e. three bars ago. You have to increase the required number of bars to the highest value of your (respective) lookback periods before you allow the code to run. If you use multiple data series, make sure do that for ALL of them.
    if (CurrentBars[0] < x || CurrentBars[1] < y)
    return;
    NT-Roland

    Comment


      #3
      Hi Roland,


      thank you for your help. I appreciate it.

      I tried the same thing before but unfortunately it didn't work.

      Code:
      if (CurrentBars[0] < 4 || CurrentBars[1] < 3 || CurrentBars[2] < 2 || CurrentBars[3] < 1)
      return;

      I've attached the code if you want to take a look.

      EMACross.cs



      Comment


        #4
        Hi Tradeer,
        I'm not sure, I fully understand what the below shall refer to.
        longEMA = ((Lows[1][2]) < Low[3]) && ((Highs[1][2]) > High[3]);
        shortEMA = ((Highs[1][2]) < High[3]) && ((Lows[1][2]) > Low[3]);
        I thought, you had added an additional data series like AddDataSeries( Data.BarsPeriodType.Day, 1 ); and were referring to it (two bars ago), but couldn't find it.
        Thus, the attached is not an exact copy of your strategy, but at least it compiles :-).
        NT-Roland
        Attached Files

        Comment


          #5
          Hi Tradeer, thanks for your question.

          The script you posted needs to add another series to use Highs, Lows, etc. If your script only has the primary series there are no other data series to reference, so you get the indexing error.

          Please also read this guide on creating multi time frame and instrument scripts:


          Please let me know if I can assist any further.

          Comment


            #6
            Hi Roland & ChrisL,

            Roland Thank you for your time and effort.
            Your code helped me.

            Yes the error was because of the data series. I misspelled the code. I.e. instead of Highs[1][2] I rewrote the logic to High[0] and High[1].
            I just wanted to use one data series, not a multi time Series.









            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by NullPointStrategies, Yesterday, 05:17 AM
            0 responses
            54 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            131 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
            44 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            49 views
            0 likes
            Last Post TheRealMorford  
            Working...
            X