Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error OnBarUpdate for strategy

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

    Error OnBarUpdate for strategy

    Hi,

    I am trying to make a simple strategy that prints out some values, though using multiple time frames. I keep getting the following error

    Error on calling 'OnBarUpdate' method for strategy 'PrintSwing/b122f02ce1144ea8be09dba0c60f5631': You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.


    Any idea why?

    Here is the first few lines of code in my strategy

    PHP Code:
    protected override void Initialize()
    {
    Add(Swing(5)); //add swing indicator with strength of 5, not 5minutes!
    Add(PeriodType.Minute, 30); //add second time frame, 30mis
    Add(PeriodType.Minute, 1); // add a third timeframe, 1min
    Add(EMA(20));
    Add(VWAP());
    //assumes that the initial time period chosen for strategy is 1min
    CalculateOnBarClose = true;
    }
    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    { // opening bracket
    // code for calculating fibonacci levels on the opening range 9:30am - 10:30
    double opening_high = 0;
    double opening_low = 0;
    // Resets the highest high and lowest low at the start of every new session
     
    if (Bars.FirstBarOfSession && FirstTickOfBar)
    { 
    opening_high = 0;
    opening_low = 0;
    }
     
    DateTime startTime;
    DateTime endTime;
    endTime = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, 10, 30, 0);
    startTime = new DateTime(9,30,0);
    int startBarsAgo = GetBar(startTime);
    int endBarsAgo = GetBar(endTime);
     
    //calculates opening range high and low
    opening_high = MAX(High, startBarsAgo - endBarsAgo)[endBarsAgo];
    opening_low = MIN(Low, startBarsAgo - endBarsAgo)[endBarsAgo];
     
    //fib level calculations on opening range
     
    double opening_range_fibo_thirty = opening_low + (opening_high - opening_low) * .382;
    double opening_range_fibo_sixty = opening_low + (opening_high - opening_low) * .618;
    double opening_range_fibo_fifty = opening_low + (opening_high - opening_low) * .5; 
    double opening_range_fibo_thirty_extension_high = opening_low + (opening_high - opening_low) * 1.382; 
    double opening_range_fibo_thirty_extension_low = opening_low - (opening_high - opening_low) * 1.382; 
    


    #2
    Hi eurostoxx trader,

    I would start by doing a CurrentBar check for all your series, something like...

    if (CurrentBarArray[0] > 0 && CurrentBarArray[1] > 0 && CurrentBarArray[2] > 0)
    {
    all your code here
    }

    Then, use try/catch blocks to find the offending line.
    More info at - http://www.ninjatrader.com/support/f...ead.php?t=9825
    TimNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    648 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    369 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    108 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    572 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    573 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X