Exemple #1
0
        public static int GetCompanyId(string ticker)
        {
            int id = 0;

            try
            {
                using (var ctx = new YHScrape.Entities.YahooFinanceContext())
                {
                    id = ctx.YahooCompanyDatas.Single(x => x.Ticker == ticker).Id;
                }
            }
            catch (Exception e) {  }
            return(id);
        }
Exemple #2
0
        /// <summary>
        /// Save Yahoo Key Stats to DB
        /// </summary>
        /// <param name="tickerListFile">location of tickerlist file</param>
        public void SaveKeyStatsToDB(string tickerListFile)
        {
            HtmlDocument htmlDoc = new HtmlDocument();

            try
            {
                string[] tickers = GetTickerListFromFile(tickerListFile).Split('+');

                using (var ctx = new YHScrape.Entities.YahooFinanceContext())
                {
                    foreach (string ticker in tickers)
                    {
                        htmlDoc = FetchKeyStats(ticker);
                        var query = //from table in htmlData.DocumentNode.SelectNodes("//table[contains(@class,'datamodoutline')]").Cast<HtmlNode>()
                                    from row in htmlDoc.DocumentNode.SelectNodes(".//tr[td[contains(@class,'yfnc_tablehead')]]|.//tr[td[contains(@class,'yfnc_tabledata')]]").Cast <HtmlNode>()
                                    //from cellData in table.SelectNodes(".//td[contains(@class,'yfnc_tabledata')]").Cast<HtmlNode>()
                                    //select row;
                                    select new { Head = row.FirstChild.InnerText, Data = row.LastChild.InnerText };
                        int             iCount          = 0;
                        List <decimal?> valMeasureData  = new List <decimal?>();
                        List <decimal?> financeHL       = new List <decimal?>();
                        List <decimal?> tradeInfo       = new List <decimal?>();
                        DateTime?       fiscalYearEnds  = null;
                        DateTime?       recentQtr       = null;
                        DateTime?       dividendDate    = null;
                        DateTime?       exDividendDate  = null;
                        DateTime?       lastSplitDate   = null;
                        string          lastSplitFactor = "";
                        foreach (var row in query)
                        {
                            if (iCount <= 8)
                            {
                                valMeasureData.Add(Helper.ParseToDecimalValue(row.Data));
                            }
                            else if (iCount == 9)
                            {
                                fiscalYearEnds = Helper.ParseToDateTime(row.Data);
                            }
                            else if (iCount == 10)
                            {
                                recentQtr = Helper.ParseToDateTime(row.Data);
                            }
                            else if (iCount > 10 && iCount <= 30)
                            {
                                financeHL.Add(Helper.ParseToDecimalValue(row.Data));
                            }
                            else if (iCount > 30 && iCount <= 53)
                            {
                                tradeInfo.Add(Helper.ParseToDecimalValue(row.Data));
                            }
                            else if (iCount == 54)
                            {
                                dividendDate = Helper.ParseToDateTime(row.Data);
                            }
                            else if (iCount == 55)
                            {
                                exDividendDate = Helper.ParseToDateTime(row.Data);
                            }
                            else if (iCount == 56)
                            {
                                lastSplitFactor = row.Data;
                            }
                            else if (iCount == 57)
                            {
                                lastSplitDate = Helper.ParseToDateTime(row.Data);
                            }
                            iCount++;
                        }

                        CompanyData           comData  = new CompanyData();
                        CompanyStatisticsData statData = new CompanyStatisticsData();
                        statData.CollectionDate             = DateTime.Now;
                        statData.CompanyValuationMeasures   = new CompanyValuationMeasures(valMeasureData);
                        statData.CompanyFinancialHighlights = new CompanyFinancialHighlights(fiscalYearEnds, recentQtr, financeHL);
                        statData.CompanyTradingInfo         = new CompanyTradingInfo(tradeInfo, dividendDate, exDividendDate, lastSplitDate, lastSplitFactor);
                        var comInfo = ctx.YahooCompanyDatas.Where(c => c.Ticker == ticker).SingleOrDefault();
                        if (comInfo == null)
                        {
                            comData.Ticker      = ticker;
                            comData.CompanyName = "";
                            ctx.YahooCompanyDatas.Add(comData);
                            comData.CompanyStatDatas.Add(statData);
                            ctx.YahooCompanyDatas.Add(comData);
                        }
                        else
                        {
                            comInfo.CompanyStatDatas = comData.CompanyStatDatas;
                        }
                    }

                    ctx.SaveChanges();
                }
            }
            catch (Exception e) { }
        }