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

How to identify the last loaded bar

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

    How to identify the last loaded bar

    Hello all,
    I need to do some calculations in an indicator when the chart has loaded all requested bars, not just on each bar. So I'm looking for a way to know when a chart has finished loading all historical bars. What is the best way? (supported or not)
    Thanks a lot

    #2
    Hello supernatural,

    Thank you for the question.

    There is way to do this using the "Historical" switch.

    Code:
    if (Historical) 
             return;
    The line above if placed at the beginning of your OnBarUpdate will skip the remainder of the code below it if the bar is historical.

    Histoical is simply a boolean value so you can check if it is true or false and either run the code or not based on its value.

    Here is a link to the helpguide reference



    Please let me know if I may be of additional assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hello Jesse,

      Thanks for the advice. However, this is not working in my case. First problem is that, outside of trading hours, there will not be any real-time bar to run my code. Second problem is that I need this code to run immediately, I don't want to wait for the next live bar, which may come after a long time.
      Any other solutions?

      Comment


        #4
        Hello supernatural,

        In this case rather than using the Historical bool you could use the bar count to determine if you have reached the most recent bar and then only calculate on the last bar loaded.

        You would need to compare the current bar being processed against the total count of bars that were loaded. The CurrentBar will always be <= Count -1.

        Here is a link to the help guide reference.


        This is a simple if statement that returns if the count is not at the last closed bar. The reason that I have used count - 2 is because count -1 will return the current bar that is still active. Count - 2 will return the most recent bar that is complete.
        Code:
        if(CurrentBar < Count - 2)
        return;
        Also because you want to immediately execute the code you may want to set CalculateOnBarClose = false; in your initialize.

        Code:
        protected override void Initialize()
        {
        CalculateOnBarClose = false; 
        }
        Please let me know if I may be of additional assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          It works great, thanks!

          Comment


            #6
            Thanks, it helped!

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by NRITV, Today, 01:15 PM
            2 responses
            9 views
            0 likes
            Last Post NRITV
            by NRITV
             
            Started by frankthearm, Today, 09:08 AM
            7 responses
            31 views
            0 likes
            Last Post NinjaTrader_Clayton  
            Started by maybeimnotrader, Yesterday, 05:46 PM
            5 responses
            26 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by quantismo, Yesterday, 05:13 PM
            2 responses
            19 views
            0 likes
            Last Post quantismo  
            Started by adeelshahzad, Today, 03:54 AM
            5 responses
            33 views
            0 likes
            Last Post NinjaTrader_BrandonH  
            Working...
            X