/// <summary> /// Combines lists of returns for each instrument into a single list using parallel execution /// </summary> /// <param name="analyticCollections"></param> /// <returns></returns> public static IEnumerable <Analytic> CombineAnalyticsInParallel(IList <IList <Analytic> > analyticCollections) { AnalyticDates = new List <DateTime>(); AnalyticValues = new List <decimal>(); FinalAnalyticCollection = new List <Analytic>(); IEnumerable <Analytic> combinedAnalyticCollection = new List <Analytic>(); combinedAnalyticCollection = analyticCollections.Aggregate(combinedAnalyticCollection, (current, list) => current.Concat(list)).ToList(); var combineByDate = combinedAnalyticCollection.GroupBy(d => d.Date); foreach (var date in combineByDate) { AnalyticDates.Add(date.Key); foreach (var value in date) { AnalyticValues.Add(value.CalculatedAnalytic); } Analytic SummedAnalytic = new Analytic(); SummedAnalytic.Date = date.Key; SummedAnalytic.CalculatedAnalytic = Math.Round(AnalyticValues.Sum(), 2); FinalAnalyticCollection.Add(SummedAnalytic); AnalyticValues.Clear(); } return(FinalAnalyticCollection.AsParallel()); }
/// <summary> /// Functional method to calculate a historical analytic /// </summary> /// <param name="position"></param> /// <param name="periodQuotes"></param> /// <param name="historicalAnalytic"></param> /// <returns></returns> public static IList <Analytic> CalculateHistoricalAnalytic(this Position position, IEnumerable <SourceInstrumentQuote> periodQuotes, Func <decimal, decimal, decimal, decimal> historicalAnalytic) { List <Analytic> result = new List <Analytic>(); foreach (var period in periodQuotes) { Analytic calculatedAnalytic = new Analytic(); calculatedAnalytic.Date = period.Date; calculatedAnalytic.CalculatedAnalytic = historicalAnalytic(position.EntryPrice, position.Units, Convert.ToDecimal(period.Close)); result.Add(calculatedAnalytic); } return(result); }