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

"... value that is invalid since it is out of range...etc."

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

    "... value that is invalid since it is out of range...etc."

    I've been writing a multi time frame script, I just got it working, but it only works on a day chart. Minute and tick give me the error: "... Error on calling 'OBU' method on bar 372: you are accessing an index with a value that is invalid since it is out of range...etc." My state.configure looks like:

    else if (State == State.Configure)
    {
    max = MAX(High, 3);
    min = MIN(Low, 3);


    if (BarsPeriod.BarsPeriodType == BarsPeriodType.Tick)
    {
    tickValue = BarsPeriod.Value;
    AddDataSeries(BarsPeriodType.Tick, (tickValue * 3));
    AddDataSeries(BarsPeriodType.Tick, (tickValue * 9));
    }
    else if (BarsPeriod.BarsPeriodType == BarsPeriodType.Minute)
    {
    minuteValue = BarsPeriod.Value;
    AddDataSeries(BarsPeriodType.Minute, (minuteValue * 3));
    AddDataSeries(BarsPeriodType.Minute, (minuteValue * 9));
    }
    else if (BarsPeriod.BarsPeriodType == BarsPeriodType.Day)
    {
    dayValue = BarsPeriod.Value;
    AddDataSeries(BarsPeriodType.Day, (dayValue * 3));
    AddDataSeries(BarsPeriodType.Day, (dayValue * 9));
    }
    else if (BarsPeriod.BarsPeriodType == BarsPeriodType.Week)
    {
    weekValue = BarsPeriod.Value;
    AddDataSeries(BarsPeriodType.Week, (weekValue * 3));
    AddDataSeries(BarsPeriodType.Week, (weekValue * 9));
    }
    else if (BarsPeriod.BarsPeriodType == BarsPeriodType.Month)
    {
    monthValue = BarsPeriod.Value;
    AddDataSeries(BarsPeriodType.Month, (monthValue * 3));
    AddDataSeries(BarsPeriodType.Month, (monthValue * 9));
    }

    }​
    At the start of OBU I use
    if (CurrentBars[0] <= BarsRequiredToPlot || CurrentBars[1] <= BarsRequiredToPlot || CurrentBars[2] <= BarsRequiredToPlot)
    return;​
    I am not running any data feeds. The code contains a few while loops. Day charts work fine. I have not checked week and month since I rarely use those.
    Any advice would be appreciated.

    Thank you.

    #2
    Hello imalil,

    Thanks for your post.

    " Error on calling 'OBU' method on bar 372: you are accessing an index with a value that is invalid since it is out of range"

    This error will be displayed if you try to use a BarsAgo or index when it 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.

    Please note that dynamically adding data series as seen in the code you shared is not recommended/supported as stated in the AddDataSeries() help guide page. From the help guide page: "Arguments supplied to AddDataSeries() should be hardcoded and NOT dependent on run-time variables which cannot be reliably obtained during State.Configure (e.g., Instrument, Bars, or user input). Attempting to add a data series dynamically is NOT guaranteed and therefore should be avoided.​"

    AddDataSeries: https://ninjatrader.com/support/help...=adddataseries

    I recommend debugging your code and finding the exact index that is causing the error and then checking the size of that collection to understand the cause of the error. See the forum thread below for more information about this error.
    https://ninjatrader.com/support/foru...13#post1048513

    You may use prints to find the exact line of code triggering the error. See the forum thread below demonstrating how to use prints to understand behavior and debug a script.
    https://ninjatrader.com/support/foru...121#post791121

    Note that it is important to print the BarsInProgress value and CurrentBars[BarsInProgress] along with the time of the bar.

    Please let me know if I may assist further.​
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Hello, thanks for replying. I think I know the problem I just have to figure out how to fix it. The question I have is this: If I don't dynamically add data series, which means don't use
      AddDataSeries(BarsPeriodType.Minute, (minuteValue * 3));
      , the problem arises where I can't use one indicator on different minute charts. For example, how can I use one indicator on a 1, 3, 9 minute chart and the same indicator on a 5, 15, 45 minute chart if I hard code the time frame? Is this an impossible thing I'm trying to do?

      Thank you.

      Comment


        #4
        Hello imalil,

        Thanks for your note.

        You would need to add an additional data series to your script by calling AddDataSeries() for each data series you want to add. In the example you shared, you would need to call AddDataSeries() a total of 6 times to add a 1-minute, 3-minute, 5-minute, 9-minute, 15-minute, and 45-minute secondary series to the script.

        AddDataSeries((BarsPeriodType.Minute, 1);
        AddDataSeries((BarsPeriodType.Minute, 3);
        AddDataSeries((BarsPeriodType.Minute, 5);
        //...and so on


        Then, you may instantiate an indicator on each secondary series in your script, access indicator values, or assign that indicator value to a plot if you wish. I have attached a simple example script of what this may look like.

        Note that indicators will only calculate values based on the added series, the indicator can only plot on the primary data series the script is running on.

        Please review the help guide pages linked below.

        AddDataSeries(): https://ninjatrader.com/support/help...dataseries.htm
        BarsArray: https://ninjatrader.com/support/help.../barsarray.htm
        CurrentBars: https://ninjatrader.com/support/help...urrentbars.htm
        Working with Multi-Timeframe/Multi-Instrument NinjaScripts: https://ninjatrader.com/support/help...nstruments.htm

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

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Segwin, 05-07-2018, 02:15 PM
        14 responses
        1,789 views
        0 likes
        Last Post aligator  
        Started by Jimmyk, 01-26-2018, 05:19 AM
        6 responses
        838 views
        0 likes
        Last Post emuns
        by emuns
         
        Started by jxs_xrj, 01-12-2020, 09:49 AM
        6 responses
        3,294 views
        1 like
        Last Post jgualdronc  
        Started by Touch-Ups, Today, 10:36 AM
        0 responses
        13 views
        0 likes
        Last Post Touch-Ups  
        Started by geddyisodin, 04-25-2024, 05:20 AM
        11 responses
        63 views
        0 likes
        Last Post halgo_boulder  
        Working...
        X