• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Referencing the correct bar

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

    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, 03:24 PM.
    JoshNinjaTrader Customer Service

    #2
    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, 08:12 AM.
    JesseNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by algotrading, 07-18-2018, 10:41 PM
    2 responses
    917 views
    1 like
    Last Post gbux11
    by gbux11
     
    Started by supermht, Yesterday, 10:13 PM
    0 responses
    2 views
    0 likes
    Last Post supermht  
    Started by supermht, Yesterday, 07:59 PM
    0 responses
    8 views
    0 likes
    Last Post supermht  
    Started by mohdhm, Yesterday, 05:13 PM
    1 response
    17 views
    0 likes
    Last Post nkhoi
    by nkhoi
     
    Started by 3lliot, Yesterday, 05:55 PM
    0 responses
    4 views
    0 likes
    Last Post 3lliot
    by 3lliot
     
    Working...
    X