예제 #1
0
        private void GetListOfStocksFromYahoo()
        {
            string csvData;

            using (WebClient web = new WebClient())
            {
                csvData = web.DownloadString("http://finance.yahoo.com/d/quotes.csv?s=AAPL+GOOG+MSFT+ADBE+AMGN+ADBE+CMCSA+CSX+INTC+KHC+NVDA+SBUX+AMDA+AOBC+EA+CERN+CERS+ETFC+EBAY&f=snbaopl1vhgkj");
            }


            //list of stocks from Yahoo
            List <YahooStock> ListOfStocksFromYahoo = YahooStock.Parse(csvData);


            //List of Stocks from database
            List <String> SymbolStringLIst = new List <String>();

            SymbolStringLIst = Globals.Db.GetAllSymbolsFromDatabase();



            // this part is cheking if record already exists in database
            // if exists it updates record
            // if not it adds new record
            try
            {
                foreach (YahooStock stock in ListOfStocksFromYahoo)
                {
                    try
                    {
                        if (SymbolStringLIst.Contains(stock.Symbol, StringComparer.OrdinalIgnoreCase))
                        {
                            Globals.Db.UpdateStockToStockTable(stock);
                        }
                        else
                        {
                            Globals.Db.AddStockToStockTable(stock);
                        }
                    }
                    catch (SqlException ext)
                    {
                        MessageBox.Show("Error adding/updating record: " + ext.Message, "Confirmation", MessageBoxButton.OK);
                        Console.Write(ext.StackTrace);
                    }
                }
            }
            catch (NullReferenceException e)
            {
                Console.Write(e.StackTrace);
            }
        }
        //Lets Prase CSVdata to list of objects

        // caller must handle ParseException
        public static List <YahooStock> Parse(string csvData)
        {
            List <YahooStock> StockFromApiList = new List <YahooStock>();

            string[] rows = csvData.Replace("\r", "").Split('\n');


            foreach (string row in rows)
            {
                if (string.IsNullOrEmpty(row))
                {
                    continue;
                }

                string[] cols = row.Split(',');


                YahooStock s = new YahooStock();
                s.StockID = 12;
                try
                {
                    s.Symbol        = Convert.ToString(cols[0]).Trim('"');
                    s.Name          = Convert.ToString(cols[1]).Trim('"');
                    s.Bid           = (cols[2].ToUpper().Contains("N/A")) ? (decimal?)null : Convert.ToDecimal(cols[2]);
                    s.Ask           = (cols[3].ToUpper().Contains("N/A")) ? (decimal?)null : Convert.ToDecimal(cols[3]);
                    s.Open          = (cols[4].ToUpper().Contains("N/A")) ? (decimal?)null : Convert.ToDecimal(cols[4]);
                    s.PreviousClose = (cols[5].ToUpper().Contains("N/A")) ? (decimal?)null : Convert.ToDecimal(cols[5]);
                    s.LastTrade     = (cols[6].ToUpper().Contains("N/A")) ? (decimal?)null : Convert.ToDecimal(cols[6]);
                    s.Volume        = (cols[7].ToUpper().Contains("N/A")) ? (int?)null : Convert.ToInt32(cols[7]);
                    s.High          = (cols[8].ToUpper().Contains("N/A")) ? (decimal?)null : Convert.ToDecimal(cols[8]);
                    s.Low           = (cols[9].ToUpper().Contains("N/A")) ? (decimal?)null : Convert.ToDecimal(cols[9]);
                    s.High52        = (cols[10].ToUpper().Contains("N/A")) ? (decimal?)null : Convert.ToDecimal(cols[10]);
                    s.Low52         = (cols[11].ToUpper().Contains("N/A")) ? (decimal?)null : Convert.ToDecimal(cols[11]);
                    StockFromApiList.Add(s);
                }
                catch (FormatException e)
                {
                    MessageBox.Show("Unable to conver value", "Confirmation", MessageBoxButton.OK);
                    Console.Write(e.StackTrace);
                }
            }
            return(StockFromApiList);
        }