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 Taddypole, 04-26-2024, 02:47 PM
            5 responses
            31 views
            0 likes
            Last Post eDanny
            by eDanny
             
            Started by kujista, 04-23-2024, 06:23 AM
            6 responses
            44 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by giulyko00, 04-24-2024, 12:03 PM
            7 responses
            32 views
            0 likes
            Last Post eDanny
            by eDanny
             
            Started by NM_eFe, Today, 10:13 AM
            0 responses
            6 views
            0 likes
            Last Post NM_eFe
            by NM_eFe
             
            Started by hdge4u, Yesterday, 12:23 PM
            1 response
            10 views
            0 likes
            Last Post hdge4u
            by hdge4u
             
            Working...
            X