This tutorial shows how to link your application to Yahoo Finance API via Yahoo! Query Language (YQL). Yahoo has lots of stock market data, and you can get full access to it by following this tutorial!

What’s YQL?

YQL stands for Yahoo! Query Language and like SQL it’s a nice way to select data from a repository. Yahoo! describes it as:

The Yahoo! Query Language is an expressive SQL-like language that lets you query, filter, and join data across Web services. With YQL, apps run faster with fewer lines of code and a smaller network footprint.Yahoo! and other websites across the Internet make much of their structured data available to developers, primarily through Web services. To access and query these services, developers traditionally endure the pain of locating the right URLs and documentation to access and query each Web service.With YQL, developers can access and shape data across the Internet through one simple language, eliminating the need to learn how to call different APIs.

Yahoo Finance

Yahoo has quite a nice set of stock data on Yahoo Finance. If you go look at a quote such as AAPL (Apple) you can see all the data that is available.  And the great news is that all that data is available via the Yahoo Finance API: YQL. (well ok YQL is great for many things but this one is sure nice.)

YQL 101

When you write a YQL statement it looks similiar to this:

select * from yahoo.finance.stocks where symbol=”aapl”

That query gets all the stock information for Apple (AAPL).

YQL works through a REST API. You pass the REST API your query and other optional parameters such as the format to return your query in: XML or JSON.

So the full URL looks like so:

You can see that the query is passed in there as the QueryString “q” and the query is HTML encoded.  XML is the default format so I didn’t specify it, but you could add &format=json to get JSON data if you prefer it to XML.

Lets cut some C# code!

The full source code for a c# stock application I call CardStock is available, so grab it and follow along!

Download the Source Code

YQL does all the hard work for us, so the app isn’t complicated and very easy to follow.  The most interesting part is the YahooStockEngine class. This class calls the Yahoo Finance API (YQL) and gets the result:

YahooStockEngine C# Class

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

using System;

using System.Collections.ObjectModel;

using System.Linq;

using System.Xml.Linq;

using Jarloo.CardStock.Models;

namespace Jarloo.CardStock.Helpers

{

public class YahooStockEngine

{

"select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20({0})" +

"&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";

public static void Fetch(ObservableCollection quotes)

{

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 quotes, XDocument doc)

{

XElement results = doc.Root.Element("results");

foreach (Quote 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").Value + " " + 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;

}

}

}

The Quote class shown in the above code is the Model or state-bag.  It is just a container for the data that implements INotifyPropertyChanged so it can be bound to the UI easily.

More Yahoo Stock API and YQL Info

This tutorial doesn’t really do YQL justice, it’s an amazing tool that seems to have gone rather unnoticed by the development community.  Whats interesting to note is that the YQL table were using in this query was created by other developers like you and not Yahoo!  You can make your own queries for a wide variety of things.

Yahoo has some great info on YQL on the Yahoo Developer Network.  Go check out the console and be sure to check “Show community tables”, it’s a small link on the right sidebar, checking that will let you see so much more.

If you liked this tutorial please leave a comment to let me know.

matlab 获取雅虎数据,Get Yahoo Finance API Data via YQL,通过YQL获取雅虎财经API数据 - 小众知识...相关推荐

  1. 如何使用 Yahoo! Finance stock API 获取股票数据

    本站曾介绍过,通过代码添加雅虎财经的股票走势图到自己网站的方法(添加美国股市 ,添加沪深股市 ),调用的是一张图片.今天在德馨 网站,看到了从雅虎财经频道获取股票数据的API(Yahoo! Finan ...

  2. 使用Yahoo API获取雅虎的证券股票数据接口(时价 K线等)

    从网上搜索到的Yahoo 财经API接口,基本可以取到世界各个市场的股票,指数,外汇等数据 但是测试下来好像日本国内数据没有,不过调查之后发现可以使用其他方式获取,获取的方法会在随后的博文再附上. 1 ...

  3. 关于使用pandas-datareader获取Yahoo Finance数据失败的问题。(RemoteDataError)

    python 学习日志 设备: MacBook Air 开发环境:Jupyter 6.1.4 问题:关于使用pandas-datareader获取Yahoo Finance数据失败的问题. Troub ...

  4. 用matplotlib获取雅虎股票数据并作图

    matplotlib有一个finance子模块提供了一个获取雅虎股票数据的api接口:quotes_historical_yahoo_ochl 感觉非常好用! 示例一 获取数据并作折线图 import ...

  5. 用matplotlib获取雅虎股票数据并作图【转载】

    原文链接:http://www.cnblogs.com/hhh5460/p/5120079.html matplotlib有一个finance子模块提供了一个获取雅虎股票数据的api接口:quotes ...

  6. wcf afterreceiverequest获取body数据_阿里面试官的灵魂拷问:究竟如何保证API接口数据安全?...

    前言 前后端分离的开发方式,我们以接口为标准来进行推动,定义好接口,各自开发自己的功能,最后进行联调整合.无论是开发原生的APP还是webapp还是PC端的软件,只要是前后端分离的模式,就避免不了调用 ...

  7. 获取各大电商平台,item_get_app - 获得淘宝app商品详情原数据API返回数据说明

    今天跟大家分享的是item_get_app - 获得淘宝app商品详情原数据API返回数据说明 item_get_app  获得淘宝商品详情 [查看演示] API测试工具 注册链接(获取Key和sec ...

  8. 电商API数据采集,教你如何获取商品详情数据

    电商数据采集的网页抓取数据.淘宝.天 猫.京东等平台的电商数据抓取,网页爬虫.采集网站数据.网页数据采集软件.python爬虫.HTM网页提取.APP数据抓包.APP数据采集.一站式网站采集技术.BI ...

  9. 腾讯股票接口API(3)——根据股票代码获取分时数据

    腾讯股票API相关章节 腾讯股票接口API(1)--根据股票代码获取详情 腾讯股票接口API(2)--根据股票代码获取K线数据 腾讯股票接口API(3)--根据股票代码获取分时数据 腾讯股票接口API ...

最新文章

  1. monkey命令_何小伟:Monkey与MonkeyRunner区别
  2. LPC55S69 IoT Kit专属 Micropython模组和库函数简介
  3. socket编码问题
  4. Python:Sklearn概述
  5. Leetcode905.Sort Array By Parity按奇偶排序数组
  6. 两阶段最小二乘法原理_两阶段最小二乘法.PPT
  7. sql示例_SQL Server Lead功能概述和示例
  8. Python+selenium+eclipse执行web自动化(四)控件处理
  9. Java并发(基础知识)—— 创建、运行以及停止一个线程
  10. 中国联通沃支付echop支付插件
  11. 硬盘助手写入文件的正确提取
  12. html修改鼠标手势,css要怎么设置鼠标手势?
  13. win7计算机屏幕休眠,windows7系统怎么设置屏幕不休眠
  14. 坦克世界怎么显示服务器准心,坦克世界设置方法 坦克世界如何设置图像
  15. 微信订阅消息模板消息推送-JAVA
  16. 备战数学建模16-相关性分析SPSSMATLAB
  17. mantis 邮件配置 linux,mantis安装与配置(Windows+Mysql+PHP+IIS)
  18. 写字机器人制作教程 midt-bot
  19. 首批企业入驻“一县一店”:多元化方式助力农产外销
  20. QT学习笔记(一)之本地播放器

热门文章

  1. 《土力学与地基基础(二)》在线平时作业1
  2. mac下给文件夹授权 增加权限
  3. pycharm每次新建项目都会创建虚拟环境问题,导致很多库安装后无法导入
  4. BGP专线 解决南北互联互通
  5. QQ浏览器使用infinity新标签
  6. 使用HTML5/CSS3五步快速制作便签贴特效
  7. 旭日X3派更新最小启动固件
  8. 【流媒体】视频点播流媒体服务器调研
  9. Z-Stack3.0协议栈组网
  10. pfam的使用-自用