Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

OHLC data in backtesting is different than in real time

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

    OHLC data in backtesting is different than in real time

    Hello,

    I'm using the following code:

    Code:
      protected override void OnBarUpdate()
            {
                if (CurrentBar < 0) return;
    
                    if (BarsInProgress == 0)
                    {
                        if (IsFirstTickOfBar)
                        {​
                              Print( Open[0] + High[0] + Low[0]  + Close[0])
                        }
    When looking at the output in history, I see that the OHLC data is from the previous bar. ie. on m1, if this is executing on the first tick of the 11:10 bar, then OHLC data is from the 11:09 bar.

    However, when looking at output live the platform gives me the same value for everything - presumably the first tick of the 11:10 bar.

    How can I harmonize this? Historical acts precisely as I want it. But if I use Open[1] for historical I would get the data from the 11:08 bar and my strategy is one bar late all the time.

    Thanks in advance!

    #2
    Hello -kman-,

    Welcome to the NinjaTrader forums!

    When Calculate is OnEachTick, TickReplay has to be enabled for this to work in historical data.

    May I confirm TickReplay is enabled?

    Below are links to the help guide.

    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello,

      Thanks for the quick answer - let me try that. While it's loading, I did have another question: My strategy doesn't actually need to calculate on every tick - just once per bar, so I could use calculate on close.

      If I do this, does my OnBarUpdate get called on the first tick of the next bar, thus harmonizing the behavior, and will this also carry over to realtime? ie. Suppose that I calculate on bar close and want to GoLong if the closed bar was green...is it this:

      if (Close[0] > Open[0])
      GoLong(...)


      Or this....?

      if (Close[1] > Open[1])
      GoLong(...)


      And in general, is there documentation that outlines how all this works?

      Thanks again!

      Comment


        #4
        Hello -kman-,

        When Calculate is OnBarClose the updating bar, [0] barsAgo, is the previous bar that has fully closed and not the currently building bar.
        When Calculate is OnEachTick the updating bar, [0] barsAgo, is the currently building bar. The most recently fully closed bar would be [1] bar ago.

        Below is a link to the help guide.

        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Ah - ok. I think I understand.

          Can I work around this inconsistency by subscribing to a lower timeframe, like 1 second?

          If I was on m1, it should call my code with BarsInProgress == 0 once, then sixty times with BarsInProgress ==1?

          Could I use calculate on bar close then? Or would I still need to use tick replay?

          Thanks!

          Comment


            #6
            Hello -kman-,

            You can add a 1 second series and use Calculate.OnBarClose if you would like.

            Below is a link to AddDataSeries().
            https://ninjatrader.com/support/helpGuides/nt8/adddataseries.htm
            https://ninjatrader.com/support/helpGuides/nt8/barsinprogress.htm
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hi again Chelsea,

              Ninjascript exposes a state == State.Historical. Can I use this to calculate offset to normalize the numbers?

              ie:
              Code:
              else if (State == State.Historical)
                          {
                              this.Offset = -1;
                          }else if (State == State.Realtime)
                          {
                              this.Offset = 0;
                          }​
              Then whenever I access a bar I do:

              Code:
              Close[1 + this.Offset]
              That seems to work the way I think it should? Or do I need to be careful of any threading issues?

              Comment


                #8
                Hello -kman-,

                You could choose to do this.. The results will not match real-time unless TickReplay is enabled, but you can still choose that.

                There will not be threading issues, as no other thread (such as the UI thread) is being accessed.
                Chelsea B.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NullPointStrategies, Yesterday, 05:17 AM
                0 responses
                62 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                134 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                75 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                45 views
                0 likes
                Last Post Deep42
                by Deep42
                 
                Started by TheRealMorford, 03-05-2026, 06:15 PM
                0 responses
                50 views
                0 likes
                Last Post TheRealMorford  
                Working...
                X