Monday, 28 May 2012

How To fetch Yahoo Finanacial data using YQL

Compare to Google,  yahoo provides more basic analysis data with 15 min delay input.One question that the YQL(Yahoo query language)and Pipes teams get asked is “how can I get stock quotes? There isn’t an API for it on developer.yahoo.com”. Interestingly, while there isn’t a more traditional web service API, Yahoo finance does provide a very nice way to get a lot of well structured information on a given company. Following is the sample YQL uel for fetching the Stock data fro "ACC"


url : http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20("ACC")&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys



<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:count="1" yahoo:created="2012-05-28T11:02:11Z" yahoo:lang="en-US">
<results>
<quote symbol="ACC">
<Ask/>
<AverageDailyVolume>715119</AverageDailyVolume>
<Bid/>
<AskRealtime>44.85</AskRealtime>
<BidRealtime>43.03</BidRealtime>
<BookValue>19.326</BookValue>
<Change_PercentChange>-0.09 - -0.20%</Change_PercentChange>
<Change>-0.09</Change>
<Commission/>
<ChangeRealtime>-0.09</ChangeRealtime>
<AfterHoursChangeRealtime>N/A - N/A</AfterHoursChangeRealtime>
<DividendShare>1.352</DividendShare>
<LastTradeDate>5/25/2012</LastTradeDate>
<TradeDate/>
<EarningsShare>0.814</EarningsShare>
<ErrorIndicationreturnedforsymbolchangedinvalid/>
<EPSEstimateCurrentYear>2.02</EPSEstimateCurrentYear>
<EPSEstimateNextYear>2.26</EPSEstimateNextYear>
<EPSEstimateNextQuarter>0.41</EPSEstimateNextQuarter>
<DaysLow>43.7001</DaysLow>
<DaysHigh>44.41</DaysHigh>
<YearLow>32.57</YearLow>
<YearHigh>45.75</YearHigh>
<HoldingsGainPercent>- - -</HoldingsGainPercent>
<AnnualizedGain/>
<HoldingsGain/>
<HoldingsGainPercentRealtime>N/A - N/A</HoldingsGainPercentRealtime>
<HoldingsGainRealtime/>
<MoreInfo>cnprmiIed</MoreInfo>
<OrderBookRealtime/>
<MarketCapitalization>3.285B</MarketCapitalization>
<MarketCapRealtime/>
<EBITDA>191.6M</EBITDA>
<ChangeFromYearLow>+11.40</ChangeFromYearLow>
<PercentChangeFromYearLow>+35.00%</PercentChangeFromYearLow>
<LastTradeRealtimeWithTime>N/A - <b>43.97</b></LastTradeRealtimeWithTime>
<ChangePercentRealtime>N/A - -0.20%</ChangePercentRealtime>
<ChangeFromYearHigh>-1.78</ChangeFromYearHigh>
<PercebtChangeFromYearHigh>-3.89%</PercebtChangeFromYearHigh>
<LastTradeWithTime>May 25 - <b>43.97</b></LastTradeWithTime>
<LastTradePriceOnly>43.97</LastTradePriceOnly>
<HighLimit/>
<LowLimit/>
<DaysRange>43.7001 - 44.41</DaysRange>
<DaysRangeRealtime>N/A - N/A</DaysRangeRealtime>
<FiftydayMovingAverage>44.132</FiftydayMovingAverage>
<TwoHundreddayMovingAverage>41.422</TwoHundreddayMovingAverage>
<ChangeFromTwoHundreddayMovingAverage>+2.548</ChangeFromTwoHundreddayMovingAverage>
<PercentChangeFromTwoHundreddayMovingAverage>+6.15%</PercentChangeFromTwoHundreddayMovingAverage>
<ChangeFromFiftydayMovingAverage>-0.162</ChangeFromFiftydayMovingAverage>
<PercentChangeFromFiftydayMovingAverage>-0.37%</PercentChangeFromFiftydayMovingAverage>
<Name>American Campus C</Name>
<Notes/>
<Open>44.20</Open>
<PreviousClose>44.06</PreviousClose>
<PricePaid/>
<ChangeinPercent>-0.20%</ChangeinPercent>
<PriceSales>8.17</PriceSales>
<PriceBook>2.28</PriceBook>
<ExDividendDate>Feb 9</ExDividendDate>
<PERatio>54.13</PERatio>
<DividendPayDate>May 29</DividendPayDate>
<PERatioRealtime/>
<PEGRatio>2.25</PEGRatio>
<PriceEPSEstimateCurrentYear>21.81</PriceEPSEstimateCurrentYear>
<PriceEPSEstimateNextYear>19.50</PriceEPSEstimateNextYear>
<Symbol>ACC</Symbol>
<SharesOwned/>
<ShortRatio>5.30</ShortRatio>
<LastTradeTime>4:00pm</LastTradeTime>
<TickerTrend>&nbsp;=-+==+&nbsp;</TickerTrend>
<OneyrTargetPrice>45.45</OneyrTargetPrice>
<Volume>313271</Volume>
<HoldingsValue/>
<HoldingsValueRealtime/>
<YearRange>32.57 - 45.75</YearRange>
<DaysValueChange>- - -0.20%</DaysValueChange>
<DaysValueChangeRealtime>N/A - N/A</DaysValueChangeRealtime>
<StockExchange>NYSE</StockExchange>
<DividendYield>3.07</DividendYield>
<PercentChange>-0.20%</PercentChange>
</quote>
</results>
</query>
<!-- total: 985 -->
<!-- engine1.yql.ird.yahoo.com -->



C# Code for format the URL:

 public static void Fetch()
        {



// quotes list contains the symbol list...
            string symbolList = String.Join("%2C", quotes.Select(w => "%22" + w.Symbol + "%22").ToArray());
            string url = string.Format(BASE_URL,symbolList);
            
            XDocument doc = XDocument.Load(url);
            Parse(quotes,doc);
        }


        private static void Parse(ObservableCollection<YahooQuote> quotes, XDocument doc)
        {
            XElement results = doc.Root.Element("results");

            foreach (YahooQuote quote in quotes)
            {
                XElement q = results.Elements("quote").First(w => w.Attribute("symbol").Value == quote.Symbol);

                quote.Ask = GetDecimal(q.Element("Ask").Value);
                quote.Bid = GetDecimal(q.Element("Bid").Value);
                quote.AverageDailyVolume = GetDecimal(q.Element("AverageDailyVolume").Value);
                quote.BookValue = GetDecimal(q.Element("BookValue").Value);
                quote.Change = GetDecimal(q.Element("Change").Value);
                quote.DividendShare = GetDecimal(q.Element("DividendShare").Value);
                quote.LastTradeDate = GetDateTime(q.Element("LastTradeDate") + " " + q.Element("LastTradeTime").Value);
                quote.EarningsShare = GetDecimal(q.Element("EarningsShare").Value);
                quote.EpsEstimateCurrentYear = GetDecimal(q.Element("EPSEstimateCurrentYear").Value);
                quote.EpsEstimateNextYear = GetDecimal(q.Element("EPSEstimateNextYear").Value);
                quote.EpsEstimateNextQuarter = GetDecimal(q.Element("EPSEstimateNextQuarter").Value);
                quote.DailyLow = GetDecimal(q.Element("DaysLow").Value);
                quote.DailyHigh = GetDecimal(q.Element("DaysHigh").Value);
                quote.YearlyLow = GetDecimal(q.Element("YearLow").Value);
                quote.YearlyHigh = GetDecimal(q.Element("YearHigh").Value);
                quote.MarketCapitalization = GetDecimal(q.Element("MarketCapitalization").Value);
                quote.Ebitda = GetDecimal(q.Element("EBITDA").Value);
                quote.ChangeFromYearLow = GetDecimal(q.Element("ChangeFromYearLow").Value);
                quote.PercentChangeFromYearLow = GetDecimal(q.Element("PercentChangeFromYearLow").Value);
                quote.ChangeFromYearHigh = GetDecimal(q.Element("ChangeFromYearHigh").Value);
                quote.LastTradePrice = GetDecimal(q.Element("LastTradePriceOnly").Value);
                quote.PercentChangeFromYearHigh = GetDecimal(q.Element("PercebtChangeFromYearHigh").Value); //missspelling in yahoo for field name
                quote.FiftyDayMovingAverage = GetDecimal(q.Element("FiftydayMovingAverage").Value);
                quote.TwoHunderedDayMovingAverage = GetDecimal(q.Element("TwoHundreddayMovingAverage").Value);
                quote.ChangeFromTwoHundredDayMovingAverage = GetDecimal(q.Element("ChangeFromTwoHundreddayMovingAverage").Value);
                quote.PercentChangeFromTwoHundredDayMovingAverage = GetDecimal(q.Element("PercentChangeFromTwoHundreddayMovingAverage").Value);
                quote.PercentChangeFromFiftyDayMovingAverage = GetDecimal(q.Element("PercentChangeFromFiftydayMovingAverage").Value);
                quote.Name = q.Element("Name").Value;
                quote.Open = GetDecimal(q.Element("Open").Value);
                quote.PreviousClose = GetDecimal(q.Element("PreviousClose").Value);
                quote.ChangeInPercent = GetDecimal(q.Element("ChangeinPercent").Value);
                quote.PriceSales = GetDecimal(q.Element("PriceSales").Value);
                quote.PriceBook = GetDecimal(q.Element("PriceBook").Value);
                quote.ExDividendDate = GetDateTime(q.Element("ExDividendDate").Value);
                quote.PeRatio = GetDecimal(q.Element("PERatio").Value);
                quote.DividendPayDate = GetDateTime(q.Element("DividendPayDate").Value);
                quote.PegRatio = GetDecimal(q.Element("PEGRatio").Value);
                quote.PriceEpsEstimateCurrentYear = GetDecimal(q.Element("PriceEPSEstimateCurrentYear").Value);
                quote.PriceEpsEstimateNextYear = GetDecimal(q.Element("PriceEPSEstimateNextYear").Value);
                quote.ShortRatio = GetDecimal(q.Element("ShortRatio").Value);
                quote.OneYearPriceTarget = GetDecimal(q.Element("OneyrTargetPrice").Value);
                quote.Volume = GetDecimal(q.Element("Volume").Value);
                quote.StockExchange = q.Element("StockExchange").Value;

                quote.LastUpdate = DateTime.Now;
            }
        }

        private static decimal? GetDecimal(string input)
        {
            if (input == null) return null;

            input = input.Replace("%", "");

            decimal value;

            if (Decimal.TryParse(input, out value)) return value;
            return null;
        }

        private static DateTime? GetDateTime(string input)
        {
            if (input == null) return null;

            DateTime value;

            if (DateTime.TryParse(input, out value)) return value;
            return null;
        }



0 comments: