Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Determining "Last Bar On Chart"

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

    Determining "Last Bar On Chart"

    If you want to incorporate some logic to have your indicator or strategy only run on real-time data, then you should incorporate the following.

    Checks if bar data is historical or real-time. Ifhistorical, return out of the method.

    protected override void OnBarUpdate()
    {
    if (Historical)
    return;
    }
    If you want to know if the bar being processed is the last bar on the chart.

    When CalculateOnBarClose == false, then OnBarUpdate() is being called for the last bar on the chart:

    protected override void OnBarUpdate()
    {
    if (CurrentBar == Bars.Count - 1)
    // Is last bar on chart
    }
    When CalculateOnBarClose == true, then OnBarUpdate() is being called for the last closed bar on the chart, not thein processbar:

    protected override void OnBarUpdate()
    {
    if (CurrentBar== Bars.Count -2)
    // Is last bar on chart
    }

    Ray
    RayNinjaTrader Customer Service

    #2
    imported post

    Ray,
    This is my implementation of this, as a user function in the UserDefinedMethods Indicator?

    This seems to work OK, but I'm confused about the offset from Bars.Count. The only way I can see it is if Bars.Count starts at 1, and Currentbar starts at 0.

    If one has an indicator that should be processed on the lastbar displayed on the chart (like one controlling white space), this will return true whether or not connected to a data feed.

    If one checks for Historical while not connected, I assume that code would not execute.

    Am I on the right track?

    public bool LastBarOnChart()
    {
    bool LBOC = false;
    if ( CalculateOnBarClose == false && CurrentBar == Bars.Count - 1)
    LBOC=true;
    else
    if ( CalculateOnBarClose == true && CurrentBar == Bars.Count - 2)
    LBOC=true;
    else LBOC=false;

    return LBOC;
    }

    Comment


      #3
      imported post

      In C#, indexes of a collection are zero based. CurrentBar is an index and so its zero based and starts with zero. Collection.Count is 1 based. Therefore, Bars.Count will return the total number of bars.

      Your code will work but I would writeit as a property that would look something like this:

      public bool LastBarOnChart
      {
      get
      {
      if (!CalculateOnBarClose && CurrentBar == Bars.Count - 1)
      return true;
      else if (CalculateOnBarClose && CurrentBar == Bars.Count - 2)
      return true;

      return false;
      }
      }
      RayNinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by GLFX005, Today, 03:23 AM
      0 responses
      1 view
      0 likes
      Last Post GLFX005
      by GLFX005
       
      Started by XXtrader, Yesterday, 11:30 PM
      2 responses
      11 views
      0 likes
      Last Post XXtrader  
      Started by Waxavi, Today, 02:10 AM
      0 responses
      6 views
      0 likes
      Last Post Waxavi
      by Waxavi
       
      Started by TradeForge, Today, 02:09 AM
      0 responses
      14 views
      0 likes
      Last Post TradeForge  
      Started by Waxavi, Today, 02:00 AM
      0 responses
      3 views
      0 likes
      Last Post Waxavi
      by Waxavi
       
      Working...
      X