Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strange behaviour of CurrentBar

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

    Strange behaviour of CurrentBar

    Hello Folks,
    I ran into some spooky issues, regarding out of range errors.

    By investigating into this, I figured out, that CurrentBar, most of the time is 2 lower than Count of the Bar Series.

    When I was trying to iterate over a Series by using Count as condition: for(int i = 0; i < High.Count; i++) this is likely to crash.
    When I am using CurrentBar, or decrease the range by 1, it works. But I have a problem with understanding why.
    If I have a Count of 5, but I cannot access the fifth element, e. a. element[4], then what does this Count represent??
    Last edited by felix.seib; 10-26-2023, 08:43 AM.

    #2
    Hello felix.seib,

    Thank you for your post.

    So I may assist you more accurately, please answer all of the following questions:
    • What version of NinjaTrader are you using? Please provide the entire version number. This can be found under Help -> About (Example: 8.?.?.?)
    • What instrument(s) (and expiry if applicable) have you selected?
    • What Data Series Type have you selected? Example: Tick, Minute, Day
    • Is your script a multi-instrument or multi-time frame script?
    • Do you receive an error on the screen (you had mentioned out-of-range errors)? Are there errors on the Log tab of the Control Center? If so, what do these errors report?
    • You mentioned that this code is "in a wrapper methode, which I call at transition state" - what are the results if you put this into OnBarUpdate() directly? For example, I tried the following in a test script on a 1-minute chart for ES 12-23 and the output showed expected values. There were no prints at State.Transition; only State.Historical and State.Realtime.
    Code:
            protected override void OnBarUpdate()
            {
                Print("State: " + State);
                for (int i = 0; i < CurrentBar; i++)
                {
                    Print(string.Format("i = {0}, CurrentBar: {1}", i, CurrentBar));
                }
                Print(string.Format("{0} CurrentBar: {1}", Time[0], CurrentBar));
            }​

    Thanks in advance; I look forward to further investigating this item.​

    Comment


      #3
      Hi,
      I figured out, that the strange output came from a mistake I made in the format string. I used the identifier {1} two times.
      So sorry for that.
      So CurrentBar is not changing its value. That was my mistake.

      The code, which I am developing has to run in transition state, because I need it to be finished before I enter realtime state, but it should not run for every bar in the past.

      This is why I am calling a methode named MyOnBarUpdate() from OnBarUpdate() and from OnStateChange() one time in Transition state.

      The code you saw was from this MyOnBarUpdate() methode. And OnBarUpdate() does nothing else than to call this methode. The reason for this is, that I could not call OnBarUpdate() from OnStateChange().

      But however the strange output issue is gone. That was my fault.

      What is still strange to my is why CurrentBar is 2 lower than the Count atribute of a Bar Series.

      When I was trying to iterate over a Series by using Count as condition: for(int i = 0; i < High.Count; i++) this is likely to crash.
      When I am using CurrentBar, or decrease the range by 1, it works. But I have a problem with understanding why.

      If I have a Count of 5, but I cannot access the fifth element, e. a. element[4], then what does this Count represent??

      - NinjaTrader Version: 8
      - Effected instruments: ALL
      - Selected dataSeries: 60 Minutes
      - Not multi instument, not multitimeframe
      - Error Message:
      Indicator 'MyWavesDevel2': Error on calling 'OnStateChange' method: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
      - Calling in OnBarUpdate() directly results in the same behaviour.



      Comment


        #4
        Hello felix.seib,

        Thank you for your reply.

        Count represents the number of bars or data points. It is noted in the help guide that CurrentBar is guaranteed to be less than or equal to Count = 1. Count could have additional bars as in-flight ticks and may be larger than CurrentBars. This is expected. Here is the help guide page:


        Please let us know if we may be of further assistance.

        Comment


          #5
          Ah, ok. I saw this mentioned in the help, but I was confused by this index gap. CurrentBar for me is almost always Count-2, not Count-1. But if the underlying structure reserves space for unprocessed data, which I should not reference, then this makes totally sense.

          Thanks for your help.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          597 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          343 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          103 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          556 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          555 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X