Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to know when actual bar is finished with Calculate.OnEachTick ?

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

    How to know when actual bar is finished with Calculate.OnEachTick ?

    Hi! I've a problem inside OnBarUpdate(). My indicator has two data series, one's 3 minutes and other's 1 Tick.

    There's two separated regions inside OnBarUpdate(), one if (State == State.Historical) an the other if (State != State.Historical), but the problem is when the historical state finishes I'm seeing that the script calls to if(BarsInProgress == 0), that is to say, the 3 minutes data series, but the actual bar doesn't finish and that generates me a bug when real processing begins.

    Can I check if that last historical bar (minutes series) it's not finished?

    Thanks.

    #2
    Hello franjcy,

    Historical bars are always calculated OnBarClose so to know the last historical bar finished you would need to use if(State == State.Realtime) for the logic following that bar. In realtime to know when a bar has finished you would need to use IsFirstTickOfBar, the first tick of the new bar is the same tick that closes the previous bar.


    Comment


      #3
      For a given series, if the indicator is designed to run an on arbitrary bar type, and if Calculate is not OnBarClose, and if you are in State.Realtime, you cannot know on a specific tick that this will have been the last tick of the bar until the first tick of the next bar arrives. At that time, IsFirstTickOfBar will be true, and the now closed bar's information is available at Close[1] etc.

      For some very specific bar types e.g. N tick bars and a few others, you can determine that the tick is the last tick of the bar before the next tick arrives because it meets the bartype-specific qualification e.g. it is the tick that reaches N.
      Bruce DeVault
      QuantKey Trading Vendor Services
      NinjaTrader Ecosystem Vendor - QuantKey

      Comment


        #4
        Thanks for your answers. I understand all you`re saying, but I can't solve the bug.

        Here's is my code:



        Code:
        protected override void OnBarUpdate()
        {
          if(BarsInProgress == 0) {
             if (IsFirstTickOfBar && State != State.Historical) {
                 buys = 0;
               ....
             }
          }
        
          Values[0][0] = buys - sells;
        
        
        if (State == State.Historical && IsFirstTickOfBar)
            {
             buys = 0;
            ...
             }
        if(BarsInProgress == 1) {   
                buys += volume;
                ---
        } 
        
        
        }



        I see when Historical process finishes, "if(State == State.Historical)" is called and my bar buys is setted to 0. I want that last call no ocurrs, just i want buys setted to 0 when real minutes bars is called .











        Comment


          #5
          Hello franjcy,

          This logic is very similar to the BuySellVolume indicator, I would likely suggest looking at how that indicator works for more direction about accumulation buys and sells and plotting it, that uses OnMarketData to get the ask and bid values but would be able to be adapted to use a secondary series by using BarsInProgress conditions. In realtime when using OnEachTick you need to set the accumulation to the previous bar plot and then reset the values on the first tick of the bar. The buy sell volume does that by using the CurrentBar index, you can also use IsFirstTickOfBar to achieve that in realtime. Historical bars are always evaluated as first tick of bar = true.




          Comment


            #6
            Originally posted by NinjaTrader_Jesse View Post
            Hello franjcy,

            This logic is very similar to the BuySellVolume indicator, I would likely suggest looking at how that indicator works for more direction about accumulation buys and sells and plotting it, that uses OnMarketData to get the ask and bid values but would be able to be adapted to use a secondary series by using BarsInProgress conditions. In realtime when using OnEachTick you need to set the accumulation to the previous bar plot and then reset the values on the first tick of the bar. The buy sell volume does that by using the CurrentBar index, you can also use IsFirstTickOfBar to achieve that in realtime. Historical bars are always evaluated as first tick of bar = true.

            Thanks. With that option I'd make so much changes, but first I'll try to solve my problem with my actual logic.

            I didn't know "Historical bars are always evaluated as first tick of bar = true " so now i just know why thats "if" it's not working.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            580 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            335 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            102 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            554 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            552 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X