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

Bar out of range

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

    Bar out of range

    Hello!
    I have a custom function in my helper's section.
    The code is as followed:
    HTML Code:
    #region Helpers
     double myCustom(double price, double period, int i)
     {
        myFunction here
     }
    #endregion
    Afterwards in protected override void OnBarUpdate(), I have:
    HTML Code:
    if(CurrentBars[0] < 500)
    return;
    
    mySMA = _SMA[i];
    double myCust = myCustom(mySMA, period, i);
    In the section Variables, I have:
    HTML Code:
    private SMA _SMA;
    and in if(State == State.DataLoaded), I have:
    HTML Code:
    _SMA = SMA(aPeriod);
    When I compile the code, I have the error message that I am trying to acces a bar out of range.
    When I replace myCust just by EMA(Close, 21)[i] for example, I have no error message. So I conclude that the problem should be on myCust.

    Could you help by pointing out what is wrong here?

    Many thanks!

    #2
    Hello Stanfillirenfro,

    Thanks for your post.

    This error message indicates that you are trying to access a BarsAgo value that is not valid. A more simple example using one series would be on bar 5 you check for 6 BarsAgo. There are not yet 6 bars so the CurrentBar minus 6 would be a negative number or a non-existent bar.

    To determine what exactly is throwing the error, you would need to simplify your script to figure out exactly what bars ago value is being accessed and make sure that it is less than the current bar value (CurrentBar).

    First, it is great to create a copy of the script so that the original work is kept safe.

    Below is a link to a video that demonstrates making a copy of script.
    https://www.youtube.com/watch?v=BA0W...utu.be&t=8m15s

    Once the copy is created, the copy can be modified in any way without losing the original work.

    The next step is to start commenting out code.

    Its helpful to think about when the error is occurring to target areas, and comment code from everywhere else quickly.

    If at any point after removing some code, the error stops, add the last removed item back.

    If the error is occurring while the script is loading, the issue is likely somewhere in OnStateChange(). In this situation its best to remove any logic in OnBarUpdate, remove any custom methods and classes, and remove as many properties as possible, while still being able to reproduce the error.

    If the error is occurring when saving a template, the issue is likely with a private or public variable declaration. First, remove all logic leaving the variable declarations behind. (Empty State.Configure, State.Dataloaded, State.Historical, OnBarUpdate, OnMarketData, and remove any custom methods.) Then start removing the variables one by one and testing until the error stops. When the error stops add the last removed item back. Ensure that this item is private or property serialized or using the Browsable(false) attribute. (More about the Browsable attribute below)

    If the error only occurs while the strategy is trading, this is likely a run time method. For this its best to use prints to understand the behavior.

    Let me know if I may assist further.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Many thanks NinjaTrader_BrandonH for your post.

      I have actually proceeded partially as you mentioned above, and I found that the problem occurs when I have
      HTML Code:
      mySMA = _SMA[i];
      double myCust = myCustom(mySMA, period, i);
      because when I replace this function by EMA(Close, 21)[i] for exmaple the error disappears.
      In the Helpers section I am using two dimensional arrays and I wanted to know if such combinasons goes easyly with costum fucntions. Also, I have the condition
      HTML Code:
      if(CurrentBars[0] < 500) return;
      in protected override void OnBarUpdate(), and in the section Helpers I have for example
      HTML Code:
      myarray[5][r]
      with
      HTML Code:
      r < 200
      . In this case, I was supposed NOT to have CurrentBar reaching 500.
      Because I have
      HTML Code:
      mySMA = _SMA[i];
      I was thinking to remove [i] here, but that bring another problem to the function double myCust = myCustom(mySMA, period, i).
      I would appreciate any advice!

      Thanks!

      Comment


        #4
        Hello Stanfillirenfro,

        Thanks for your note.

        Ultimately, you would need to know exactly what variable 'i' is in your script. You are accessing a bars ago value that is not valid.

        You should remove your custom method and simplify your code. Note that using arrays and custom methods would not be considered a simplified script. Once you know your code works, you could create methods, use loops, etc.

        You would need to add debugging prints to the script to know exactly what value "i" is in your script so that you could create a CurrentBar check for that value.

        I see you noted using arrays in your script. This would fall under C# education and is not something we would be able to provide assistance with. You could remove that part of code from your script until you figure out the rest of the issues with the script and then add that logic back to the script.

        Note that if you are using a Multi-Instrument/Multi-TimeFrame script, you would need to make sure you have enough bars for each series added in the script.

        Let me know if I may assist further.
        Brandon H.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by llanqui, Today, 03:53 AM
        0 responses
        4 views
        0 likes
        Last Post llanqui
        by llanqui
         
        Started by burtoninlondon, Today, 12:38 AM
        0 responses
        10 views
        0 likes
        Last Post burtoninlondon  
        Started by AaronKoRn, Yesterday, 09:49 PM
        0 responses
        14 views
        0 likes
        Last Post AaronKoRn  
        Started by carnitron, Yesterday, 08:42 PM
        0 responses
        11 views
        0 likes
        Last Post carnitron  
        Started by strategist007, Yesterday, 07:51 PM
        0 responses
        14 views
        0 likes
        Last Post strategist007  
        Working...
        X