Example #1
0
        public PortfolioCost Get()
        {
            PortfolioCost result = new PortfolioCost();

            MicexISSClient micexClient = new MicexISSClient(new CommonLib.WebApiClient());

            var snap = _context.SnapshootSet.OrderByDescending(s => s.ChangeDate).First();

            PortfolioSnapshoot portfolioSnapshoot = new PortfolioSnapshoot();

            portfolioSnapshoot.read(snap.Data);

            var ds = portfolioSnapshoot.Accounts.SelectMany(a => a.Value.PositionItems, (a, pos) =>
                                                            new
            {
                account = a.Key,
                limit   = pos.Value.Sum(p => p.Limit),
                code    = pos.Key
            }).GroupBy(v => new { v.code }).Select(g => new
            {
                code  = g.Key.code,
                limit = g.Sum(p => p.limit)
            }).ToList();

            foreach (var el in ds)
            {
                Security security = _context.SecuritySet.Single(s => s.Code == el.code);

                Quote quote = _context.QuoteSet.Single(q => q.symbol == security.Code && q.Board == security.Board);

                //ISSResponse issResp = await micexClient.GetSecurityInfo(security.Market, security.Board, el.code);

                decimal cost = 0m;

                if (security.Market == "shares")
                {
                    cost = quote.price * el.limit;
                }

                if (security.Market == "bonds")
                {
                    cost = (quote.price / 100) *
                           ((Bond)security).NominalPrice * el.limit +
                           el.limit * quote.NKD.Value;
                }

                if (security.Currency == "USD")
                {
                    Quote usdrub = _context.QuoteSet.Single(q => q.symbol == "USD000UTSTOM");
                    cost = cost * usdrub.price;
                }

                result.AddItem(el.code, security.Name, el.limit, cost, security.Type);
            }

            result.SharesTotal = result.Items.Where(i => i.Type == "stock").Sum(s => s.Cost);
            result.BondsTotal  = result.Items.Where(i => i.Type == "bond").Sum(s => s.Cost);

            foreach (PortfolioItem etfItem in result.Items.Where(i => i.Type == "etf"))
            {
                var     structure = JObject.Parse(_context.ETFSet.Single(s => s.Code == etfItem.Code).Structure);
                decimal bondPrc   = structure.Value <decimal>("bond") + structure.Value <decimal>("gold");
                decimal bondCost  = etfItem.Cost * (bondPrc / 100);
                decimal stockCost = etfItem.Cost * ((100 - bondPrc) / 100);

                result.SharesTotal += stockCost;
                result.BondsTotal  += bondCost;
            }

            result.SharesPerc = Math.Round(result.SharesTotal / (result.SharesTotal + result.BondsTotal) * 100, 2);
            result.BondsPerc  = Math.Round(result.BondsTotal / (result.SharesTotal + result.BondsTotal) * 100, 2);

            return(result);
        }
Example #2
0
        public IActionResult Post()
        {
            //calculate all

            _context.Database.ExecuteSqlCommand("TRUNCATE TABLE \"SnapshootSet\"");
            _context.SaveChanges();

            List <PortfolioSnapshoot>   snapshoots     = new List <PortfolioSnapshoot>();
            IDictionary <DateTime, int> snapshootIndex = new Dictionary <DateTime, int>();

            Func <PortfolioSnapshoot, PortfolioSnapshoot> copySnap = (snap) =>
            {
                PortfolioSnapshoot result = new PortfolioSnapshoot();

                string json = snap.toJson();

                result.read(json);
                //result.PositionItems = snap.PositionItems.ToList();

                return(result);
            };

            Func <DateTime, PortfolioSnapshoot> getSnap = (dt) =>
            {
                var _dt = dt.Date;

                PortfolioSnapshoot result = null;

                if (snapshootIndex.ContainsKey(_dt))
                {
                    result = snapshoots[snapshootIndex[_dt]];
                }

                if (!snapshootIndex.ContainsKey(_dt))
                {
                    int lastIndex = snapshootIndex.Values.Max();
                    result = copySnap(snapshoots[lastIndex]);

                    snapshootIndex.Add(_dt, lastIndex + 1);
                    snapshoots.Add(result);
                }

                return(result);
            };

            Action <PortfolioSnapshoot, Deal> increase = (snap, deal) =>
            {
                snap.increse(deal.accountId, deal.security.Code, deal.Date.Date, deal.Count);
            };

            Action <PortfolioSnapshoot, Deal> decrease = (snap, deal) =>
            {
                snap.decrease(deal.accountId, deal.security.Code, deal.Date.Date, deal.Count);
            };

            var deals = _context.DealSet.Include(d => d.security)
                        //.Where(d => (d.Date > new DateTime(2017, 01, 18)) && (d.security.Code == "LKOH"))
                        //.OrderBy(d => d.Number).ToList();
                        .OrderBy(d => d.Date).ThenBy(d => d.security.Region).ThenBy(d => d.Number);

            var dates = deals.Select(d => d.Date.Date).Distinct().OrderBy(d => d).ToList();

            snapshoots.Add(new PortfolioSnapshoot());
            snapshootIndex.Add(dates.First(), 0);

            foreach (Deal deal in deals)
            {
                var snap = getSnap(deal.Date);

                if (deal.Operation == OrderOperationEnum.Buy)
                {
                    increase(snap, deal);
                }

                if (deal.Operation == OrderOperationEnum.Sell)
                {
                    decrease(snap, deal);
                }
            }

            foreach (int index in snapshootIndex.Values)
            {
                SnapshootData snapshootData = new SnapshootData()
                {
                    Id         = Guid.NewGuid(),
                    ChangeDate = snapshootIndex.Single(kv => kv.Value == index).Key,
                    Data       = snapshoots[index].toJson()
                };

                _context.SnapshootSet.Add(snapshootData);
            }

            _context.SaveChanges();

            string last = snapshoots.Last().toJson();

            return(Ok());
        }