Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

CurrentBars doesn't match bars on the chart.

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

    CurrentBars doesn't match bars on the chart.

    Sorry the title says CurrentBars, I mean CurrentBar. I have my strategy outputs CurrentBar with a print. However, the bar numbers in the Ninjascript output do not match the bar numbers on the chart.

    I have indicators on the chart that number the bars, starting at 0 when the session starts. Makes perfect sense.

    But my Ninjascript output is showing bar numbers that are much different. Bar 81 on the chart is referred to as Bar 576 in the output. Both the strategy and the chart are set to the same tick value. I set the strategy to load only 1 day of data but nothing seems to match up.

    It seems that the strategy starts counting bars based on the 'days to load' setting in the strategy properties. But this is minimum of 1.

    If I load market replay data for 25th June ---> 26th June, move the playhead to the right to the end. Enable the strategy, the bar count is synced up. But as soon as I moved the playhead and start playing it gets messed up again.

    Am I missing something simple? Is there a trick to getting the bar numbers to match.
    Last edited by lucyb; 06-26-2024, 06:14 PM.

    #2
    Hello lucyb,

    From the description it sounds like the indicator you are using is what is incorrect, the bar indexes don't reset at the start of a session. The CurrentBar variable is the index in the data that is being processed so that goes from 0 left most bar on the chart to the count of the bars right most bar.

    Comment


      #3
      ok, if that's the case do you know an indicator that replicates this same counting? I cannot find one.

      It seems like you have to load the strategy with at least 1 day of data, so this makes the count different to the indicator which begins at the start of the session

      Comment


        #4
        Hello lucyb,

        I am not aware of an item like that myself but that type of logic would be simple to create if you wanted to use that type of logic in your script. An indicator could be created with a Plot and you would only need to increment a variable and plot that variable. That variable would need to get reset on the first bar of the session to replicate the indicator you are using.

        If the indicator that you are using already exposes a plot with those values that would be able to be used in your strategy to get the count as well. You may want to check the charts data box to see if the indicator has a plot and what the values are, if you see the same count you could call that indicator to get its values.


        Comment


          #5
          ok I think I did it. I opened up someone's indicator and deleted the part that reset the count when it was the first part of the session. Thanks

          Comment


            #6
            Hello again. Unfortunately I still have a problem with this.

            Whilst the indicator now counts the bars from the left side of the chart instead of the start of the session. CurrentBar() is not counting the same way. Please see attached screenshot. I can manually count the bars on my whole chart and the indicator is 100% correct, so where is CurrentBar() getting its number from? which is some 1000 bars different.

            You can see the dates I have loaded on my chart.
            Attached Files
            Last edited by lucyb; 06-28-2024, 07:08 AM.

            Comment


              #7
              Hello lucyb,

              CurrentBar is the bar being processed, when your script starts it works from bar 0 forward. OnBarUpdate is called and CurrentBar is incremented. This happens once for every bar through processing.

              The problem you are seeing depends on what the indicator is doing in its logic, as I don't know what was coded I couldn't really know what result it would make.

              If you want to display the bar index the drawings in the indicator would need to use CurrentBar and then draw that value as text. Depending on how the indicator was coded and if its using OnRender to draw the text it may need to use different logic to get the bar index.

              Comment


                #8
                I don't understand, the indicator works from Bar 0 and increments +1 every bar.

                The problem is CurrentBar is not starting at Bar 0 on my chart. I just need a visual reference to bar able to know which bar CurrentaBar is referring to because at the moment it's not at all clear where it starts its count because it isn't bar 0 as you can see from the screenshot.

                My currently open chart has 500 bars or so. CurrentBar is counting already 1500+ bars. Where does it consider bar 0 to be?

                Perhaps if we just forget the indictor for a second. I would just like to know which bar CurrentBar is processing because it does not relate to the number of bars I see on my chart.
                Last edited by lucyb; 06-28-2024, 07:34 AM.

                Comment


                  #9
                  Hello lucyb,

                  As mentioned I don't know how the code you are using works to comment on the result you see in the chart. That is based on what the indicators logic is doing.

                  To see how your script processes you could use the following print. That would work on playback, historical or realtime. All charts start from a specific bar which is considered the first bar or bar index 0. For each bar OnBarUpdate is called until the script reaches realtime. When in realtime OnBarUpdate is still called and CurrentBar increments each time a new bar is created.

                  Try creating a new strategy or indicator and then use the following print, that will let you see which bar index goes with specific bar times.

                  protected override void OnBarUpdate()
                  {
                  Print(Time[0] + " " + CurrentBar);
                  }​

                  Comment


                    #10
                    If we could ignore my script or any indicator for a second please. Previously you said

                    Originally posted by NinjaTrader_Jesse View Post
                    The CurrentBar variable is the index in the data that is being processed so that goes from 0 left most bar on the chart to the count of the bars right most bar.
                    Is this correct or not? If it starts counting at the left most bar of the chart and the chart then prints 500 bars. Then surely CurrentBar would return 500?

                    I already have Print(Time[0] + " " + CurrentBar); in my strategy. It's included in the screenshot I posted. The bar it outputs does not match what you said above and the number does not match the number of bars on my chart. I simply seek to ask where CurrentBar starts counting from?

                    If my chart has 10 bars starting from the left most bar. And currently CurrentBar() is showing the most recent bar to be bar 80, then it is not starting it's counting from the left most bar of the chart.
                    Last edited by lucyb; 06-28-2024, 07:51 AM.

                    Comment


                      #11
                      Hello lucyb,

                      What I said was correct, if you need further clairifcation you can see the CurrentBar and OnBarUpdate documentation:



                      For your script if you have any secondary series that may be why you are seeing that result, again no code has been provided so far for me to know how your script should work so I can only say how the platform is intended to work for the property being used. If you make a new empty strategy and only add that print you should see the result as described in the help guide page for CurrentBar.


                      Comment


                        #12
                        I already created a new script with only this

                        Print(Convert.ToString(Times[0][0]) + " " + Convert.ToString(CurrentBars[0]));

                        Nothing else. The CurrentBar does not match the number of bars on my chart.

                        Comment


                          #13
                          So I have the date 11-12 June loaded in market replay. I have my data series settings set to load 1 day of data also. There is not other data series.

                          My chart loads and I can see back 1 day of historical data. The chart I can see has 300 bars. However, currentBar is currently counting 1500+ bars.

                          I can see in the output that the bar count started on 06.06.24. So now I know where it's counting from. But do you know why it would go back to this specific date?

                          Comment


                            #14
                            Hello lucyb,

                            I am not sure what may be happening in your test, did you put the print inside OnBarUpdate?

                            This is an image of using that print on a chart with 30 bars. The total bar count is 30 but indexing starts at 0 so we get 29 with the building bar, the building bar is excluded from processing when using OnbarClose so we have 28 as the last bar index during historical processing.

                            Comment


                              #15
                              See attached. Everything is done in strategy builder. No code is touched
                              Attached Files

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Yesterday, 05:17 AM
                              0 responses
                              59 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
                              74 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