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

Identifying Close of Bar when CalculateOnBarClose = true

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

    Identifying Close of Bar when CalculateOnBarClose = true

    How can one identify the close of a bar when COBC is true, given that Update() can cause OnBarUpdate() to be called in the middle of a bar ?

    #2
    Hi splat,

    OnBarUpdate() is only called at the close of a bar (not during) when COBC = true.

    A bar closing event is the same as the next bar opening, so you can monitor for FirstTickOfBar to check for this when COBC = false.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Hi Ryan,

      Please look at the NinjaTrader help topic for Update(): "This method will force the OnBarUpdate() method to be called...". This suggests to me that if I call Update(), it should cause OnBarUpdate() to be called, regardless of whether the current bar is complete (and this seems like a useful feature to me). Did I misunderstand ?

      Comment


        #4
        It depends where and how you're calling Update(). It has a specific purpose for exposing indicator values from other indicators.

        For this purpose, you want to remove any COBC statements from called indicators. Your calling indicator will still obey COBC rules.
        Last edited by NinjaTrader_RyanM1; 04-20-2011, 03:30 PM.
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          I should explain what I am trying to do. I want to write a multi-timeframe strategy that calls indicators across a wide range of timeframes. I would like the small-scale indicators to be very timely (e.g. to the tick), but do not want to calculate all the large-scale indicators (e.g. EMA(13) on 60-minute bars) tick by tick. At the same time, I do not want to just leave the large-scale indicators on COBC, because then they will not be updated for a long time (e.g. 60 minutes). I would like to have each large-scale indicator on COBC, but use a bar-close event on a faster timeframe to trigger an update on the large-scale indicator, via Update(). In this case, the resulting OnBarUpdate call on the large-scale indicator would (by design) typically occur in the middle of a bar, so I am wondering how to distinguish this from an actual bar-close on the large-scale ?

          Comment


            #6
            You can't combine COBC logic like this, where your called indicators have one COBC setting and your calling indicator has another. From the called indicators you should remove any COBC assignment in Initialize() and assign only once(or only through the GUI) for the calling indicator.

            You can combine COBC = true with COBC = false, but have to code for this, following the approach in this sample:


            With this approach you need COBC = false for the calling indicator, and can monitor for bar closing events using FirstTickOfBar.
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              Thank you for the sample, which I have already studied. It uses COBC false throughout.
              I do not actually plan to mix COBC false and true, rather I am thinking of using COBC true throughout (set from the top-level, as you suggest).
              It seems to me that I can use COBC true, and still get tick-by-tick events by adding a 1-tick bar series to the strategy (note that this would be COBC true, where each bar is 1 tick).
              I do not see anything special about the use of a 1-tick bar series here either. I think the following setup would be essentially the same: just a 60-minute bar series with a 1-minute bar series, both COBC true, but sometimes using a 1-minute bar-close event to Update() an indicator (e.g. EMA(13)) on the 60-minute data series (e.g. the 1-minute event might update the 60-minute indicator at 25%, 50%, 75% and perhaps 100% completion of a 60-minute bar).
              BTW, do you think I need to be very concerned about memory consumption if putting 1-tick bars on a strategy ? It seems to me that it should not be a problem if the data-series is only keeping a short history, and I see no particular reason to keep a long history of the 1-tick bars (they are mainly for timing).
              So my question remains about how, in OnBarUpdate, to tell if the bar has closed...
              Last edited by splat; 04-21-2011, 10:06 AM.

              Comment


                #8
                If you use COBC = true then you're only processing logic once per bar. If you want to know when the primary series bar closed, filter using BarsInProgress.

                if (BarsInProgress == 0)
                {
                Print("A Bar just closed");
                }
                Ryan M.NinjaTrader Customer Service

                Comment


                  #9
                  Okay, let me see if I understand.

                  If I call Update() from a 1-minute bar-close, 25% of the way into a 60-minute bar, then the 60-minute indicator will get its OnBarUpdate called, but BarsInProgress will identify the 1-minute series as the cause. Whereas, when the 60-minute bar actually closes, the 60-minute indicator will get its OnBarUpdate called, and BarsInProgress will identify the 60-minute series as the cause. Hence I can tell when the 60-minute bar has closed by referring to BarsInProgress.

                  That raises a further question for me: if the 60-minute indicator just uses the 60-minute data series (being the only data-series it has), but its Update() method is called from a strategy that also has the 1-minute series, triggered by a 1-minute bar close, what does the 60-minute indicator see for BarsInProgress ? Especially if the 1-minute series is the primary data series for the strategy (BarsInProgress == 0) and the 60-minute data series is the primary data series for the 60-minute indicator (BarsInProgress == 0).

                  Maybe the answer is that the distinction has no meaning for the 60-minute indicator, and is only meaningful to the strategy (where BarsInProgress will discriminate it as you suggest).

                  Comment


                    #10
                    It may help to look into the expected usage of Update(), outlined in this reference sample:


                    It's not intended to be used within one script to change the timing of OBU calls within this script. For this, you'll want to work with COBC = false, and custom code when you want calculations made, maybe through a timer object.

                    The bar referencing model for multiseries scripts is detailed here:


                    It varies whether you're accessing real time with COBC = false or historical.
                    Ryan M.NinjaTrader Customer Service

                    Comment


                      #11
                      Thank you, Ryan.

                      The sample is interesting, and similar to the example in the Update() help topic (which I wish was a bit more informative).
                      I have already carefully read the help section you mentioned.

                      I have difficulty understanding the exact reason for the use of Update() in the sample. I had been thinking that COBC false means each indicator will get updated on every tick, regardless of whether its value is being used. From careful reading of the Update() help topic, it seems that sometimes NinjaTrader skips calling OnBarUpdate in certain circumstances (e.g. backtesting), if the indicator value is not being used. Does this laziness only occur in backtesting, or is NinjaTrader clever enough to do it generally ?

                      If I have an indicator, with COBC false, on a chart, obviously it must get updated every tick, so it can be drawn properly on the chart. But suppose the indicator is not on any chart, only in a strategy, and the strategy never calls it, will it get updated ? If the strategy only calls it occasionally, will it be updated on every tick, or only when needed ?

                      I am just trying to understand how to write a multi-timeframe (and multi-instrument) strategy that can take account of timely data without using tons of CPU time calculating things on every tick when most of them only need calculating every minute or two. Taking the example of an EMA(13) on 60-minute bars, I seem to get two unsatisfactory choices:
                      (i) COBC true: the indicator is only updated once every 60 minutes, which makes it not timely enough (although an update every 5 minutes would be enough).
                      (ii) COBC false: the indicator is updated on every tick, even though it uses 60-minute bars, and one update per 5 minutes would be enough. With a large number of timeframes, and multiple instruments, there is a serious risk that the strategy cannot keep up in an active market.. Furthermore, backtesting will behave differently.

                      Comment


                        #12
                        Update() is the mechanism for getting the most up to date value of a variable from another indicator.

                        The model is:
                        Assign a private variable a value in OBU.
                        This private variable is linked to a public one.

                        You don't have access to the private variable from another script. You're accessing the public one, which is not assigned during OnBarUpdate. Placing Update() in the get accessor for this variable ensures you're using the most recent value of the private variable.

                        Strategies follow the same COBC rules and best practices as indicators. If you reference an indicator in a strategy, its value is updated tick by tick or bar close, depending on the strategies COBC settings.
                        Ryan M.NinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Balage0922, Today, 07:38 AM
                        0 responses
                        1 view
                        0 likes
                        Last Post Balage0922  
                        Started by JoMoon2024, Today, 06:56 AM
                        0 responses
                        6 views
                        0 likes
                        Last Post JoMoon2024  
                        Started by Haiasi, 04-25-2024, 06:53 PM
                        2 responses
                        19 views
                        0 likes
                        Last Post Massinisa  
                        Started by Creamers, Today, 05:32 AM
                        0 responses
                        6 views
                        0 likes
                        Last Post Creamers  
                        Started by Segwin, 05-07-2018, 02:15 PM
                        12 responses
                        1,786 views
                        0 likes
                        Last Post Leafcutter  
                        Working...
                        X