Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Processing order of multiple bars

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

    Processing order of multiple bars

    I'm developing both an indicator and a strategy that will be accessing multiple bars in addition to the primary. For example, the two bars would be a bid bar and an ask bar, both on the same time periods. I want to access both at the same time but need to be sure both have been updated before I do that. I know that BarsInProgress tells me in OnBarUpdate() which bar has been updated. But can I assume that the data coming into OnBarUpdate() do so in order? For example, if I created the bars such that the bid bar is BarsInProgress == 1 and the ask bar is BarsInProgress == 2, can I assume correctly that I can access both that bid and ask bars when BarsInProgress == 2? For example, I know I can access Closes[1][0] and Closes[2][0], but will both represent the same tick time when BarsInProgress == 2?

    #2
    Hello dweems,

    Thanks for your post.

    You could print out the BarsInProgress value and Time in your script to see the order that the data series in your script is processing.

    Let's assume that we have two data series, the primary series and an additional secondary series. Historical bars are processed according to their timestamps with the primary bars first, followed by the secondary, which is NOT guaranteed to be the same sequence that these events occurred in real-time. In circumstances where multiple bars share the same exact timestamps, your primary bars series will always be processed first, followed by the secondary bars series (regardless of the period value used).

    If you are wanting to access data from a specific added series, you would need to specify that added series. For example, if you want to access Close price data from the first added series, you would need to call Closes[1][0]. If you want to access Close price data from a second added series, you would need to call Closes[2][0].

    Please review this help guide page to get a thorough understanding of the data processing sequence when working with multi-timeframe objects: https://ninjatrader.com/support/help...meFrameObjects

    Let me know if I may assist further.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      I have to admit I am quite confused. I have done what you recommended and as OnBarUpdate() is entered, I'm printing out BarsInProgress followed by the datetime. This is what I'm seeing.

      0 - 11/22/2022 6:32:00 PM
      1 - 11/22/2022 6:32:00 PM
      0 - 11/22/2022 6:36:00 PM
      1 - 11/22/2022 6:36:00 PM
      0 - 11/22/2022 6:40:00 PM
      1 - 11/22/2022 6:40:00 PM
      0 - 11/22/2022 6:44:00 PM
      1 - 11/22/2022 6:44:00 PM
      0 - 11/22/2022 6:48:00 PM
      1 - 11/22/2022 6:48:00 PM
      0 - 11/22/2022 6:52:00 PM
      1 - 11/22/2022 6:52:00 PM
      0 - 11/22/2022 6:56:00 PM
      1 - 11/22/2022 6:56:00 PM
      2 - 11/22/2022 6:40:00 PM
      3 - 11/22/2022 6:40:00 PM​

      In this case BarsInProgress 0 & 1 correspond to periods of 4 minutes and BarsInProgress 2 & 3 are for 20 minutes. So what I'm seeing is that I cannot depend on the data to appear in sequence with the datetime if the bars periods differ. Is that correct? So there's no way to get BarsInProgress to give me all 4 data at the same timestamp when they are the same? Why is that and is there a work around so that, for example, all 4 data series for 6:40:00 PM load sequentially before the data at 6:44:00 PM? I want to evaluate the data for the 4 minute bars between the 20 minute bars. But this seems to say there is no correlation with respect to time between bars of different periods. It almost sees like I will have to create my own 20 minute bars from the 4 minute bars rather than getting it directly from your data series. Is that a correct assessment?

      Comment


        #4
        Hello dweems,

        Thanks for your note.

        OnBarUpdate() will update at the close of each bar for the period of the data series that the script is using when Calculate.OnBarClose is used. For example, if you are processing 2 data series in your script with a period of 4 minutes and 2 data series with a period of 20 minutes, the 4-minute data series will process OnBarUpdate() every 4 minutes that pass. OnBarUpdate() will process every 20 minutes for the 2 data series with periods of 20 minutes.

        In your post, we can see this occurring. We see BarsInProgress 0 and 1 process logic every 4 minutes.

        0 - 11/22/2022 6:32:00 PM
        1 - 11/22/2022 6:32:00 PM

        0 - 11/22/2022 6:36:00 PM
        1 - 11/22/2022 6:36:00 PM

        BarsInProgress 2 and 3 will process data when the 20-minute bar closes.

        2 - 11/22/2022 6:40:00 PM
        3 - 11/22/2022 6:40:00 PM​

        ​The BarsInProgress values would only have the same timestamps if the bar for all 4 data series were to close at the same time.

        See the attached screenshot demonstrating this concept.

        I have created a test script that adds one 4-minute data series and one 20-minute data series to an indicator with AddDataSeries(). In OnBarUpdate() we print out the BarsInProgress value and the Time. When running the script on a 4-minute chart, we can see that the BIP 0, 1, and 2 will process at the same timestamp when the data series bars close at the same time. In the screenshot, we see BarsInProgress 0, 1, and 2 all close at 1:00 PM and at 1:20 PM.

        I have attached this example script used to test this.

        See the help guide documentation below for more information.
        OnBarUpdate(): https://ninjatrader.com/support/help...nbarupdate.htm
        BarsInProgress: https://ninjatrader.com/support/help...ess.htm​
        Calculate: https://ninjatrader.com/support/help.../calculate.htm

        Let me know if I may assist further.
        Attached Files
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #5
          Fantastic! But I don't see the attached script. Can you provide that so I can see what I'm doing wrong? That would be awesome!

          Comment


            #6
            Hello dweems,

            Thanks for your note.

            The BIPTest example script could be found under the screenshot that was shared in post # 4.

            That said, I am also attaching the script to this post as well.

            Please let me know if I may assist further.
            Attached Files
            <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

            Comment


              #7
              I think I figured out what my problem is. It's due to the fact OnBarUpdate() runs non-concurrently. I need to do more processing when the slower bars arrive than with the fast bars. So while I'm processing one of the slow bars, other fast bars are arriving before it finishes. Therefore I'm out of sequence.

              Comment


                #8
                Originally posted by dweems View Post
                I think I figured out what my problem is. It's due to the fact OnBarUpdate() runs non-concurrently. I need to do more processing when the slower bars arrive than with the fast bars. So while I'm processing one of the slow bars, other fast bars are arriving before it finishes. Therefore I'm out of sequence.
                I struggled with exactly this issue for multi instrument development. How did you manage to solve it? I ended up using on price change and isfirsttickofbar .. but i thought this is because of multi instrument.

                question to devs:
                - if same instrument, multiple dataseries: for bars that share the same time stamps, does ninja move on to barsinprogress 2 after completely processing the code of the barsinprogress 1? Or is there a possibility if there is a delay due to a longer/ more complex code in 1 , barsinprogress 2 processing completion might preceded 1?

                Comment


                  #9
                  Originally posted by madb123 View Post

                  I struggled with exactly this issue for multi instrument development. How did you manage to solve it? I ended up using on price change and isfirsttickofbar .. but i thought this is because of multi instrument.
                  The issue here is that you have to cycle at least twice through OnBarUpdate() and evaluate the time stamps each time until they are both the same. Most of the time I get them equivalent on the second iteration but I've seen some take as many as 10 times. My data is coming from Interactive Brokers, so I don't know if it's the same behavior for other data providers.

                  Comment


                    #10
                    Originally posted by dweems View Post

                    The issue here is that you have to cycle at least twice through OnBarUpdate() and evaluate the time stamps each time until they are both the same. Most of the time I get them equivalent on the second iteration but I've seen some take as many as 10 times. My data is coming from Interactive Brokers, so I don't know if it's the same behavior for other data providers.
                    thanks! good solution actually.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    582 views
                    0 likes
                    Last Post Geovanny Suaza  
                    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                    0 responses
                    338 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by Mindset, 02-09-2026, 11:44 AM
                    0 responses
                    103 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