Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

BarsSinceEntry() real-time vs. Historical

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

    BarsSinceEntry() real-time vs. Historical

    I get different result from BarSinceEntry() in real-time vs. historical data. In real-time I get the entrybar with BarSinceEntry() == 1 / on historical data with BarSinceEntry() == 0 ( CalculateOnBarClose is true).

    My goal is to set profit target to BE +/- 1 if entry bar is not moving (close equal or below/above close of signal bar).

    My test code for illustration (OnBarUpdate Method):

    ...
    int barSinceEntryLimit = 0;
    if (!Historical)
    barSinceEntryLimit = 1;
    if (isLongTrade && BarsSinceEntry() == barSinceEntryLimit && ( Close[0] <= Close[1] ))
    beExitLong = true;
    if (isShortTrade && BarsSinceEntry() == barSinceEntryLimit && ( Close[0] >= Close[1] ))
    beExitShort = true;
    ...

    Can someone please explain why BarsSinceEntry() works different on historical / real-time data?

    #2
    It depends on when your entry order was actually submitted/filled. Please see this article about the discrepancies between real-time and historical: http://www.ninjatrader-support.com/H...sBacktest.html
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Dont understand your answer. Link to does not help at all.

      Did some debugging to understand what is going on.

      From my point of view BarsSinceEntry() is not working correct.

      Example:

      Signal bar is the bar ending 10:34 (EntryLong is called on barclose of this bar).

      In backtest order if filled at 10:34:00 / in market replay order is filled at 10:34:00 / in real time order is filled at 10:34:01.

      So entry bar is 10:38 bar in my understanding?

      On bar close of 10:38 bar next OnBarUpdate is executed. At this time in backtest BarsSinceEntry() is 0 / in market replay and in real-time BarsSinceEntry() is 1 (real-time not tested / debugged but it would explain error I got yesterday) .

      In my opinion BarsSinceEntry() should be 0 on 10:38 bar. But I understand that at processing of OnBarUpdate the 10:42 bar has already started (OnBarUpdate is fired on first tick of 10:42 bar?). So its ok as well if you think that it should be 1. But then it should also be 1 in backtest!!

      Comment


        #4
        BarsSinceEntry historical and realtime is the same value: the numbers of bars between when an execution occurred and "CurrentBar". Please make sure you have CalculateOnBarClose=True on your real time tests.

        Sample: Is the execution sits on the 10:34 bar of your 1 minute series (you could check on chart) and OnBarUpdate is processing the 10:37 bar, then BarsSinceEntry will be 3 real time and historical.

        Comment


          #5
          As I told you from my tests it is not the same!

          Do you expect the same value or did you test it?

          In your example at processing the 10:37 bar BarsSinceEntry() will return 3 on historical data and 4 on market replay or real-time. I assume that "processing the 10:37 bar" means the bar with Time[0] is 10:37 (CalculateOnBarClose=True).

          That is result of my tests/debugging.

          Comment


            #6
            Please double check that the execution on chart in all cases is on the 10:34 bar.

            Comment


              #7
              As posted above (was an 4 min Chart):
              - EntryLong is called in OnBarUpdate (CalculateOnBarClose=True) of bar with Time[0] == 10:34 for backtest / market replay / real-time
              - Execution is at 10:34:00 / 10:34:00 / 10:34:01 (this is bar with Time[0] == 10:38)
              - at OnBarUpdate Event of this bar (10:38) return value of BarsSinceEntry() Method is 0/1/1

              Backtest and market replay return values of BarSinceEntry() from debugger.

              Comment


                #8
                To double check. You observe this behavior:
                - backtest: on chart you see the execution at the 10:34 bar, BarsSinceEntry on OnBarUpdate for the 10:38 bar yields 0
                - market replay: on chart you see the execution at the 10:34 bar, BarsSinceEntry on OnBarUpdate for the 10:38 bar yields 1
                - real time: on chart you see the execution at the 10:34 bar, BarsSinceEntry on OnBarUpdate for the 10:38 bar yields 1

                Please verify and confirm. Thanks

                Comment


                  #9
                  Originally posted by NinjaTrader_Dierk View Post
                  To double check. You observe this behavior:
                  - backtest: on chart you see the execution at the 10:34 bar, BarsSinceEntry on OnBarUpdate for the 10:38 bar yields 0
                  - market replay: on chart you see the execution at the 10:34 bar, BarsSinceEntry on OnBarUpdate for the 10:38 bar yields 1
                  - real time: on chart you see the execution at the 10:34 bar, BarsSinceEntry on OnBarUpdate for the 10:38 bar yields 1

                  Please verify and confirm. Thanks
                  Execution is on 10:38 bar for backtest, market replay and real-time - rest is correct

                  Comment


                    #10
                    Thanks for clarification and for reporting this. This is a bug which will be fixed with next update: real time and replay will yield 0 like back test in scenarios below.

                    Comment


                      #11
                      BrasSinceEntry() real-time vs. Historical

                      I still find that BarsSinceEntry() function returns differenet results between historical data(backtesting) and real-time data. Possibly, it perform abnormally during the real-time situation.

                      For Example :
                      I try to implement "Time-based Exit" using BarsSinceEntry() function. But, the problem is during the real-time testing, it always fails to RESET and calcuate Bars since last entry. Actually, it aggregate the number of bars since very First entry.




                      Setup : 1. MACD crossover for Long/Short trigger point.
                      2. Fixed Time Exit. Set 5minutes(==300 seconds)

                      Please check the attched pictures and below OutPut window. You will find that "Time-based Exit" function perform correctly only one time.



                      OutPut Window
                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                      03:54:05.9132500ES 09-08 Globex - Short( 1 ) 1.202 Position Waiting
                      Current Minute :52
                      ~~~~~TimeOut~~~~~ 300
                      ~~~~~TimeOut~~~~~ 301
                      ~~~~~TimeOut~~~~~ 302
                      ~~~~~TimeOut~~~~~ 303
                      ~~~~~TimeOut~~~~~ 304
                      ~~~~~TimeOut~~~~~ 305
                      ~~~~~TimeOut~~~~~ 306
                      ~~~~~TimeOut~~~~~ 307
                      ~~~~~TimeOut~~~~~ 308
                      ~~~~~TimeOut~~~~~ 309
                      ~~~~~TimeOut~~~~~ 310
                      ~~~~~TimeOut~~~~~ 311
                      ~~~~~TimeOut~~~~~ 312
                      ~~~~~TimeOut~~~~~ 313
                      03:54:06.5538750ES 09-08 Globex - Flat( 0 ) 0 Position Waiting
                      Current Minute :54
                      03:55:53.3351250ES 09-08 Globex - Flat( 0 ) 0 Position Waiting
                      Current Minute :56
                      03:57:52.8507500ES 09-08 Globex - Flat( 0 ) 0 Position Waiting
                      Current Minute :58
                      03:59:52.9913750ES 09-08 Globex - Flat( 0 ) 0 Position Waiting
                      Current Minute :0
                      04:01:52.7257500ES 09-08 Globex - Flat( 0 ) 0 Position Waiting
                      Current Minute :2
                      04:04:09.1007500ES 09-08 Globex - Flat( 0 ) 0 Position Waiting
                      Current Minute :4
                      04:05:54.4913750ES 09-08 Globex - Flat( 0 ) 0 Position Waiting
                      Current Minute :6
                      04:05:54.4913750ES 09-08 Globex - CrossAbove Checking
                      04:05:55.6945000ES 09-08 Globex - ENTRY Long_Cross
                      ~~~~~TimeOut~~~~~ 453
                      04:08:21.3351250ES 09-08 Globex - Flat( 0 ) 0 Position Waiting
                      Current Minute :8
                      04:09:52.5070000ES 09-08 Globex - Flat( 0 ) 0 Position Waiting
                      Current Minute :10
                      04:11:56.0695000ES 09-08 Globex - Flat( 0 ) 0 Position Waiting
                      Current Minute :12
                      2008/7/14 上午 04:11:56ES 09-08 Globex - CrossBelow Checking
                      04:11:56.0695000ES 09-08 Globex - ENTRY Short_Cross
                      ~~~~~TimeOut~~~~~ 489
                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                      Attached Files

                      Comment


                        #12
                        Hi chehming,

                        I will investigate this for you. Can you clarify that this is what you are using for your exit?
                        Code:
                        if (BarsSinceEntry() > 5)
                             ExitLong();
                        Or some variant of it? Thanks.
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          BarsSinceEntry() real-time vs. Historical

                          Yes, it is similar. 300 seconds == 5 mins. Please test the code in real-
                          time situation.

                          THANKS.

                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

                          private int timeout = 300; // Default setting for TimeOut

                          protected override void Initialize()
                          {
                          CalculateOnBarClose = true;
                          Add(PeriodType.Second,1);
                          }



                          protected override void OnBarUpdate()
                          {


                          if (BarsInProgress == 1)

                          {

                          if (BarsSinceEntry(1,"CrossEntry",0) >= timeout && Positions
                          [1].MarketPosition ==
                          MarketPosition.Long )

                          { timeoutOrder=ExitLongLimit(1,true,1,Closes[1]
                          [0],"TimeOut_Long","CrossEntry");
                          Print("~~~~~TimeOut~~~~~ " + BarsSinceEntry(1,"CrossEntry",0));
                          }

                          else if (BarsSinceEntry(1,"CrossEntry",0) >= timeout && Positions[1].MarketPosition

                          == MarketPosition.Short )
                          { timeoutOrder=ExitShortLimit(1,true,1,Closes[1]

                          [0],"TimeOut_Short","CrossEntry");
                          Print("~~~~~TimeOut~~~~~ " + BarsSinceEntry(1,"CrossEntry",0));
                          }

                          }
                          }

                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~

                          Comment


                            #14
                            Please see my attached strategy. I am unable to produce problems with the exits in real-time on my end.

                            My output logs show it count from 0 up to 6 to exit (which is correct since CalculateOnBarClose is set to true).

                            Woops. Didn't see your earlier post. Will check that implementation too now.
                            Attached Files
                            Josh P.NinjaTrader Customer Service

                            Comment


                              #15
                              To make sure we all are on the same page: please double check your are on latest 6.5.1000.4. Thanks

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              666 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              377 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