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

Ninjatrader 8 and Arrays: A Bar Problem

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

    Ninjatrader 8 and Arrays: A Bar Problem

    Hi there,

    When converting my NT7 code to NT8, I also changed a lot of my variables to arrays to clean things up. However, when I loaded my strategy against a paper account feed from IB, the strategy immediately disables and shows the following error message:

    Code:
    Strategy 'PPJuly2018': Error on calling 'OnBarUpdate' method on bar 1: Index was outside the bounds of the array.
    After a bit of searching here on the forums and elsewhere, I thought it could be a matter of data not being loaded in, so I took the following code that AlanP had posted that waited for bars to be loaded before executing OnBarUpdate:

    Code:
    if ( CurrentBar < 100)
    			{
    			return;
    			}
    After a while, the output window showed this, and my strategy disabled again:
    Code:
    Strategy 'PPJuly2018': Error on calling 'OnBarUpdate' method on bar 100: Index was outside the bounds of the array.
    Disabling NinjaScript strategy (...)
    So... where I should start in terms of debugging this problem?
    Thanks in advance for any answers!

    #2
    Hello Spiderbird,

    Thanks for your post.

    Though this is commonly seen when a script does not make sure that there enough bars for your NinjaScripts calculations, "Index was outside the bounds of the array" could be seen in other instances where you are referencing an index that does exist in an object.

    My recommendation would be to add prints throughout your strategy's OnBarUpdate() method so you may narrow the issue down to the line of code that is throwing the error. Once you see which line is throwing the error, the indexing issue should be evident and you should be able to fix it.

    Additional debugging tips can be found in our help guide. I'll provide a link below.

    Debugging Tips - https://ninjatrader.com/support/help...script_cod.htm

    Please let us know if we can be of further assistance.
    JimNinjaTrader Customer Service

    Comment


      #3
      Hi Jim,

      Thanks for your response. I'll start on that this weekend and work through the code.

      Quick question... generally in NT8, is it considered best practice to define the values of arrays through State.Configure?

      I'm referencing the following...

      Code:
      protected override void OnStateChange()
      {
        // Complex operations should be delayed to >= State.Configure
        if (State == State.Configure)
        {
          for (int i = 0; i < = array.length; i ++)
              DoWork(i);
        }
       
        // information related to market data is not available until at least State.DataLoaded
        else if (State == State.DataLoaded)
        {
          Period = 5 * TickSize;
        }
      }
      Thanks in advance!

      Comment


        #4
        Hello Spiderbird,

        It will really depend on what you are using the array for, but it would be best practice to instantiate the array in State.Configure and assign values then if those values do not depend on data to be loaded or processed in OnBarUpdate().

        If the elements depend on data that does not need processing in OnBarUpdate() these values should be assigned in State.DataLoaded.

        Generally, any complex operation that only needs to be done once in the NinjaScript's life should be done at the beginning of the script's life before data gets processed. State.Configure or State.DataLoaded would be used for those purposes. More information on State Management Best Practices can be referenced below.

        State Management Best Practices - https://ninjatrader.com/support/help...urceManagement

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

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by DJ888, 04-16-2024, 06:09 PM
        4 responses
        12 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Started by terofs, Today, 04:18 PM
        0 responses
        7 views
        0 likes
        Last Post terofs
        by terofs
         
        Started by nandhumca, Today, 03:41 PM
        0 responses
        6 views
        0 likes
        Last Post nandhumca  
        Started by The_Sec, Today, 03:37 PM
        0 responses
        3 views
        0 likes
        Last Post The_Sec
        by The_Sec
         
        Started by GwFutures1988, Today, 02:48 PM
        1 response
        9 views
        0 likes
        Last Post NinjaTrader_Clayton  
        Working...
        X