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

OnStateChange out-of-range Error - Can't find why

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

    OnStateChange out-of-range Error - Can't find why

    I am developing an Indicator based on Multi Time Frame. I get an error, once all history data have done the processing (I print the series count).

    I read all previous threads on this error, and I don't think I have any of the common mistakes.

    Code:
    Indicator 'IB_TPO': Error on calling 'OnStateChange' method: 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.
    Where can I send you my code (2 files)?

    #2
    Hello Shai Samuel,

    Thank you for the post.

    This error is a common error that comes up when you try to access data before it is available. Based on this happening in OnStateChange this is likely some syntax that is in the wrong location. The error alone is not enough information to know what may be happening here, I would need to see the offending syntax to better understand why this might come up.

    If you have formed a simple sample of the problem you can use the support forum to upload the sample and we can review that together. If you are trying to send a complete script, I do want to mention our support does not provide debugging services so you would likely need to reduce your script to find the portion of logic causing the problem. Once you isolate the syntax needed to see the problem, we can review that together.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hello Jesse,

      Thank you for your support, I did find the issue, which leads me to a separate question, that I will start in a new thread. I would like to copy your reply here, and what I have done to allocate the problem.

      Hello Shai,

      Thank you for the reply.

      You won't need to debug OnBarUpdate further, as I noted the problem is specifically with the OnStateChange logic per the error.

      Here is the error again with the location highlighted:

      Indicator 'IB_TPO': Error on calling 'OnStateChange' method: 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.

      The beginning of the error message will state the general location of the error which in this case will be in OnStateChange specifically.

      Because the error is in OnStateChange you may need to print the State to find out where the problem is. At the top of the OnStateChange override you can add a print for the state which I see you have:

      Print("State="+State.ToString());

      This was not included in your output for me to provide any other help here, but you are on the right path to finding the answer. Knowing which state happened prior to the error will give you a location to look at. I am copying the following from the prior email as it is relevant to this error:

      I see you are calling methods from the OnStateChange events such as InitTPO or ResetTPO, these would be good places to further review. Object reference not set to an instance of an object specifically means something you are using is null when it is used, very likely you need a null check before using that object.

      To find the error, you first would need to find the general location which in this case will be the State that the error is coming from. After that, you would need to further explore the logic being used in that particular state.

      If you find that State.DataLoaded is throwing the error, you could explore InitTPO(). If historical you have SetTPOsessionHours(). For Terminated you have ResetTPO(). You have a few different paths of logic in OnStateChange right now that you would need to further explore which has the error.

      Once you find which of these paths is generating the error if the syntax is not apparent as to why that is happening we could review that together knowing the specific location.
      What I have done, was to add a return at the beginning of OnBarUpdate, and ten debug OnStateChane with Print("NameX Begin") and Print("NameX End") in ecah function.

      I did find some issue with one of my loops, which raise another question that will now open a new tread.

      I hope this will be helpful for others.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Radano, 06-10-2021, 01:40 AM
      19 responses
      604 views
      0 likes
      Last Post Radano
      by Radano
       
      Started by KenneGaray, Today, 03:48 AM
      0 responses
      3 views
      0 likes
      Last Post KenneGaray  
      Started by thanajo, 05-04-2021, 02:11 AM
      4 responses
      470 views
      0 likes
      Last Post tradingnasdaqprueba  
      Started by aa731, Today, 02:54 AM
      0 responses
      5 views
      0 likes
      Last Post aa731
      by aa731
       
      Started by Christopher_R, Today, 12:29 AM
      0 responses
      11 views
      0 likes
      Last Post Christopher_R  
      Working...
      X