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

Multi Time Frame - Real Time vs Historical

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

    Multi Time Frame - Real Time vs Historical

    Hey all,

    I have a multi-instrument multi-time frame indicator. It is giving different results with historical vs real-time data.

    Here is some output from an example indicator reproducing the issue. In this scenario, the indicator is running at 1 minute, the strategy at 2 minutes.

    The strategy is just printing "S" + Time[0], the indicator "I" + Time[0].

    Note: everything is using CalculateOnBarClose=true
    Code:
    I 1/28/2014 2:50:00 PM
    I 1/28/2014 2:51:00 PM
    S 1/28/2014 2:50:00 PM
    I 1/28/2014 2:52:00 PM
    I 1/28/2014 2:53:00 PM
    S 1/28/2014 2:52:00 PM
    I 1/28/2014 2:54:00 PM
    I 1/28/2014 2:55:00 PM
    S 1/28/2014 2:54:00 PM
    -- begin real-time
    S 1/28/2014 2:56:00 PM
    I 1/28/2014 2:56:00 PM
    I 1/28/2014 2:57:00 PM
    S 1/28/2014 2:58:00 PM
    I 1/28/2014 2:58:00 PM
    I 1/28/2014 2:59:00 PM
    I 1/28/2014 3:00:00 PM
    S 1/28/2014 3:00:00 PM
    I 1/28/2014 3:01:00 PM
    As you can see, the order of execution here switches when transitioning from historical to realtime data.

    It seems that either the indicator is lagging 2 minutes behind when running on realtime data, or it is peeking 2 minutes ahead when running on backtested data.

    Can anyone shed some light on this and what I can do about it? (Note that in my actual strategy, the period types are different)

    EDIT:
    Additionally, and perhaps more disconcerting, the indicator's value doesn't seem to be populated either. So when I call MyIndicator()[0] from the strategy, I just get the last close of the primary sequence, not the latest value of the indicator (since it hasn't run yet). Everything is fine in historical/backtesting.
    Last edited by typedef struct; 01-28-2014, 02:23 PM.

    #2
    Here is a sample test strategy + indicator
    Attached Files

    Comment


      #3
      Thanks Typedef,

      I will look into this and report back my findings
      Cal H.NinjaTrader Customer Service

      Comment


        #4
        Thanks Cal, I appreciate it

        Comment


          #5
          Continuing to look at this, I'm starting to suspect that NT backtesting does indeed peek ahead and calculate bars in the wrong order.

          Code:
          I1 1/29/2014 12:04:00 PM 1/29/2014 11:58:00 AM
          I2 1/29/2014 12:04:00 PM 1/29/2014 11:58:00 AM
          I2 1/29/2014 12:04:00 PM 1/29/2014 11:59:00 AM
          S1 1/29/2014 12:04:00 PM 1/29/2014 11:58:00 AM
          I1 1/29/2014 12:04:00 PM 1/29/2014 12:00:00 PM
          I2 1/29/2014 12:04:00 PM 1/29/2014 12:00:00 PM
          I2 1/29/2014 12:04:00 PM 1/29/2014 12:01:00 PM
          S1 1/29/2014 12:04:00 PM 1/29/2014 12:00:00 PM
          I1 1/29/2014 12:04:00 PM 1/29/2014 12:02:00 PM
          I2 1/29/2014 12:04:00 PM 1/29/2014 12:02:00 PM
          I2 1/29/2014 12:04:00 PM 1/29/2014 12:03:00 PM
          S1 1/29/2014 12:04:00 PM 1/29/2014 12:02:00 PM
          I1 1/29/2014 12:04:00 PM 1/29/2014 12:04:00 PM
          I2 1/29/2014 12:04:00 PM 1/29/2014 12:04:00 PM
          S1 1/29/2014 12:04:00 PM 1/29/2014 12:04:00 PM
          1/29/2014 12:05:00 PM begin real time
          I2 1/29/2014 12:05:00 PM 1/29/2014 12:05:00 PM
          I1 1/29/2014 12:06:01 PM 1/29/2014 12:06:00 PM
          S1 1/29/2014 12:06:01 PM 1/29/2014 12:06:00 PM
          I2 1/29/2014 12:06:05 PM 1/29/2014 12:06:00 PM
          I2 1/29/2014 12:07:00 PM 1/29/2014 12:07:00 PM
          I1 1/29/2014 12:08:00 PM 1/29/2014 12:08:00 PM
          S1 1/29/2014 12:08:00 PM 1/29/2014 12:08:00 PM
          I2 1/29/2014 12:08:00 PM 1/29/2014 12:08:00 PM
          I2 1/29/2014 12:09:00 PM 1/29/2014 12:09:00 PM
          I1 1/29/2014 12:10:00 PM 1/29/2014 12:10:00 PM
          S1 1/29/2014 12:10:00 PM 1/29/2014 12:10:00 PM
          I2 1/29/2014 12:10:00 PM 1/29/2014 12:10:00 PM
          I2 1/29/2014 12:11:00 PM 1/29/2014 12:11:00 PM
          In this example, I'm printing the real time followed by Time[0]. "S1" is the strategy's primary (2 min) bar update, "I1" is the indicator's primary bar update, "I2" is the indicator's secondary (1 min) bar update.

          In the historical, I'm getting I2 for 11:59 before the S1 for 11:58
          In the realtime, I get the I2 for 12:08 after the S1 for 12:08

          If this is the case, it really calls into question the integrity of the whole backtesting engine.

          I hope I'm wrong here

          Comment


            #6
            Hi typedef struct,

            I wanted to offer you some assistance with your indicator to help understand what is going on.

            However, I am noticing that the prints in the sample you have provided in post #5 does not look like the prints that are in your last post.

            May we have the code that you are using to make these prints?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Sure, here you go.
              Attached Files

              Comment


                #8
                Hi typedef struct,

                I just wanted to let you know that we've had a very busy day but I have not forgotten about this.

                I will need to do some testing, and ask a few questions to clarify what should be happening here and why.

                I appreciate your patience.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Hi typedef struct,

                  Are you adding this to a 2 minute chart and then using the same instrument with 1 minute in the strategy parameters?

                  I ask because I was only able to reproduce these results with that setup.

                  This would make sense as the indicator will run twice on the secondary data series before the strategy makes its print.

                  Try moving the print in the strategy above the call to the indicator. Does make more sense this way?

                  After making the change my prints look like:
                  S1 1/30/2014 10:29:54 AM 1/29/2014 4:44:00 PM
                  I1 1/30/2014 10:29:54 AM 1/29/2014 4:44:00 PM
                  I2 1/30/2014 10:29:54 AM 1/29/2014 4:44:00 PM
                  I2 1/30/2014 10:29:54 AM 1/29/2014 4:45:00 PM
                  S1 1/30/2014 10:29:54 AM 1/29/2014 4:46:00 PM
                  I1 1/30/2014 10:29:54 AM 1/29/2014 4:46:00 PM
                  I2 1/30/2014 10:29:54 AM 1/29/2014 4:46:00 PM
                  I2 1/30/2014 10:29:54 AM 1/29/2014 4:47:00 PM
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Yes, the strategy is 2 minutes and the indicator is 1 (could be the same instrument or different).

                    This would make sense as the indicator will run twice on the secondary data series before the strategy makes its print.
                    That's correct, the indicator runs 2 times before the strategy makes its print. The problem is it is running on future data that the strategy has not seen yet.

                    In your log, these 2 lines:
                    Code:
                    I2 1/30/2014 10:29:54 AM 1/29/2014 4:44:00 PM
                    I2 1/30/2014 10:29:54 AM 1/29/2014 4:45:00 PM
                    run inside the strategy's 4:44:00 PM OnBarUpdate, so the strategy can see the indicator's 4:45:00 PM bar from it's 4:44:00 PM bar.

                    For example, the attached strategy will trade almost perfectly by looking ahead.
                    Attached Files

                    Comment


                      #11
                      Hello typedef struct,

                      This is because of the way bars are built.

                      The two minute bar that has closed in the strategy was "forming" while two of the 1 minute bars closed.

                      Historically, this may be hard to visualize as it would make more sense in real time.

                      However, this is explained visually in the help guide on Multi-Time Frame & Instruments in the section 'How Bar Data is Referenced'.
                      http://www.ninjatrader.com/support/h...nstruments.htm
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Hi Chelsea,

                        No where in that page does it describe a scenario where your primary bar sequence can see closed future secondary bars. For example, if you run the strategy I posted on 60 minute bars, the strategy will be looking at the closed value of a 1 minute bar 59 minutes in the future (and then making a trade decision on that).

                        Comment


                          #13
                          Hello typedef struct,

                          Take a look at figure 1 in the section 'How Bar Data is Referenced'.

                          Lets use the following prints:
                          S1 1/30/2014 10:29:54 AM 1/29/2014 4:44:00 PM
                          I2 1/30/2014 10:29:54 AM 1/29/2014 4:44:00 PM
                          I2 1/30/2014 10:29:54 AM 1/29/2014 4:45:00 PM

                          Imagine that the 3 minute bar below in yellow is the 4:44:00 stamp from the strategy.
                          The 4:44 and 4:45 bar in the indicator are both going to reference the 4:44 bar. Notice in figure 1 how the 3 bars in the 1 minute series (this would be the indicator secondary series) reference the older 3 minute bar below.

                          Let me know if this still does not make sense.
                          Chelsea B.NinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by pechtri, 06-22-2023, 02:31 AM
                          10 responses
                          124 views
                          0 likes
                          Last Post Leeroy_Jenkins  
                          Started by judysamnt7, 03-13-2023, 09:11 AM
                          4 responses
                          59 views
                          0 likes
                          Last Post DynamicTest  
                          Started by ScottWalsh, Yesterday, 06:52 PM
                          4 responses
                          36 views
                          0 likes
                          Last Post ScottWalsh  
                          Started by olisav57, Yesterday, 07:39 PM
                          0 responses
                          7 views
                          0 likes
                          Last Post olisav57  
                          Started by trilliantrader, Yesterday, 03:01 PM
                          2 responses
                          22 views
                          0 likes
                          Last Post helpwanted  
                          Working...
                          X