public IHttpActionResult Getresult(int isin)
        {
            var query = from p in PriceHistoryDao.getPriceHistorysByisin(isin)
                        select new { p.Date, p.Price };

            return(Ok(query));
        }
Exemple #2
0
        public IHttpActionResult deletePriceHistorys(PriceHistory c)
        {
            // PriceHistory c1 = new PriceHistory { Id=1, Isin=3, Date=Convert.ToDateTime("1992-03-20"), Price=2 };
            int changeLine = PriceHistoryDao.deletePriceHistorys(c);

            return(Ok(changeLine));
        }
        public IHttpActionResult Updatestockprice(dynamic price)
        {
            PriceHistory history = new PriceHistory();

            history.Isin       = price.isin;
            history.OfferPrice = price.OfferPrice;
            history.Type       = "Stock";
            history.BidPrice   = price.BidPrice;
            history.Date       = DateTime.Now;

            int changeline = PriceHistoryDao.setPriceHistory(history);

            List <Position> positions = PositionDao.getPositionsByIsin(history.Isin);
            Portfolio       portfolio = new Portfolio();
            int             changline = 0;

            foreach (Position p in positions)
            {
                portfolio = PortfolioDao.getPortfoliosById(p.PortfolioId);
                double           pnl         = Getportfoliopnl(portfolio.PortfolioId);
                PortfolioHistory porthistory = new PortfolioHistory();
                porthistory.PNL         = pnl;
                porthistory.Date        = DateTime.Now;
                porthistory.PortfolioId = portfolio.PortfolioId;
                int line = PortfolioHistoryDao.setPortfolioHistory(porthistory);
                changline++;
            }

            return(Ok("success"));
        }
        public IHttpActionResult GetPId(int id)
        {
            PriceHistory p = PriceHistoryDao.getPriceHistorysById(id);

            if (p != null)
            {
                return(Ok(p));
            }
            else
            {
                return(NotFound());
            }
        }
        public IHttpActionResult GetoneStockPrice(string isin)
        {
            List <PriceHistory> p     = PriceHistoryDao.getPriceHistorysByisin(isin);
            List <Decimal>      price = new List <decimal>();
            List <string>       time  = new List <string>();

            foreach (PriceHistory history in p)
            {
                price.Add(history.OfferPrice);
                time.Add(history.Date.ToString("D"));
            }

            return(Ok(new OneStockresult(time, price)));
        }
        public IHttpActionResult Getstockavg(string isin)
        {
            List <PriceHistory> p   = PriceHistoryDao.getPriceHistorysByisin(isin);
            decimal             avg = (from stock in p
                                       select stock.OfferPrice).Average();
            decimal max = (from stock in p
                           select stock.OfferPrice).Max();
            decimal min = (from stock in p
                           select stock.OfferPrice).Min();
            decimal offer = PriceHistoryDao.getLastPriceHistorysByisin(isin).OfferPrice;
            decimal bid   = PriceHistoryDao.getLastPriceHistorysByisin(isin).BidPrice;

            return(Ok(new Securityinfo(avg, max, min, offer, bid)));
        }
        public IHttpActionResult GetPositionsbyPortID(int portfolioid)
        {
            List <Position>     positionlist = PositionDao.getPositionsByPortfolioId(portfolioid);
            List <Positionlist> returnlist   = new List <Positionlist>();

            foreach (Position p in positionlist)
            {
                double porfit = 0;
                porfit = Convert.ToDouble((PriceHistoryDao.getLastPriceHistorysByisin(p.Isin).OfferPrice - p.Price) / p.Price);
                returnlist.Add(new Positionlist(p.PositionId, getSecurityname(p.Isin, p.Type), p.Price, p.Quantity, PriceHistoryDao.getLastPriceHistorysByisin(p.Isin).OfferPrice, porfit.ToString("P"), p.Type, p.Isin));
            }

            return(Ok(returnlist));
        }
        public IHttpActionResult GetPositionsbyPortID(int portfolioid)
        {
            List <Position>     positionlist = PositionDao.getPositionsByPortfolioId(portfolioid);
            List <Positionlist> returnlist   = new List <Positionlist>();

            foreach (Position p in positionlist)
            {
                double porfit = 0;
                porfit = Convert.ToDouble((PriceHistoryDao.getLastPriceHistorysByisin(p.Isin).Price - p.Price) / p.Price);
                returnlist.Add(new Positionlist(p.PositionId, StockDao.getStocksById(p.Isin).Name, p.Quantity, porfit));
            }


            return(Ok(returnlist));
        }
        public static double Getportfoliopnl(int portid)
        {
            decimal         amountbefore = 0;
            decimal         amountafter  = 0;
            double          pnl          = 0;
            List <Position> posilist     = PositionDao.getPositionsByPortfolioId(portid);

            foreach (Position p in posilist)
            {
                string isin = p.Isin;
                amountbefore += p.Quantity * p.Price;
                amountafter  += p.Quantity * PriceHistoryDao.getLastPriceHistorysByisin(isin).OfferPrice;
            }
            pnl = Convert.ToDouble((amountafter - amountbefore) / amountbefore);
            return(pnl);
        }
        public IHttpActionResult addPositionlist(dynamic c)
        {
            int      changeline = 0;
            Position position   = new Position();

            foreach (var p in c)
            {
                position.Quantity    = p.Quantity;
                position.PortfolioId = p.PortfolioId;
                position.Isin        = p.Isin;
                position.Type        = p.Type;
                position.Price       = PriceHistoryDao.getLastPriceHistorysByisin(position.Isin).BidPrice;
                int line = PositionDao.setPosition(position);
                changeline += line;
            }
            return(Ok(changeline));
        }
        public IHttpActionResult addPositions(dynamic c)
        {
            Portfolio p = PortfolioDao.getPortfolioBySomething(new Portfolio {
                Name = c.Name, UserId = c.UserId
            });
            Position position = new Position();

            position.Isin        = c.Isin;
            position.PortfolioId = p.PortfolioId;
            position.Price       = PriceHistoryDao.getLastPriceHistorysByisin(position.Isin).BidPrice;
            position.Quantity    = c.Quantity;
            position.Type        = c.Type;

            int changeLine = PositionDao.setPosition(c);

            return(Ok(changeLine));
        }
Exemple #12
0
        public static double Caculatepnl(int portfolitid)
        {
            decimal         amountbefore = 0;
            decimal         amountafter  = 0;
            double          pnl          = 0;
            List <Position> posilist     = PositionDao.getPositionsByPortfolioId(portfolitid);

            foreach (Position p in posilist)
            {
                if (p.Type == "Stock")
                {
                    int isin = p.Isin;
                    amountafter  += p.Quantity * p.Price;
                    amountbefore += p.Quantity * PriceHistoryDao.getLastPriceHistorysByisin(isin).Price;
                }
                else
                {
                }
            }
            pnl = Convert.ToDouble((amountafter - amountbefore) / amountbefore);
            return(pnl);
        }
        public IHttpActionResult GetallStockPrice()
        {
            List <OneStockresult> Allresult    = new List <OneStockresult>();
            List <PriceHistory>   Pricehistory = PriceHistoryDao.getPriceHistorys();
            var query = (from p in Pricehistory
                         select p.Isin).Distinct();

            foreach (string isin in query)
            {
                List <Decimal>      price  = new List <decimal>();
                List <string>       time   = new List <string>();
                List <PriceHistory> result = PriceHistoryDao.getPriceHistorysByisin(isin);
                foreach (PriceHistory history in result)
                {
                    price.Add(history.OfferPrice);
                    time.Add(history.Date.ToString("D"));
                }


                Allresult.Add(new OneStockresult(time, price));
            }

            return(Ok(Allresult));
        }
        public IHttpActionResult addPriceHistorys(PriceHistory c)
        {
            int changeLine = PriceHistoryDao.setPriceHistory(c);

            return(Ok(changeLine));
        }
 public IHttpActionResult Get()
 {
     return(Ok(PriceHistoryDao.getPriceHistorys()));
 }
        public async Task <bool> RunAsync()
        {
            var webPageParser = new WebPageParserByHtmlAgilityPack();

            using (var client = new SqliteContext(_ozonPriceCheckerSecret.SQliteConnectionString))
            {
                client.Database.EnsureCreated();
            }

            var priceHistoryDao = new PriceHistoryDao(_ozonPriceCheckerSecret.SQliteConnectionString);

            var allOzonProducts = await priceHistoryDao.GetAllOzonProductsAsync();

            var historySaveMoment = DateTime.Now;
            var emailBody         = new StringBuilder($"Проверка цен OZON: {DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss")}<br />");

            foreach (var ozonProduct in allOzonProducts)
            {
                emailBody.Append($"Товар: {ozonProduct.Name} ");
                _logger.Trace($"Load product id = {ozonProduct.Id}, name = {ozonProduct.Name}!");
                var lastPriceEntry = await priceHistoryDao.SelectLastProductPriceAsync(ozonProduct.Id);

                var product = webPageParser.GetOzonProductInfoByLink(ozonProduct.ProductLink);
                if (product == null)
                {
                    emailBody.Append("- получить данные не удалось!");
                    _logger.Error($"Failed load product id = {ozonProduct.Id}, name = {ozonProduct.Name}!");
                    continue;
                }

                var postfix        = product.IsAvailable ? string.Empty : "<span style='color:red;'>нет в продаже!</span>";
                var productHistory = MapProductIntoOzonProductHistory(product, historySaveMoment);
                productHistory.Id = await priceHistoryDao.SaveProductHistoryAsync(productHistory);

                ResetConsoleColour();
                Console.ForegroundColor = ConsoleColor.Gray;
                if (lastPriceEntry != null)
                {
                    if (lastPriceEntry.Price > productHistory.Price || lastPriceEntry.PricePremium > productHistory.PricePremium)
                    {
                        ChangeConsoleColourPriceDecrease();
                    }

                    if (lastPriceEntry.Price < productHistory.Price || lastPriceEntry.PricePremium < productHistory.PricePremium)
                    {
                        ChangeConsoleColourPriceIncrease();
                    }
                }
                ResetConsoleColour();

                if (product.PricePremium == 0)
                {
                    emailBody.Append($"цена = <b>{product.Price.ToString("N2")}</b> {postfix}");
                }
                else
                {
                    emailBody.Append($"цена = <b>{product.Price.ToString("N2")}</b>, цена Premium = <b style='color:green;'>{product.PricePremium.ToString("N2")}</b> {postfix}");
                }

                emailBody.Append("<br />");
            }

            var emailMessage = new EmailMessage();

            emailMessage.AddressTo    = new MailAddress("*****@*****.**", "Бобовский Константин");
            emailMessage.Subject      = $"Импорт цен Ozon {DateTime.Now.ToString("dd.MM.yyyy HH:mm")}";
            emailMessage.Body         = emailBody.ToString();
            emailMessage.IsBodyInHtml = true;

            if (!_emailSender.TrySendEmail(emailMessage))
            {
                _logger.Error($"Failed to send email!");
            }

            return(true);
        }