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

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:
        012345678910 

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

        PHP Code:
        567891011(?) 

        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<TGetRange(int indexint 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 resRangemyArray.ToList().GetRange(firstIndexlastIndex 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 royhagerman, Today, 04:30 AM
            0 responses
            1 view
            0 likes
            Last Post royhagerman  
            Started by haas88, 03-21-2024, 02:22 AM
            18 responses
            208 views
            0 likes
            Last Post haas88
            by haas88
             
            Started by Board game geek, Today, 02:20 AM
            0 responses
            6 views
            0 likes
            Last Post Board game geek  
            Started by knighty6508, Today, 01:20 AM
            2 responses
            15 views
            0 likes
            Last Post knighty6508  
            Started by franatas, Today, 01:53 AM
            0 responses
            5 views
            0 likes
            Last Post franatas  
            Working...
            X