Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Index issues while calling OnBarUpdate

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

    Index issues while calling OnBarUpdate

    Hi.
    I'm currently having an issue with the code I attached to this topic.

    In this code, I'm trying to use the volumetric delta of a 1 minute dataserie (called in the code by "AddVolumetric()"), on a unirenko chart (both same instrument : MES).

    Firts, everything goes well. And at some point, I have an index error (see the screenshot attached).

    I don't understand what makse this error occurred.

    My goal is to be able to return the volumetric delta from an instrument and to use it in a unirenko chart of the same instrument. Maybe there is a better way to do so ?

    Thank you in advance for your reply.
    Attached Files

    #2
    Hello thanajo,

    Thanks for your post.

    After getting that error, we would use prints to identify the line of code that throws the error and any indexing that may be used. Indexing can also be indexing on a collection, or an array, and is not limited to indexing a Series with BarsAgo values.

    Understanding how the error was hit requires knowing which line of code the script is erroring on.

    When you add additional prints, what is the line that causes the script to error out?

    What indexes are being provided on this line?

    While assisting a client on another ticket, I prepared an example script that prints bar delta on the chart, fetching from an added Volumetric bar. If the example I prepared is helpful, you may find it below.

    Hi, this is an additional post to my earlier post https://ninjatrader.com/support/forum/node/1121004 I plotted the Cumulative Delta indicator on a 1 Range ES chart My settings for this indicator are: 1. Delta type = BidAsk 2. Period = Bar I let the indicator run for a while and then pressed F5 to refresh. To my shock, the


    We look forward to assisting.

    Comment


      #3
      Hi. Thanks you for your reply.
      Using the example you prepared, I exactly got the same issue.
      On this new screenshot you can see the number "153" appeared in the chart (at 11:40:53). It's not the value the code is supposed to return, and then the code crash a fex bars after with this index error. I really don't understand what's going on. May you help me ?
      Attached Files

      Comment


        #4
        I added some Prints all along the code, so it does look like that :

        ___________________________________________

        else if (State == State.Configure)
        {
        Print("Volumetric dataserie to be added");
        AddVolumetric(null, BarsPeriodType.Minute, 1, VolumetricDeltaType.BidAsk, 1);
        Print("Volumetric dataserie correctly added");
        }

        protected override void OnBarUpdate()
        {
        Print("New OnBarUpdate sequence on bar : " + CurrentBar);
        if (Bars == null)
        {
        Print("Return on bars = null");
        return;
        }
        Print("barsType to be created");
        NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsTy pe barsType = BarsArray[1].BarsType as NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsTy pe;

        if (barsType == null)
        {
        Print("Return on barstype = null");
        return;
        }
        Print("barsType correctly created");
        Print("New text to come");
        Draw.Text(this, "BarDelta " + CurrentBar, barsType.Volumes[CurrentBar].BarDelta.ToString(), 0, Low[0] - 5 * TickSize);
        Print("New text correctly shown");
        }

        __________________________________________________ _

        The log (I attached) is stange. Everything goes well to the bar number 116, where the code seems to start a new array. And then the OnBarUpdate sometimes occur with the initial array, and sometimes with this secondary array.
        Attached Files

        Comment


          #5
          The output log also shows that the AddVolumetric is occuring twice during the configure state. Why that ?

          Comment


            #6
            Hello thanajo,

            State.SetDefaults and State.Configure will be called multiple times throughout a scripts life as it is added to the indicator's dialog, configured and then added to the chart.

            NinjaScript LifeCycle - https://ninjatrader.com/support/help...fecycle_of.htm

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

            You have identified the code is erroring out on this line:

            Draw.Text(this, "BarDelta " + CurrentBar, barsType.Volumes[CurrentBar].BarDelta.ToString(), 0, Low[0] - 5 * TickSize);

            We can note that in this example, the drawing code is being called for both data series, the added Volumetric data series, and the primary data series. CurrentBar will represent the CurrentBar index of the processing data series. So the logic is referencing barsType.Volumes[CurrentBar] when the primary data series is processed and when the secondary data series is processed, and CurrentBar can either be of the primary or the secondary when the error is encountered. Referencing a CurrentBar index for the primary series on the secondary series will result in this type of error if the secondary series does not have a bar at that index number.

            The code was quickly adapted from our Volumetric Bars Data Access examples which just demonstrates implementation when the primary data series is the Volumetric Series.

            If you encapsulate the code there within a check for BarsInProgress == 1, then that code will only be processed on the added Volumetric series, and CurrentBar will then only reflect the CurrentBar index for the Volumetric series, since CurrentBar would only be referenced in BarsInProgress 1.

            Please consider the following:

            Code:
            if (BarsInProgress == 1)
            {
                if (Bars == null)
                    return;
            
                NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsType barsType = BarsArray[1].BarsType as NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsType;
            
                if (barsType == null)
                    return;
            
                Draw.Text(this, "BarDelta " + CurrentBar, barsType.Volumes[CurrentBar].BarDelta.ToString(), 0, Low[0] - 5 * TickSize);
            }
            More information on working with multi time frame scripts can be found here - https://ninjatrader.com/support/help...nstruments.htm

            We look forward to assisting.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            601 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            347 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            103 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            559 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            558 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X