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

1 bar discrepancy between ISeries<T>.Count and real count

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

    1 bar discrepancy between ISeries<T>.Count and real count

    For all Series data types, both ISeries<T> and others like TimeSeries, Count method returns one bar more than one can access in reality. To see this, place the following in OnBarUpdate:
    Code:
    Print("CurrentBar+1: " + (CurrentBar + 1) + " OpenCount: " + Open.Count + " TimeCount: " + Time.Count);
    and reload the indicator/strategy
    On the last bar loaded you should see all of the above being equal, in reality CurrentBar+1 (since it is 0 indexed) is still one less than, eg. Open.Count or Time.Count.

    One additional test you can do is in OnStateChange() in the State.Transition from State.Historical you can place the following:
    Code:
    Print("Test: " + Open[Open.Count - 1]);
    Having all historical data loaded the attempt to access Open.Count - 1 index results in out of range error.

    #2
    Hello gregid,

    When the scripts starts, the Count is set to the full slot collection including the first real-time bar that will exist and will start building as real-time data is received.

    To find the last historical bar, you would want to subtract 1 to find the bar previous to this first real-time bar, and adjust minus 1 as the index is 0 based and does not start at 1.

    The first real-time bar, before it is populated will be null.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks Chelsea

      Correct me if I am wrong, but I think I know what/why it is happening (and is needed).
      The default indexing is for Calculate.OnEachTick (or OnPriceChange) but in order to allow for easy referencing when Calculate.OnBarClose, NT implements custom indexer that is the default - 1 for OnBarClose case. So when the bar closes the new item is instantly added to the Series but OnBarClose indexing uses 0 for referencing recently closed bar and skipping the newly created.
      I have updated my code taking Calculate method into cosideration and it seems to be working fine for all cases now.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by DJ888, 04-16-2024, 06:09 PM
      4 responses
      12 views
      0 likes
      Last Post DJ888
      by DJ888
       
      Started by terofs, Today, 04:18 PM
      0 responses
      7 views
      0 likes
      Last Post terofs
      by terofs
       
      Started by nandhumca, Today, 03:41 PM
      0 responses
      6 views
      0 likes
      Last Post nandhumca  
      Started by The_Sec, Today, 03:37 PM
      0 responses
      3 views
      0 likes
      Last Post The_Sec
      by The_Sec
       
      Started by GwFutures1988, Today, 02:48 PM
      1 response
      9 views
      0 likes
      Last Post NinjaTrader_Clayton  
      Working...
      X