Announcement

Collapse
No announcement yet.

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


                #8
                NinjaTrader_JoshG
                "...always be [2]two[2] less CurrentBar index[s] than the size(Count) of array when using Calculate.OnBarClose"
                "If one uses Calculate.OnEachTick ... it will be [1]one[1] less(since it includes currently building bar)"



                experience on this end is contrary to these statements?
                this end version 8.1.3.1 64-bit
                even with Calculate = Calculate.OnEachTick;
                still off by 2 rather than off by 1 .!?



                line 028 namespace NinjaTrader.NinjaScript.Indicators
                line 029 {
                line 030 public class _so_pp_1d : Indicator
                line 031 {
                ...
                line 068 static int TraceLineNum([CallerLineNumber] int lineNumber = 0)
                line 069 {
                line 070 return lineNumber;
                line 071 }
                ...
                line 083 protected override void OnStateChange()
                line 084 {
                line 085 if (State == State.SetDefaults)
                line 086 {
                ...
                line 089 Calculate = Calculate.OnEachTick;
                ...
                line 141 }
                ...
                line 151 protected override void OnBarUpdate()
                line 152 {
                ...
                line 217 if (EnableXtraTracing){Print("line "+TraceLineNum()+" "+"BarsInProgress = "+BarsInProgress);}
                line 218 if (EnableXtraTracing){Print("line "+TraceLineNum()+" "+"CurrentBar = "+CurrentBar);}
                line 219 if (EnableXtraTracing){Print("line "+TraceLineNum()+" "+"BarsArray[0].Count = "+BarsArray[0].Count);}
                line 220 if (EnableXtraTracing){Print("line "+TraceLineNum()+" "+"if(BarsInProgress == 0) Print(CurrentBar = "+CurrentBar+"+ ""+" "+"" "+"+BarsArray[0].Count) = "+BarsArray[0].Count+");");}
                line 221 if (EnableXtraTracing){Print("line "+TraceLineNum()+" "+"if(BarsInProgress == 0) Print(CurrentBar +""+" "+""+ BarsArray[0].Count);");}
                line 222 if(BarsInProgress == 0) Print(CurrentBar +" "+ BarsArray[0].Count);
                line 223 if (EnableXtraTracing){Print("line "+TraceLineNum());}
                ...
                line 285 }
                ...
                line 428 }


                produces this in "Ooutput 1"
                ...
                line 217 BarsInProgress = 0
                line 218 CurrentBar = 1376
                line 219 BarsArray[0].Count = 1378
                line 220 if(BarsInProgress == 0) Print(CurrentBar = 1376+ " " +BarsArray[0].Count) = 1378);
                line 221 if(BarsInProgress == 0) Print(CurrentBar +" "+ BarsArray[0].Count);
                1376 1378
                line 223
                ...​

                Comment


                  #9
                  pretty sure figured it out on this end . . .

                  abandoned using CurrentBar
                  instead using
                  BarsArray[0].Count - CurrentBars[0] == 1
                  BarsArray[1].Count - CurrentBars[1] == 1

                  . . . now only see diff of [1]one[1] less with Calculate = Calculate.OnEachTick;

                  Comment


                    #10
                    Hello slingonce,

                    CurrentBar is the currently processing bar in OnBarUpdate.
                    Count is the total number of bar slots in the bars object.

                    Use CurrentBar if you are trying to work with information about the currently updating (or most recently updated) bar.
                    Use Count if you want to know how many total bars are loaded in the bars object.

                    It is expected the count will always be greater than CurrentBar. In historical the last bar does not update until the first realtime bar begins to form, so this would be 2 less than the total loaded bar count.
                    Chelsea B.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    648 views
                    0 likes
                    Last Post Geovanny Suaza  
                    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                    0 responses
                    369 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by Mindset, 02-09-2026, 11:44 AM
                    0 responses
                    108 views
                    0 likes
                    Last Post Mindset
                    by Mindset
                     
                    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                    0 responses
                    572 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by RFrosty, 01-28-2026, 06:49 PM
                    0 responses
                    573 views
                    1 like
                    Last Post RFrosty
                    by RFrosty
                     
                    Working...
                    X