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

CurrentBar and Bars.Count off by 2?

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

    CurrentBar and Bars.Count off by 2?

    In debugging some code I found that that the Current Bar variable and the Bars.Count numbers are off by 2.

    Place the below code in any indicator or strategy and you will see what I am talking about, the last print statement shows them off.

    PHP Code:
    if(BarsInProgress == 0) Print(CurrentBar +" " BarsArray[0].Count); 

    #2
    Hello cutzpr,
    Thanks for your post.

    That is because CurrentBar and BarsArray[0].Count are not the same values. CurrentBar is a NinjaTrader value for how many bars are on the chart. Count is going to be how many indexes there are in that Array.

    There will always be two less CurrentBar indexs than the size(Count) of the array when using Calculate.OnBarClose. If you use Calculate.OnEachTick for the same code you can see that it will be one less(since it includes the currently building bar).
    Josh G.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by cutzpr View Post
      In debugging some code I found that that the Current Bar variable and the Bars.Count numbers are off by 2.

      Place the below code in any indicator or strategy and you will see what I am talking about, the last print statement shows them off.

      PHP Code:
      if(BarsInProgress == 0) Print(CurrentBar +" " BarsArray[0].Count); 
      Count is indexed starting at 1: CurrentBar is indexed starting from 0. If using Calculate.OnBarClose, then the forming bar would not be counted in CurrentBar, but would be in Count, so there should be a difference of 2.
      Last edited by koganam; 03-10-2019, 06:29 PM.

      Comment


        #4
        I´m borrowing this thread for a related question:

        Two series with same timeframe (seconds) with different count:

        BarsArray[0].Count = 3459
        BarsArray[1].Count = 2654


        1. In what way are these two series in sync?
        2. How can I know they are synced in time at the same index?

        Closes[0][56] = x
        Closes[1][56] = y



        Confused .

        Comment


          #5
          A quick test of two series in one second bars (same time frames):

          Test 1:

          if (CurrentBar == BarsArray[1].Count - 2) Print(Times[0][3000]);
          if (CurrentBar == BarsArray[1].Count - 2) Print(Times[1][3000]);

          Prints:

          9/25/2020 3:06:46 PM
          9/25/2020 3:09:59 PM

          Test 2:

          if (CurrentBar == BarsArray[0].Count - 2) Print(Times[0][3000]);
          if (CurrentBar == BarsArray[1].Count - 2) Print(Times[1][3000]);

          Prints:

          9/25/2020 3:06:46 PM
          9/25/2020 3:09:59 PM

          Test 3:

          if (CurrentBar == Bars.Count - 2) Print(Times[0][3000]);
          if (CurrentBar == Bars.Count - 2) Print(Times[1][3000]);

          Prints:

          9/25/2020 3:06:46 PM
          9/25/2020 3:09:59 PM

          Test 4: accessing BarsArray[0] outside the BarsArray[0].Count but inside BarsArray[1].Count index does as expected give out if index:

          if (CurrentBar == Bars.Count - 2) Print("BarsArray[0].Count: " + BarsArray[0].Count + " ::: BarsArray[1].Count: " + BarsArray[1].Count);
          if (CurrentBar == Bars.Count - 2) Print(Times[0][600000]);
          if (CurrentBar == Bars.Count - 2) Print(Times[1][600000]);

          Prints:

          ...out if index...


          I don´t get how two series in the same time frame are in sync?
          Last edited by FREEN; 08-31-2021, 03:12 PM.

          Comment


            #6
            Originally posted by FREEN View Post
            A quick test of two series in one second bars (same time frames):

            Test 1:

            if (CurrentBar == BarsArray[1].Count - 2) Print(Times[0][3000]);
            if (CurrentBar == BarsArray[1].Count - 2) Print(Times[1][3000]);

            Prints:

            9/25/2020 3:06:46 PM
            9/25/2020 3:09:59 PM

            Test 2:

            if (CurrentBar == BarsArray[0].Count - 2) Print(Times[0][3000]);
            if (CurrentBar == BarsArray[1].Count - 2) Print(Times[1][3000]);

            Prints:

            9/25/2020 3:06:46 PM
            9/25/2020 3:09:59 PM

            Test 3:

            if (CurrentBar == Bars.Count - 2) Print(Times[0][3000]);
            if (CurrentBar == Bars.Count - 2) Print(Times[1][3000]);

            Prints:

            9/25/2020 3:06:46 PM
            9/25/2020 3:09:59 PM

            Test 4: accessing BarsArray[0] outside the BarsArray[0].Count but inside BarsArray[1].Count index does as expected give out if index:

            if (CurrentBar == Bars.Count - 2) Print("BarsArray[0].Count: " + BarsArray[0].Count + " ::: BarsArray[1].Count: " + BarsArray[1].Count);
            if (CurrentBar == Bars.Count - 2) Print(Times[0][600000]);
            if (CurrentBar == Bars.Count - 2) Print(Times[1][600000]);

            Prints:

            ...out if index...


            I don´t get how two series in the same time frame are in sync?
            They are in sync: your code queries are not. Try using CurrentBars[n] syntax: CurrentBar is specific to the primary BarSeries, unless specifically gated by BarsInProgress.

            Comment


              #7
              Thanks koganam. It was a while since I used NT but I recall now. Yes, they are in sync with respect to the chart. If 6000 seconds are loaded and there are ticks/data only for 4000 seconds the primary series is "time compressed" with a time lapse effect. It´s very unintuitive.

              If I would be the engineer to NT I would "fill the empty slots" with a null/NaN/NoTrade value to make sure the index is consistent with the loaded time frames/interval. Time doesn´t cease to exist just because there´s no trade going on in that time frame .
              Last edited by FREEN; 09-01-2021, 01:45 AM.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by andrewtrades, Today, 04:57 PM
              1 response
              8 views
              0 likes
              Last Post NinjaTrader_Manfred  
              Started by chbruno, Today, 04:10 PM
              0 responses
              6 views
              0 likes
              Last Post chbruno
              by chbruno
               
              Started by josh18955, 03-25-2023, 11:16 AM
              6 responses
              436 views
              0 likes
              Last Post Delerium  
              Started by FAQtrader, Today, 03:35 PM
              0 responses
              7 views
              0 likes
              Last Post FAQtrader  
              Started by rocketman7, Today, 09:41 AM
              5 responses
              19 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Working...
              X