Announcement

Collapse
No announcement yet.

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.

    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 Hwop38, 05-04-2026, 07:02 PM
            0 responses
            182 views
            0 likes
            Last Post Hwop38
            by Hwop38
             
            Started by CaptainJack, 04-24-2026, 11:07 PM
            0 responses
            335 views
            0 likes
            Last Post CaptainJack  
            Started by Mindset, 04-21-2026, 06:46 AM
            0 responses
            259 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by M4ndoo, 04-20-2026, 05:21 PM
            0 responses
            358 views
            0 likes
            Last Post M4ndoo
            by M4ndoo
             
            Started by M4ndoo, 04-19-2026, 05:54 PM
            0 responses
            188 views
            0 likes
            Last Post M4ndoo
            by M4ndoo
             
            Working...
            X