Ejemplo n.º 1
0
        public static void RefreshQuotesFromCbr()
        {
            Thread.Sleep(15000);

            int i           = 0;
            var usdTickerId = 7 /*USD ЦБ*/;
            var eurTickerId = 8 /*EUR ЦБ*/;

            while (true)
            {
                i++;
                var tickers     = TickersDAL.GetTickers().Where(t => t.Id == usdTickerId || t.Id == eurTickerId).ToList();
                var quotes      = QuotesDAL.GetQuotes(fromDate: DateTime.Now.AddDays(-14));
                var updateLines = new StringBuilder();
                try
                {
                    var xml = GetContentByUrl("http://www.cbr.ru/scripts/XML_daily.asp", "windows1251");

                    int startCbrDateInd = xml.IndexOf("<ValCurs Date=\"");

                    var cbrDateStr = xml.Substring(startCbrDateInd + "<ValCurs Date=\"".Length);
                    cbrDateStr = cbrDateStr.Remove(cbrDateStr.IndexOf('"'));

                    var dateParts  = cbrDateStr.Split('.');
                    var year       = Convert.ToInt32(dateParts[2]);
                    var month      = Convert.ToInt32(dateParts[1]);
                    var day        = Convert.ToInt32(dateParts[0]);
                    var cbrDate    = new DateTime(year, month, day);
                    var cbrSqlDate = TextHelper.GetTSqlDate(cbrDate);
                    if (day != DateTime.Now.Day)
                    {
                        continue;
                    }
                    var lastQuoteSqlDate = TextHelper.GetTSqlDate(DateTime.Now);

                    int ind1 = xml.IndexOf("<CharCode>USD</CharCode>");
                    if (ind1 != -1)
                    {
                        string afterInd1  = xml.Substring(ind1);
                        int    ind2       = afterInd1.IndexOf("<Value>");
                        string afterInd2  = afterInd1.Substring(ind2 + "<Value>".Length);
                        string usdValue   = afterInd2.Remove(afterInd2.IndexOf("</Value>")).Replace(',', '.');
                        var    updateLine = $"update dbo.Tickers " +
                                            $"set LastQuote = {usdValue}, " +
                                            $"LastQuoteDate = '{lastQuoteSqlDate}', " +
                                            $"CbrDate = '{cbrDate}' " +
                                            $"where Id = 7";
                        updateLines.AppendLine(updateLine);

                        var lastUsdQuoteInDb = quotes.Where(_ => _.TickerId == usdTickerId).OrderByDescending(_ => _.CbrDate).FirstOrDefault();
                        if (lastUsdQuoteInDb == null || cbrDate > lastUsdQuoteInDb.CbrDate)
                        {
                            var newUsdQuote = new Quote()
                            {
                                TickerId = usdTickerId,
                                CbrDate  = cbrDate,
                                Value    = Convert.ToSingle(usdValue.Replace('.', ','))
                            };

                            QuotesDAL.AddQuote(newUsdQuote);

                            quotes.Add(newUsdQuote);
                        }
                    }

                    ind1 = xml.IndexOf("<CharCode>EUR</CharCode>");
                    if (ind1 != -1)
                    {
                        string afterInd1  = xml.Substring(ind1);
                        int    ind2       = afterInd1.IndexOf("<Value>");
                        string afterInd2  = afterInd1.Substring(ind2 + "<Value>".Length);
                        string eurValue   = afterInd2.Remove(afterInd2.IndexOf("</Value>")).Replace(',', '.');
                        var    updateLine = $"update dbo.Tickers " +
                                            $"set LastQuote = {eurValue}, " +
                                            $"LastQuoteDate = '{lastQuoteSqlDate}', " +
                                            $"CbrDate = '{cbrDate}' " +
                                            $"where Id = 8";
                        updateLines.AppendLine(updateLine);

                        var lastEurQuoteInDb = quotes.Where(_ => _.TickerId == eurTickerId).OrderByDescending(_ => _.CbrDate).FirstOrDefault();
                        if (lastEurQuoteInDb == null || cbrDate > lastEurQuoteInDb.CbrDate)
                        {
                            var newEurQuote = new Quote()
                            {
                                TickerId = eurTickerId,
                                CbrDate  = cbrDate,
                                Value    = Convert.ToSingle(eurValue.Replace('.', ','))
                            };

                            QuotesDAL.AddQuote(newEurQuote);

                            quotes.Add(newEurQuote);
                        }
                    }

                    var sql = updateLines.ToString();
                    TickersDAL.UpdateTickers(sql);
                }
                catch (Exception)
                {
                }

                Thread.Sleep(10 * 60 * 1000);                //10 минут
            }
        }
Ejemplo n.º 2
0
        public ActionResult Index()
        {
            var usdTickerId = 7;
            var eurTickerId = 8;

            var tickers             = TickersDAL.GetTickers();
            var investingComTickers = tickers.Where(t => t.InvestingComPairId != null).ToList();
            var lmeTickers          = tickers.Where(t => t.LmeName != null).ToList();

            var quotes    = QuotesDAL.GetQuotes(fromDate: DateTime.Now.AddDays(-14));
            var usdQuotes = quotes.Where(q => q.TickerId == usdTickerId).OrderByDescending(q => q.CbrDate).Take(2).ToList();
            var eurQuotes = quotes.Where(q => q.TickerId == eurTickerId).OrderByDescending(q => q.CbrDate).Take(2).ToList();

            var viewModel = new HomeIndexViewModel
            {
                InvestingComQuotes = new List <QuoteItemViewModel>(),
                LmeQuotes          = new List <QuoteItemViewModel>(),
                TodayUsdQuote      = usdQuotes.Count > 1
                                                                ? usdQuotes[1]
                                                                : usdQuotes.Count > 0
                                                                ? usdQuotes[0]
                                                                : null,
                TomorrowUsdQuote = usdQuotes.Count > 0
                                                                   ? usdQuotes[0]
                                                                   : null,
                TodayEuroQuote = eurQuotes.Count > 1
                                                                ? eurQuotes[1]
                                                                : eurQuotes.Count > 0
                                                                ? eurQuotes[0]
                                                                : null,
                TomorrowEuroQuote = eurQuotes.Count > 0
                                                                        ? eurQuotes[0]
                                                                        : null
            };

            if (usdQuotes.Count > 0 && DateTime.Now >= usdQuotes[0].CbrDate)
            {
                viewModel.TodayUsdQuote  = viewModel.TomorrowUsdQuote;
                viewModel.TodayEuroQuote = viewModel.TomorrowEuroQuote;
            }

            foreach (var ticker in investingComTickers)
            {
                viewModel.InvestingComQuotes.Add(new QuoteItemViewModel()
                {
                    TickerName    = ticker.Description,
                    Change        = (decimal)ticker.ChangeFromYesterdayClose,
                    ChangePercent = (decimal)ticker.ChangeFromYesterdayClosePercent,
                    Quote         = (decimal)ticker.LastQuote,
                    TickerId      = ticker.Id,
                    TimeStr       = ticker.TimeStr
                                    //ChartUrl = Urls.Quotes + "/" + tickerType.NameForUrl + "/" + ticker.NameForUrl,
                                    //ATitle = "Открыть график котировок акций " + ticker.Description
                });
            }

            foreach (var ticker in lmeTickers)
            {
                viewModel.LmeQuotes.Add(new QuoteItemViewModel()
                {
                    TickerName = ticker.Description,
                    Quote      = (decimal)ticker.LastQuote,
                    TickerId   = ticker.Id
                                 //ChartUrl = Urls.Quotes + "/" + tickerType.NameForUrl + "/" + ticker.NameForUrl,
                                 //ATitle = "Открыть график котировок акций " + ticker.Description
                });
            }

            ViewBag.Heading = "М-Контракт";
            return(View(viewModel));
        }