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 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.


    JesseNinjaTrader Customer Service

    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.




          JesseNinjaTrader Customer Service

          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 fx.practic, 10-15-2013, 12:53 AM
            5 responses
            5,406 views
            0 likes
            Last Post Bidder
            by Bidder
             
            Started by Shai Samuel, 07-02-2022, 02:46 PM
            4 responses
            98 views
            0 likes
            Last Post Bidder
            by Bidder
             
            Started by DJ888, Yesterday, 10:57 PM
            0 responses
            8 views
            0 likes
            Last Post DJ888
            by DJ888
             
            Started by MacDad, 02-25-2024, 11:48 PM
            7 responses
            160 views
            0 likes
            Last Post loganjarosz123  
            Started by Belfortbucks, Yesterday, 09:29 PM
            0 responses
            9 views
            0 likes
            Last Post Belfortbucks  
            Working...
            X