Exemplo n.º 1
0
        public async Task DownloadSummaryAsync()
        {
            if (Symbol == "")
            {
                throw new Exception("Stock symbol not provided.");
            }

            try
            {
                Summary = await EquitySummaryData.CreateAsync(Symbol);
            }
            catch
            {
                throw new Exception("Fatal error while downloading summary data.");
            }
        }
Exemplo n.º 2
0
        public static async Task <EquitySummaryData> CreateAsync(string symbol)
        {
            EquitySummaryData ToReturn = new EquitySummaryData();

            ToReturn.QueriedSymbol = symbol.ToUpper().Trim();

            //Get from yahoo finance
            string              url = "https://finance.yahoo.com/quote/" + symbol;
            HttpClient          cl  = new HttpClient();
            HttpResponseMessage hrm = await cl.GetAsync(url);

            string web = await hrm.Content.ReadAsStringAsync();


            //Test for invalid
            if (web.ToLower().Contains(">Please check your spelling.".ToLower()))
            {
                throw new Exception("Stock '" + symbol + "' is not a valid stock symbol.");
            }


            int           loc1;
            int           loc2;
            List <string> splitter = new List <string>();

            //The name and price were left outside of a try bracket intentionally.
            //This should be the minimum amount of information that can be accessed.  If you can't get this, fail.

            //Get name and stock symbol
            string name_and_stock_symbol = ToReturn.GetDataByClassName(web, "D(ib) Fz(18px)");

            loc1                 = name_and_stock_symbol.IndexOf("(");
            ToReturn.Name        = name_and_stock_symbol.Substring(0, loc1 - 1).Trim();
            ToReturn.Name        = ToReturn.Name.Replace("&amp;", "&");
            ToReturn.Name        = ToReturn.Name.Replace("&#x27;", "'");
            loc2                 = name_and_stock_symbol.IndexOf(")", loc1 + 1);
            ToReturn.StockSymbol = name_and_stock_symbol.Substring(loc1 + 1, loc2 - loc1 - 1);



            //Get price
            ToReturn.Price = System.Convert.ToSingle(ToReturn.GetDataByClassName(web, "Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)"));


            //Get day change
            try
            {
                loc1 = web.IndexOf("Trsdu(0.3s) Fw(500) Pstart(10px) Fz(24px)");
                loc1 = web.IndexOf(">", loc1 + 1);
                loc2 = web.IndexOf("<", loc1 + 1);
                string dayc = web.Substring(loc1 + 1, loc2 - loc1 - 1);
                splitter.Clear();
                splitter.Add(" ");
                string[] partsdc = dayc.Split(splitter.ToArray(), StringSplitOptions.None);
                ToReturn.DollarChange  = System.Convert.ToSingle(partsdc[0].Replace("+", ""));
                ToReturn.PercentChange = System.Convert.ToSingle(partsdc[1].Replace("+", "").Replace("(", "").Replace(")", "").Replace("%", "").Trim()) / 100;
            }
            catch
            {
                ToReturn.DollarChange  = 0;
                ToReturn.PercentChange = 0;
            }

            //Get previous close
            try
            {
                ToReturn.PreviousClose = System.Convert.ToSingle(ToReturn.GetDataByDataTestName(web, "PREV_CLOSE-value"));
            }
            catch
            {
                ToReturn.PreviousClose = 0;
            }


            //Get open
            try
            {
                ToReturn.Open = System.Convert.ToSingle(ToReturn.GetDataByDataTestName(web, "OPEN-value"));
            }
            catch
            {
                ToReturn.Open = 0;
            }



            //Get bid information
            try
            {
                loc1 = 0;
                loc2 = 0;
                loc1 = web.IndexOf("BID-value");
                loc1 = web.IndexOf(">", loc1 + 1);
                loc2 = web.IndexOf("<", loc1 + 1);
                string bid_info = web.Substring(loc1 + 1, loc2 - loc1 - 1);
                if (bid_info == "")
                {
                    loc1     = web.IndexOf(">", loc2 + 1);
                    loc2     = web.IndexOf("<", loc1 + 1);
                    bid_info = web.Substring(loc1 + 1, loc2 - loc1 - 1);
                }
                splitter.Clear();
                splitter.Add(" x ");
                string[] parts = bid_info.Split(splitter.ToArray(), StringSplitOptions.None);
                ToReturn.BidPrice    = System.Convert.ToSingle(parts[0]);
                ToReturn.BidQuantity = System.Convert.ToInt32(parts[1]);
            }
            catch
            {
                ToReturn.BidPrice    = 0;
                ToReturn.BidQuantity = 0;
            }



            //Get ask informmation
            try
            {
                loc1 = 0;
                loc2 = 0;
                loc1 = web.IndexOf("ASK-value");
                loc1 = web.IndexOf(">", loc1 + 1);
                loc2 = web.IndexOf("<", loc1 + 1);
                string ask_info = web.Substring(loc1 + 1, loc2 - loc1 - 1);
                if (ask_info == "")
                {
                    loc1     = web.IndexOf(">", loc2 + 1);
                    loc2     = web.IndexOf("<", loc1 + 1);
                    ask_info = web.Substring(loc1 + 1, loc2 - loc1 - 1);
                }
                splitter.Clear();
                splitter.Add(" x ");
                string[] parts2 = ask_info.Split(splitter.ToArray(), StringSplitOptions.None);
                ToReturn.AskPrice    = System.Convert.ToSingle(parts2[0]);
                ToReturn.AskQuantity = System.Convert.ToInt32(parts2[1]);
            }
            catch
            {
                ToReturn.AskPrice    = 0;
                ToReturn.AskQuantity = 0;
            }



            //Get Day Range information
            try
            {
                loc1 = web.IndexOf("DAYS_RANGE-value");
                loc1 = web.IndexOf(">", loc1 + 1);
                loc2 = web.IndexOf("<", loc1 + 1);
                string day_range_info = web.Substring(loc1 + 1, loc2 - loc1 - 1);
                day_range_info = day_range_info.Replace(" ", "");
                splitter.Clear();
                splitter.Add("-");
                string[] parts3 = day_range_info.Split(splitter.ToArray(), StringSplitOptions.None);
                ToReturn.DayRangeLow  = System.Convert.ToSingle(parts3[0].Trim());
                ToReturn.DayRangeHigh = System.Convert.ToSingle(parts3[1].Trim());
            }
            catch
            {
                ToReturn.DayRangeLow  = 0;
                ToReturn.DayRangeHigh = 0;
            }


            //Get Day Range information
            try
            {
                loc1 = web.IndexOf("FIFTY_TWO_WK_RANGE-value");
                loc1 = web.IndexOf(">", loc1 + 1);
                loc2 = web.IndexOf("<", loc1 + 1);
                string year_range_info = web.Substring(loc1 + 1, loc2 - loc1 - 1);
                year_range_info = year_range_info.Replace(" ", "");
                splitter.Clear();
                splitter.Add("-");
                string[] parts4 = year_range_info.Split(splitter.ToArray(), StringSplitOptions.None);
                ToReturn.YearRangeLow  = System.Convert.ToSingle(parts4[0].Trim());
                ToReturn.YearRangeHigh = System.Convert.ToSingle(parts4[1].Trim());
            }
            catch
            {
                ToReturn.YearRangeLow  = 0;
                ToReturn.YearRangeHigh = 0;
            }



            //Get volume
            try
            {
                ToReturn.Volume = System.Convert.ToInt32(ToReturn.GetDataByDataTestName(web, "TD_VOLUME-value").Replace(",", ""));
            }
            catch
            {
                ToReturn.Volume = 0;
            }


            //Get average volume
            try
            {
                ToReturn.AverageVolume = System.Convert.ToInt32(ToReturn.GetDataByDataTestName(web, "AVERAGE_VOLUME_3MONTH-value").Replace(",", ""));
            }
            catch
            {
                ToReturn.AverageVolume = 0;
            }

            //Get market cap
            try
            {
                string market_cap = ToReturn.GetDataByDataTestName(web, "MARKET_CAP-value");
                string last_char  = market_cap.Substring(market_cap.Length - 1, 1).ToLower();
                float  mcf        = System.Convert.ToSingle(market_cap.Substring(0, market_cap.Length - 1));
                if (last_char == "th")
                {
                    ToReturn.MarketCap = System.Convert.ToDouble(mcf * 1000);
                }
                else if (last_char == "m")
                {
                    ToReturn.MarketCap = System.Convert.ToDouble(mcf * 1000000);
                }
                else if (last_char == "b")
                {
                    ToReturn.MarketCap = System.Convert.ToDouble(mcf * 1000000000);
                }
                else if (last_char == "t")
                {
                    ToReturn.MarketCap = System.Convert.ToDouble(mcf * 1000000000000);
                }
            }
            catch
            {
                ToReturn.MarketCap = 0;
            }



            //Get beta
            try
            {
                ToReturn.Beta = System.Convert.ToSingle(ToReturn.GetDataByDataTestName(web, "BETA_5Y-value"));
            }
            catch
            {
                ToReturn.Beta = null;
            }

            //Get PE Ratio
            try
            {
                ToReturn.PriceEarningsRatio = System.Convert.ToSingle(ToReturn.GetDataByDataTestName(web, "PE_RATIO-value"));
            }
            catch
            {
                ToReturn.PriceEarningsRatio = null;
            }

            //Get EPS ratio
            try
            {
                ToReturn.EarningsPerShare = System.Convert.ToSingle(ToReturn.GetDataByDataTestName(web, "EPS_RATIO-value"));
            }
            catch
            {
                ToReturn.EarningsPerShare = 0;
            }


            //Get earnings date
            try
            {
                ToReturn.EarningsDate = DateTime.Parse(ToReturn.GetDataByDataTestName(web, "EARNINGS_DATE-value"));
            }
            catch
            {
                ToReturn.EarningsDate = null;
            }



            //Get Forward Dividend Info
            try
            {
                loc1 = web.IndexOf("DIVIDEND_AND_YIELD-value");
                loc1 = web.IndexOf(">", loc1 + 1);
                loc2 = web.IndexOf("<", loc1 + 1);
                string dividendinfo = web.Substring(loc1 + 1, loc2 - loc1 - 1);
                if (dividendinfo == "N/A (N/A)")
                {
                    ToReturn.ForwardDividend      = null;
                    ToReturn.ForwardDividendYield = null;
                }
                else
                {
                    splitter.Clear();
                    splitter.Add(" ");
                    string[] partsfd = dividendinfo.Split(splitter.ToArray(), StringSplitOptions.None);
                    ToReturn.ForwardDividend      = System.Convert.ToSingle(partsfd[0]);
                    ToReturn.ForwardDividendYield = System.Convert.ToSingle(partsfd[1].Replace("%", "").Replace("(", "").Replace(")", "")) / 100;
                }
            }
            catch
            {
                ToReturn.ForwardDividend      = 0;
                ToReturn.ForwardDividendYield = 0;
            }



            //Get ex-dividend date
            try
            {
                ToReturn.ExDividendDate = DateTime.Parse(ToReturn.GetDataByDataTestName(web, "EX_DIVIDEND_DATE-value"));
            }
            catch
            {
                ToReturn.ExDividendDate = null;
            }

            //Get one year targets
            try
            {
                ToReturn.YearTargetEstimate = System.Convert.ToSingle(ToReturn.GetDataByDataTestName(web, "ONE_YEAR_TARGET_PRICE-value"));
            }
            catch
            {
                ToReturn.YearTargetEstimate = 0;
            }

            return(ToReturn);
        }
Exemplo n.º 3
0
        private EquitySummaryData[] GetEquitySummariesFromWebData(string web_data)
        {
            int           loc1     = 0;
            int           loc2     = 0;
            List <string> Splitter = new List <string>();

            loc1 = web_data.IndexOf("<tbody");
            loc2 = web_data.IndexOf("</tbody>", loc1 + 1);
            string table_data = web_data.Substring(loc1 + 1, loc2 - loc1 - 1);

            //Split into rows
            Splitter.Clear();
            Splitter.Add("<tr class");
            string[] rows = table_data.Split(Splitter.ToArray(), StringSplitOptions.None);

            //Get all data
            List <EquitySummaryData> ToReturn = new List <EquitySummaryData>();
            int r = 0;

            for (r = 1; r < rows.Length; r++)
            {
                EquitySummaryData esd = new EquitySummaryData();

                Splitter.Clear();
                Splitter.Add("<td ");
                string[] cols = rows[r].Split(Splitter.ToArray(), StringSplitOptions.None);

                //Get symbol
                try
                {
                    loc1            = cols[1].IndexOf("a href");
                    loc1            = cols[1].IndexOf(">", loc1 + 1);
                    loc2            = cols[1].IndexOf("<", loc1 + 1);
                    esd.StockSymbol = cols[1].Substring(loc1 + 1, loc2 - loc1 - 1);
                }
                catch
                {
                    esd.StockSymbol = "?";
                }


                //Get name
                try
                {
                    loc1     = cols[2].IndexOf("react-text");
                    loc1     = cols[2].IndexOf(">", loc1 + 1);
                    loc2     = cols[2].IndexOf("<", loc1 + 1);
                    esd.Name = cols[2].Substring(loc1 + 1, loc2 - loc1 - 1);
                    esd.Name = esd.Name.Replace("&#x27;", "'");
                    esd.Name = esd.Name.Replace("&amp;", "&");
                }
                catch
                {
                    esd.Name = "";
                }


                //Get price
                try
                {
                    loc1      = cols[3].IndexOf("span class");
                    loc1      = cols[3].IndexOf(">", loc1 + 1);
                    loc2      = cols[3].IndexOf("<", loc1 + 1);
                    esd.Price = Convert.ToSingle(cols[3].Substring(loc1 + 1, loc2 - loc1 - 1));
                }
                catch
                {
                    esd.Price = 0;
                }


                //Get dollar change
                try
                {
                    loc1             = cols[4].IndexOf("span class");
                    loc1             = cols[4].IndexOf(">", loc1 + 1);
                    loc2             = cols[4].IndexOf("<", loc1 + 1);
                    esd.DollarChange = Convert.ToSingle(cols[4].Substring(loc1 + 1, loc2 - loc1 - 1).Replace("+", ""));
                }
                catch
                {
                    esd.DollarChange = 0;
                }

                //Get Percent Change
                try
                {
                    loc1 = cols[5].IndexOf("span class");
                    loc1 = cols[5].IndexOf(">", loc1 + 1);
                    loc2 = cols[5].IndexOf("<", loc1 + 1);
                    string PercentChange       = cols[5].Substring(loc1 + 1, loc2 - loc1 - 1).Replace("+", "").Replace("%", "");
                    float  percent_change_perc = Convert.ToSingle(PercentChange);
                    percent_change_perc = percent_change_perc / 100;
                    esd.PercentChange   = percent_change_perc;
                }
                catch
                {
                    esd.PercentChange = 0;
                }


                //Get volume
                try
                {
                    loc1 = cols[6].IndexOf("span class");
                    loc1 = cols[6].IndexOf(">", loc1 + 1);
                    loc2 = cols[6].IndexOf("<", loc1 + 1);
                    string vol = cols[6].Substring(loc1 + 1, loc2 - loc1 - 1);
                    esd.Volume = Convert.ToInt32(YahooFinanceToolkit.GetMarketCapFromString(vol));
                }
                catch
                {
                    esd.Volume = 0;
                }


                //Get average volume
                try
                {
                    loc1 = cols[7].IndexOf("react-text");
                    loc1 = cols[7].IndexOf(">", loc1 + 1);
                    loc2 = cols[7].IndexOf("<", loc1 + 1);
                    string avgvol = cols[7].Substring(loc1 + 1, loc2 - loc1 - 1);
                    esd.AverageVolume = Convert.ToInt32(YahooFinanceToolkit.GetMarketCapFromString(avgvol));
                }
                catch
                {
                    esd.AverageVolume = 0;
                }

                //Get market cap
                try
                {
                    loc1 = cols[8].IndexOf("span class");
                    loc1 = cols[8].IndexOf(">", loc1 + 1);
                    loc2 = cols[8].IndexOf("<", loc1 + 1);
                    string marcap = cols[8].Substring(loc1 + 1, loc2 - loc1 - 1);
                    esd.MarketCap = YahooFinanceToolkit.GetMarketCapFromString(marcap);
                }
                catch
                {
                    esd.MarketCap = 0;
                }


                //Get price earnings ratio
                try
                {
                    loc1 = cols[9].IndexOf("react-text");
                    loc1 = cols[9].IndexOf(">", loc1 + 1);
                    loc2 = cols[9].IndexOf("<", loc1 + 1);
                    string pestr = cols[9].Substring(loc1 + 1, loc2 - loc1 - 1);
                    esd.PriceEarningsRatio = Convert.ToSingle(cols[9].Substring(loc1 + 1, loc2 - loc1 - 1));
                }
                catch
                {
                    esd.PriceEarningsRatio = null;
                }



                ToReturn.Add(esd);
            }


            return(ToReturn.ToArray());
        }