Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Mode from a Series double

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

    #16
    Chelsea I've tried just adding

    listOfSeriesValues.Add(Rng0Rng1); in OnBarUpdate scope,

    with private List<double> listOfSeriesValues; at class level scope

    and List<double> listOfSeriesValues = new List<double>(); in State.Dataloaded scope.

    (Just those 3 steps without the stackoverflow list mode at class level scope method yet.)

    It throws those errors:
    NinjaScript File Error Code Line Column
    Testa.cs The best overloaded method match for 'System.Collections.Generic.List<double>.Add(doubl e)' has some invalid arguments CS1502 427 4
    NinjaScript File Error Code Line Column
    Testa.cs Argument 1: cannot convert from 'NinjaTrader.NinjaScript.Series<double>' to 'double' CS1503 427 27
    I think what the compiler is saying is
    The New List listOfSeriesValues needs/expects a single double value, but cannot add it to its elements from Rng0Rng1 <Series> Double because
    Rng0Rng1 ('NinjaTrader.NinjaScript.Series<double>') cannot be converted to a single double, since it is a series of doubles, not a single double.

    Is that the correct meaning? If not please correct.

    I then search for how to add a series of double (Rng0Rng1) (not just a single double) to a list (listOfSeriesValues).
    I found
    How to add List<> to a List<>
    Append a Lists Contents to another List C#

    which tell to use .AddRange() instead of .Add() .

    I adapted your tip to
    listOfSeriesValues.AddRange(Rng0Rng1);

    now I get those errors
    NinjaScript File Error Code Line Column
    Testa.cs The best overloaded method match for 'System.Collections.Generic.List<double>.AddRange( System.Collections.Generic.IEnumerable<double>)' has some invalid arguments CS1502 1384 19
    NinjaScript File Error Code Line Column
    Testa.cs Argument 1: cannot convert from 'NinjaTrader.NinjaScript.Series<double>' to 'System.Collections.Generic.IEnumerable<double>' CS1503 1384 47

    Why doesn't the .AddRange() method work to add the Series double values to the list?

    I understand Series<double> vs IEnumerable<double> are not the same type.
    I understand Ninjatrader Series<double> are generic collections (Series<T> / IEnumerable<T>).
    I understand generic collections need only one type of object



    From that it seems I'd need a List Series<double> instead of a List<double> to get the same object type for both the List (listOfSeriesValues) and the Series<double> (Rng0Rng1).
    but that's not working.
    I checked IEnumerable (which is required with the .AddRange(IEnumerable<T>)
    but apparently we can't add or remove items to the list with it

    https://stackoverflow.com/questions/...let-collection

    Can you please help getting the Series<double> (Rng0Rng1) added to the the List (listOfSeriesValues) ? Thanks!



    I've researched about list, IEnumerable<T> (IEnumerator<T> and GetEnumerator()), .Add(), .AddRange(), List<t> Class, Dictionary

    How to Create List in C#

    Dotnetperls/list

    C# - List<T>

    Click image for larger version  Name:	list.png Views:	0 Size:	13.6 KB ID:	1192409


    How to Add Items to a C# List

    How to Insert an Item into a C# List

    List In C#

    C# List Tutorial

    C# | List Class

    List<T>.AddRange(IEnumerable<T>) Method

    List<T>.AddRange(IEnumerable<T>) Method

    IEnumerable<T> Interface

    List<T> Class


    Interfaces in C# Explained - In-Depth guide on how to use interfaces


    IEnumerable IEnumerator Interfaces in C#







    ienumerable vs list | Difference between ienumerable and list

    Can't add items to the collection

    IEnumerable with List and Array C#


    What is a Dictionary in C#?

    Comment


      #17
      Hello PaulMohn,

      The first set of compile errors indeed mean that you are trying assign a Series<double> when a double is expected.

      Recall with accessing Price Series, that Close is the Series, and Close[0] is the most recent double from that series. The concept applies to using Series<double>'s

      Working with Price Series - https://ninjatrader.com/support/help...ice_series.htm

      AddRange is not a NinjaScript method and is a C# method. It expects an IEnumberable as input. You can check the Microsoft documentation where a List is added to another List with AddRange.

      A Series<double> is not an IEnumerable type, so you get the compiler error.

      You could consider making a new List<double> and using yourList.Add() to add individual doubles from your script.

      Comment


        #18
        Hello Jim and thank you for the tips.

        I think I can adjust (adding the [0] index to the .add() parameter) the code from you tips as

        At class level scope

        PHP Code:
        private List<double> listOfSeriesValues; 
        

        and in State.Dataloaded scope

        PHP Code:
        List<double> listOfSeriesValues = new List<double>(); 
        

        in OnBarUpdate scope

        PHP Code:
        double var4 = listOfSeriesValues.Add(Rng0Rng1[0]); 
        

        I got that compile error

        NinjaScript File Error Code Line Column
        Testa.cs Cannot implicitly convert type 'void' to 'double' CS0029 1384 19
        I could find a possible fix (separation of the .add() from the variable assignment) for from
        cannot implicitly convert from type void to Systems.Collections.Generics.List<>

        PHP Code:
        listOfSeriesValues.Add(Rng0Rng1[0]);
        
        double var4 = listOfSeriesValues; 
        

        which gave the new compile error

        NinjaScript File Error Code Line Column
        Testa.cs Cannot implicitly convert type 'System.Collections.Generic.List<double>' to 'double' CS0029 1386 19
        which I could find a possible fix by adding the [0] index (to turn the Series to it's most recent double value)

        PHP Code:
        listOfSeriesValues.Add(Rng0Rng1[0]);
        
        double var4 = listOfSeriesValues[0]; 
        

        Now I get the Log Tab error
        Time Category Message
        05/03/2022 21:18:40 Default Indicator 'Testa': Error on calling 'OnBarUpdate' method on bar 6: Object reference not set to an instance of an object.
        my BarsBack value is set to 5 (calculate only for the 5 bars back from the current bar)

        PHP Code:
        protected override void OnStateChange()
        {
             if (State == State.SetDefaults)
             {
                 BarsBack = 5;
        
        
        [Range(1, int.MaxValue), NinjaScriptProperty]
        [Display(ResourceType = typeof(Custom.Resource), Name = "BarsBack", GroupName = "Number of Bars for Stats", Order = 0)]
        public int BarsBack
        { get; set; } 
        

        and my CurrentBar check is
        PHP Code:
        protected override void OnBarUpdate()
        {
             if(CurrentBar <= BarsBack)
             {
                  return;
             } 
        

        I think the compiler refers to the CurrentBar check and is saying
        at bar 6 (the 6th bar from the starting one from the given period, 5 days worth of 1 min bars loaded on the chart, means the 6th bar from the first bars of the day 5 days ago)
        from the CurrentBar index calculation starting point,
        "I cannot calculate the 'listOfSeriesValues.Add(Rng0Rng1[0]) / double var4 = listOfSeriesValues[0];' ."
        "Please make sure there are enough bars from the starting point of my calculations, thank you."

        At bar 6 (from CurrentBar index starting point (bar 0 zero from 5 days ago)), we have 7 bars loaded on the chart.
        I think the 5 values from the BarsBack should be returned since there are enough bars back?
        But there must be another reason. Or and or my inference is not correct.

        What's the correction you'd suggest? Thanks!


        Besides how I grasp the [0] index to the series is
        It seems to be adding only the most recent single double value from the Rng0Rng1 series to the list. Isn't it?
        If so I would need all the proceeding single double values from the Rng0Rng1 series up to the first bar from the BarsBack User Input defined bar range.
        But I don't grasp how to formulate the range back instead of the single most recent double from the Rng0Rng1 series.
        Or does it do it for every single bar as is?
        meaning
        returns double value 5.71 for ( CurrentBar[0] ) i.e. the 1st bar from 5 days ago , i.e Bar[891] for the BarsAgo indexing
        returns double value 3.20 for ( CurrentBar[1] ) i.e. the 2nd bar from 5 days ago , i.e Bar[890] for the BarsAgo indexing
        returns double value 0.97 for ( CurrentBar[2] ) i.e. the 3rd bar 5 from days ago , i.e Bar[889] for the BarsAgo indexing
        returns double value 1.49 for ( CurrentBar[3] ) i.e. the4th bar 5 from days ago , i.e Bar[888] for the BarsAgo indexing
        returns double value 6.21 for ( CurrentBar[4] ) i.e. the 5th bar 5 from days ago , i.e Bar[887] for the BarsAgo indexing
        ...
        returns double value 3.94 for ( CurrentBar[891] ) i.e. the 891st bar from 5 days ago , i.e Bar[0] (the current bar from today) for the BarsAgo indexing

        If that's correct, why doesn't it return values from bars 6 and up? Thanks for your corrections again!
        Last edited by PaulMohn; 03-05-2022, 03:19 PM.

        Comment


          #19
          Hello PaulMohn,

          In State.DataLoaded:
          List<double> listOfSeriesValues = new List<double>();

          This would not be correct, and that would be my fault from my suggestion in post #15. This is re-declaring a local variable in the if statement action block, which will only exist in that action block and will not be available any where else.

          When you use a type before a variable name, you are declaring that variable to exist with that type. If you don't use the type before the variable name, then you are doing an assignment.

          double myDoubleVariable = 1; <-- this is a declaration, and an assignment.
          myDoubleVariable = 1; <-- this is just an assignment.

          So just assign a new object without re-declaring the variable.
          listOfSeriesValues = new List<double>();


          In OnBarUpdate():
          listOfSeriesValues.Add(Rng0Rng1[0]);
          double var4 = listOfSeriesValues[0];

          This would be correct.. (once listOfSeriesValues exists outside of State.DataLoaded in the scope of the class).

          Chelsea B.NinjaTrader Customer Service

          Comment


            #20
            Thank you very much Chelsea for the very helpful explanation. It didn't occur to me that the variable was declared twice. Now I know it must not be redeclared in the State.DataLoaded thanks!

            I've corrected as

            At class level scope
            PHP Code:
            private List<double> listOfSeriesValues; 
            

            in State.Dataloaded scope
            PHP Code:
            listOfSeriesValues = new List<double>(); 
            

            in OnBarUpdate scope
            PHP Code:
            listOfSeriesValues.Add(Rng0Rng1[0]);
            
            double var4 = listOfSeriesValues[0]; 
            

            now var4/listOfSeriesValues do print.

            But it prints always the same double value for every minute

            The prints for the current time
            var4 : 0.230000000000004 Time : 07/03/2022 12:07:00
            var4 : 0.230000000000004 Time : 07/03/2022 12:08:00
            var4 : 0.230000000000004 Time : 07/03/2022 12:09:00
            var4 : 0.230000000000004 Time : 07/03/2022 12:10:00
            var4 : 0.230000000000004 Time : 07/03/2022 12:11:00
            var4 : 0.230000000000004 Time : 07/03/2022 12:12:00
            var4 : 0.230000000000004 Time : 07/03/2022 12:13:00
            var4 : 0.230000000000004 Time : 07/03/2022 12:14:00
            var4 : 0.230000000000004 Time : 07/03/2022 12:15:00
            var4 : 0.230000000000004 Time : 07/03/2022 12:16:00
            var4 : 0.230000000000004 Time : 07/03/2022 12:17:00
            var4 : 0.230000000000004 Time : 07/03/2022 12:18:00
            var4 : 0.230000000000004 Time : 07/03/2022 12:19:00
            var4 : 0.230000000000004 Time : 07/03/2022 12:20:00
            var4 : 0.230000000000004 Time : 07/03/2022 12:21:00
            var4 : 0.230000000000004 Time : 07/03/2022 12:22:00
            var4 : 0.230000000000004 Time : 07/03/2022 12:23:00
            var4 : 0.230000000000004 Time : 07/03/2022 12:24:00
            var4 : 0.230000000000004 Time : 07/03/2022 12:25:00
            var4 : 0.230000000000004 Time : 07/03/2022 12:26:00
            var4 : 0.230000000000004 Time : 07/03/2022 12:27:00
            var4 : 0.230000000000004 Time : 07/03/2022 12:28:00
            var4 : 0.230000000000004 Time : 07/03/2022 12:29:00
            var4 : 0.230000000000004 Time : 07/03/2022 12:30:00
            var4 : 0.230000000000004 Time : 07/03/2022 12:31:00
            var4 : 0.230000000000004 Time : 07/03/2022 12:32:00
            The prints 4 days ago
            var4 : 0.230000000000004 Time : 02/03/2022 00:07:00
            var4 : 0.230000000000004 Time : 02/03/2022 00:08:00
            var4 : 0.230000000000004 Time : 02/03/2022 00:09:00
            var4 : 0.230000000000004 Time : 02/03/2022 00:10:00
            var4 : 0.230000000000004 Time : 02/03/2022 00:11:00
            var4 : 0.230000000000004 Time : 02/03/2022 00:12:00
            var4 : 0.230000000000004 Time : 02/03/2022 00:13:00
            var4 : 0.230000000000004 Time : 02/03/2022 00:14:00
            var4 : 0.230000000000004 Time : 02/03/2022 00:15:00
            var4 : 0.230000000000004 Time : 02/03/2022 00:16:00
            var4 : 0.230000000000004 Time : 02/03/2022 00:17:00
            var4 : 0.230000000000004 Time : 02/03/2022 00:18:00
            var4 : 0.230000000000004 Time : 02/03/2022 00:19:00
            var4 : 0.230000000000004 Time : 02/03/2022 00:20:00
            var4 : 0.230000000000004 Time : 02/03/2022 00:21:00
            I think the 0.23... double value isn't even the most recent Rng0Rng1 single double value because it doesn't update for new minutes

            The new minutes prints
            var4 : 0.230000000000004 Time : 07/03/2022 12:37:00
            var4 : 0.230000000000004 Time : 07/03/2022 12:38:00
            var4 : 0.230000000000004 Time : 07/03/2022 12:39:00
            I'm not sure what double it's returning and why it's constant.


            The prints with CurrentBar value added (it's not pointing which CurrentBar the 0.23... value stems from)
            var4 : 0.230000000000004 Time : 07/03/2022 12:51:00 CurrentBar : 4910
            var4 : 0.230000000000004 Time : 07/03/2022 12:52:00 CurrentBar : 4911
            var4 : 0.230000000000004 Time : 07/03/2022 12:53:00 CurrentBar : 4912
            var4 : 0.230000000000004 Time : 07/03/2022 12:54:00 CurrentBar : 4913
            var4 : 0.230000000000004 Time : 07/03/2022 12:55:00 CurrentBar : 4914
            var4 : 0.230000000000004 Time : 07/03/2022 12:56:00 CurrentBar : 4915
            var4 : 0.230000000000004 Time : 07/03/2022 12:57:00 CurrentBar : 4916
            var4 : 0.230000000000004 Time : 07/03/2022 12:58:00 CurrentBar : 4917
            var4 : 0.230000000000004 Time : 07/03/2022 12:59:00 CurrentBar : 4918
            var4 : 0.230000000000004 Time : 07/03/2022 13:00:00 CurrentBar : 4919
            var4 : 0.230000000000004 Time : 07/03/2022 13:01:00 CurrentBar : 4920
            var4 : 0.230000000000004 Time : 07/03/2022 13:02:00 CurrentBar : 4921
            var4 : 0.230000000000004 Time : 07/03/2022 13:03:00 CurrentBar : 4922
            var4 : 0.230000000000004 Time : 07/03/2022 13:04:00 CurrentBar : 4923
            var4 : 0.230000000000004 Time : 07/03/2022 13:05:00 CurrentBar : 4924
            My Rng0Rng1 use is as
            PHP Code:
            namespace NinjaTrader.NinjaScript.Indicators
            {
                 private Series<double> varO;
                 private Series<double> varH;
                 private Series<double> varL;
                 private Series<double> varC;
            
                 private Series<double> Rng0;
                 private Series<double> Rng1;
            
                 private Series<double> Rng0Rng1;
            
                 private List<double> listOfSeriesValues;
            
            
                 public class Testa : Indicator
                 {
                      protected override void OnStateChange()
                      {
                           if (State == State.SetDefaults)
                           {
                                BarsBack = 360;
                                Rng0Index = 0;
                                Rng1Index = 0;
                           }    
                      }
                      else if (State == State.Configure)
                      {
                      }
                      else if (State == State.DataLoaded)
                      {
                                varO = new Series<double>(this, MaximumBarsLookBack.Infinite);
                                varH = new Series<double>(this, MaximumBarsLookBack.Infinite);
                                varL = new Series<double>(this, MaximumBarsLookBack.Infinite);
                                varC = new Series<double>(this, MaximumBarsLookBack.Infinite);
            
                                Rng0 = new Series<double>(this, MaximumBarsLookBack.Infinite);
                                Rng1 = new Series<double>(this, MaximumBarsLookBack.Infinite);
            
                                Rng0Rng1 = new Series<double>(this, MaximumBarsLookBack.Infinite);
            
                                listOfSeriesValues = new List<double>();
            
                      protected override void OnBarUpdate()
                      {
                                Rng0Rng1[Rng0Index] = Rng0[Rng0Index] - Rng1[Rng1Index];
            
                                listOfSeriesValues.Add(Rng0Rng1[0]);
            
                                double var4 = listOfSeriesValues[0];
                      }
                      #region Properties
                      [Range(1, int.MaxValue), NinjaScriptProperty]
                      [Display(ResourceType = typeof(Custom.Resource), Na me = "BarsBack", GroupName = "Number of Bars for S tats", Order = 0)]
                      public int BarsBack
                      { get; set; }
                      #endregion
                }
            } 
            

            How would I get all the Rng0Rng1 double values from the BarsBack time/bars range period into the list listOfSeriesValues and just a single one (I don't know which one it is but it seems not to be the latest)? Many thanks again for your tips!

            I thought of doing something like that

            PHP Code:
            listOfSeriesValues.Add(Rng0Rng1[0] - Rng0Rng1[891]);
            
            double var4 = listOfSeriesValues[0]; 
            
            Last edited by PaulMohn; 03-07-2022, 06:19 AM.

            Comment


              #21
              Hello PaulMohn,

              double var4 = listOfSeriesValues[0];

              This is setting var4 to the first element added to the listOfSeriesValues list (the first bar).

              Is this not what you intention?

              Does this variable need to exist at all? (The values are already saved in the listOfSeriesValues list)
              Chelsea B.NinjaTrader Customer Service

              Comment


                #22
                Thanks Chelsea for the reply. I used var4 = listOfSeriesValues[0]; for the prints to check if it returned all the Rng0Rng1 double values, not a single one of them (i'm not sure it is the first one) in
                PHP Code:
                listOfSeriesValues.Add(Rng0Rng1[0]);
                
                double var4 = listOfSeriesValues[0];
                
                Print("var4 : " +var4 + " Time : " +Time[0] + " CurrentBar : " + CurrentBar); 
                

                My end use would be to retrieve the mode value from the list of Rng0Rng1 double values.
                I would try and do as shown in post #14 once I can get to the point were I do get all the Rng0Rng1 values in the list listOfSeriesValues.
                As in post #14, once I get confirmation form the prints that I do get all the Rng0Rng1 double values into the listOfSeriesValues I would then use listOfSeriesValues in an outside of OnBarUpdate method new custom method (CalcMode() at class level scope) to calculate the mode.

                Post #14 snippet
                PHP Code:
                    private double CalcMode()
                    {
                      List<double> list = new List<double>(Current Bar);
                
                      // Initialize the return value
                      double mode = default(double);
                      // Test for a null reference and an empty list
                      if (list != null && list.Count() > 0)
                      {
                        // Store the number of occurences for each element
                        Dictionary<double, double> counts = new Di ctionary<double, double>();
                        // Add one to the count for the occurence of a chara cter
                        foreach (double Rng0Rng1 in list)
                        {
                          if (counts.ContainsKey(Rng0Rng1))
                            counts[Rng0Rng1]++;
                          else
                            counts.Add(Rng0Rng1, 1);
                        }
                        // Loop through the counts of each element and find  the
                        // element that occurred most often
                        double max = 0;
                        foreach (KeyValuePair<double, double> coun t in counts)
                        {
                          if (count.Value > max)
                          {
                            // Update the mode
                            mode = count.Key;
                            max = count.Value;
                          }
                        }
                      }
                      return mode;
                    } 
                

                But since the value returned from double var4 = listOfSeriesValues[0]; is constant and of no specific range I can't proceed to the CalcMode() class level method part of the problem.

                How do I get the whole collection of double values from Rng0Rng1 Series<double> into the listOfSeriesValues list? To then try and isolate the mode value from it? Thanks!
                Last edited by PaulMohn; 03-07-2022, 10:35 AM.

                Comment


                  #23
                  Hello PaulMohn,

                  The first element added to the list is not going to change.

                  Are your trying to find the last element added to the list?

                  (list.Count() - 1)

                  Have you looped through the list and printed each element to determine that this is not being filled with the whole collection of values from Rng0Rng1?

                  How do you do know this does not contain the whole collection?

                  Did you try the suggested code in post #2 to get the mode from a list?
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #24
                    Thanks Chelsea for the questions.
                    Just to get clarification please thank you

                    You say
                    Are your trying to find the last element added to the list?
                    No, neither the first nor the last element added to the list. I'm trying to get all the elements to then extract the mode value (the value that occurs the most number of times in the whole collection).

                    Have you looped through the list and printed each element to determine that this is not being filled with the whole collection of values from Rng0Rng1?
                    No, I thought from post #12 I wouldn't need a loop with list

                    (As a tip, an int[] array is not resizable so it needs to be re-instantiated if the array size grows. A list would be resizable and the loop would not be necessary. You could just list.Add() the new value on each bar update)
                    But I'd like to try. But previously there was the type error (post #4).

                    How do you do know this does not contain the whole collection?
                    I'm not sure it doesn't contain the whole collection, but from the prints I can only see it returns a single double value. How can I check other than with the prints?

                    Did you try the suggested code in post #2 to get the mode from a list?
                    Do you mean post#4? the int array?

                    PHP Code:
                    dataSet = new int[CurrentBar];
                    for (int = 0; i < CurrentBar; i++)
                    {
                         dataSet[i] = (int)Rng0Rng1[i];
                    } 
                    

                    No I haven't as following your post #12 suggestion i would use a list to avoid the array re-instantiation need and the loop.

                    I'm wondering how to do it with a list and no loop. I'm thinking of doing that loop
                    At class level scope
                    PHP Code:
                    private double CalcMode()
                    {
                         listOfSeriesValues = new List<double>();
                         for (int element = 0; element < Rng0Rng1[0]; element++)
                         {
                              listOfSeriesValues[element] = (double)Rng0Rng1[element];
                         }
                    
                         return listOfSeriesValues;
                    } 
                    

                    The prints in OnBarUpdate scope
                    PHP Code:
                    protected override void OnBarUpdate()
                    {
                                Print("CalcMode : " + CalcMode()); 
                    

                    But I get this compile error at line return listOfSeriesValues;

                    NinjaScript File Error Code Line Column
                    Testa.cs Cannot implicitly convert type 'System.Collections.Generic.List<double>' to 'double' CS0029 201 11

                    I tried adding the [0] index as

                    PHP Code:
                    private double CalcMode()
                    {
                         listOfSeriesValues = new List<double>();
                         for (int element = 0; element < Rng0Rng1[0]; element++)
                         {
                              listOfSeriesValues[element] = (double)Rng0Rng1[element];
                         }
                    
                         return listOfSeriesValues[0];
                    } 
                    

                    Now no compile error but a Log Tab error
                    Time Category Message
                    07/03/2022 18:56:00 Default Indicator 'Testa': Error on calling 'OnBarUpdate' method on bar 6: 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.

                    My CurrentBar Check
                    PHP Code:
                    protected override void OnBarUpdate()
                    {
                         if(CurrentBar <= BarsBack)
                         {
                              return;
                         }
                    
                         Print("CalcMode : " + CalcMode()); 
                    

                    My BarsBack value = 5
                    PHP Code:
                    protected override void OnStateChange()
                    {
                         if (State == State.SetDefaults)
                         {
                              BarsBack = 5;
                         } 
                    

                    Do you think my CalcMode() method makes sense? If yes what next fix would be right to solve the bar 6 error? Thanks!

                    Comment


                      #25
                      Hello PaulMohn,

                      You are using an index of [0] to get the first element. Then you are assigning that first value in the list to a variable. This is one value. A double variable can only hold a single value.

                      You would not be able to assign a list of multiple values to a double variable which holds one value.

                      Adding the values to a list on each bar update would not need a loop. However, once the values are added how are you planning to use the collection of values?

                      Do you want to loop through all of the values to print them?

                      for (int i = 0; i < list.Count(); i++)

                      You have not confirmed if you have used the linked sample code to get the mode from a list of values. Is that what you want to do?

                      If you are using a list instead of an array, then you don't need the loop to fill the list with values. Just add the value on each bar update. The CalcMode() method does not appear necessary.


                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #26
                        Thanks for these directions
                        You are using an index of [0] to get the first element. Then you are assigning that first value in the list to a variable. This is one value. A double variable can only hold a single value.

                        You would not be able to assign a list of multiple values to a double variable which holds one value.

                        Adding the values to a list on each bar update would not need a loop. However, once the values are added how are you planning to use the collection of values?

                        Do you want to loop through all of the values to print them?

                        for (int i = 0; i < list.Count(); i++)

                        ...

                        If you are using a list instead of an array, then you don't need the loop to fill the list with values. Just add the value on each bar update. The CalcMode() method does not appear necessary.
                        Removing the Class level method CalcMode(), I've modified as
                        PHP Code:
                        protected override void OnBarUpdate()
                        {
                             listOfSeriesValues.Add(Rng0Rng1[0]);
                             for (int element = 0; element < listOfSeriesValues.Count(); element++)
                             {
                                  Print("CalcMode : " + listOfSeriesValues[0]);
                             } 
                        
                        Which prints
                        CalcMode : 0
                        CalcMode : 0
                        CalcMode : 0
                        CalcMode : 0
                        CalcMode : 0
                        CalcMode : 0
                        CalcMode : 0
                        CalcMode : 0
                        CalcMode : 0
                        CalcMode : 0
                        CalcMode : 0
                        CalcMode : 0
                        if instead that
                        PHP Code:
                        protected override void OnBarUpdate()
                        {
                             listOfSeriesValues.Add(Rng0Rng1[0]);
                             for (int element = 0; element < listOfSeriesValues.Count(); element++)
                             {
                                  Print("CalcMode : " + listOfSeriesValues);
                             } 
                        
                        it prints
                        CalcMode : System.Collections.Generic.List`1[System.Double]
                        CalcMode : System.Collections.Generic.List`1[System.Double]
                        CalcMode : System.Collections.Generic.List`1[System.Double]
                        CalcMode : System.Collections.Generic.List`1[System.Double]
                        CalcMode : System.Collections.Generic.List`1[System.Double]
                        CalcMode : System.Collections.Generic.List`1[System.Double]
                        CalcMode : System.Collections.Generic.List`1[System.Double]
                        CalcMode : System.Collections.Generic.List`1[System.Double]
                        CalcMode : System.Collections.Generic.List`1[System.Double]
                        CalcMode : System.Collections.Generic.List`1[System.Double]
                        CalcMode : System.Collections.Generic.List`1[System.Double]
                        CalcMode : System.Collections.Generic.List`1[System.Double]
                        CalcMode : System.Collections.Generic.List`1[System.Double]
                        You have not confirmed if you have used the linked sample code to get the mode from a list of values. Is that what you want to do?
                        I would like to try using this snippet to retrieve the mode value (once I can confirm I get all the Rng0Rng1 double values into the list listOfSeriesValues)
                        PHP Code:
                            private double CalcMode()
                            {
                              List<double> list = new List<double>(Current  Bar);
                        
                              // Initialize the return value
                              double mode = default(double);
                              // Test for a null reference and an empty list
                              if (list != null && list.Count() > 0)
                              {
                                // Store the number of occurences for each element
                                Dictionary<double, double> counts = new Di  ctionary<double, double>();
                                // Add one to the count for the occurence of a chara  cter
                                foreach (double Rng0Rng1 in list)
                                {
                                  if (counts.ContainsKey(Rng0Rng1))
                                    counts[Rng0Rng1]++;
                                  else
                                    counts.Add(Rng0Rng1, 1);
                                }
                                // Loop through the counts of each element and find   the
                                // element that occurred most often
                                double max = 0;
                                foreach (KeyValuePair<double, double> coun  t in counts)
                                {
                                  if (count.Value > max)
                                  {
                                    // Update the mode
                                    mode = count.Key;
                                    max = count.Value;
                                  }
                                }
                              } 
                        
                        However, once the values are added how are you planning to use the collection of values?

                        Do you want to loop through all of the values to print them?
                        I'd like to loop through it or any other way that can retrieve the mode value as end use. But I'd like to (if there's no other way) prior print all the available Rng0Rng1 double values to verify they are the correct ones. Thanks!
                        Last edited by PaulMohn; 03-07-2022, 01:39 PM.

                        Comment


                          #27
                          Hello PaulMohn,

                          As the values are assigned to a new element in the list, you can loop through the list to confirm the values.

                          for (int i = 0; i < list.Count(); i++)
                          {
                          Print(list[i]);
                          }
                          Last edited by NinjaTrader_ChelseaB; 03-07-2022, 02:26 PM.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #28
                            Thanks for the direction (I didn't think of using the element as index for the print action)

                            I added the element as index for the print action
                            PHP Code:
                            protected override void OnBarUpdate()
                            {
                                 listOfSeriesValues.Add(Rng0Rng1[0]);
                                 for (int element = 0; element < listOfSeriesV alues.Count(); element++)
                                 {
                                      Print("CalcMode : " + listOfSeriesValues[element] );
                                 } 
                            
                            It still return
                            CalcMode : 0
                            CalcMode : 0
                            CalcMode : 0
                            CalcMode : 0
                            CalcMode : 0
                            CalcMode : 0
                            CalcMode : 0
                            CalcMode : 0
                            CalcMode : 0
                            CalcMode : 0
                            CalcMode : 0
                            CalcMode : 0
                            CalcMode : 0
                            CalcMode : 0
                            CalcMode : 0
                            Why aren't the Rng0Rng1 double values printed instead of 0 zero? Thanks!

                            If that's useful I get this at the top of the prints
                            You have reached the maximum threshold of the NinjaScript Output window. Some of your output messages have been suppressed.
                            Last edited by PaulMohn; 03-07-2022, 02:01 PM.

                            Comment


                              #29
                              Oh, thanks Chelsea! I just realized why it didn't print the values in spite of having the element index in the action.
                              It simply was because I had kept the snippet at the top/ above the Rng0Rng1 assignement as

                              PHP Code:
                              protected override void OnBarUpdate()
                              {
                                   ...
                              
                                   listOfSeriesValues.Add(Rng0Rng1[0]);
                                   for (int element = 0; element < listOfSeriesV  alues.Count(); element++)
                                   {
                                        Print("CalcMode : " + listOfSeriesValues[element] );
                                   }
                              
                                   ...
                              
                                   Rng0Rng1[Rng0Index] = Rng0[Rng0Index] - Rng1[Rng1Index]; 
                              

                              I corrected as

                              PHP Code:
                              protected override void OnBarUpdate()
                              {
                                   ...
                              
                                   Rng0Rng1[Rng0Index] = Rng0[Rng0Index] - Rng1[Rng1Index];
                                   ...
                              
                                   listOfSeriesValues.Add(Rng0Rng1[0]);
                                   for (int element = 0; element < listOfSeriesV alues.Count(); element++)
                                   {
                                        Print("CalcMode : " + listOfSeriesValues[element] );
                                   } 
                              

                              Now it prints some double values

                              CalcMode : 0.00999999999999091
                              CalcMode : 0.039999999999992
                              CalcMode : 0.269999999999996
                              CalcMode : 0.140000000000001
                              CalcMode : 0.240000000000009
                              CalcMode : 0.209999999999994
                              CalcMode : 0.00999999999999091
                              CalcMode : 0.0300000000000011
                              CalcMode : 0.109999999999999
                              CalcMode : 0.0300000000000011
                              CalcMode : 0.0700000000000074
                              CalcMode : 0.200000000000003
                              CalcMode : 0.120000000000005
                              CalcMode : 0.120000000000005
                              CalcMode : 0.100000000000009
                              Many thanks for the direction, I'll check the doubles values now and try and get the mode snippet working next. Thanks!
                              Last edited by PaulMohn; 03-07-2022, 02:25 PM.

                              Comment


                                #30
                                Ok from this snippet
                                PHP Code:
                                        listOfSeriesValues.Add(Rng0Rng1[0]);
                                
                                        // Initialize the return value
                                        double mode = 1;
                                        double max = 0;
                                
                                        // Test for a null reference and an empty list and limit to the BarsBack period
                                        if (listOfSeriesValues != null
                                            && listOfSeriesValues.Count() > 0
                                            && listOfSeriesValues.Count() <= BarsBack)
                                        {
                                          // Store the number of occurences for each element
                                          Dictionary<double, double> counts = new Dictionary<double, double>();
                                          // Add one to the count for the occurence of a doubles values from Rng0Rng1
                                          foreach (double element in listOfSeriesValues)
                                          {
                                            if (counts.ContainsKey(element))
                                            {
                                              counts[element]++;
                                            }
                                            else
                                            {
                                              counts.Add(element, 1);
                                            }
                                          }
                                          // Loop through the counts of each element and find the
                                          // element that occurred most often
                                          foreach (KeyValuePair<double, double> count in counts)
                                          {
                                            if (count.Value > max)
                                            {
                                              // Update the mode
                                              mode = count.Key;
                                              max = count.Value;
                                            }
                                          }
                                        }
                                
                                        Print("CMode : " + mode + " Time : " + Time[0]);
                                        Print("CMax : " + max + " Time : " + Time[0]); 
                                

                                I'm getting those prints

                                Earliest prints
                                CMode : 0.0300000000000011 Time : 03/03/2022 06:59:00
                                CMax : 9 Time : 03/03/2022 06:59:00
                                CMode : 0.0300000000000011 Time : 03/03/2022 07:00:00
                                CMax : 9 Time : 03/03/2022 07:00:00
                                CMode : 0.0300000000000011 Time : 03/03/2022 07:01:00
                                CMax : 9 Time : 03/03/2022 07:01:00
                                CMode : 0.0300000000000011 Time : 03/03/2022 07:02:00
                                CMax : 9 Time : 03/03/2022 07:02:00
                                CMode : 0.0300000000000011 Time : 03/03/2022 07:03:00
                                CMax : 9 Time : 03/03/2022 07:03:00
                                CMode : 0.0300000000000011 Time : 03/03/2022 07:04:00
                                CMax : 9 Time : 03/03/2022 07:04:00
                                CMode : 0.0300000000000011 Time : 03/03/2022 07:05:00
                                CMax : 9 Time : 03/03/2022 07:05:00
                                CMode : 0.0300000000000011 Time : 03/03/2022 07:06:00
                                CMax : 9 Time : 03/03/2022 07:06:00
                                CMode : 0.0300000000000011 Time : 03/03/2022 07:07:00
                                CMax : 9 Time : 03/03/2022 07:07:00
                                CMode : 0 Time : 03/03/2022 07:08:00
                                CMax : 10 Time : 03/03/2022 07:08:00
                                CMode : 0 Time : 03/03/2022 07:09:00
                                CMax : 10 Time : 03/03/2022 07:09:00
                                and latest prints

                                CMode : 1 Time : 08/03/2022 13:06:00
                                CMax : 0 Time : 08/03/2022 13:06:00
                                CMode : 1 Time : 08/03/2022 13:07:00
                                CMax : 0 Time : 08/03/2022 13:07:00
                                CMode : 1 Time : 08/03/2022 13:08:00
                                CMax : 0 Time : 08/03/2022 13:08:00
                                CMode : 1 Time : 08/03/2022 13:09:00
                                CMax : 0 Time : 08/03/2022 13:09:00
                                CMode : 1 Time : 08/03/2022 13:10:00
                                CMax : 0 Time : 08/03/2022 13:10:00
                                CMode : 1 Time : 08/03/2022 13:11:00
                                CMax : 0 Time : 08/03/2022 13:11:00
                                CMode : 1 Time : 08/03/2022 13:12:00
                                CMax : 0 Time : 08/03/2022 13:12:00


                                Getting two problems.

                                1st problem
                                The current number of CMode prints is total 4575 (9150/2)

                                Click image for larger version

Name:	9150.png
Views:	159
Size:	941.8 KB
ID:	1192792


                                It suggest the mode snippet is taking the entire available bars on the chart doubles collection as collection (5 days worth of 1 min bars).
                                How would I limit it to the BarsBack User input defined variable value instead?

                                I used this && listOfSeriesValues.Count() <= BarsBack as condition but it seems it's not limiting the number of bars accordingly.



                                2nd problem
                                It seems to be telling the mode is zero as we get to recent counts.

                                I tried only getting modes values > 0.00001 as
                                PHP Code:
                                double modeplus = 0;
                                
                                if(mode > 0.00001)
                                {
                                     modeplus = mode;
                                }
                                
                                Print("Modeplus : " + modeplus + " Time : " + Time[0]); 
                                

                                But it prints 1s now for recent bars
                                Modeplus : 1 Time : 08/03/2022 13:17:00
                                Modeplus : 1 Time : 08/03/2022 13:18:00
                                Modeplus : 1 Time : 08/03/2022 13:19:00
                                Modeplus : 1 Time : 08/03/2022 13:20:00
                                Modeplus : 1 Time : 08/03/2022 13:21:00
                                Modeplus : 1 Time : 08/03/2022 13:22:00
                                Modeplus : 1 Time : 08/03/2022 13:23:00
                                Modeplus : 1 Time : 08/03/2022 13:24:00
                                Modeplus : 1 Time : 08/03/2022 13:25:00
                                Modeplus : 1 Time : 08/03/2022 13:26:00
                                Modeplus : 1 Time : 08/03/2022 13:27:00
                                vs 0 and > 0 for earlier bars
                                Modeplus : 0.0300000000000011 Time : 03/03/2022 06:58:00
                                Modeplus : 0.0300000000000011 Time : 03/03/2022 06:59:00
                                Modeplus : 0.0300000000000011 Time : 03/03/2022 07:00:00
                                Modeplus : 0.0300000000000011 Time : 03/03/2022 07:01:00
                                Modeplus : 0.0300000000000011 Time : 03/03/2022 07:02:00
                                Modeplus : 0.0300000000000011 Time : 03/03/2022 07:03:00
                                Modeplus : 0.0300000000000011 Time : 03/03/2022 07:04:00
                                Modeplus : 0.0300000000000011 Time : 03/03/2022 07:05:00
                                Modeplus : 0.0300000000000011 Time : 03/03/2022 07:06:00
                                Modeplus : 0.0300000000000011 Time : 03/03/2022 07:07:00
                                Modeplus : 0 Time : 03/03/2022 07:08:00
                                Modeplus : 0 Time : 03/03/2022 07:09:00
                                Modeplus : 0 Time : 03/03/2022 07:10:00
                                Modeplus : 0 Time : 03/03/2022 07:11:00
                                Modeplus : 0 Time : 03/03/2022 07:12:00
                                Why does it still prints values < 0.00001?
                                Why does it prints 1s for recent bars (it seems it's wrong value as it's not a double and seems unlikely to be the mode)? Thanks!

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                                0 responses
                                624 views
                                0 likes
                                Last Post Geovanny Suaza  
                                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                                0 responses
                                359 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
                                562 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by RFrosty, 01-28-2026, 06:49 PM
                                0 responses
                                567 views
                                1 like
                                Last Post RFrosty
                                by RFrosty
                                 
                                Working...
                                X