Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error on calling 'OnBarUpdate' method on bar 0

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

    Error on calling 'OnBarUpdate' method on bar 0

    Hi,
    I'm having an error saying that i'm accessing a bar that is out of range. I don't get it since I only use current index 0.

    "Indicator 'VWAR': Error on calling 'OnBarUpdate' method on bar 0: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
    "


    Here is the code (the problem is when a call the vwap[0], because when I take it out it works)


    1st indicator not working but the second is working and both call vwap[0],

    1st

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Volume Weighted Average Range";
    Name = "VWAR";
    Calculate = Calculate.OnBarClose;
    IsOverlay = true;
    DisplayInDataBox = true;
    DrawOnPricePanel = true;
    DrawHorizontalGridLines = true;
    DrawVerticalGridLines = true;
    PaintPriceMarkers = true;
    ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
    //See Help Guide for additional information.
    IsSuspendedWhileInactive = true;
    AddPlot(Brushes.Purple, "PlotVWAR");
    }
    else if (State == State.DataLoaded)
    {
    vwap = new VWAP8();
    }
    }

    protected override void OnBarUpdate()
    {

    if (Bars.IsFirstBarOfSession)
    {
    iCumVolume = VOL()[0];
    iCumVolumeRange = VOL()[0] * (High[0] - Low[0]);
    }
    else
    {
    iCumVolume = iCumVolume + VOL()[0];
    iCumVolumeRange = (iCumVolumeRange + (VOL()[0] *(High[0] - Low[0]))) ;
    }

    PlotVWAR[0] = (iCumVolumeRange / iCumVolume) + vwap[0] ;

    }

    2nd script working no problem

    else if (State == State.DataLoaded)
    {
    vwap = VWAP8();
    stdDev1 = StdDev(vwap,Period);

    }


    }

    protected override void OnBarUpdate()
    {

    PlotVWAP[0] = vwap[0];
    MinusVAR1[0] = vwap[0] - NumStdDev * stdDev1[0];
    MinusVAR2[0] = vwap[0] - 2 *NumStdDev * stdDev1[0];
    MinusVAR3[0] = vwap[0] - 3 * NumStdDev * stdDev1[0];
    PlusVAR1[0] = vwap[0] + NumStdDev * stdDev1[0];
    PlusVAR2[0] = vwap[0] + 2 * NumStdDev * stdDev1[0];
    PlusVAR3[0] = vwap[0] + 3 * NumStdDev * stdDev1[0];


    Thanks

    #2
    Hello altij,
    Correct way to call indicator is below:-
    1. Before State change : private VWAP8 vwap;
    2. State.DataLoaded : vwap = VWAP8();
    3. OnBarUpdate :
    PlotVWAP[0] = vwap[0];
    MinusVAR1[0] = vwap[0] - NumStdDev * stdDev1[0];
    MinusVAR2[0] = vwap[0] - 2 *NumStdDev * stdDev1[0];
    MinusVAR3[0] = vwap[0] - 3 * NumStdDev * stdDev1[0];
    PlusVAR1[0] = vwap[0] + NumStdDev * stdDev1[0];
    PlusVAR2[0] = vwap[0] + 2 * NumStdDev * stdDev1[0];
    PlusVAR3[0] = vwap[0] + 3 * NumStdDev * stdDev1[0];

    Be sure to define all plots & variables.
    For other query, if you wish to have VWAP with Range, open VWAP8 in NinjaScript editor - > right click -> save as new name. Now you can replace (High[0] + Low[0] + Close[0]) / 3) with (High[0] - Low[0]), you're done now go ahead & compile it.
    Hope it helps!
    Last edited by s.kinra; 11-04-2020, 02:36 AM. Reason: missed one part

    Comment


      #3
      Hello altij,

      Thanks for your post and welcome to the NinjaTrader forums!

      The difference between the two sections is that in the first you are using vwap = new VWAP8(); and in the 2nd you are using vwap = VWAP8(); the difference being the use of the "new" keyword which is not needed as you are creating a private instance of the existing VWAP8 indicator.



      Comment


        #4
        Thank you. A peer review was needed.
        Thank you very much

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        597 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        343 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
        556 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        555 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X