ref. https://forum.ninjatrader.com/forum/...ed-list-method
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") );
}
}​
((7.7 + 6.6 + 5.5 + 4.4 + 3.3 +2.2) / 6) = 5.50
Tested ok:
ref. double.NaN
ref. AsQueryable()

Comment