Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Make sure you have enough bars in the data series you are accessing

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

    Make sure you have enough bars in the data series you are accessing

    Applies to: NinjaTrader 8 and NinjaTrader 7

    A common programming error is not checking to ensure there are enough bars contained in the data series you are accessing. This will explain some of the concepts to check for this situation,

    For example:

    Code:
    protected override void OnBarUpdate()
    {
        if (Close[0] > Close[1])
            // Do something
    }
    In the code snippet above, the OnBarUpdate() method is called for each bar contained in your data series.

    On the very first bar (think of the 1st bar on the chart from left to right) the value of "close of 1 bar ago" (Close[1]) does not yet exist and your indicator/strategy will not work and throw an exception to the Control Center Log tab "Index was out of range...".

    Following are two ways to ways to resolve this:

    Code:
    protected override void OnBarUpdate()
    {
        [COLOR=BLUE][B]if (CurrentBar < 1)
            return;[/B][/COLOR]
    
        if (Close[0] > Close[1])
            // Do something
    }
    The resolution above is to check how many bars we have seen (CurrentBar) and to exit the OnBarUpdate() method if an insufficient number of bars has been seen.

    Code:
    protected override void OnBarUpdate()
    {
        if (Close[0] > Close[[COLOR=BLUE][B]Math.Min(CurrentBar, 1)[/B][/COLOR]])
            // Do something
    }
    The resolution above substitutes the minimum value between the current bar being processed and the desired number of bars ago value, in this case 1.
    Last edited by NinjaTrader_Jesse; 06-03-2015, 11:47 AM.
    RayNinjaTrader Customer Service

Latest Posts

Collapse

Topics Statistics Last Post
Started by thanajo, 05-04-2021, 02:11 AM
3 responses
468 views
0 likes
Last Post tradingnasdaqprueba  
Started by Christopher_R, Today, 12:29 AM
0 responses
10 views
0 likes
Last Post Christopher_R  
Started by sidlercom80, 10-28-2023, 08:49 AM
166 responses
2,237 views
0 likes
Last Post sidlercom80  
Started by thread, Yesterday, 11:58 PM
0 responses
4 views
0 likes
Last Post thread
by thread
 
Started by jclose, Yesterday, 09:37 PM
0 responses
9 views
0 likes
Last Post jclose
by jclose
 
Working...
X