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

Syncing 2 datastreams in Historic and Realtime

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

    Syncing 2 datastreams in Historic and Realtime

    Hi, I just realized why my 2 datastream indicator is producing different results in history vs. realtime.

    One datastream has delayed data, the other is realtime.

    When I refer to a bar of the 2nd datastream (with Closes[1][1]) in State.Historical then it refers to the bar that lines up with the bar on the 1st datastream. However, when the state changes to State.Realtime I'm now referring to an entirely different bar of the 2nd datastream.

    How do I keep this consistent, so that e.g. Closes[1][1] refers to the same bar in both historical and realtime?

    Thanks

    #2
    Hello bubblegum,

    You should see more bars being generated for one series in realtime if you have delayed data for one instrument. When processing historical data, that will go through all of the data for both instruments and would be processed in a specific way. Once you enter realtime, that no longer applies because it would now depend on the market to generate new bars. If one instrument is delayed, it is not generating bars as fast as the other so it would not directly relate to a different instrument how you have seen.

    Even with live data for both instruments it would not always be exactly consistent because of the market. If one instrument stops trading before the other or has low volume, you may see areas where one series is behind until more data is present to build a bar.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      I'm looking for some example code of how I would deal with is this. I'm assuming this has been asked and solved many times before.

      I know there are going to be issues with real-time synchronization, but I still want to be able to access the data in both datastreams that 'line up'. I'm using e.g. Closes[1][1] that works historically, so what would I use in real-time to equivalent data from the 2nd datastream?

      Thanks

      Comment


        #4
        Hello bubblegum,

        I am unaware of an existing sample of this concept, this is not something which is commonly asked.

        Using Closes[1][1] in historical only lines up because there is data present for both series and that is the order both series are processed. This just represents the second series 1 BarsAgo value. This does not apply in realtime as each instrument is unique. With the delayed instrument that would not be expected to line up in most cases and using Closes[1][0] would represent the stationay last closed bar. To find the bar where they lined up last would require you to use some logic.

        If in your example the Primary instrument is the one with live data, then Closes[1][0] would represent the secondary last closed bar. You could use Times[1][0] to know the last closed bars time to do other calculations based on times. One way to find a bar is by its time, you could use GetBarIdxByTime for that:


        With a bars ID you can use the GetValueAt methods to get values from a specific bar by its ID:



        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hi, it's actually the other way around. Primary is delayed, Secondary is live. I know the time of the primary bar, so - assuming GetBarIdxByTime doesn't work on the Secondary bars - I guess I would need to loop through the Secondary bars to find that time (using Times[1][x]). Is that the correct way to do this?

          Thanks

          Comment


            #6
            Hello bubblegum,

            There is not a specific correct way to do this as what you are asking for is custom. Depending on what your overall goal is will determine how you want to approach the problem.

            If your series are the opposite of what I last said, you would instead reverse the ideas there. Times[0][0] would then represent the primary so that could be used to get the primary bars time. You could then use the GetBar method from the secondary series with a time:



            If this was executed within the context of BarsInProgress 1, Bars would equate to the secondary series:

            Code:
            if(BarsInProgress == 1)
            {
               Bars // is now secondary bar series, you can use any Bars methods 
            }
            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Thank you. I didn't know GetBar() would access different datastreams based on BarsInProgress.

              Comment


                #8
                Hello bubblegum,

                Correct, the easiest way to understand variables your multi series script includes would be to use prints in OnBarUpdate, for example this print is useful to show what is being processed while using the Bars object:

                Code:
                Print(BarsInProgress + " " + Bars.Instrument.FullName);
                I look forward to being of further assistance.
                JesseNinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by WeyldFalcon, 08-07-2020, 06:13 AM
                10 responses
                1,413 views
                0 likes
                Last Post Traderontheroad  
                Started by firefoxforum12, Today, 08:53 PM
                0 responses
                8 views
                0 likes
                Last Post firefoxforum12  
                Started by stafe, Today, 08:34 PM
                0 responses
                10 views
                0 likes
                Last Post stafe
                by stafe
                 
                Started by sastrades, 01-31-2024, 10:19 PM
                11 responses
                168 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Started by kmunroe478, Today, 05:39 PM
                0 responses
                6 views
                0 likes
                Last Post kmunroe478  
                Working...
                X