/// <summary> /// 取得投资统计共通信息 /// </summary> /// <param name="source"></param> /// <param name="stockClosePrices"></param> /// <returns></returns> public static InvestStatisticsCommonEntity GetInvestStatisticsCommonInfo(this IList <DeliveryRecord> source, IList <TKLineToday> stockClosePrices) { if (source?.Count == 0 || stockClosePrices?.Count == 0) { return(new InvestStatisticsCommonEntity()); } //成交额 decimal dealAmount = source.ToList().CalculateDealAmount(); //持仓市值 decimal positionValue = 0; //持仓收益 decimal positionProfit = 0; //累计发生金额 decimal accumulatedActualAmount = 0; //累计收益额 decimal accumulatedProfit = 0; accumulatedActualAmount = source.Sum(x => x.ActualAmount); var recordsByStock = source.GroupBy(x => x.StockCode); foreach (var stockGroup in recordsByStock) { ////各只股票发生金额 //decimal actualAmountPerStock = stockGroup.Sum(x => x.ActualAmount); //accumulatedActualAmount += actualAmountPerStock; //各只股票的持股数 decimal holdingVolume = stockGroup.Sum(x => x.DealVolume); decimal closePrice = 0; if (holdingVolume != 0 && stockClosePrices.Any()) { //System.Diagnostics.Debug.WriteLine(date.ToString() + ": " + stockGroup.Key); closePrice = (stockClosePrices.LastOrDefault(x => x.StockCode.Trim() == stockGroup.Key) ?? new TKLineToday()).Close; } //持仓市值 positionValue += Math.Abs(holdingVolume) * closePrice; //持仓收益 positionProfit += holdingVolume * closePrice; } accumulatedProfit = accumulatedActualAmount + positionProfit; var info = new InvestStatisticsCommonEntity() { DealAmount = dealAmount, PositionValue = positionValue, PositionProfit = positionProfit, AccumulatedActualAmount = accumulatedActualAmount, AccumulatedProfit = accumulatedProfit, }; return(info); }
/// <summary> /// 取得交易记录的每日投资统计共通信息 /// </summary> /// <param name="source"></param> /// <param name="tradeDates"></param> /// <param name="stockClosePrices"></param> /// <returns></returns> public static IList <InvestStatisticsCommonEntity> GetDailyInvestStatisticsCommonInfo(this IList <DailyRecord> source, IList <DateTime> tradeDates, IList <TKLineToday> stockClosePrices) { var result = new List <InvestStatisticsCommonEntity>(); if (source == null || !source.Any() || tradeDates == null) { return(result); } tradeDates = tradeDates.OrderBy(x => x).ToList(); decimal baseDateAccumulatedProfit = 0; DateTime?nextTradeDate = null; foreach (var date in tradeDates) { var currentRecords = source.Where(x => x.TradeDate < date.AddDays(1)); var currentStockClosePrices = stockClosePrices.Where(x => x.TradeDate == date); var info = new InvestStatisticsCommonEntity() { TradeDate = date, DealAmount = currentRecords.Where(x => x.TradeDate == date).ToList().CalculateDealAmount(), PositionValue = 0, PositionProfit = 0, AccumulatedActualAmount = currentRecords.Sum(x => x.ActualAmount), AccumulatedProfit = 0, AnnualProfit = 0, }; var recordsByStock = currentRecords.GroupBy(x => x.StockCode); foreach (var stockGroup in recordsByStock) { //各只股票的持股数 decimal holdingVolume = stockGroup.Sum(x => x.DealVolume); decimal closePrice = 0; if (holdingVolume != 0 && currentStockClosePrices.Any()) { closePrice = (currentStockClosePrices.LastOrDefault(x => x.StockCode.Trim() == stockGroup.Key) ?? new TKLineToday()).Close; } //持仓市值 info.PositionValue += Math.Abs(holdingVolume) * closePrice; //持仓收益 info.PositionProfit += holdingVolume * closePrice; } info.AccumulatedProfit = info.AccumulatedActualAmount + info.PositionProfit; info.AnnualProfit = info.AccumulatedProfit - baseDateAccumulatedProfit; nextTradeDate = tradeDates.Where(x => x > date).FirstOrDefault(); if (nextTradeDate.HasValue && nextTradeDate.Value.Year > date.Year) { baseDateAccumulatedProfit = info.AccumulatedProfit; } result.Add(info); } return(result); }
/// <summary> /// 取得每日投资统计共通信息 /// </summary> /// <param name="source"></param> /// <param name="tradeDates"></param> /// <param name="stockClosePrices"></param> /// <returns></returns> public static IList <InvestStatisticsCommonEntity> GetDailyInvestStatisticsCommonInfo(this IList <DeliveryRecord> source, IList <DateTime> tradeDates, IList <TKLineToday> stockClosePrices) { var result = new List <InvestStatisticsCommonEntity>(); if (source == null || !source.Any() || tradeDates == null) { return(result); } tradeDates = tradeDates.OrderBy(x => x).ToList(); foreach (var date in tradeDates) { var currentRecords = source.Where(x => x.TradeDate < date.AddDays(1)); var currentStockClosePrices = stockClosePrices.Where(x => x.TradeDate == date); //成交额 decimal dealAmount = currentRecords.Where(x => x.TradeDate == date).ToList().CalculateDealAmount(); //持仓市值 decimal positionValue = 0; //持仓收益 decimal positionProfit = 0; //累计发生金额 decimal accumulatedActualAmount = 0; //累计收益额 decimal accumulatedProfit = 0; accumulatedActualAmount = currentRecords.Sum(x => x.ActualAmount); var recordsByStock = currentRecords.GroupBy(x => x.StockCode); foreach (var stockGroup in recordsByStock) { ////各只股票发生金额 //decimal actualAmountPerStock = stockGroup.Sum(x => x.ActualAmount); //accumulatedActualAmount += actualAmountPerStock; //各只股票的持股数 decimal holdingVolume = stockGroup.Sum(x => x.DealVolume); decimal closePrice = 0; if (holdingVolume != 0 && currentStockClosePrices.Any()) { //System.Diagnostics.Debug.WriteLine(date.ToString() + ": " + stockGroup.Key); closePrice = (currentStockClosePrices.LastOrDefault(x => x.StockCode.Trim() == stockGroup.Key) ?? new TKLineToday()).Close; } //持仓市值 positionValue += Math.Abs(holdingVolume) * closePrice; //持仓收益 positionProfit += holdingVolume * closePrice; } accumulatedProfit = accumulatedActualAmount + positionProfit; var info = new InvestStatisticsCommonEntity() { TradeDate = date, DealAmount = dealAmount, PositionValue = positionValue, PositionProfit = positionProfit, AccumulatedActualAmount = accumulatedActualAmount, AccumulatedProfit = accumulatedProfit, }; result.Add(info); } return(result); }