Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Latest 6 List's items Average number Double Method

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

    Latest 6 List's items Average number Double Method

    ref. https://forum.ninjatrader.com/forum/...oubles-in-list
    ref. https://forum.ninjatrader.com/forum/...ed-list-method

    PHP Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
                        
    public class Program
    {
        public static void Main()
        {
            List<double> duplList = new List<double> {1.1, 2.2, 2.2, 3.3, 4.4, 5.5, 5.5, 6.6, 7.7};
            
            double AvgShortListDouble(List<double> _List, int _ListItems)
            {                      
                if (_List.Count < 1)
                    return double.NaN;
                
                List<double> _avgshortList = new List<double>();
                for (int i = _List.Count - _ListItems; i < _List.Count; i++)
                {
                    _avgshortList.Add(_List[i]);
                }
                
                double _avg = _avgshortList.Count > 0 ?
                              Queryable.Average(_avgshortList.AsQueryable())
                              : double.NaN;
                return _avg;
            }
            
            Console.WriteLine(" ");
    
            Console.WriteLine("Latest 6 List's items Average number \n ((7.7 + 6.6 + 5.5 + 5.5 + 4.4 + 3.3) / 6)  = "
                              + AvgShortListDouble(duplList, 6).ToString("N2") );
        }
    }&#8203; 
    
    Output:

    Latest 6 List's items Average number
    ((7.7 + 6.6 + 5.5 + 4.4 + 3.3 +2.2) / 6) = 5.50​

    Tested ok:​

    Test your C# code online with .NET Fiddle code editor.


    ref. ​double.NaN
    I want to make code that will on the first click start rotorSpeed stopwatch then on the second click add rotorSpeed.ElapsedMilliseconds to list. On the second click resets stopwatch and starts timing


    ref. ​AsQueryable()
    Is the purpose of AsQueryable() just so you can pass around an IEnumerable to methods that might expect IQueryable, or is there a useful reason to represent IEnumerable as IQueryable? For example, ...
    Last edited by PaulMohn; 09-19-2024, 01:02 PM.

    #2
    Average from Deduplicated List Method

    PHP Code:
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
                        
    public class Program
    {
        public static void Main()
        {
            List<double> duplList = new List<double> {1.1, 2.2, 2.2, 3.3, 4.4, 5.5, 5.5, 6.6, 7.7};
            
            Console.WriteLine(" ");
            
            for (int i = 0; i < duplList.Count; i++)
            {
                Console.WriteLine("DuplList List Items indices: " + i + " DuplList List Items: " + duplList[i]);
            }
            
            List<double> DeDuplList(List<double> _List)
            {                      
                if (_List.Count < 1)
                    return new List<double>();
                
                for (int i = 0 ; i < _List.Count -1 ; i++)
                {      
                    double st1  = _List[i+1];
                    double nd2  = _List[i];
                    
                    if (st1 == nd2)
                    {
                        _List.Remove(st1);
                    }
                }
                
                return _List;
            }
            
            duplList = DeDuplList(duplList);
            
            double AvgShortListDouble(List<double> _List, int _ListItems)
            {                      
                if (_List.Count < 1)
                    return double.NaN;
                
                List<double> _avgshortList = new List<double>();
                for (int i = _List.Count - _ListItems; i < _List.Count; i++)
                {
                    _avgshortList.Add(_List[i]);
                }
                
                double _avg = _avgshortList.Count > 0 ?
                              Queryable.Average(_avgshortList.AsQueryable())
                              : double.NaN;
                return _avg;
            }
            
            Console.WriteLine(" ");
            
            for (int i = 0; i < duplList.Count; i++)
            {
                Console.WriteLine("DeDuplList List Items indices: " + i + " DeDuplList List Items: " + duplList[i]);
            }
            
            Console.WriteLine(" ");
    
            Console.WriteLine("Latest 6 List's items Average number \n ((7.7 + 6.6 + 5.5 + 4.4 + 3.3 +2.2) / 6)  = "
                              + AvgShortListDouble(duplList, 6).ToString("N2") );
        }
    }&#8203; 
    

    Output:


    DuplList List Items indices: 0 DuplList List Items: 1.1
    DuplList List Items indices: 1 DuplList List Items: 2.2
    DuplList List Items indices: 2 DuplList List Items: 2.2
    DuplList List Items indices: 3 DuplList List Items: 3.3
    DuplList List Items indices: 4 DuplList List Items: 4.4
    DuplList List Items indices: 5 DuplList List Items: 5.5
    DuplList List Items indices: 6 DuplList List Items: 5.5
    DuplList List Items indices: 7 DuplList List Items: 6.6
    DuplList List Items indices: 8 DuplList List Items: 7.7

    DeDuplList List Items indices: 0 DeDuplList List Items: 1.1
    DeDuplList List Items indices: 1 DeDuplList List Items: 2.2
    DeDuplList List Items indices: 2 DeDuplList List Items: 3.3
    DeDuplList List Items indices: 3 DeDuplList List Items: 4.4
    DeDuplList List Items indices: 4 DeDuplList List Items: 5.5
    DeDuplList List Items indices: 5 DeDuplList List Items: 6.6
    DeDuplList List Items indices: 6 DeDuplList List Items: 7.7

    Latest 6 List's items Average number
    ((7.7 + 6.6 + 5.5 + 4.4 + 3.3 +2.2) / 6) = 4.95​
    Tested Ok:

    Test your C# code online with .NET Fiddle code editor.


    ref. https://forum.ninjatrader.com/forum/...ed-list-method

    Comment


      #3
      Originally posted by PaulMohn View Post
      ref. https://forum.ninjatrader.com/forum/...oubles-in-list
      ref. https://forum.ninjatrader.com/forum/...ed-list-method

      PHP Code:
      using System;
      using System.Collections.Generic;
      using System.Linq;
      
      public class Program
      {
      public static void Main()
      {
      List<double> duplList = new List<double> {1.1, 2.2, 2.2, 3.3, 4.4, 5.5, 5.5, 6.6, 7.7};
      
      double AvgShortListDouble(List<double> _List, int _ListItems)
      {
      if (_List.Count < 1)
      return double.NaN;
      
      List<double> _avgshortList = new List<double>();
      for (int i = _List.Count - _ListItems; i < _List.Count; i++)
      {
      _avgshortList.Add(_List[i]);
      }
      
      double _avg = _avgshortList.Count > 0 ?
      Queryable.Average(_avgshortList.AsQueryable())
      : double.NaN;
      return _avg;
      }
      
      Console.WriteLine(" ");
      
      Console.WriteLine("Latest 6 List's items Average number \n ((7.7 + 6.6 + 5.5 + 5.5 + 4.4 + 3.3) / 6) = "
      + AvgShortListDouble(duplList, 6).ToString("N2") );
      }
      }&#8203; 
      
      Output:



      Tested ok:​

      Test your C# code online with .NET Fiddle code editor.

      Initial version without Double Callable Method:

      PHP Code:
      
      using System;
      using System.Collections.Generic;
      using System.Linq;
                          
      public class Program
      {
          public static void Main()
          {
              List<double> duplList = new List<double> {1.1, 2.2, 2.2, 3.3, 4.4, 5.5, 5.5, 6.6, 7.7};
              
              List<double> avgshortList = new List<double>();
              for (int i = duplList.Count - 6; i < duplList.Count; i++)
              {
                  avgshortList.Add(duplList[i]);
              }
      
              double avg = avgshortList.Count > 0 ?
                  Queryable.Average(avgshortList.AsQueryable())
                  : double.NaN;
              
              Console.WriteLine(" ");
      
              Console.WriteLine("Latest 6 List's items Average number \n ((7.7 + 6.6 + 5.5 + 5.5 + 4.4 + 3.3) / 6)  = "
                                + avg.ToString("N2") );
          }
      }&#8203; 
      

      Output:
      Latest 6 List's items Average number
      ((7.7 + 6.6 + 5.5 + 5.5 + 4.4 + 3.3) / 6) = 5.50​
      Tested ok:​
      Test your C# code online with .NET Fiddle code editor.



      Comment


        #4
        Originally posted by PaulMohn View Post
        Average from Deduplicated List Method

        PHP Code:
        
        using System;
        using System.Collections.Generic;
        using System.Linq;
        
        public class Program
        {
        public static void Main()
        {
        List<double> duplList = new List<double> {1.1, 2.2, 2.2, 3.3, 4.4, 5.5, 5.5, 6.6, 7.7};
        
        Console.WriteLine(" ");
        
        for (int i = 0; i < duplList.Count; i++)
        {
        Console.WriteLine("DuplList List Items indices: " + i + " DuplList List Items: " + duplList[i]);
        }
        
        List<double> DeDuplList(List<double> _List)
        {
        if (_List.Count < 1)
        return new List<double>();
        
        for (int i = 0 ; i < _List.Count -1 ; i++)
        {
        double st1 = _List[i+1];
        double nd2 = _List[i];
        
        if (st1 == nd2)
        {
        _List.Remove(st1);
        }
        }
        
        return _List;
        }
        
        duplList = DeDuplList(duplList);
        
        double AvgShortListDouble(List<double> _List, int _ListItems)
        {
        if (_List.Count < 1)
        return double.NaN;
        
        List<double> _avgshortList = new List<double>();
        for (int i = _List.Count - _ListItems; i < _List.Count; i++)
        {
        _avgshortList.Add(_List[i]);
        }
        
        double _avg = _avgshortList.Count > 0 ?
        Queryable.Average(_avgshortList.AsQueryable())
        : double.NaN;
        return _avg;
        }
        
        Console.WriteLine(" ");
        
        for (int i = 0; i < duplList.Count; i++)
        {
        Console.WriteLine("DeDuplList List Items indices: " + i + " DeDuplList List Items: " + duplList[i]);
        }
        
        Console.WriteLine(" ");
        
        Console.WriteLine("Latest 6 List's items Average number \n ((7.7 + 6.6 + 5.5 + 4.4 + 3.3 +2.2) / 6) = "
        + AvgShortListDouble(duplList, 6).ToString("N2") );
        }
        }&#8203; 
        

        Output:



        Tested Ok:

        Test your C# code online with .NET Fiddle code editor.


        ref. https://forum.ninjatrader.com/forum/...ed-list-method

        Initial version without Double Callable Method with Deduplicated List:

        PHP Code:
        using System;
        using System.Collections.Generic;
        using System.Linq;
                            
        public class Program
        {
            public static void Main()
            {
                List<double> duplList = new List<double> {1.1, 2.2, 2.2, 3.3, 4.4, 5.5, 5.5, 6.6, 7.7};
                
                Console.WriteLine(" ");
                
                for (int i = 0; i < duplList.Count; i++)
                {
                    Console.WriteLine("DuplList List Items indices: " + i + " DuplList List Items: " + duplList[i]);
                }
                    
                for (int i = 0 ; i < duplList.Count -1 ; i++)
                {      
                    double st1  = duplList[i+1];
                    double nd2  = duplList[i];
        
                    if (st1 == nd2)
                    {
                        duplList.Remove(st1);
                    }
                }
                
                Console.WriteLine(" ");
                
                for (int i = 0; i < duplList.Count; i++)
                {
                    Console.WriteLine("DeDuplList List Items indices: " + i + " DeDuplList List Items: " + duplList[i]);
                }
                
                List<double> avgshortList = new List<double>();
                for (int i = duplList.Count - 6; i < duplList.Count; i++)
                {
                    avgshortList.Add(duplList[i]);
                }
        
                double avg = avgshortList.Count > 0 ?
                    Queryable.Average(avgshortList.AsQueryable())
                    : double.NaN;
                
                Console.WriteLine(" ");
        
                Console.WriteLine("Latest 6 List's items Average number \n ((7.7 + 6.6 + 5.5 + 4.4 + 3.3 + 2.2) / 6)  = "
                                  + avg.ToString("N2") );
            }
        }&#8203; 
        

        Output:


        DuplList List Items indices: 0 DuplList List Items: 1.1
        DuplList List Items indices: 1 DuplList List Items: 2.2
        DuplList List Items indices: 2 DuplList List Items: 2.2
        DuplList List Items indices: 3 DuplList List Items: 3.3
        DuplList List Items indices: 4 DuplList List Items: 4.4
        DuplList List Items indices: 5 DuplList List Items: 5.5
        DuplList List Items indices: 6 DuplList List Items: 5.5
        DuplList List Items indices: 7 DuplList List Items: 6.6
        DuplList List Items indices: 8 DuplList List Items: 7.7

        DeDuplList List Items indices: 0 DeDuplList List Items: 1.1
        DeDuplList List Items indices: 1 DeDuplList List Items: 2.2
        DeDuplList List Items indices: 2 DeDuplList List Items: 3.3
        DeDuplList List Items indices: 3 DeDuplList List Items: 4.4
        DeDuplList List Items indices: 4 DeDuplList List Items: 5.5
        DeDuplList List Items indices: 5 DeDuplList List Items: 6.6
        DeDuplList List Items indices: 6 DeDuplList List Items: 7.7

        Latest 6 List's items Average number
        ((7.7 + 6.6 + 5.5 + 4.4 + 3.3 + 2.2) / 6) = 4.95​
        Tested ok:​

        Test your C# code online with .NET Fiddle code editor.

        Comment


          #5
          Print Sublist with Linq - Sum, Average Sublist with Linq

          https://forum.ninjatrader.com/forum/...list-with-linq

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          576 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          334 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          101 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          553 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          551 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X