Beispiel #1
0
        public IActionResult Data(string id)
        {
            GooglePortfolioBuilder builder = new GooglePortfolioBuilder();

            if (String.IsNullOrEmpty(id))
            {
                id = "1";
            }

#if DEBUG
            string portfolioPath = Path.Combine(_hostingEnvironment.ContentRootPath, "Data", id + ".csv");
#else
            string portfolioPath = $"https://raw.githubusercontent.com/lionelschiepers/MyStock/master/MyStockWeb/Data/{id}.csv";
#endif

            Portfolio portfolio = builder.Build(portfolioPath);
            portfolio.Update();
            List <Position> positions = portfolio.CalculatePosition();

            PortfolioDTO result = new PortfolioDTO();
            result.MarketCost        = portfolio.TotalCostPrice();
            result.MarketPrice       = portfolio.TotalMarketPrice();
            result.Positions         = new List <PositionDTO>();
            result.MarketPositions   = new List <MarketPositionDTO>();
            result.CurrencyPositions = new List <CurrencyPositionDTO>();

            double expectedDividend = 0;
            double totalDay         = 0;

            foreach (var position in positions.OfType <SecurityPosition>().Where(p => p.Shares > 0).OrderBy(p => p.Security.DayChange))
            {
                expectedDividend += ExchangeRates.ConvertTo(position.Currency, position.MarketPrice * position.Security.NetDividendYield, "EUR");
                totalDay         += ExchangeRates.ConvertTo(position.Currency, position.DayDiff, "EUR");

                PositionDTO dto = new PositionDTO()
                {
                    MarketCost  = position.CostPrice,
                    MarketPrice = position.MarketPrice,
                    Shares      = position.Shares,
                    Currency    = position.Currency,
                    Security    = new SecurityDTO()
                    {
                        Currency           = position.Security.Curreny,
                        Price              = position.Security.Price,
                        PreviousClosePrice = position.Security.PreviousClosePrice,
                        Symbol             = position.Security.Symbol,
                        Market             = position.Security.Market,
                        Name          = position.Security.Name,
                        DividendYield = position.Security.DividendYield,
                    }
                };

                var rsi = MyStock.Provider.YahooProvider.Rsi(position.Security.YahooSymbol);
                if (rsi.Any())
                {
                    dto.Security.Rsi = (int)rsi.Last().Item2;
                }


                result.Positions.Add(dto);
            }

            result.DayChange = totalDay / result.MarketPrice;

            result.DividendYield = expectedDividend / result.MarketPrice;

            foreach (var positionsByMarket in positions.OfType <SecurityPosition>().GroupBy(p => p.Security.Market))
            {
                MarketPositionDTO dto = new MarketPositionDTO();
                dto.Name     = positionsByMarket.Key;
                dto.Currency = positionsByMarket.First().Currency;
                dto.Price    = positionsByMarket.Sum(p => p.MarketPrice);
                dto.PriceEUR = ExchangeRates.ConvertTo(dto.Currency, dto.Price, "EUR");
                if (result.MarketPrice > 0)
                {
                    dto.ShareInPortfolio = dto.PriceEUR / result.MarketPrice;
                }

                result.MarketPositions.Add(dto);
            }

            foreach (var positionsByCurrency in positions.OfType <SecurityPosition>().GroupBy(p => p.Currency))
            {
                CurrencyPositionDTO dto = new CurrencyPositionDTO();
                dto.Currency = positionsByCurrency.Key;
                dto.Price    = positionsByCurrency.Sum(p => p.MarketPrice);
                dto.PriceEUR = ExchangeRates.ConvertTo(dto.Currency, dto.Price, "EUR");
                if (result.MarketPrice > 0)
                {
                    dto.ShareInPortfolio = dto.PriceEUR / result.MarketPrice;
                }

                result.CurrencyPositions.Add(dto);
            }

            return(Json(result));
        }