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

Detecting new bar

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

    Detecting new bar

    Hi,

    In strategy OnBarUpdate() and CalculateOnBarClose = false

    I need to detect when a new bar formed, which works on tick and time chart. How to do this?

    #2
    You could for example check via CurrentBar, if you're on the same bar with tick updates coming in, this would not advance.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Originally posted by ssg10 View Post
      Hi,

      In strategy OnBarUpdate() and CalculateOnBarClose = false

      I need to detect when a new bar formed, which works on tick and time chart. How to do this?
      if (FirstTickOfBar)
      {
      // ...
      }

      ref: http://www.ninjatrader.com/support/h...ttickofbar.htm

      Comment


        #4
        Nailing the actual start of a new bar

        For backfill, every tick is a new bar because there is only one tick per bar.

        For real time data, FirstTickOfBar is not set to true until AFTER the first OnBarUpdate event has fired for the new bar. So, FirstTickOfBar really should be called SecondTickOfBar. But the Bars.Count property updates BEFORE that, when FirstTickOfBar would be expected to become true, but isn't yet true.. (RJay uncovered this, so thank him!)

        Therefore the best way to detect the beginning of a new bar is to declare a boolean called FirstTick and an integer called barcount. Then use this code.

        Code:
        [FONT="Courier New"]if( Historical || ( !Historical && barcount!=Bars.Count) )
        { FirstTick=true:
          barcount=Bars.Count
        }
        else FirstTick=false;[/FONT]
        Then instead of testing for the value of FirstTickOfBar, test for the value of FirstTick.
        Last edited by Ricam; 10-27-2011, 09:45 PM.

        Comment


          #5
          Originally posted by Ricam View Post
          For backfill, every tick is a new bar because there is only one tick per bar.

          For real time data, FirstTickOfBar is not set to true until AFTER the first OnBarUpdate event has fired for the new bar. So, FirstTickOfBar really should be called SecondTickOfBar. But the Bars.Count property updates BEFORE that, when FirstTickOfBar would be expected to become true, but isn't yet true.. (RJay uncovered this, so thank him!)

          Therefore the best way to detect the beginning of a new bar is to declare a boolean called FirstTick and an integer called barcount. Then use this code.

          Code:
          [FONT=Courier New]if( Historical || ( !Historical && barcount!=Bars.Count) )
          { FirstTick=true:
            barcount=Bars.Count
          }
          else FirstTick=false;[/FONT]
          Then instead of testing for the value of FirstTickOfBar, test for the value of FirstTick.
          I do not see how this makes a difference. This code will also only run on the first OnBarUpdate() event, so FirstTick is also only true after the first OnBarUpdate() event.

          Comment


            #6
            FirstTickOfBar is true on the first tick of the bar, the fact that Bars.Count has already been incremented when OnBarUpdate() is called, doesn't mean it's a different tick being processed.

            Which is how it should be, when OnBarUpdate() is called, it needs to know that a new bar has formed.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by aligator, 01-06-2022, 12:14 PM
            4 responses
            235 views
            0 likes
            Last Post john_44573  
            Started by reynoldsn, Today, 05:56 PM
            0 responses
            5 views
            0 likes
            Last Post reynoldsn  
            Started by bortz, 11-06-2023, 08:04 AM
            51 responses
            1,990 views
            0 likes
            Last Post aligator  
            Started by dmking, 11-12-2019, 12:31 PM
            4 responses
            4,151 views
            0 likes
            Last Post jasonw
            by jasonw
             
            Started by roblogic, Today, 04:31 PM
            0 responses
            11 views
            0 likes
            Last Post roblogic  
            Working...
            X