// aggregation (quantization)
    /// <include file='./info.xml' path='info/type[@name="Aggregate"]/*' />
    ///
    public static IEnumerable <Quote> Aggregate <TQuote>(
        this IEnumerable <TQuote> quotes,
        PeriodSize newSize)
        where TQuote : IQuote
    {
        if (newSize != PeriodSize.Month)
        {
            // parameter conversion
            TimeSpan newTimeSpan = newSize.ToTimeSpan();

            // convert
            return(quotes.Aggregate(newTimeSpan));
        }
        else // month
        {
            return(quotes
                   .OrderBy(x => x.Date)
                   .GroupBy(x => new DateTime(x.Date.Year, x.Date.Month, 1))
                   .Select(x => new Quote
            {
                Date = x.Key,
                Open = x.First().Open,
                High = x.Max(t => t.High),
                Low = x.Min(t => t.Low),
                Close = x.Last().Close,
                Volume = x.Sum(t => t.Volume)
            }));
        }
    }
        // quantization
        public static IEnumerable <Quote> Aggregate <TQuote>(
            this IEnumerable <TQuote> history,
            PeriodSize newSize)
            where TQuote : IQuote
        {
            TimeSpan newPeriod = newSize.ToTimeSpan();

            return

                // handle no history scenario
                (history == null || !history.Any() ? new List <Quote>()

                 // validate parameters
                : newPeriod == TimeSpan.Zero ?

                 throw new ArgumentOutOfRangeException(nameof(newSize), newSize,
                                                       "History Aggregation must use a New Size value of at least " +
                                                       "one minute and not more than one week.")

                 // return aggregation
                : history
                 .OrderBy(x => x.Date)
                 .GroupBy(x => x.Date.RoundDown(newPeriod))
                 .Select(x => new Quote
            {
                Date = x.Key,
                Open = x.First().Open,
                High = x.Max(t => t.High),
                Low = x.Min(t => t.Low),
                Close = x.Last().Close,
                Volume = x.Sum(t => t.Volume)
            }));
        }
Beispiel #3
0
    // aggregation (quantization)
    /// <include file='./info.xml' path='info/type[@name="Aggregate"]/*' />
    ///
    public static IEnumerable <Quote> Aggregate <TQuote>(
        this IEnumerable <TQuote> quotes,
        PeriodSize newSize)
        where TQuote : IQuote
    {
        // parameter conversion
        TimeSpan newTimeSpan = newSize.ToTimeSpan();

        // convert
        return(quotes.Aggregate(newTimeSpan));
    }