NinjaTrader Support Forum  

Go Back   NinjaTrader Support Forum > NinjaScript Educational Resources > Tips

Tips Official NinjaScript tips and tricks

Reply
 
Thread Tools Display Modes
Old 08-31-2007, 11:44 AM   #1
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
Default Make sure you have enough bars in the data series you are accessing

A common programming error is not checking to ensure there are enough bars contained in the data series you are accessing.

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()
{
    if (CurrentBar < 1)
        return;

    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[Math.Min(CurrentBar, 1)])
        // 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_Josh; 08-01-2008 at 11:36 PM.
NinjaTrader_Ray is offline  
Reply With Quote
The following 2 users say thank you to NinjaTrader_Ray for this post:
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Format Data Series tagomi Charting 5 09-21-2008 07:04 PM
Useing a Data Series nigeleyre General Programming 5 08-22-2008 07:03 AM
Format Data Series Bars Back Problem praveen_d Miscellaneous Support 3 08-08-2008 06:24 AM
data series argito Indicator Development 3 05-30-2008 11:06 PM
Data Series question NinjaTrader_Josh Indicator Development 4 05-25-2007 12:43 AM


All times are GMT -6. The time now is 03:26 AM.