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

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.
    Brandon H.NinjaTrader Customer Service

    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
        Brandon H.NinjaTrader Customer Service

        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
            Brandon H.NinjaTrader Customer Service

            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

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by jaybedreamin, Today, 05:56 PM
              0 responses
              3 views
              0 likes
              Last Post jaybedreamin  
              Started by DJ888, 04-16-2024, 06:09 PM
              6 responses
              18 views
              0 likes
              Last Post DJ888
              by DJ888
               
              Started by Jon17, Today, 04:33 PM
              0 responses
              1 view
              0 likes
              Last Post Jon17
              by Jon17
               
              Started by Javierw.ok, Today, 04:12 PM
              0 responses
              9 views
              0 likes
              Last Post Javierw.ok  
              Started by timmbbo, Today, 08:59 AM
              2 responses
              10 views
              0 likes
              Last Post bltdavid  
              Working...
              X