Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

understanding OnBarUpdate()

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

    understanding OnBarUpdate()

    Hi,

    I am switching from TradeStation (EasyLanguage) to NT. One thing I'd like to understand is how OnBarUpdate() works.

    When I set "CalculateOnBarClose = true;" I was under the impression that the strategy code segment "OnBarUpdate()" would execute AFTER the bar closed. What appears to happen in reality is that the strategy executes only when a new tick arrives that will go into a new bar.

    Please clearify when that code segment is executed:
    a) after the last tick of the bar arrived
    b) when the first tick for the next bar arrived
    c) after the time for a time based bar has ended

    Is there a difference for tick-based (i.e., 150 tick bars) or volume based (i.e., 1000 shares per bar) bars when compared to time based bars as it is already known after the tick arrived whether any more ticks can go into the bar?

    One consequence is that the strategy does NOT execute after the very last bar in the chart. If I run a strategy while the exchange is closed (=> no new ticks are coming in) I will not see any results from calculations that should run on the close of the last bar of the previous session.

    Is there a way to force the execution of the "OnBarUpdate()" section for the last bar in the chart after the session has ended?

    Thanks,
    NutFlush

    #2
    NutFlush, that would be expected and answer 2 you listed - everything is event based in NT, so even if time or volume wise your bar would be complete the bar is not considered closed if the next tick is not seen.

    To see the OnBarUpdate() getting called on your last (open) bar on the chart, you would need to run it with CalculateOnBarClose set to false.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Bertrand View Post
      NutFlush, that would be expected and answer 2 you listed - everything is event based in NT, so even if time or volume wise your bar would be complete the bar is not considered closed if the next tick is not seen.
      Thanks for the clarification! So independently of the bar type (time, tick, volume) only the event of the "next" tick can trigger the "OnBarUpdate()", right?

      Am I correct that with:
      a) "CalculateOnBarClose = TRUE;" I am still "looking" at the bar that just finished (i.e. Close[0] gives me the closing price of the last completed bar).
      b) "CalculateOnBarClose = FALSE;" I am already "looking" at the new bar that is just beginning to form (i.e. Close[0] gives me the closing price of the currently forming bar).

      Thanks!

      Comment


        #4
        Yes, correct it depends on your CalculateOnBarClose setting. On 'true' you look at the last completed bar, while on false it would be the last seen tick for the current bar.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          OK, thanks!

          Please allow me to follow up with another question:
          Let's assume I run a strategy on a 1-minute chart of symbol "A". The strategy adds symbol "B" also on the 1-minute scale. CalculateOnBarClose is set to TRUE.
          What happens when the first tick for the 09:06 candle of "B" arrives, but the was NO first tick in the 09:06 bar yet for symbol "A"?

          Will Times[0][0].Minute (symbol "A") be equal to Times[1][0].Minute (symbol "B"), both giving me "5" of "09:05"?

          If yes, this would mean that the first tick in any of the symbols that are included in the strategy will end a same scale bar of the other symbols, right?

          Thanks!

          Comment


            #6
            NutFlush, the OnBarUpdate() for each series is called independently, so in this case if no closing tick yet was seen for Symbol B, that's what the Times timestamp would reflect for it in realtime.
            BertrandNinjaTrader Customer Service

            Comment


              #7
              I'm sorry, but I need to understand this correctly.

              If I have 2 symbols with the same time interval (i.e., "A" & "B" both on the 1 minute scale) how can I make sure that the 2 Bars objects are always "lined up" so that by using the same reference index I always get the bar for the identical time period? CalculateOnBarClose is set to TRUE!

              In other words: Can I be certain that Times[0][0].Minute (symbol "A") will always be equal to Times[1][0].Minute (symbol "B")?

              In both cases I use the index value of "0" to adress the most recent completed bar. I am aware that the OnBarUpdate() method will be called for each symbol individually. So if the first tick of the new bar for "B" arrives this will finish the bar at 09:05 and then return "5" as Times[1][0].Minute. But since the first tick for symbol "A" has not yet arrived I am not sure what Times[0][0].Minute will return!

              I also use the index value of "0", so I would like to get information on the bar that represent the same time span as in symbol "B" where the index "0" adressed the bar that finished at 09:05. I would NOT want to get information on symbol A's bar that finished at 09:04.

              So how can I make sure that the 2 Bars objects are always "lined up" so that by using the same reference index (i.e., [0]) I always get the bar for the identical time period?

              Thanks!

              Comment


                #8
                Your understanding is correct that you cannot be sure that those indices are lined up, as the series do not have to update just because the time interval would dictate so, especially if you're working on instruments with differing liquidity you could see non alignments in the OnBarUpdate() calls, as the closing tick / open tick of new bar would not yet be in for one or more symbols.

                For the series individually the 0 index will always refer to the last completed bar with CalculateOnBarClose == true and to the last tick update if this property is false (the last completed then would move to an index of 1).

                For ideas on sync methods to line up as you desired you can for example check into Kdoren's contributions in our sharing section (spread / index indicators posted) - http://www.ninjatrader.com/support/f...=spread&desc=1
                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  Great! That made it clear for me. Thanks for the link!

                  Comment


                    #10
                    Originally posted by NinjaTrader_Bertrand View Post
                    Your understanding is correct that you cannot be sure that those indices are lined up, as the series do not have to update just because the time interval would dictate so, especially if you're working on instruments with differing liquidity you could see non alignments in the OnBarUpdate() calls, as the closing tick / open tick of new bar would not yet be in for one or more symbols.

                    For the series individually the 0 index will always refer to the last completed bar with CalculateOnBarClose == true and to the last tick update if this property is false (the last completed then would move to an index of 1).
                    I made some tests and it appears that what you wrote above is not correct. But I might be doing something wrong so maybe you can confirm or reject my observation:

                    I have added a second symbol through the strategy. The OnBarUpdate() looks like this:
                    Code:
                     protected override void OnBarUpdate()
                            {
                                    
                                if (BarsInProgress == 0) // New bar for MAIN SYMBOL
                                {
                                    DateTime MyBarTimeStampA = Times[0][0];
                                    Print("BarUpdate for MAIN symbol on: " + MyBarTimeStampA.ToString());
                                }
                                if (BarsInProgress == 1) // New bar for SECOND SYMBOL
                                {
                                    DateTime MyBarTimeStampB = Times[1][0];
                                    Print("BarUpdate for SECOND symbol on: " + MyBarTimeStampB.ToString());
                                }
                                if (Times[0][0] == Times[1][0]) // both symbols show the same time stamp
                                {
                                    // this section runs TWICE for each bar
                                    Print("Identical time stamp: " + Times[0][0].ToString() + " found by symbol #: " + BarsInProgress.ToString());
                                }
                            }
                    The output then looks like this:
                    Code:
                    BarUpdate for MAIN symbol on: 15.07.2013 21:56:00
                    Identical time stamp: 15.07.2013 21:56:00 found by symbol #: 0
                    BarUpdate for SECOND symbol on: 15.07.2013 21:56:00
                    Identical time stamp: 15.07.2013 21:56:00 found by symbol #: 1
                    BarUpdate for MAIN symbol on: 15.07.2013 21:57:00
                    Identical time stamp: 15.07.2013 21:57:00 found by symbol #: 0
                    BarUpdate for SECOND symbol on: 15.07.2013 21:57:00
                    Identical time stamp: 15.07.2013 21:57:00 found by symbol #: 1
                    As you can see the part of the codes that prints the identical time runs TWICE. What's worse: it already runs BEFORE the part of the code that deals with an individual symbol was processed for ALL symbols.

                    It appears that different to what I understood from your explanation (see quote) the first tick in ANY symbol ends the current bar in a way so that the bars for the other symbols are also ended. The index reference appears to stay aligned, meaning Times[x][0] is identical for all symbols.

                    Can you confirm that behaviour? Any more insights about what's going on "behind the scenes"?

                    Cheers,
                    NutFlush

                    Comment


                      #11
                      NutFlush, that print was for backtesting and not for realtime processing, correct?

                      You also don't have to see an issue, as if you compare 2 liquid instruments it simply may not skip a bar update as both would have updated when ticks were seen.
                      BertrandNinjaTrader Customer Service

                      Comment


                        #12
                        Originally posted by NutFlush View Post
                        if (Times[0][0] == Times[1][0]) // both symbols show the same time stamp
                        {
                        // this section runs TWICE for each bar
                        Print("Identical time stamp: " + Times[0][0].ToString() + " found by symbol #: " + BarsInProgress.ToString());
                        }
                        It should and would: the way you have written it means that that block will be run for every tick that comes through. You have 2 barseries. Ergo, 2 ticks will come in per update.

                        Comment


                          #13
                          Originally posted by NinjaTrader_Bertrand View Post
                          NutFlush, that print was for backtesting and not for realtime processing, correct?

                          You also don't have to see an issue, as if you compare 2 liquid instruments it simply may not skip a bar update as both would have updated when ticks were seen.
                          Yes, backtesting appears to behave differently from live data:

                          Code:
                          === historical data === section runs TWICE
                          BarUpdate for MAIN symbol on: 16.07.2013 17:45:00 detected at: 16.07.2013 17:45:30
                          Time stamp: 16.07.2013 17:45:00 found by symbol #: 0 detected at: 16.07.2013 17:45:30
                          BarUpdate for SECOND symbol on: 16.07.2013 17:45:00 detected at: 16.07.2013 17:45:30
                          Time stamp: 16.07.2013 17:45:00 found by symbol #: 1 detected at: 16.07.2013 17:45:30
                          
                          === live data === section runs ONCE
                          BarUpdate for SECOND symbol on: 16.07.2013 17:46:00 detected at: 16.07.2013 17:46:00
                          BarUpdate for MAIN symbol on: 16.07.2013 17:46:00 detected at: 16.07.2013 17:46:02
                          Time stamp: 16.07.2013 17:46:00 found by symbol #: 0 detected at: 16.07.2013 17:46:02
                          
                          BarUpdate for SECOND symbol on: 16.07.2013 17:47:00 detected at: 16.07.2013 17:47:00
                          BarUpdate for MAIN symbol on: 16.07.2013 17:47:00 detected at: 16.07.2013 17:47:00
                          Time stamp: 16.07.2013 17:47:00 found by symbol #: 0 detected at: 16.07.2013 17:47:00
                          
                          BarUpdate for SECOND symbol on: 16.07.2013 17:48:00 detected at: 16.07.2013 17:48:00
                          BarUpdate for MAIN symbol on: 16.07.2013 17:48:00 detected at: 16.07.2013 17:48:05
                          Time stamp: 16.07.2013 17:48:00 found by symbol #: 0 detected at: 16.07.2013 17:48:05
                          @koganam
                          It should and would: the way you have written it means that that block will be run for every tick that comes through. You have 2 barseries. Ergo, 2 ticks will come in per update.
                          As you can see this is not a code issue as the expression "if (Times[0][0] == Times[1][0])" is TRUE only once on live data, but twice on historical data. The OnBarUpdate() section runs twice, but that expression acts as a filter to wait until both barseries are in sync (which happens when both symbols had their first tick for the next bar).

                          Cheers,
                          NutFlush

                          Comment


                            #14
                            NutFlush, I would expect the outcome since for backtesting the pointers are synched. In realtime it would be down to what series / event triggered first, it would be purely event based.
                            BertrandNinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Barry Milan, Yesterday, 10:35 PM
                            5 responses
                            16 views
                            0 likes
                            Last Post NinjaTrader_Manfred  
                            Started by DanielSanMartin, Yesterday, 02:37 PM
                            2 responses
                            13 views
                            0 likes
                            Last Post DanielSanMartin  
                            Started by DJ888, 04-16-2024, 06:09 PM
                            4 responses
                            13 views
                            0 likes
                            Last Post DJ888
                            by DJ888
                             
                            Started by terofs, Today, 04:18 PM
                            0 responses
                            12 views
                            0 likes
                            Last Post terofs
                            by terofs
                             
                            Started by nandhumca, Today, 03:41 PM
                            0 responses
                            8 views
                            0 likes
                            Last Post nandhumca  
                            Working...
                            X