Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

New bar info arrives too early in OnBarUpdate

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

    New bar info arrives too early in OnBarUpdate

    Hello,

    I have a BarsType that counts the delta and when delta reaches a max value then a new bar is added. E.g. if max value is 500 and the delta reaches -500 or + 500 the bar is updated and a new bar starts. That works fine, without problems.

    To monitor the correct behaviour of the bar, I implemented a small indicator that also counts the delta.
    In the indicator I added a 1 tick data series and in OnBarUpdate I calculate the delta with every tick until a new bar starts.
    To find out if a new bar starts, I use a variable lastBar and remember CurrentBars[0].
    My assumption is, that when CurrentBars[0] > lastbar, then a new bar starts and I reset the delta counter.


    if ((BarsInProgress == 0) && (CurrentBars[0] > lastBar))
    {
    dbgStr = DateTime.Now + " " + Name + " FirstTickOfBar Bars[0]: " + CurrentBars[0] + " Time: " + BarsArray[1].GetTime(CurrentBars[1]) + " Bars[1]: " + CurrentBars[1] + " delta: " + delta;
    delta = 0;
    deltaMax = -999;
    deltaMin = 999;
    lastBar = CurrentBars[0];


    }

    Now it happens that in the indicator the new bar starts starts to early. The barsType has not yet added the new bar, but the indicator already got the new bar.

    In the attached screenshot you can see on the left the debug info from the barsType and on the right the debug from the indicator.
    On the left, at the tick with the volume 25 the delta is more then 500 and the new bar is added.
    On the right, the bars counter increases before the tick with the 25 volume is processed. The correct tick to increase the CurrentBars counter would be after the tick with 25 volume.

    My question is now, how can I identify the correct tick when a new bar was really created?

    #2
    Hello Armin,

    Thank you for your post.

    Can you clarify what you mean by "the barstype has not been added to the new bar"?

    Are you expecting that the logic in the indicator should be happening at the same frequency as the logic in your BarsType script?

    Comment


      #3
      Hello Gaby,

      thanks for the fast answer!

      In the barsType code there is a OnDataPoint method. In this method is either a UpdateBar or a AddBar called.
      I mean that the counter in the indicators OnBarUpdate is already increased, before the OnDataPoint calls the AddBar.

      In the screenshot from the first post, you can see an entry on the left, marked in blue. This is the tick where AddBar is called.
      There is also on the right side an entry marked in blue. This is the tick where the counter in OnBarUpdate increased.
      And this happened too early. Why can the counter increase before the bar was added?

      Comment


        #4
        Hello Armin,

        I tested this out with two simple test scripts and I'm seeing the bar update before the indicator as expected.

        I created a copy of the MinuteBarsType script that prints out the local PC time, along with the TimeToBarTime() (bar's timestamp) from within OnDataPoint().
        In the test indicator script, the local PC time and the time of the bar (Time[0]) are printed out from within OnBarUpdate().

        Once the indicator starts processing in real-time I am seeing the BarsType updating before the Indicator script.

        Code:
        BarsType Computer Time: 11/27/2024 7:16:56 AM BarsType TimeToBar: 11/27/2024 7:17:00 AM
        BarsType Computer Time: 11/27/2024 7:17:01 AM BarsType TimeToBar: 11/27/2024 7:18:00 AM
        Indicator Computer Time: 11/27/2024 7:17:01 AM, Indicator Bar Time: 11/27/2024 7:17:00 AM​
        You will need to look at your custom logic to determine why the indicator is seemingly "updating" before the barstype.
        Attached Files

        Comment


          #5
          Hi Gaby,

          thanks for your respond!

          It is difficult to explain as I'm not a native english speaker. Maybe the attached example helps. It is a copy of the original tick barstype and a simple indicator.

          The indicator writes to Output 1 with every tick that it processes. To see when the bar changes and that it can be compared with the output of the barstype it prints the bar index of main data series and bar index of additional 1-tick data series, also the volume and the close price.
          The barstype writes to Output 2. Also here with every tick that it processed it prints the bars count, the volume and the close price.

          Load the barstype TickTestBarsType with e.g. instrument ES DEC24, 2000 ticks, 2 days and then add the indicator TestTickIndicator.
          After loading go to the Output1 and scroll up to a line where the CurrentBars[0] count changes. Then go to the Output 2 and scroll up to find the corresponding tick where the new bar was added. You will see that this happens at a different tick.

          If NT processes realtime data it is more accurate, when it processes historical data it never fits.

          I would expect that it must be possible, for historical and realtime data, to get the correct tick in the indicators OnBarChange when the new bar starts.
          Otherwise it is not possible to count ticks or delta for bars in a correct way.
          Attached Files

          Comment


            #6
            Hello Armin,

            The sample script was to demonstrate that the BarsType does indeed update before the Indicator. The indicator would not be able to process any bar info before the BarsType has updated the bar to be used as the input series in the script.

            "Then go to the Output 2 and scroll up to find the corresponding tick where the new bar was added. You will see that this happens at a different tick."

            If you're adding additional logic, you'll need to debug your custom logic in order to determine why your indicators logic is determining that a bar should be created before your BarsType script actually creates that bar.

            "If NT processes realtime data it is more accurate, when it processes historical data it never fits"

            Please review the help guide page on the differences on real-time vs backtest (historical).



            A script running real-time (live brokerage account, live market simulation, Playback connection etc...) will produce different results than the performance results generated during a backtest.

            ​When in historical data, only the Open, High, Low, and Close will be available and there will be no intra-bar data. This means actions cannot happen intra-bar, fills cannot happen intra-bar. All prices and actions come from and occur when the bar closes as this is all the information that is known.

            Because of this, OnBarUpdate will only update 'On bar close' as it does not have the intra-bar information necessary for 'On price change' or 'On each tick'.

            You would need to use TickReplay in order to have logic processed intrabar historically. Tick Replay would be used to have the logic process OnEachTick or OnPriceChange with historical data.​

            Comment


              #7
              Hello Gaby,

              thanks, the things become clearer.

              Using TickReplay is not possible, I use indicators that do not support TickReplay.

              In the output of the TickTestIndicator, that I have attached in previous post, I can see that in the added 1-tick dataseries every single tick is processed.
              Is it somehow possible in an indicator, to find out which tick of the 1-tick dataseries corresponds with the last tick of the main dataseries (2000-tick dataseries).
              If I understood you correctly the last tick of a bar is correct and defined, also in historical data.

              Comment


                #8
                NinjaTrader_Gaby: Is it possible in an indicator with a main dataseries, e.g. 2000-ticks and an additonal 1-tick dataseries, to find out which tick of the 1-tick dataseries corresponds with the close tick of the main 2000-tick dataseries?

                Thanks!
                Armin

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                556 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                324 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                101 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                545 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                547 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X