![]() |
This website will be down for maintenance from Friday May 24th at 6PM MDT until Sunday May 26th at 12PM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com
|
|||||||
| Tips Official NinjaScript tips and tricks |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
|
When coding an indicator or strategy it is important to be able to access the intended bars for correct calculations. In NinjaScript we are able to access the bars we want through proper use of the bar’s indexing.
The bar’s indexing is setup in a reverse chronological order. This means "0" refers to the most recent bar, "1" refers to the previous bar, "2" refers to the bar before that one, etc. For example, if we wanted to subtract the high and low of 10 bars ago from each other we would do this: Code:
double value = High[10] – Low[10]; CurrentBar CurrentBar returns an int representing the number of bars existing on the chart. This property is most useful when you want to run calculations from the very beginning of the chart. For example, if you wanted to find the average high value of the first 10 bars on the chart you could do this: Code:
double highValue = 0;
int x = CurrentBar;
while (x > CurrentBar - 10)
{
highValue += High[x];
x--;
}
Print("The average high value: " + highValue/10);
BarsSinceSession BarsSinceSession is another property that can help you find the first bar of the current session. The difference between BarsSinceSession and CurrentBar is that BarsSinceSession resets its count whenever a new session begins. This means if you use it in an index it will only get you to the beginning of the current session and not any previous sessions. For example, if you wanted to find the open of the current session you could do this: Code:
double openValue = Open[Bars.BarsSinceSession]; Note: In NinjaTrader 7, if you wish to access values older than 256 bars ago you will need to ensure the MaximumBarsLookBack is set to .Infinite. Other Properties and Methods There are also a number of other properties and methods that can be useful in helping you locate the correct bars index to reference. Please take a look at these in the help guide: BarsSinceEntry(), BarsSinceExit(), GetBar(), GetDayBar(), HighestBar(), LowestBar(), LRO(), and MRO().
Josh
NinjaTrader Customer Service |
|
|
|
|
The following user says thank you to NinjaTrader_Josh for this post: |
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Ninjatrader not using the correct strategy | ninjauser | SuperDOM and other Order Entry Windows | 5 | 12-14-2007 01:14 PM |
| Pulling the correct values in a strategy? | Burga1 | Strategy Development | 4 | 11-20-2007 09:30 AM |
| Referencing other Instruments as Indicator | shortymcshort | Indicator Development | 1 | 08-16-2007 08:37 AM |
| Is this correct? | ct | General Programming | 2 | 05-02-2007 05:06 PM |
| Number of stop orders not correct | BradB | Strategy Development | 26 | 03-24-2007 07:35 AM |