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

Calculation on the last bar on chart

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

    Calculation on the last bar on chart

    Hello guys,

    Im working on an indicator and I want to make a calculation, but only when the last historical bar (last bar on the chart) is processed. ¿How I can achieve this?

    Thank you!

    Felipe

    #2
    How about: if (CurrentBar >= Bars.Count - (State == Calculate.OnBarClose ? 2 : 1) { /* whatever here */ }

    or not quite the same thing, but close:

    if (State == State.Realtime) { /* whatever here */ }
    Last edited by QuantKey_Bruce; 11-25-2023, 10:20 AM.
    Bruce DeVault
    QuantKey Trading Vendor Services
    NinjaTrader Ecosystem Vendor - QuantKey

    Comment


      #3
      Hello facuevasm,

      Thank you for your post.

      To find the last historical bar, you can use:


      Code:
      if (State == State.Historical && CurrentBar == Count - 2)
      
      // do something

      You will want to check the State to make sure the script is still processing historical data to ensure you are finding the last historical bar.

      Please read through this forum post for more details:

      https://forum.ninjatrader.com/forum/...541#post811541

      If you have any other questions, please let us know.
      Gaby V.NinjaTrader Customer Service

      Comment


        #4
        Originally posted by NinjaTrader_Gaby View Post
        Code:
        if (State == State.Historical && CurrentBar == Count - 2)
        
        // do something
        This code only works if Calculate == Calculate.OnBarClose. If Calculate == Calculate.OnEachTick then it should be Count - 1.​

        If the indicator is used by a Market Analyzer Column, this also won't work as the indicator states are not processed the same as they are when used on a chart. See https://forum.ninjatrader.com/forum/...nd-onbarupdate for details about why indicators in Market Analyzer Columns will behave differently.

        I have an extension method for IndicatorRenderBase defined as follows

        Code:
                public static bool IsLastHistoricalBar(this IndicatorRenderBase indicator)
                {
                    switch(indicator.Calculate)
                    {
                        case Calculate.OnBarClose:
                            return indicator.CurrentBar == indicator.Bars.Count - 2;
                        case Calculate.OnEachTick:
                            throw new NotImplementedException("Doesn't support on each tick yet");
                        case Calculate.OnPriceChange:
                            throw new NotSupportedException("OnPriceChange not supported");
                        default:
                            throw new NotImplementedException(string.Format("{0} is not implemented", indicator.Calculate));
                    }
                }
        I haven't verified the behavior for OnEachTick but I am believe, like QuantKey_Bruce pointed out, you just subtract 1 instead of 2.
        Last edited by ntbone; 11-28-2023, 09:05 AM.

        Comment


          #5
          As Bruce demonstrated, if (CurrentBar >= Bars.Count - (State == Calculate.OnBarClose ? 2 : 1) is a single line of code to accomplish this.
          eDanny
          NinjaTrader Ecosystem Vendor - Integrity Traders

          Comment


            #6
            Originally posted by eDanny View Post
            As Bruce demonstrated, if (CurrentBar >= Bars.Count - (State == Calculate.OnBarClose ? 2 : 1) is a single line of code to accomplish this.
            Yes but
            1. I'd rather not have to remember how to code it up
            2. If you happen to use an indicator with something other then the two choices, you'll be warned.
            3. If the method of determining the last historical bar ever changes, I only have one place to fix it. I have had to check for last historical bar in more than one indicator.

            Code:
            if(State == State.Historical && IsLastHistoricalBar(this))
            {
                // Do some stuff
            }
            I did not include the State.Historical mostly due to that bug I referred to in the link. The IsLastHistoricalBar can still be useful but the state might actually be State.RealTime and extra steps need to be done. If you are sure your indicators are never going to be used in Market Analyzer Columns you can modify my code to include the State check as well.
            Last edited by ntbone; 11-29-2023, 09:29 PM.

            Comment


              #7
              Originally posted by ntbone View Post
              If you are sure your indicators are never going to be used you can modify my code to include the State check as well.
              [EDIT: ntbone has since fixed his above comment. My
              attempt at somewhat dark humor remains unedited below.
              I was able to appreciate his original comment as humorous
              sarcasm, and was quite impressed with his wording. As
              ntbone says below, his sarcasm was unintentional.]

              And that, my friends, is the definition of sarcasm.

              I've read that 5 times, and I'm pretty sure that it is sarcasm.

              Right?

              I mean, it sure reads like a well-worded dig to me.

              A very good one, too.

              As Darth Vader would say, in his deep emotionless voice,

              "Impressive."

              Last edited by bltdavid; 11-29-2023, 10:37 PM. Reason: added some EDIT notes for posterity

              Comment


                #8
                Another approach you could use is... in OnStateChange understand that it hits State.Transition on the last historical bar, whatever the Calculate mode is set to. So, either do your special "last bar" business there or set a flag there so you can pick it up in OnBarUpdate next time. If you just do it there, you don't have to worry about the pesky details of how the indexing works for each mode in each state.
                Bruce DeVault
                QuantKey Trading Vendor Services
                NinjaTrader Ecosystem Vendor - QuantKey

                Comment


                  #9
                  Originally posted by bltdavid View Post

                  And that, my friends, is the definition of sarcasm.

                  I've read that 5 times, and I'm pretty sure that it is sarcasm.

                  Right?

                  I mean, it sure reads like a well-worded dig to me.

                  A very good one, too.

                  As Darth Vader would say, in his deep emotionless voice,

                  "Impressive."

                  Oops. I left out the "Market Analyzer Columns" part. Fixed the original post.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by fx.practic, 10-15-2013, 12:53 AM
                  5 responses
                  5,404 views
                  0 likes
                  Last Post Bidder
                  by Bidder
                   
                  Started by Shai Samuel, 07-02-2022, 02:46 PM
                  4 responses
                  95 views
                  0 likes
                  Last Post Bidder
                  by Bidder
                   
                  Started by DJ888, Yesterday, 10:57 PM
                  0 responses
                  8 views
                  0 likes
                  Last Post DJ888
                  by DJ888
                   
                  Started by MacDad, 02-25-2024, 11:48 PM
                  7 responses
                  159 views
                  0 likes
                  Last Post loganjarosz123  
                  Started by Belfortbucks, Yesterday, 09:29 PM
                  0 responses
                  8 views
                  0 likes
                  Last Post Belfortbucks  
                  Working...
                  X