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 cre8able, Yesterday, 01:16 PM
              3 responses
              11 views
              0 likes
              Last Post cre8able  
              Started by ChartTourist, Today, 08:22 AM
              0 responses
              6 views
              0 likes
              Last Post ChartTourist  
              Started by LiamTwine, Today, 08:10 AM
              0 responses
              2 views
              0 likes
              Last Post LiamTwine  
              Started by Balage0922, Today, 07:38 AM
              0 responses
              5 views
              0 likes
              Last Post Balage0922  
              Started by JoMoon2024, Today, 06:56 AM
              0 responses
              6 views
              0 likes
              Last Post JoMoon2024  
              Working...
              X