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

    #61
    Chelsea I've reproduced the issue with your script with BarsBack.
    Why does BarsBack throw the error? Thanks!
    Attached Files

    Comment


      #62
      Hello PaulMohn,

      This is no longer the script I gave you and the error is not with the code I provided.

      Use prints to find out why your custom logic is causing an error.

      The error is an index error.

      What is the size of the rng0Rng1 series at the time the error? (hint its the size of CurrentBar)

      What is the invalid index that was used as a barsAgo value?
      Chelsea B.NinjaTrader Customer Service

      Comment


        #63
        Thanks Chelsea for the reply.
        I modified post #60 1st snippet as below to reflect the correct BarsBack index.

        PHP Code:
        double sum 0;
        for(
        int i=0i<BarsBacki++)
        {
           
        sum += rng0rng1[i];
        }

        double var3 = (sum BarsBack);

        double var03 Instrument.MasterInstrument.RoundTo TickSize(var3 TickSize); 

        Building upon your print file example,

        I tested printing with the following prints statements and it does print, as long as the Rng0Rng1 index is below the CurrentBar index
        (in this case the CurrentBar index = 1038, and the prints max Rng0Rng1 = 21)

        PHP Code:
        Print("CurrentBar : " CurrentBar);
        Print(
        " Rng0Rng1[0] " Rng0Rng1[0]);
        Print(
        " Rng0Rng1[1] " Rng0Rng1[1]);
        Print(
        " Rng0Rng1[2] " Rng0Rng1[2]);
        Print(
        " Rng0Rng1[3] " Rng0Rng1[3]);
        Print(
        " Rng0Rng1[4] " Rng0Rng1[4]);
        Print(
        " Rng0Rng1[5] " Rng0Rng1[5]);
        Print(
        " Rng0Rng1[6] " Rng0Rng1[6]);
        Print(
        " Rng0Rng1[7] " Rng0Rng1[7]);
        Print(
        " Rng0Rng1[8] " Rng0Rng1[8]);
        Print(
        " Rng0Rng1[9] " Rng0Rng1[9]);
        Print(
        " Rng0Rng1[10] " Rng0Rng1[10]);
        Print(
        " Rng0Rng1[11] " Rng0Rng1[11]);
        Print(
        " Rng0Rng1[12] " Rng0Rng1[12]);
        Print(
        " Rng0Rng1[13] " Rng0Rng1[13]);
        Print(
        " Rng0Rng1[14] " Rng0Rng1[14]);
        Print(
        " Rng0Rng1[15] " Rng0Rng1[15]);
        Print(
        " Rng0Rng1[16] " Rng0Rng1[16]);
        Print(
        " Rng0Rng1[17] " Rng0Rng1[17]);
        Print(
        " Rng0Rng1[18] " Rng0Rng1[18]);
        Print(
        " Rng0Rng1[19] " Rng0Rng1[19]);
        Print(
        " Rng0Rng1[20] " Rng0Rng1[20]);
        Print(
        " Rng0Rng1[21] " Rng0Rng1[21]);

        //Print("\n Rng0Rng1[2000] " + Rng0Rng1[2000]);

        double sum 0;
        for(
        int i=0i<CurrentBari++)
        {
           
        sum += Rng0Rng1[i];
        }

        double var3 = (sum CurrentBar);

        double var03 Instrument.MasterInstrument.RoundToTickSize(var3 TickSize); 

        Click image for larger version

Name:	chrome_FE4Bxjlv0U.png
Views:	106
Size:	1.06 MB
ID:	1193440

        But when I try to print for a Rng0Rng1 index > the CurrentBar (1038), for example Rng0Rng1[2000]

        with

        PHP Code:
        Print("CurrentBar : " CurrentBar);
        Print(
        " Rng0Rng1[0] " Rng0Rng1[0]);
        Print(
        " Rng0Rng1[1] " Rng0Rng1[1]);
        Print(
        " Rng0Rng1[2] " Rng0Rng1[2]);
        Print(
        " Rng0Rng1[3] " Rng0Rng1[3]);
        Print(
        " Rng0Rng1[4] " Rng0Rng1[4]);
        Print(
        " Rng0Rng1[5] " Rng0Rng1[5]);
        Print(
        " Rng0Rng1[6] " Rng0Rng1[6]);
        Print(
        " Rng0Rng1[7] " Rng0Rng1[7]);
        Print(
        " Rng0Rng1[8] " Rng0Rng1[8]);
        Print(
        " Rng0Rng1[9] " Rng0Rng1[9]);
        Print(
        " Rng0Rng1[10] " Rng0Rng1[10]);
        Print(
        " Rng0Rng1[11] " Rng0Rng1[11]);
        Print(
        " Rng0Rng1[12] " Rng0Rng1[12]);
        Print(
        " Rng0Rng1[13] " Rng0Rng1[13]);
        Print(
        " Rng0Rng1[14] " Rng0Rng1[14]);
        Print(
        " Rng0Rng1[15] " Rng0Rng1[15]);
        Print(
        " Rng0Rng1[16] " Rng0Rng1[16]);
        Print(
        " Rng0Rng1[17] " Rng0Rng1[17]);
        Print(
        " Rng0Rng1[18] " Rng0Rng1[18]);
        Print(
        " Rng0Rng1[19] " Rng0Rng1[19]);
        Print(
        " Rng0Rng1[20] " Rng0Rng1[20]);
        Print(
        " Rng0Rng1[21] " Rng0Rng1[21]);

        Print(
        "\n Rng0Rng1[2000] " Rng0Rng1[2000]);

        double sum 0;
        for(
        int i=0i<CurrentBari++)
        {
           
        sum += Rng0Rng1[i];
        }

        double var3 = (sum CurrentBar);

        double var03 Instrument.MasterInstrument.RoundToTickSize(var3 TickSize); 

        I get the error thrown
        Indicator 'Testa': Error on calling 'OnBarUpdate' method on bar 21: 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.
        Click image for larger version

Name:	chrome_gYnMg4W8qE.png
Views:	105
Size:	1.06 MB
ID:	1193439

        I think I can understand the why of the error.
        It is because in this case I'm asking to print the value of the 2000th bar, but since there are only 1038 bar (the CurrentBar) available on the chart ta this time, then it cannot return the value of the 2000th bar because it's yet to be populated (in 62 bars from now).

        What I don't understand, using the same reasoning, using now the BarsBack in the loop instead of the CurrentBar as

        PHP Code:
        double sum 0;
        for(
        int i=0i<BarsBacki++)
        {
           
        sum += Rng0Rng1[i];
        }

        double var3 = (sum BarsBack);

        double var03 Instrument.MasterInstrument.RoundToTickSize(var3 TickSize); 
        is
        BarsBack = 20
        CurrentBar = 1038 (number of Bars currently available for calculation on the chart)
        Rng0Rng1 = CurrentBar = 1038 (number of Bars currently available for calculation on the chart)

        For i = 0 is 0 < 20 ? Yes, then add Rng0Rng1[0] double value to the sum variable
        For i = 1 is 1 < 20 ? Yes, then add Rng0Rng1[1] double value to the sum variable
        For i = 2 is 2 < 20 ? Yes, then add Rng0Rng1[2] double value to the sum variable
        For i = 3 is 3 < 20 ? Yes, then add Rng0Rng1[3] double value to the sum variable
        For i = 4 is 4 < 20 ? Yes, then add Rng0Rng1[4] double value to the sum variable
        For i = 5 is 5 < 20 ? Yes, then add Rng0Rng1[5] double value to the sum variable
        For i = 6 is 6 < 20 ? Yes, then add Rng0Rng1[6] double value to the sum variable
        For i = 7 is 7 < 20 ? Yes, then add Rng0Rng1[7] double value to the sum variable
        For i = 8 is 8 < 20 ? Yes, then add Rng0Rng1[8] double value to the sum variable
        For i = 9 is 9 < 20 ? Yes, then add Rng0Rng1[9] double value to the sum variable
        For i = 10 is 10 < 20 ? Yes, then add Rng0Rng1[10] double value to the sum variable
        For i = 11 is 11 < 20 ? Yes, then add Rng0Rng1[11] double value to the sum variable
        For i = 12 is 12 < 20 ? Yes, then add Rng0Rng1[12] double value to the sum variable
        For i = 13 is 13 < 20 ? Yes, then add Rng0Rng1[13] double value to the sum variable
        For i = 14 is 14 < 20 ? Yes, then add Rng0Rng1[14] double value to the sum variable
        For i = 15 is 15 < 20 ? Yes, then add Rng0Rng1[15] double value to the sum variable
        For i = 16 is 16 < 20 ? Yes, then add Rng0Rng1[16] double value to the sum variable
        For i = 17 is 17 < 20 ? Yes, then add Rng0Rng1[17] double value to the sum variable
        For i = 18 is 18 < 20 ? Yes, then add Rng0Rng1[18] double value to the sum variable
        For i = 19 is 19 < 20 ? Yes, then add Rng0Rng1[19] double value to the sum variable
        For i = 20 is 20 < 20 ? No, then do not add Rng0Rng1[20] double value to the sum variable and stop the loop

        What's the contrast with the previous example you see?


        I think I got it working by using

        PHP Code:
        if (CurrentBar <= BarsBack)
        return; 
        Instead of
        PHP Code:
        if (CurrentBar BarsBack)
        return; 
        or
        PHP Code:
        if (CurrentBar 21)
        return; 

        And making sure the second CurrentBar Check preceeds the snippet as follows

        PHP Code:
        if (CurrentBar <= BarsBack)
        return;

        Print(
        "CurrentBar : " CurrentBar);
        Print(
        string.Format("{0} | listOfSeriesValuesBarsBack.Count(): {1} - BarsBack: {2} = {3} | listOfSeriesValues.Count() ): {4} "Time[0], listOfSeriesValuesBarsBack.Count(), BarsBack, (listOfSeriesValuesBarsBack.Count() - BarsBack), listOfSeriesValues.Count() ));

        listOfSeriesValuesBarsBack listOfSeriesValues.GetRange(0BarsBack);

        double sum 0;
        for(
        int i=0i<BarsBacki++)
        {
           
        sum += Rng0Rng1[i];
        }

        double var3 = (sum BarsBack);

        double var03 Instrument.MasterInstrument.RoundToTickSize(var3 TickSize);

        Print(
        "var03 " var03); 

        I'm not sure yet why it works and I'll do some more testing to find out. Thanks!
        Last edited by PaulMohn; 03-11-2022, 11:54 AM.

        Comment


          #64
          Hello PaulMohn,

          I think you may be understanding.

          If there are 5 apples on the table, and someone asks you for the 9th apple, what do you give them?

          If there are 1038 bars, there is no bar 2000 bars ago. It does not exist so what can the computer provide you.

          The same is true in loops and with list.Range(). You can only ask for the element indexes that exists.

          If list.Count is 1, mean you have 1 element in it, and you ask for element 20 you get an index error.
          This means you want to start adding elements to the list when CurrentBar is 0 (or 1) and then check back after 20 bars have passed, CurrentBar is 20 (or 21), and 20 elements have been added to the list, to get the range of the last 20 elements.

          Below is a link to a forum post on index errors.
          https://ninjatrader.com/support/foru...13#post1048513
          Chelsea B.NinjaTrader Customer Service

          Comment


            #65
            Ok, but when you say
            If list.Count is 1, mean you have 1 element in it, and you ask for element 20 you get an index error.
            This means you want to start adding elements to the list when CurrentBar is 0 (or 1) and then check back after 20 bars have passed, CurrentBar is 20 (or 21), and 20 elements have been added to the list, to get the range of the last 20 elements.
            I had the concept that

            using that reasoning
            For i = 0 is 0 < 20 ? Yes, then add Rng0Rng1[0] double value to the sum variable
            For i = 1 is 1 < 20 ? Yes, then add Rng0Rng1[1] double value to the sum variable
            For i = 2 is 2 < 20 ? Yes, then add Rng0Rng1[2] double value to the sum variable
            For i = 3 is 3 < 20 ? Yes, then add Rng0Rng1[3] double value to the sum variable
            For i = 4 is 4 < 20 ? Yes, then add Rng0Rng1[4] double value to the sum variable
            For i = 5 is 5 < 20 ? Yes, then add Rng0Rng1[5] double value to the sum variable
            For i = 6 is 6 < 20 ? Yes, then add Rng0Rng1[6] double value to the sum variable
            For i = 7 is 7 < 20 ? Yes, then add Rng0Rng1[7] double value to the sum variable
            For i = 8 is 8 < 20 ? Yes, then add Rng0Rng1[8] double value to the sum variable
            For i = 9 is 9 < 20 ? Yes, then add Rng0Rng1[9] double value to the sum variable
            For i = 10 is 10 < 20 ? Yes, then add Rng0Rng1[10] double value to the sum variable
            For i = 11 is 11 < 20 ? Yes, then add Rng0Rng1[11] double value to the sum variable
            For i = 12 is 12 < 20 ? Yes, then add Rng0Rng1[12] double value to the sum variable
            For i = 13 is 13 < 20 ? Yes, then add Rng0Rng1[13] double value to the sum variable
            For i = 14 is 14 < 20 ? Yes, then add Rng0Rng1[14] double value to the sum variable
            For i = 15 is 15 < 20 ? Yes, then add Rng0Rng1[15] double value to the sum variable
            For i = 16 is 16 < 20 ? Yes, then add Rng0Rng1[16] double value to the sum variable
            For i = 17 is 17 < 20 ? Yes, then add Rng0Rng1[17] double value to the sum variable
            For i = 18 is 18 < 20 ? Yes, then add Rng0Rng1[18] double value to the sum variable
            For i = 19 is 19 < 20 ? Yes, then add Rng0Rng1[19] double value to the sum variable
            For i = 20 is 20 < 20 ? No, then do not add Rng0Rng1[20] double value to the sum variable and stop the loop

            if Rng0Rng1 = 1038
            and
            BarsBack = 20
            then
            there must be enough Rng0Rng1 bars/double values up to index [19] to fill the requirement of the
            PHP Code:
            for(int i=0i<BarsBacki++) 

            Or is simply the index [20] that's causing the error (instead of just stopping the loop)?

            Or is it otherwise? Thanks!

            Comment


              #66
              Hello PaulMohn,

              I'm not understanding what you are asking.

              The index [index] must be less than the size of the collection.

              If CurrentBar is 1038, then any index less than this is fine to use for an index of a Series (which has one element for every bar on the chart).
              Chelsea B.NinjaTrader Customer Service

              Comment


                #67
                Ok thanks for confirming, I haven't yet reviewed the previous snippet but I will tomorrow. Thanks again!

                Comment


                  #68
                  Chelsea, I got the mode apparently working in image attached

                  with
                  PHP Code:
                        Rng0Rng1[Rng0Index] = Rng0[Rng0Index] - Rng1[Rng1Index];

                        
                  listOfSeriesValues.Add(RangeTopRangeBottom[0]);


                        if (
                  CurrentBar <= BarsBack)
                        return;

                        
                  listOfSeriesValuesBarsBack listOfSeriesValues.GetRange(0BarsBack);


                        
                  // Initialize the return value
                        
                  double mode 1;
                        
                  double max 0;

                        if (
                  listOfSeriesValuesBarsBack != null && listOfSeriesValuesBarsBack.Count() > 0)
                        {
                  //         for (int element = 0; element < listOfSeriesValuesBarsBack.Count(); element++)
                  //         {
                  //           Print("CurrentBar : " + CurrentBar);
                  ////           Print("listOfSeriesValuesBarsBack[element] : " + listOfSeriesValuesBarsBack[element]);
                  //           Print("listOfSeriesValuesBarsBack.Count() : " + listOfSeriesValuesBarsBack.Count());
                  //         }

                      // Store the number of occurences for each element
                      
                  Dictionary<doubledoublecounts = new Dictionary<doubledouble>();
                      
                  // Add one to the count for the occurence of a character
                      
                  foreach (double element in listOfSeriesValuesBarsBack)
                      {
                            if (
                  counts.ContainsKey(element))
                      
                  counts[element]++;
                            else
                      
                  counts.Add(element1);
                      }
                      
                  // Loop through the counts of each element and find the
                      // element that occurred most often
                      
                  foreach (KeyValuePair<doubledoublecount in counts)
                      {
                            if (
                  count.Value max)
                            {
                              
                  // Update the mode
                              
                  mode count.Key;
                              
                  max count.Value;
                            }
                          }

                            Print(
                  "\n=== mode | Max | Count | CurrentBar ===\n");
                            Print(
                  "mode : " mode);
                            Print(
                  "max : " max);
                            Print(
                  "listOfSeriesValuesBarsBack.Count() : " listOfSeriesValuesBarsBack.Count());
                            Print(
                  "CurrentBar : " CurrentBar);
                          Print(
                  "\n=== listOfSeriesValuesBarsBack Double values for Indexes 0-19 ===\n");
                            Print(
                  " listOfSeriesValuesBarsBack[0] " listOfSeriesValuesBarsBack[0]);
                            Print(
                  " listOfSeriesValuesBarsBack[1] " listOfSeriesValuesBarsBack[1]);
                            Print(
                  " listOfSeriesValuesBarsBack[2] " listOfSeriesValuesBarsBack[2]);
                            Print(
                  " listOfSeriesValuesBarsBack[3] " listOfSeriesValuesBarsBack[3]);
                            Print(
                  " listOfSeriesValuesBarsBack[4] " listOfSeriesValuesBarsBack[4]);
                            Print(
                  " listOfSeriesValuesBarsBack[5] " listOfSeriesValuesBarsBack[5]);
                            Print(
                  " listOfSeriesValuesBarsBack[6] " listOfSeriesValuesBarsBack[6]);
                            Print(
                  " listOfSeriesValuesBarsBack[7] " listOfSeriesValuesBarsBack[7]);
                            Print(
                  " listOfSeriesValuesBarsBack[8] " listOfSeriesValuesBarsBack[8]);
                            Print(
                  " listOfSeriesValuesBarsBack[9] " listOfSeriesValuesBarsBack[9]);
                            Print(
                  " listOfSeriesValuesBarsBack[10] " listOfSeriesValuesBarsBack[10]);
                            Print(
                  " listOfSeriesValuesBarsBack[11] " listOfSeriesValuesBarsBack[11]);
                            Print(
                  " listOfSeriesValuesBarsBack[12] " listOfSeriesValuesBarsBack[12]);
                            Print(
                  " listOfSeriesValuesBarsBack[13] " listOfSeriesValuesBarsBack[13]);
                            Print(
                  " listOfSeriesValuesBarsBack[14] " listOfSeriesValuesBarsBack[14]);
                            Print(
                  " listOfSeriesValuesBarsBack[15] " listOfSeriesValuesBarsBack[15]);
                            Print(
                  " listOfSeriesValuesBarsBack[16] " listOfSeriesValuesBarsBack[16]);
                            Print(
                  " listOfSeriesValuesBarsBack[17] " listOfSeriesValuesBarsBack[17]);
                            Print(
                  " listOfSeriesValuesBarsBack[18] " listOfSeriesValuesBarsBack[18]);
                            Print(
                  " listOfSeriesValuesBarsBack[19] " listOfSeriesValuesBarsBack[19]);
                  //           Print(" listOfSeriesValuesBarsBack[20] " + listOfSeriesValuesBarsBack[20]);
                  //           Print(" listOfSeriesValuesBarsBack[21] " + listOfSeriesValuesBarsBack[21]);

                  //           Print("\n listOfSeriesValuesBarsBack[2000] " + listOfSeriesValuesBarsBack[2000]);

                        


                  However there is still the issue of sometimes the mode = 0 zero.

                  for example with
                  PHP Code:
                  Print("\n=== mode | Max | Count | CurrentBar ===");
                  Print(
                  "\nmode : " mode);
                  Print(
                  "max : " max);
                  Print(
                  "listOfSeriesValuesBarsBack.Count() : " listOfSeriesValuesBarsBack.Count());
                  Print(
                  "CurrentBar : " CurrentBar);
                  Print(
                  "\n=== listOfSeriesValuesBarsBack Double values for Indexes 0-19 ===");
                  Print(
                  " \nlistOfSeriesValuesBarsBack[0] " listOfSeriesValuesBarsBack[0]);
                  Print(
                  " listOfSeriesValuesBarsBack[1] " listOfSeriesValuesBarsBack[1]);
                  Print(
                  " listOfSeriesValuesBarsBack[2] " listOfSeriesValuesBarsBack[2]);
                  Print(
                  " listOfSeriesValuesBarsBack[3] " listOfSeriesValuesBarsBack[3]);
                  Print(
                  " listOfSeriesValuesBarsBack[4] " listOfSeriesValuesBarsBack[4]);
                  Print(
                  " listOfSeriesValuesBarsBack[5] " listOfSeriesValuesBarsBack[5]);
                  Print(
                  " listOfSeriesValuesBarsBack[6] " listOfSeriesValuesBarsBack[6]);
                  Print(
                  " listOfSeriesValuesBarsBack[7] " listOfSeriesValuesBarsBack[7]);
                  Print(
                  " listOfSeriesValuesBarsBack[8] " listOfSeriesValuesBarsBack[8]);
                  Print(
                  " listOfSeriesValuesBarsBack[9] " listOfSeriesValuesBarsBack[9]);
                  Print(
                  " listOfSeriesValuesBarsBack[10] " listOfSeriesValuesBarsBack[10]);
                  Print(
                  " listOfSeriesValuesBarsBack[11] " listOfSeriesValuesBarsBack[11]);
                  Print(
                  " listOfSeriesValuesBarsBack[12] " listOfSeriesValuesBarsBack[12]);
                  Print(
                  " listOfSeriesValuesBarsBack[13] " listOfSeriesValuesBarsBack[13]);
                  Print(
                  " listOfSeriesValuesBarsBack[14] " listOfSeriesValuesBarsBack[14]);
                  Print(
                  " listOfSeriesValuesBarsBack[15] " listOfSeriesValuesBarsBack[15]);
                  Print(
                  " listOfSeriesValuesBarsBack[16] " listOfSeriesValuesBarsBack[16]);
                  Print(
                  " listOfSeriesValuesBarsBack[17] " listOfSeriesValuesBarsBack[17]);
                  Print(
                  " listOfSeriesValuesBarsBack[18] " listOfSeriesValuesBarsBack[18]);
                  Print(
                  " listOfSeriesValuesBarsBack[19] " listOfSeriesValuesBarsBack[19]);
                  Print(
                  " listOfSeriesValuesBarsBack[20] " listOfSeriesValuesBarsBack[20]);
                  Print(
                  " listOfSeriesValuesBarsBack[21] " listOfSeriesValuesBarsBack[21]);
                  Print(
                  " listOfSeriesValuesBarsBack[22] " listOfSeriesValuesBarsBack[22]);
                  Print(
                  " listOfSeriesValuesBarsBack[23] " listOfSeriesValuesBarsBack[23]);
                  Print(
                  " listOfSeriesValuesBarsBack[24] " listOfSeriesValuesBarsBack[24]);
                  Print(
                  " listOfSeriesValuesBarsBack[25] " listOfSeriesValuesBarsBack[25]);
                  Print(
                  " listOfSeriesValuesBarsBack[26] " listOfSeriesValuesBarsBack[26]);
                  Print(
                  " listOfSeriesValuesBarsBack[27] " listOfSeriesValuesBarsBack[27]);
                  Print(
                  " listOfSeriesValuesBarsBack[28] " listOfSeriesValuesBarsBack[28]);
                  Print(
                  " listOfSeriesValuesBarsBack[29] " listOfSeriesValuesBarsBack[29]);
                  Print(
                  " listOfSeriesValuesBarsBack[30] " listOfSeriesValuesBarsBack[30]);
                  Print(
                  " listOfSeriesValuesBarsBack[31] " listOfSeriesValuesBarsBack[31]);
                  Print(
                  " listOfSeriesValuesBarsBack[32] " listOfSeriesValuesBarsBack[32]);
                  Print(
                  " listOfSeriesValuesBarsBack[33] " listOfSeriesValuesBarsBack[33]);
                  Print(
                  " listOfSeriesValuesBarsBack[34] " listOfSeriesValuesBarsBack[34]);
                  Print(
                  " listOfSeriesValuesBarsBack[35] " listOfSeriesValuesBarsBack[35]);
                  Print(
                  " listOfSeriesValuesBarsBack[36] " listOfSeriesValuesBarsBack[36]);
                  Print(
                  " listOfSeriesValuesBarsBack[37] " listOfSeriesValuesBarsBack[37]);
                  Print(
                  " listOfSeriesValuesBarsBack[38] " listOfSeriesValuesBarsBack[38]);
                  Print(
                  " listOfSeriesValuesBarsBack[39] " listOfSeriesValuesBarsBack[39]);
                  Print(
                  " listOfSeriesValuesBarsBack[40] " listOfSeriesValuesBarsBack[40]);
                  Print(
                  " listOfSeriesValuesBarsBack[41] " listOfSeriesValuesBarsBack[41]);
                  Print(
                  " listOfSeriesValuesBarsBack[42] " listOfSeriesValuesBarsBack[42]);
                  Print(
                  " listOfSeriesValuesBarsBack[43] " listOfSeriesValuesBarsBack[43]);
                  Print(
                  " listOfSeriesValuesBarsBack[44] " listOfSeriesValuesBarsBack[44]);
                  Print(
                  " listOfSeriesValuesBarsBack[45] " listOfSeriesValuesBarsBack[45]);
                  Print(
                  " listOfSeriesValuesBarsBack[46] " listOfSeriesValuesBarsBack[46]);
                  Print(
                  " listOfSeriesValuesBarsBack[47] " listOfSeriesValuesBarsBack[47]);
                  Print(
                  " listOfSeriesValuesBarsBack[48] " listOfSeriesValuesBarsBack[48]);
                  Print(
                  " listOfSeriesValuesBarsBack[49] " listOfSeriesValuesBarsBack[49]);
                  Print(
                  " listOfSeriesValuesBarsBack[50] " listOfSeriesValuesBarsBack[50]);
                  Print(
                  " listOfSeriesValuesBarsBack[51] " listOfSeriesValuesBarsBack[51]);
                  Print(
                  " listOfSeriesValuesBarsBack[52] " listOfSeriesValuesBarsBack[52]);
                  Print(
                  " listOfSeriesValuesBarsBack[53] " listOfSeriesValuesBarsBack[53]);
                  Print(
                  " listOfSeriesValuesBarsBack[54] " listOfSeriesValuesBarsBack[54]);
                  Print(
                  " listOfSeriesValuesBarsBack[55] " listOfSeriesValuesBarsBack[55]);
                  Print(
                  " listOfSeriesValuesBarsBack[56] " listOfSeriesValuesBarsBack[56]);
                  Print(
                  " listOfSeriesValuesBarsBack[57] " listOfSeriesValuesBarsBack[57]);
                  Print(
                  " listOfSeriesValuesBarsBack[58] " listOfSeriesValuesBarsBack[58]);
                  Print(
                  " listOfSeriesValuesBarsBack[59] " listOfSeriesValuesBarsBack[59]); 

                  Please see 2nd image attached.
                  Last edited by PaulMohn; 03-11-2022, 11:47 AM.

                  Comment


                    #69
                    How would you suggest getting whatever the 3 most frequent modes ?

                    I thought of adding a new list to store the modes within it with the count.Key when count.Value == max
                    (from insight gathered from
                    Finding the mode of a list
                    How do I initialize an empty array in C#?
                    All possible array initialization syntaxes
                    Adding values to a C# array )


                    as
                    PHP Code:
                    foreach (KeyValuePair<doubledoublecount in counts)
                    {
                       if (
                    count.Value == max)
                       {
                          
                    // Update the mode
                          
                    modes.Add(count.Key);
                          
                    modesValues modes;
                       }
                    }

                    Print(
                    "\n=== modes | arrayOfModes | modesValues ===");
                    Print(
                    "\nmodes : " modes);
                    Print(
                    "arrayOfModes : " arrayOfModes);
                    Print(
                    "modesValues : " modesValues); 

                    in this reduced script
                    PHP Code:
                    namespace NinjaTrader.NinjaScript.Indicators
                    {
                       public class 
                    Testa Indicator
                       
                    {

                         private List<
                    doublemodes;
                         private List<
                    doublemodesValues;

                         protected 
                    override void OnStateChange()
                         {
                         if (
                    State == State.SetDefaults)
                         {
                         }
                         else if (
                    State == State.Configure)
                         {
                         }
                         else if (
                    State == State.DataLoaded)
                         {
                            
                    modes = new List<double>();
                            
                    modesValues = new List<double>();
                         }

                          
                    listOfSeriesValues.Add(RangeTopRangeBottom[0]);


                          if (
                    CurrentBar <= BarsBack)
                          return;

                          
                    listOfSeriesValuesBarsBack listOfSeriesValues.GetRange(0BarsBack);


                          
                    // Initialize the return value
                          
                    double mode 1;
                          
                    double max 1;

                          if (
                    listOfSeriesValuesBarsBack != null && listOfSeriesValuesBarsBack.Count() > 0)
                          {


                              
                    Dictionary<doubledoublecounts = new Dictionary<doubledouble>();
                             foreach (
                    double element in listOfSeriesValuesBarsBack)
                             {
                                 if (
                    counts.ContainsKey(element))
                                   
                    counts[element]++;
                                 else
                                   
                    counts.Add(element1);
                             }

                             foreach (
                    KeyValuePair<doubledoublecount in counts)
                             {
                                if (
                    count.Value max)
                                {
                                  
                    // Update the mode
                                  
                    mode count.Key;
                                  
                    max count.Value;
                                }
                              }

                            foreach (
                    KeyValuePair<doubledoublecount in counts)
                            {
                                if (
                    count.Value == max)
                                {
                                  
                    // Update the mode
                                  
                    modes.Add(count.Key);
                                  
                    modesValues modes;
                                }
                            }

                           Print(
                    "\n=== modes | arrayOfModes | modesValues ===");
                           Print(
                    "\nmodes : " modes);
                           Print(
                    "arrayOfModes : " arrayOfModes);
                           Print(
                    "modesValues : " modesValues); 

                    but it's printing

                    === modes | arrayOfModes | modesValues ===

                    modes : System.Collections.Generic.List`1[System.Double]
                    arrayOfModes : System.Double[]
                    modesValues : System.Collections.Generic.List`1[System.Double]
                    please see the 3rd image

                    Why aren't the count.Key double values added to the private List<double> modes; list?
                    Why do the prints return the types instead of the values? Thanks!
                    Last edited by PaulMohn; 03-11-2022, 08:28 AM.

                    Comment


                      #70
                      Chelsea I've found a way to to get the 3 most frequent modes with help from
                      How to get first object out from List<Object> using Linq
                      (needed to access and retrieve each list element value separately from their index, one by one)

                      with
                      PHP Code:
                      namespace NinjaTrader.NinjaScript.Indicators
                      {
                         public class 
                      Testa Indicator
                         
                      {

                           private List<
                      doublemodes;
                           private List<
                      doublemodesValues;

                           protected 
                      override void OnStateChange()
                           {
                           if (
                      State == State.SetDefaults)
                           {
                           }
                           else if (
                      State == State.Configure)
                           {
                           }
                           else if (
                      State == State.DataLoaded)
                           {
                              
                      modes = new List<double>();
                              
                      modesValues = new List<double>();
                           }

                           protected 
                      override void OnBarUpdate()
                           {
                              if (
                      CurrentBar 1)
                                 return;

                            
                      Rng0Rng1[Rng0Index] = Rng0[Rng0Index] - Rng1[Rng1Index];

                            
                      listOfSeriesValues.Add(Rng0Rng1[0]);


                            if (
                      CurrentBar <= BarsBack)
                            return;

                            
                      listOfSeriesValuesBarsBack listOfSeriesValues.GetRange(0BarsBack);


                            
                      // Initialize the return value
                            
                      double mode 1;
                            
                      double max 1;

                            if (
                      listOfSeriesValuesBarsBack != null && listOfSeriesValuesBarsBack.Count() > 0)
                            {
                      //         for (int element = 0; element < listOfSeriesValuesBarsBack.Count(); element++)
                      //         {
                      //           Print("CurrentBar : " + CurrentBar);
                      ////           Print("listOfSeriesValuesBarsBack[element] : " + listOfSeriesValuesBarsBack[element]);
                      //           Print("listOfSeriesValuesBarsBack.Count() : " + listOfSeriesValuesBarsBack.Count());
                      //         }

                          // Store the number of occurences for each element
                              // Key = Double value from listOfSeriesValuesBarsBack
                              // Value = The frequency of the Double value from listOfSeriesValuesBarsBack
                          
                      Dictionary<doubledoublecounts = new Dictionary<doubledouble>();
                          
                      // Add one to the count for the occurence of a character
                          
                      foreach (double element in listOfSeriesValuesBarsBack)
                          {
                                if (
                      counts.ContainsKey(element))
                                    
                      counts[element]++;
                                else
                                    
                      counts.Add(element1);
                          }
                          
                      // Loop through the counts of each element and find the
                          // element that occurred most often


                          
                      foreach (KeyValuePair<doubledoublecount in counts)
                          {
                                if (
                      count.Value max)
                                {
                                  
                      // Update the mode
                                  
                      mode count.Key;
                                  
                      max count.Value;
                                  Print(
                      "count.Key1 : " count.Key);
                                }
                                else if (
                      count.Value == max)
                                {
                                  
                      // Collect the doubles modes and the ints values
                                  
                      modes.Add(count.Key);
                                  
                      modesValues.Add(count.Value);

                                  Print(
                      "count.Key2 : " count.Key);
                                }
                              }



                              Print(
                      "\nmodes1 : " modes[0] + " modesValues1 : " modesValues[0]);
                              Print(
                      "modes2 : " modes[1] + " modesValues2 : " modesValues[1]);
                              Print(
                      "modes3 : " modes[2] + " modesValues3 : " modesValues[2]);
                              Print(
                      "modes4 : " modes[3] + " modesValues4 : " modesValues[3]); 

                      Output
                      Click image for larger version  Name:	list elements acces by index.png Views:	0 Size:	904.0 KB ID:	1193514


                      Now all that remains is getting the .GetRange() with the latest listOfSeriesValuesBarsBack values. I'll test and be back asa. Thanks!
                      Last edited by PaulMohn; 03-12-2022, 10:30 AM.

                      Comment


                        #71
                        Chelsea I think I've found a way using the listOfSeriesValues.Count() instead of the CurrentBar as parameters for the .GetRange() method.
                        (from help from How can I find the last element in a List<>? )

                        I'd be interested in knowing how to achieve the same with CurrentBar if it is possible (I won't use CurrentBar for the current use but I'm curious to know if it's possible, I couldn't derive if it is from my testing (see demos below)).

                        After extensive prints testing, I came to the view CurrentBar is not always the latest/most recent bar on the chart. Rather it is dependent of the CurrentBar Check value.

                        As CurrentBar index 0 is set by the CurrentBar check value, or by BarsBack value.

                        Demo 1
                        https://drive.google.com/file/d/10T1...ew?usp=sharing

                        PHP Code:
                        BarsAgo[0to [4vs CurrentBar[0to [4] :

                          
                        0. CurrentBar Start:

                              If 
                        no CurrentBar Check
                                Then CurrentBar stats at the leftmost bar on the chart
                                
                        -> CurrentBar Leftmost Bar

                              
                        If CurrentBar Check is set to
                                
                        if (CurrentBar 1) return;

                                  
                        Then CurrentBar stats at Bar index 1
                                    
                        -> CurrentBar Bar indexed 1 (the 2nd leftmost bar)

                              If 
                        CurrentBar Check is set to
                                
                        if (CurrentBar 2) return;

                                  
                        Then CurrentBar stats at Bar index 2
                                    
                        -> CurrentBar Bar indexed 2 (the 3nd leftmost bar)

                              
                        etc.

                          
                        1. BarsAgo starts from the CurrentBar and goes Backward

                              
                        If no CurrentBar Check
                                Then CurrentBar stats at the leftmost bar on the chart
                                
                        -> CurrentBar Leftmost Bar

                              
                        ==>   BarsAgo start Leftmost Bar

                              
                        If CurrentBar Check is set to
                                
                        if (CurrentBar 1) return;

                                  
                        Then CurrentBar stats at Bar index 1
                                    
                        -> CurrentBar Bar indexed 1 (the 2nd leftmost bar)

                              ==>   
                        BarsAgo start Bar indexed 1 (the 2nd leftmost bar)

                              If 
                        CurrentBar Check is set to
                                
                        if (CurrentBar 2) return;

                                  
                        Then CurrentBar stats at Bar index 2
                                    
                        -> CurrentBar Bar indexed 2 (the 3nd leftmost bar)

                              ==>   
                        BarsAgo start Bar indexed 2 (the 2nd leftmost bar)

                              
                        etc

                        Demo 2
                        https://drive.google.com/file/d/10p1...ew?usp=sharing


                        Also the GetRange() index (GetRange(index, Barsback)) works for 0 and 1 but not for 2 and when with 0 and 1 the start bar is the same.

                        Demo 3
                        https://drive.google.com/file/d/1NqI...ew?usp=sharing


                        listOfSeriesValues.Count() instead of the CurrentBar

                        Click image for larger version  Name:	using list.Count() not CurrentBar.png Views:	0 Size:	878.4 KB ID:	1193588

                        PHP Code:
                              listOfSeriesValues.Add(RangeTopRangeBottom[0]);


                              if (
                        CurrentBar <= BarsBack) return;

                              
                        listOfSeriesValuesBarsBack listOfSeriesValues.GetRange(((listOfSeriesValues.C ount() - 1) - BarsBack), (BarsBack 1));

                              Print(
                        "\nTest : " listOfSeriesValues.Count()); // THE TOTAL FORM THE LIST
                              
                        Print("\nTest2 : " + (listOfSeriesValues.Count()-BarsBack)); // THE TOTAL LIST - BARSBACK
                              
                        Print("\nTest3 : " + (listOfSeriesValues.Count()-1)); // THE LAST ELEMENT IN THE LIST


                              
                        Print("\nTime : " Time[0]);
                              Print(
                        "CurrentBar : " CurrentBar);
                              Print(
                        "BarsBack : " BarsBack);
                              Print(
                        "CurrentBar - BarsBack : " + (CurrentBar - (BarsBack)));
                              Print(
                        "\nlistOfSeriesValues.Count() : " listOfSeriesValues.Count());
                        //       for (int element = 0; element < listOfSeriesValues.Count(); element++)
                        //       {
                        //         Print("\nlistOfSeriesValues[element] : " + listOfSeriesValues[element]);
                        //       }
                        //       Print("listOfSeriesValuesBarsBack.Count() : " + listOfSeriesValuesBarsBack.Count());
                        //       for (int element = 0; element < listOfSeriesValuesBarsBack.Count(); element++)
                        //       {
                        //         Print("listOfSeriesValuesBarsBack[element] : " + listOfSeriesValuesBarsBack[element]);
                        //       }
                        //       Print("BarsBack + 1 : " + (BarsBack + 1));
                        //       Print("CurrentBar - 1 : " + (CurrentBar - 1));

                        //       Print("\nTime : " + Time[0]);
                        //       Print("CurrentBar : " + CurrentBar);
                        //       Print("BarsBack : " + BarsBack);
                        //       Print("CurrentBar - BarsBack : " + (CurrentBar - (BarsBack)));

                              
                        Print("\nlistOfSeriesValues[0] : " listOfSeriesValues[0]);
                              Print(
                        "listOfSeriesValues[1] : " listOfSeriesValues[1]);
                              Print(
                        "listOfSeriesValues[2] : " listOfSeriesValues[2]);
                              Print(
                        "listOfSeriesValues[3] : " listOfSeriesValues[3]);
                              Print(
                        "listOfSeriesValues[4] : " listOfSeriesValues[4]);
                              Print(
                        "listOfSeriesValues[5] : " listOfSeriesValues[5]);

                              Print(
                        "\n=== BarsAgo[0] to [5] vs CurrentBar[0] to [5] : ===");
                              Print(
                        "\nHigh[0]-Low[0] : " + (High[0]-Low[0]));
                              Print(
                        "High[0] : " High[0]);
                              Print(
                        "Low[0] : " Low[0]);

                              Print(
                        "\nHigh[1]-Low[1] : " + (High[1]-Low[1]));
                              Print(
                        "High[1] : " High[1]);
                              Print(
                        "Low[1] : " Low[1]);

                              Print(
                        "\nHigh[2]-Low[2] : " + (High[2]-Low[2]));
                              Print(
                        "High[2] : " High[2]);
                              Print(
                        "Low[2] : " Low[2]);

                              Print(
                        "\nHigh[3]-Low[3] : " + (High[3]-Low[3]));
                              Print(
                        "High[3] : " High[3]);
                              Print(
                        "Low[3] : " Low[3]);

                              Print(
                        "\nHigh[4]-Low[4] : " + (High[4]-Low[4]));
                              Print(
                        "High[4] : " High[4]);
                              Print(
                        "Low[4] : " Low[4]);

                              Print(
                        "\nHigh[5]-Low[5] : " + (High[5]-Low[5]));
                              Print(
                        "High[5] : " High[5]);
                              Print(
                        "Low[5] : " Low[5]); 

                        Thanks!
                        Last edited by PaulMohn; 03-13-2022, 10:44 AM.

                        Comment


                          #72
                          Hello PaulMohn,

                          I'm not able to assist with custom C# calculations or code, however I have linked a suggestion in post #15.

                          This thread will remain open for any communities that would like to assist with non-NinjaTrader custom C# code.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #73
                            Ok I'll review that thanks. Could you change the thread title as requested in post #3? It would be easier to find with correct title. Thanks!

                            Comment


                              #74
                              Hello PaulMohn,

                              I will change the thread name, however this would be misleading. A Series<double> does not have a mode method. In this thread we have been discussing a List<T> and not a Series.
                              Chelsea B.NinjaTrader Customer Service

                              Comment


                                #75
                                Thanks Chelsea, what other title would be better?
                                "How to get the Statistical mode value from a Series <double> doubles values from a List<T> ? "
                                If that's better please update as this. Thanks!

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by icebergdelphi, Today, 08:57 AM
                                0 responses
                                2 views
                                0 likes
                                Last Post icebergdelphi  
                                Started by z.franck, Today, 08:37 AM
                                1 response
                                4 views
                                0 likes
                                Last Post NinjaTrader_Erick  
                                Started by manueldecastro, Yesterday, 10:26 AM
                                8 responses
                                31 views
                                0 likes
                                Last Post manueldecastro  
                                Started by Gavin45, Today, 08:40 AM
                                1 response
                                3 views
                                0 likes
                                Last Post NinjaTrader_Clayton  
                                Started by cmtjoancolmenero, 04-29-2024, 03:40 PM
                                29 responses
                                96 views
                                0 likes
                                Last Post NinjaTrader_Jesse  
                                Working...
                                X