Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Ninja Trader Time Travel

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

    Ninja Trader Time Travel

    I'll freely admit I don't understand why I'm seeing what I'm seeing. I hope someine can help.

    I am backtesting a multi-instrument strategy. All trades are done on the primary instrument. Below is a piece of my log file. I will explain what is happening.

    The strategy is running on day bars and CalculateOnBarClose = true;

    1: ================= START =================
    2: OnBarUpdate: BarsInProgress 0 @ 2013-01-09 10:00:00 PM
    3: OnBarUpdate: BarsInProgress 1 @ 2013-01-10 2:00:00 PM
    4: Enter: XAU 2013-01-10 2:00:00 PM 160.55 159.84 163.61, GDX 2013-01-09 10:00:00 PM 44.48 44.65 44.36
    5: Execute: Account Value = -103.49, Transaction = -10093.5, Direction = -1, Price = 44.86, Qty = 225, Time = 2013-01-10 10:00:00 PM
    6: OnBarUpdate: BarsInProgress 0 @ 2013-01-10 10:00:00 PM

    Please note that lines 4 and 5 are wrapped.

    Line 1 is printed on entry to OnBarUpdate and shows BarsInProgress and Times[BarsInProgress][0] which should be the last bar close on the current data series.

    Line 2 and 3 same.

    Line 4 indicates the strategy has decided to go long in OnBarUpdate. The BarsInProgress = 1 but the order is being entered on series 0. The values given are series 1 symbol, Times[1[[0], Closes[1][2, 1, 0], series 0 simbol and the same data values for series 0. At this point, series 0 time is 2013-01-09 10:00:00 pm (1/9 close).

    Line 5 comes from OnExecution. Ignoring the account values, it shows
    -1 = this is a buy order
    Price = 44.86
    Qty = 225 shares
    Time 2013-01-10 10pm

    So the order was placed in the context of series 1 when the series 1 time is 1/10 2pm (1/10 close) and the order supposed was placed at the series 0 1/10 close. Fine so far.

    Until I look at the GDX chart and observe that the order price above of 44.85 is the 1/10 bar open.

    So effectively, the strategy makes a decision using the 1/10 close data, then enters at order executed at the 1/10 open. Time Travel.

    Can someone please explain what is happening here?

    Thanks,
    ++PLS

    #2
    Hi Pls2000,

    Thank you for posting.

    Would you be willing to share screen shots and possibly the order entry method you have setup in the code?
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Yes. Tell me what you want to see.

      ++PLS

      Comment


        #4
        Pls2000,

        If you could give me a screen shot or two that would explain what it is you are seeing and what you expect.

        Second, you can choose to post the code in here by exported the script and attaching it to the post.
        How to export your NinjaScript

        To attach items to the post, Click Go Advanced and then the paperclip in the top toolbar.
        Cal H.NinjaTrader Customer Service

        Comment


          #5
          I'm sending my log file, a screen **** of the chart from backtesting showing trades,and the list of executions from the same backtesting. The log file from the previous transaction is posted and annotated in a previous message. This log file is identical and the annotation still apply.

          i have replaced my entry and exit decision methods with a simpler system, but it still has the same problem.

          Lets look at the first trace.

          From the previous post, CalculateOnBarClose - true. The decision but enter long on GDX is made on the bar close of XAU at 2013-01-10 2:00:00 PM , i.e., close of 1/10 bar. GDX trades later, so I was expecting the GDX execution to happen at the1/10 extended hour close.

          OnExecution confirms this, showing that the GDX EnterLong executes at 2013-01-10 10pm.

          But if you look at the execution list, you see that the execution price was 44.86. Going to the chart shows a trade opening on 1/10, but it also shows that $44.86 is the bar open price.

          So a decision made on the 1/10 bar close results in a trade at the 1/10 open. If that actually worked I could build a pretty good system around it.

          I don't want to post the strategy so I am mailing it to [email protected] with the subject line Time Travel Trading. If you want to run it to duplicate these results, select GDX as the primrary series.

          On a different subject. I think that not having an account in backtesting and not tradking money is a very serious failing in NT. Among other things it means (1) you can't backtest the strategy your will actually run, you need two version, one which tracks money itself and one which doesn't. (2) you can't optimize based on money, since money isn't tracked. This is something that needs to fix in a near future release.
          Attached Files
          Last edited by pls2000; 09-19-2013, 01:48 PM. Reason: Add an attachment

          Comment


            #6
            Pls2000,

            I have received your .zip file and will test this on my end.

            I will report back tomorrow with my findings.
            Cal H.NinjaTrader Customer Service

            Comment


              #7
              Pls2000,

              Thank you for your patience in the testing.

              The strategy is performing the way as expected from the code.

              I believe what you are seeing from the strategy analyzer is the result of the CalculateOnBarClose.

              In a backtest this will always be true.

              Take this screenshot attached for example -

              When the Current Bar [0] closes the calculation is then done and the execution shows up on the bar being built.

              In a backtest, we only know the Open,High, Low, Close, and Volume and not necessarily what could have happened intrabar.
              Attached Files
              Cal H.NinjaTrader Customer Service

              Comment


                #8
                Please let me clear up something.

                Running with CalcualteOnBarClsoe = true, when OnBarUpdate is called, do Open{0}, Close[0], High[0], Low[0], PercentComplete, etc, represent the bar that just closed?

                When CalcualteOnBarClose = false, when OnBarUpdate is called, do these variables represent the partial bar being built, or do the represent the last complete bar? If it's the last complete bar, then how do you get OHL of the partial bar being built?

                The picture seem to indicate that Open[0] is the last complete bar and there is another bar present that is not represented in the data series. If so, Can you point me to the documentation explaining this?

                I just made a single instrument version of the strategy. The decision to enter and the EnterLong call is made at the close of one bar. The order appears at the open of the next bar, as I expected.

                Thanks,
                ++PLS

                Comment


                  #9
                  Pls2000,

                  Yes, with CalculateOnBarClose = true, this will represent the last completed bar.

                  When CalculateOnBarClose = false, the OnBarUpdate() will get called with each incoming tick and the values will represent the current bar being built but will update with new values of the High and Low. Since, the bar hasn't closed, you won't know the Close value.

                  I'm not sure what you mean another bar that is present but not represented. Could you explain further please?
                  Cal H.NinjaTrader Customer Service

                  Comment


                    #10
                    The picture you attached to your previous message shows "Bar being built" as being distinct from and following "Bar index 0". You confirm this in your message by saying when bar 0 closes, OnBarUpdate runs and the EnterLong is entered on the "bar being built". But there is a contradiction here that I'm not following.

                    I can only interpret "Bar being built" as the bar that OnBarUpdate applies to and I can only interpret "Bar index 0" as being the bar described by Open[0], etc.

                    If the bar that OnBarUpdate applies to is the same as the bar described by Open{0], then when does this picture apply? Why did you send me that picture?

                    When I run a single instrument strategy with CalcualteOnBarClose = true, an order entered in OnBarClose is executed at the open of the NEXT bar. That makes sense.

                    I suspect my problem has to do with the times of the two instrument series not being updated at the right times. Even so, I see problems in what NT is doing.

                    If I run the strategy starting from 2013-01-01 using GDX as the primary symbol, the first trade is entered on the 1/10 bar. The EnterLong is called from OnBarUpdate(1) as EnterLong(0, 100, xxx, xxx), i.e., the trade is entered for instrument 0 at the close of the 1/10 ^XAU bar at 2013-01-10 02:00PM or the close of the 1/10 bar. All as expected

                    The printout from OnExecution,
                    Execute: Account Value = -103.49, Transaction = -10093.5, Direction = -1, Price = 44.86, Qty = 225, Time = 2013-01-10 10:00:00 PM

                    says that the execution happened at 2013-01-10 10:00:00 PM or the close of the 1/10 GDX bar, as expected.

                    But, and this is the import point, if the trade executed at the 1/10 close, it should have executed at the 1/10 closing price. Instead it executed at $44.86. The 1/10 bar for GDX is
                    Date 2013-01-10 22:00
                    Open 44.86
                    High 45.59
                    Low 44.85
                    Close 45.41

                    So according to OnExecution, the order executed at the bat close, but was priced at the bar open. How can this possible be right?

                    Thanks,
                    ++PLS

                    Comment


                      #11
                      Pls2000,

                      The picture I attached is to show when COBC is set True.
                      • When COBC is set to True the CurrentBar[0] is the last completed.
                      • When COBC is set to false the CurrentBar[0] becomes the bar being built.


                      That was the difference I wanted to show. The picture was also to show that when COBC is set to false the executions will plot on the bar being built.

                      In your picture with the GDX, the actual calculation happened from the bar before, 1/09, when it closed and the plot of the execution is then placed on the next bar at the open price in this case 1/10 at $44.86
                      Cal H.NinjaTrader Customer Service

                      Comment


                        #12
                        Pls2000,

                        NT doesn't handle the time sync problem you are running into. You're going to have to deal with it in your code. If the signal generating symbol closes earlier than the trading symbol you'll need to set a variable to remember that a signal was generated and then place the order 1 bar later (in the case you have given).

                        Hope that helps,

                        Brian

                        Comment


                          #13
                          I'm coming to that conclusion. Either that of find another tool.

                          Can you explain why the execution time and execution price don't match? Why is the time the close of the bar and the price the open?

                          ++PLS

                          Comment


                            #14
                            Pls2000,

                            The Strategy analyzer will also process a strategy backtest with COBC set to true.

                            This is because we only know the Open, High, Low, Close, and Volume of the bar.

                            When the bar closes, that first tick on the next bar is also the last tick on the previous bar, it then calculates the strategy and then triggers the execution on that first tick.
                            Cal H.NinjaTrader Customer Service

                            Comment


                              #15
                              Originally posted by pls2000 View Post
                              I'm coming to that conclusion. Either that of find another tool.

                              Can you explain why the execution time and execution price don't match? Why is the time the close of the bar and the price the open?

                              ++PLS
                              It is because the bar sequence is done by time of the close of the bar. The open time is not considered. You are getting called in order of bar closes. You will find this problem in other platforms as well. You will have to make changes in your code to properly handle this otherwise your backtesting results will be useless.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              663 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              376 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              110 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              575 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              580 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X