Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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.

          Chris L.NinjaTrader Customer Service

          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 r68cervera, Today, 05:29 AM
            0 responses
            2 views
            0 likes
            Last Post r68cervera  
            Started by geddyisodin, Today, 05:20 AM
            0 responses
            3 views
            0 likes
            Last Post geddyisodin  
            Started by JonesJoker, 04-22-2024, 12:23 PM
            6 responses
            33 views
            0 likes
            Last Post JonesJoker  
            Started by GussJ, 03-04-2020, 03:11 PM
            12 responses
            3,239 views
            0 likes
            Last Post Leafcutter  
            Started by AveryFlynn, Today, 04:57 AM
            0 responses
            6 views
            0 likes
            Last Post AveryFlynn  
            Working...
            X