Go Back   NinjaTrader Support Forum > NinjaScript Educational Resources > Tips

Tips Official NinjaScript tips and tricks

NinjaTrader
Reply
 
Thread Tools Display Modes
Old 01-01-2008, 03:32 PM   #1
NinjaTrader_JoshP
NinjaTrader Product Manager
 
NinjaTrader_JoshP's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,459
Thanks: 1
Thanked 387 times in 108 posts
Default Referencing the correct bar

Applies to NinjaTrader 7

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];
Now that we know how the indexing works there are several properties and methods at our disposal that can help us access important keystone bars. The more important ones are CurrentBar and BarsSinceSession.

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);
Note: A common mistake in using CurrentBar is using it in the index to access the most recent bar. In this situation, instead of doing something like Close[CurrentBar] you will want to do Close[0].

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];
The example used in the discussion about CurrentBar can also be done with BarsSinceSession if you wanted to calculate values based on the current session instead of the start of the chart too.

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().
Last edited by NinjaTrader_Jesse; 06-03-2015 at 03:24 PM.
NinjaTrader_JoshP is offline  
Reply With Quote
The following 2 users say thank you to NinjaTrader_JoshP for this post:
Old 06-03-2015, 03:35 PM   #2
NinjaTrader_Jesse
NinjaTrader Customer Service
 
NinjaTrader_Jesse's Avatar
 
Join Date: Mar 2014
Location: Denver, CO
Posts: 4,943
Thanks: 30
Thanked 1,263 times in 1,181 posts
Default

Applies to NinjaTrader 8

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];
Now that we know how the indexing works there are several properties and methods at our disposal that can help us access important keystone bars. The more important ones are CurrentBar and BarsSinceNewTradingDay.

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);
Note: A common mistake in using CurrentBar is using it in the index to access the most recent bar. In this situation, instead of doing something like Close[CurrentBar] you will want to do Close[0].

BarsSinceNewTradingDay
BarsSinceNewTradingDay is another property that can help you find the first bar of the current trading day. The difference between BarsSinceNewTradingDay and CurrentBar is that BarsSinceNewTradingDay 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.BarsSinceNewTradingDay];
The example used in the discussion about CurrentBar can also be done with Bars.BarsSinceNewTradingDay if you wanted to calculate values based on the current session instead of the start of the chart too.

Note: 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: BarsSinceEntryExecution(), BarsSinceExitExecution(), GetBar(),
GetDayBar(), HighestBar(), LowestBar(), LRO(), and MRO().
Last edited by NinjaTrader_Jesse; 06-04-2015 at 08:12 AM.
NinjaTrader_Jesse is offline  
Reply With Quote
The following user says thank you to NinjaTrader_Jesse 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
Ninjatrader not using the correct strategy ninjauser Platform Technical Support 5 12-14-2007 02:14 PM
Pulling the correct values in a strategy? Burga1 Strategy Development 4 11-20-2007 10:30 AM
Referencing other Instruments as Indicator shortymcshort Indicator Development 1 08-16-2007 08:37 AM
Is this correct? ct General Development 2 05-02-2007 05:06 PM
Number of stop orders not correct BradB Strategy Development 26 03-24-2007 07:35 AM


All times are GMT -6. The time now is 01:10 AM.

Futures, foreign currency and options trading contains substantial risk and is not for every investor. An investor could potentially lose all or more than the initial investment. Risk capital is money that can be lost without jeopardizing ones financial security or lifestyle. Only risk capital should be used for trading and only those with sufficient risk capital should consider trading. Past performance is not necessarily indicative of future results. View Full Risk Disclosure.

CFTC Rules 4.41 - Hypothetical or Simulated performance results have certain limitations, unlike an actual performance record, simulated results do not represent actual trading. Also, since the trades have not been executed, the results may have under-or-over compensated for the impact, if any, of certain market factors, such as lack of liquidity. Simulated trading programs in general are also subject to the fact that they are designed with the benefit of hindsight. No representation is being made that any account will or is likely to achieve profit or losses similar to those shown.

This website is hosted and operated by NinjaTrader, LLC (“NT”), a software development company which owns and supports all proprietary technology relating to and including the NinjaTrader trading platform. NT is an affiliated company to NinjaTrader Brokerage (“NTB”), which is a NFA registered introducing broker (NFA #0339976) providing brokerage services to traders of futures and foreign exchange products. This website is intended for educational and informational purposes only and should not be viewed as a solicitation or recommendation of any product, service or trading strategy. No offer or solicitation to buy or sell securities, securities derivative or futures products of any kind, or any type of trading or investment advice, recommendation or strategy, is made, given, or in any manner endorsed by any NT affiliate and the information made available on this Web site is not an offer or solicitation of any kind. Specific questions related to a brokerage account should be sent to your broker directly. The content and opinions expressed on this website are those of the authors and do not necessarily reflect the official policy or position of NT or any of its affiliates. 

Vendors along with their websites, products and services, collectively referred to as (“Vendor Content”), are independent persons or companies that are in no manner affiliated with NT or any if its affiliates. NT or any of its affiliates are not responsible for, do not approve, recommend or endorse any Vendor Content referenced on this website and it’s your sole responsibility to evaluate Vendor Content. Please be aware that any performance information provided by a vendor should be considered hypothetical and must contain the disclosures required by NFA Rule 2-29(c). If you are interested in learning more about, or investigating the quality of, any such Vendor Content you must contact the vendor, provider or seller of such Vendor Content. No person employed by, or associated with, NT or any of its affiliates is authorized to provide any information about any such Vendor Content.