Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Mode from a Series double

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

    #91
    Hello,

    The proper check would be:

    Code:
    int numberOfBarsBackToLook = 10;
    
    if (CurrentBar < numberOfBarsBackToLook)
    return;
    
    Print(Time[numberOfBarsBackToLook]);
    Post #83 links a forum post on indexing errors and includes a link to the help guide documentation with the recommend approach to preventing indexing errors.
    Last edited by NinjaTrader_ChelseaB; 03-29-2022, 12:29 PM.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #92
      Hello Chelsea, I've found a new way to fix indexing error from this UserApps Share Share CongestionBoxLite

      PHP Code:
      protected override void OnBarUpdate()
      {
         if(CurrentBar > ChartBars.FromIndex -30) /// Only Calculate and paint bars on the screen + 30 bars
         {
            // DoX;
         } 
      

      I'll test and use it going forward if I get new Indexing errors and be back if I'm not making it work.

      Attached Files

      Comment


        #93
        Originally posted by NinjaTrader_ChelseaB View Post
        Hello PaulMohn,

        modes[modes.Count()]; would be an index that does not exist.

        Lets say that modes had 10 elements in it, 0 through 9. If you call modes.Count, this returns 10. But because the indexes start at 0, the highest index is 9. So calling for the 11th element with the index [10] causes an error with an invalid index.

        If you want the last element it would be .Count() - 1.
        Ok, I've found some similar explanation with illustrations

        How to work with List.GetRange()

        If you change the k from 7 to 6, it will work. This happens because GetRange, as it is stated in MSDN:
        Creates a shallow copy of a range of elements in the source




        The first parameter you pass in the GetRange method is the index it will start the copy and the second parameter the number of elements that will be copied.

        Presuming that arrayNames contains the following data:

        PHP Code:
        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 
        

        If index = 5 and count = 7, it will try to copy the values of the element in the following positions:

        PHP Code:
        5, 6, 7, 8, 9, 10, 11(?) 
        

        Since arrayNames only goes up to 10 (as it is zero-based), there isn't any element in position 11. Hence you get this error.



        7

        The reason of exception is due to invalid usage of parameters of GetRange extension method:

        PHP Code:
        public List<T> GetRange(int index, int count); 
        

        first parameter is the first element's index, whereas the second one is the number of elements you want to receive from first index. You should you it in this way:

        PHP Code:
        var resRange= myArray.ToList().GetRange(firstIndex, lastIndex - firstIndex); 
        

        Of course the result of lastIndex - firstIndex mustn't be more than myArray.Count().
        Last edited by PaulMohn; 05-07-2022, 11:39 AM.

        Comment


          #94
          I think I got the solution

          I needed to use as 1st CurrentBar Check
          PHP Code:
          if (CurrentBar < BarsBack) return; 
          

          Then as 2nd CurrentBar Check (down the script) and GetRange(index, count)
          PHP Code:
          listOfSeriesValues.Add(RangeTopRangeBottom[0]);
          
          
          if (CurrentBar < (BarsBack*3)) return;
          
          listOfSeriesValuesBarsBack = listOfSeriesValues.GetRange(((listOfSeriesValues.Count() - 1) - BarsBack), BarsBack); 
          

          It seems to work in all use cases with no more Indexing errors.


          In summary at least 3 times the BarsBack number of Bars must be present/loaded for the indicator to calculate without indexing errors with whatever value given to the BarsBack and series indexes.

          And the index parameter in the GetRange(index, count) must be

          index = listCount() - count
          or
          index = (listCount() - 1) - count)
          or at least
          index <= listCount() - count


          I'm wondering if there would be a 'non-hardcoding' way to set the 2nd CurrentBar check value? For now it seems to work for all my use cases but I'd gladly know of a way to get the right check value for all possible cases (if a dynamic value is possible). Thanks!

          Comment


            #95
            Hello PaulMohn,

            You would only need one CurrentBar check.

            CurrentBar must be greater than any barsAgo index used. Put the variable with the largest index used in the CurrentBar check.

            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
            613 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            357 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            105 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            561 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            566 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X