public static void RefreshQuotesFromLme() { int i = 0; var whitespaceRegex = @"\s+"; while (true) { i++; var tickers = TickersDAL.GetTickers().Where(t => t.LmeName != null).ToList(); var updateLines = new StringBuilder(); try { var html = GetContentByUrl("https://lme.com"); var tableContent = html.Substring(html.IndexOf("<table class=\"ring-times\"")); tableContent = tableContent.Substring(html.IndexOf(">") + 1); tableContent = tableContent.Substring(0, tableContent.IndexOf("</table>")); tableContent = Regex.Replace(tableContent, whitespaceRegex, ""); foreach (var ticker in tickers) { var lmeName = Regex.Replace(ticker.LmeName, whitespaceRegex, ""); var indexOfLmeName = tableContent.IndexOf(lmeName); var lastQuoteTdContent = tableContent.Substring(tableContent.IndexOf("<td>", indexOfLmeName)); lastQuoteTdContent = lastQuoteTdContent.Substring(lastQuoteTdContent.IndexOf(">") + 1); lastQuoteTdContent = lastQuoteTdContent.Substring(0, lastQuoteTdContent.IndexOf("</td>")); lastQuoteTdContent = lastQuoteTdContent.Replace(",", ""); lastQuoteTdContent = lastQuoteTdContent.Replace(".", ","); if (!decimal.TryParse(lastQuoteTdContent, out decimal lastQuote)) { continue; } var lastQuoteStr = lastQuote.ToString().Replace(',', '.'); var sqlDate = TextHelper.GetTSqlDate(DateTime.Now); var updateLine = $"update dbo.Tickers " + $"set LastQuote = {lastQuoteStr}, " + $"LastQuoteDate = '{sqlDate}' " + $"where Id = {ticker.Id}"; updateLines.AppendLine(updateLine); } var sql = updateLines.ToString(); TickersDAL.UpdateTickers(sql); } catch (Exception) { } Thread.Sleep(15000); } }
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 минут } }
public static void RefreshQuotesFromInvestingCom() { int i = 0; while (true) { i++; var tickers = TickersDAL.GetTickers().Where(t => t.InvestingComPairId.HasValue).ToList(); var updateLines = new StringBuilder(); try { var html = GetContentByUrl("https://ru.investing.com/commodities/metals"); foreach (var ticker in tickers) { var pairId = ticker.InvestingComPairId.Value; //<td class="pid-8831-last">2,719</td> var lastQuoteTd = $"<td class=\"pid-{pairId}-last\">"; var indexOfLastQuoteTd = html.IndexOf(lastQuoteTd); var lastQuoteTdContent = html.Substring(indexOfLastQuoteTd + lastQuoteTd.Length); lastQuoteTdContent = lastQuoteTdContent.Remove(lastQuoteTdContent.IndexOf("</td>")).Trim().Replace(".", ""); if (!decimal.TryParse(lastQuoteTdContent, out decimal lastQuote)) { continue; } //<td class="bold greenFont pid-8831-pcp" >+0,85%</td> var trContent = html.Substring(indexOfLastQuoteTd); trContent = trContent.Remove(trContent.IndexOf("</tr>")); #region Извлекаем значение из столбца "Изм." var changeTd = $"pid-{pairId}-pc\""; var indexOfChangeTd = trContent.IndexOf(changeTd); if (indexOfChangeTd == -1) { continue; } var changeTdContent = trContent.Substring(indexOfChangeTd + changeTd.Length); changeTdContent = changeTdContent.Substring(changeTdContent.IndexOf(">") + ">".Length); changeTdContent = changeTdContent.Remove(changeTdContent.IndexOf("</td>")).Trim().Replace(".", ""); if (!decimal.TryParse(changeTdContent, out decimal change)) { continue; } #endregion #region Извлекаем значение из столбца "Изм.%" var changePercentTd = $"pid-{pairId}-pcp\""; var indexOfChangePercentTd = trContent.IndexOf(changePercentTd); if (indexOfChangePercentTd == -1) { continue; } var changePercentTdContent = trContent.Substring(indexOfChangePercentTd + changePercentTd.Length); changePercentTdContent = changePercentTdContent.Substring(changePercentTdContent.IndexOf(">") + ">".Length); changePercentTdContent = changePercentTdContent.Remove(changePercentTdContent.IndexOf("</td>")).Trim().Replace(".", "").Replace("%", ""); if (!decimal.TryParse(changePercentTdContent, out decimal changePercent)) { continue; } #endregion #region Извлекаем значение из столбца "Время" var timeTd = $"pid-{pairId}-time\""; var indexOfTimeTd = trContent.IndexOf(timeTd); if (indexOfTimeTd == -1) { continue; } var timeTdContent = trContent.Substring(indexOfTimeTd + timeTd.Length); timeTdContent = timeTdContent.Substring(timeTdContent.IndexOf(">") + ">".Length); timeTdContent = timeTdContent.Remove(timeTdContent.IndexOf("</td>")).Trim(); #endregion var lastQuoteStr = lastQuote.ToString().Replace(',', '.'); var sqlDate = TextHelper.GetTSqlDate(DateTime.Now); var changeStr = change.ToString().Replace(',', '.'); var changePercentStr = changePercent.ToString().Replace(',', '.'); var updateLine = $"update dbo.Tickers " + $"set LastQuote = {lastQuoteStr}, " + $"LastQuoteDate = '{sqlDate}', " + $"ChangeFromYesterdayClose = {changeStr}, " + $"ChangeFromYesterdayClosePercent = {changePercentStr}, " + $"TimeStr = '{timeTdContent}' " + $"where Id = {ticker.Id}"; updateLines.AppendLine(updateLine); } var sql = updateLines.ToString(); TickersDAL.UpdateTickers(sql); } catch (Exception) { } Thread.Sleep(15000); } }
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)); }