Ejemplo n.º 1
0
        public static async Task <AssetAnalysisModel> Analyse()
        {
            decimal total = 0, stock = 0, bond = 0, cash = 0;
            var     funds     = new List <FundAnalysisModel>();
            var     positions = AssetDao.GetAllPositions();

            foreach (var item in positions)
            {
                if (item.Share <= 0)
                {
                    continue;
                }

                var nav = (await DataService.GetFundNavs(item.FundCode))?.LastOrDefault();
                if (nav == null)
                {
                    Log.Warning($"{item.FundCode} 无法获取到净值数据,无法进行数据分析");
                    continue;
                }
                var config = DataService.GetFundAssetConfigs(item.FundCode)?.LastOrDefault();
                if (config == null)
                {
                    Log.Warning($"{item.FundCode} 无法获取到资产配置,无法进行数据分析");
                    continue;
                }
                Log.Information($"{item.FundCode} 最新的净值日期为 {nav.Date}");
                Log.Information($"{item.FundCode} 最新的报告期为 {config.Date}");

                var net = item.Share * nav.UnitNav;
                total += net;
                stock += net * config.StockRatio;
                bond  += net * config.BondRatio;
                cash  += net * config.CashRatio;

                var info = DataService.GetFundInfo(item.FundCode);
                funds.Add(new FundAnalysisModel
                {
                    FundCode           = item.FundCode,
                    FundName           = info?.FundName,
                    AvgCost            = item.AvgCost,
                    FundNav            = nav.UnitNav,
                    NavDate            = nav.Date,
                    PositionProfit     = item.Share * (nav.UnitNav - item.AvgCost),
                    MarketValue        = item.Share * nav.UnitNav,
                    PositionProfitRate = nav.UnitNav / item.AvgCost - 1,
                    Share     = item.Share,
                    Abilities = await Ability.AnalyseFund(item.FundCode)
                });
            }

            var cost = funds.Sum(x => x.Share * x.AvgCost);

            AssetDao.UpsertAssetSnapshot(new AssetSnapshot
            {
                Date       = DateTime.Now.Date,
                TotalAsset = total,
                TotalCost  = cost,
                BondAsset  = bond,
                CashAsset  = cash,
                StockAsset = stock
            });
            return(new AssetAnalysisModel
            {
                TotalAsset = total,
                TotalCost = cost,
                BondAsset = bond,
                CashAsset = cash,
                StockAsset = stock,
                Funds = funds.OrderByDescending(x => x.PositionProfitRate).ToList(),
                LastSnapshot = AssetDao.GetAssetSnapshot(DateTime.Now.Date.AddDays(-1))
            });
        }