コード例 #1
0
        private List <YahooAPIResult> CreateYahooAPIResultList(string webResult)
        {
            var yahooResultList = new List <YahooAPIResult>();

            //string fixedResponse = Regex.Replace(webResult, @"\r\n?|\n", string.Empty);
            var lines = webResult.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

            foreach (var line in lines)
            {
                var yahooResult = new YahooAPIResult(line);

                if (yahooResult.PeRatioIsNA)
                {
                    yahooResult.PeRatio = 0;
                }
                if (yahooResult.YieldIsNA)
                {
                    yahooResult.Yield = 0;
                }
                if (yahooResult.DescriptionIsNA)
                {
                    yahooResult.Description = "Unknown Ticker";
                }
                if (yahooResult.LastPriceIsNA)
                {
                    yahooResult.LastPrice = 0;
                }
                yahooResult.MarketCap = yahooResult.MarketCap.Substring(0, yahooResult.MarketCap.Length - 1); //removed the amount suffix, e.g. "B"

                yahooResultList.Add(yahooResult);
            }

            return(yahooResultList);
        }
コード例 #2
0
        /// <summary>
        /// Determine based on N/A results if a yahoo result is not known
        /// Bid/Ask and BidSize/AskSize can be N/A after market hours
        /// so they are not used in this determination.
        /// </summary>
        /// <param name="yahooResult"></param>
        /// <returns></returns>
        private bool IsSecurityUnknown(YahooAPIResult yahooResult)
        {
            if (yahooResult.Description == @"N/A")
            {
                return(true);
            }

            return(false);
        }
コード例 #3
0
        private string DetermineIfStockOrFund(YahooAPIResult yahooResult)
        {
            if (yahooResult.MarketCapIsNA && yahooResult.PeRatioIsNA && yahooResult.Ticker.Length == 5)
            {
                return("Mutual Fund");
            }

            return("Stock");
        }
コード例 #4
0
        private Security CreateNewSecurity(YahooAPIResult yahooResult)
        {
            var determinedType = DetermineIfStockOrFund(yahooResult);

            if (determinedType == "Stock")
            {
                yahooResult.MarketCap = yahooResult.MarketCap.Substring(0, yahooResult.MarketCap.Length - 1);
                var newStock = new Stock(yahooResult);
                return(newStock);
            }

            if (determinedType == "Mutual Fund")
            {
                var newFund = new MutualFund(yahooResult);
                return(newFund);
            }

            return(new Security("", "N/A", "Unknown Ticker", 0, 0.00));
        }
コード例 #5
0
        /// <summary>
        /// Lookup ticker and refer to securityDBList for info if needed
        /// </summary>
        /// <param name="tickerToLookUp"></param>
        /// <param name="securityDBList"></param>
        /// <returns></returns>
        public Security GetSingleSecurity(string tickerToLookUp, List <Security> securityDBList)
        {
            Security securityBeingUpdated;

            var yahooRequestUrl = _baseUrl.Replace("@", tickerToLookUp);
            var response        = GetWebResponse(yahooRequestUrl);
            var yahooResult     = new YahooAPIResult(response);

            //Ticker is unknown, return blank security.
            if (IsSecurityUnknown(yahooResult))
            {
                return(new Security("", @"N/A", @"Unknown Ticker", 0, 0));
            }

            //If security database doesn't know the ticker, add it, then return.
            if (!securityDBList.Any(s => s.Ticker == tickerToLookUp))
            {
                Security newSecurity = CreateNewSecurity(yahooResult);
                securityDBList.Add(newSecurity);
                return(newSecurity);
            }

            //If security is known, update its pricing.
            var resultSecurity = securityDBList.Find(s => s.Ticker == yahooResult.Ticker);

            if (resultSecurity is Stock)
            {
                securityBeingUpdated = (Stock)securityDBList.Find(s => s.Ticker == yahooResult.Ticker);
            }
            else
            {
                securityBeingUpdated = (MutualFund)securityDBList.Find(s => s.Ticker == yahooResult.Ticker);
            }

            //If securityBeingUpdated is not null then it is known to the DB
            //and its info should be updated.
            if (securityBeingUpdated != null && resultSecurity is Stock)
            {
                //Fix properties is they came back as NA
                if (yahooResult.PeRatioIsNA)
                {
                    yahooResult.PeRatio = 0;
                }
                if (yahooResult.YieldIsNA)
                {
                    yahooResult.Yield = 0;
                }
                if (yahooResult.LastPriceIsNA)
                {
                    yahooResult.LastPrice = 0;
                }
                yahooResult.MarketCap = yahooResult.MarketCap.Substring(0, yahooResult.MarketCap.Length - 1);

                var stockBeingUpdated = (Stock)securityBeingUpdated;
                stockBeingUpdated.UpdateData(yahooResult);

                return(stockBeingUpdated);
            }
            else if (securityBeingUpdated != null && resultSecurity is MutualFund)
            {
                var updatedFund = (MutualFund)securityDBList.Find(s => s.Ticker == yahooResult.Ticker);
                updatedFund.UpdateData(yahooResult);
                return(updatedFund);
            }
            else
            {
                throw new NotImplementedException();
            }
        }
コード例 #6
0
 /// <summary>
 /// Determine based on N/A results if a yahoo result is not known
 /// Bid/Ask and BidSize/AskSize can be N/A after market hours
 /// so they are not used in this determination.
 /// </summary>
 /// <param name="yahooResult"></param>
 /// <returns></returns>
 private bool IsSecurityUnknown(YahooAPIResult yahooResult)
 {
     return(yahooResult.Description == @"N/A");
 }