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 method Error

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

    OnStateChange method Error

    Hi,
    I am getting this below error:
    "Strategy name: Error on calling 'OnStateChange' method: index was outside the bounds of the array". I have already checked your other forum posts realted to it and nothings sovles the error issue. Before you suggest me, I have alrady used print to debug and also used VS to find where its getting the null.

    I am enabling the strategy and running on market replay data. after running it for sometime, when I am changing the market replay data date to the past, it is disabling the strategy and not re-enabling the strategy and showing the error, unlike other strategies doing the same.

    Please find my prints and place where the null value is coming from below:
    protected override void OnStateChange()
    {
    Print(0);
    if (State == State.SetDefaults)
    {
    }
    else if (State == State.Configure)
    {
    Print(1);
    AddDataSeries(); // This is where the null value is coming from.
    }
    else if (State == State.Historical)
    {
    Print(2);
    }
    else if (State == State.Transition)
    {
    Print(3);
    }
    else if (State == State.DataLoaded)
    {
    Print(4);
    }
    else if (State == State.Terminated)
    {
    Print(5);
    }
    Print(6+"====================================");
    }

    When I first enable and run the strategy, it print starting with 0 and ends with 6, but when I change the market replay data, it only prints 6, never goes to 0. With help of VS, I found below is giving null value:
    else if (State == State.Configure)
    {
    Print(1);
    AddDataSeries(); // This is where the null value is coming from.
    }
    In AddDataSerires, I am using ChartBars.ToIndex to calculate no of barstoload.

    Looking forward to your reply.

    #2
    Hello asmmbillah,

    Thanks for your post.

    ChartBars.ToIndex is the far right bar that is visible on the chart. It should only be used for custom rendering, and should not be used outside of OnRender().

    Please see documentation below for what would be valid use of AddDataSeries().

    AddDataSeries() - https://ninjatrader.com/support/help...dataseries.htm

    For a better understanding on the NinjaScript LifeCycle and how scripts pass through different states, I recommend reviewing the documentation below in addition to clearing your prints, and creating a test strategy that does only does the following:

    Code:
    protected override void OnStateChange()
    {
        Print(State);
    }
    https://ninjatrader.com/support/help...fecycle_of.htm

    https://ninjatrader.com/support/help...tatechange.htm

    Publicly available video - https://www.youtube.com/watch?v=gyel...EYKym&index=17

    If you still have questions minding the above or are still hitting errors, please copy the full line of code that is throwing the error so we can share our insight.

    We look forward to assisting.
    JimNinjaTrader Customer Service

    Comment


      #3
      Hi Jim, Thanks for your reply. In that case, within state.Configure, is it possible to get the index of far right bar?

      Comment


        #4
        Hello asmmbillah,

        No, understanding which bar is going to be on the far right depends on the data that is loaded and what bars are rendered on the chart. This information would not be available in State.Configure.

        Please let us know if you have any additional questions.
        JimNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by PaulMohn, Today, 05:00 AM
        0 responses
        8 views
        0 likes
        Last Post PaulMohn  
        Started by ZenCortexAuCost, Today, 04:24 AM
        0 responses
        6 views
        0 likes
        Last Post ZenCortexAuCost  
        Started by ZenCortexAuCost, Today, 04:22 AM
        0 responses
        3 views
        0 likes
        Last Post ZenCortexAuCost  
        Started by SantoshXX, Today, 03:09 AM
        0 responses
        16 views
        0 likes
        Last Post SantoshXX  
        Started by DanielTynera, Today, 01:14 AM
        0 responses
        5 views
        0 likes
        Last Post DanielTynera  
        Working...
        X