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 f.saeidi, Today, 10:19 AM
            0 responses
            1 view
            0 likes
            Last Post f.saeidi  
            Started by kujista, Today, 06:23 AM
            4 responses
            14 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by traderqz, Yesterday, 09:06 AM
            2 responses
            16 views
            0 likes
            Last Post traderqz  
            Started by traderqz, Today, 12:06 AM
            3 responses
            6 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Started by RideMe, 04-07-2024, 04:54 PM
            5 responses
            28 views
            0 likes
            Last Post NinjaTrader_BrandonH  
            Working...
            X