ITPub博客

首页 > > > R语言金融基础:tidyquant获取数据(股票每日行情)

R语言金融基础:tidyquant获取数据(股票每日行情)

作者:浪花小雨滴 时间:2019-05-03 14:29:26 0 删除 编辑

作者:黄天元,复旦大学博士在读,热爱数据科学与开源工具(R/Python),致力于利用数据科学迅速积累行业经验优势和科学知识发现,涉猎内容包括但不限于信息计量、机器学习、数据可视化、应用统计建模、知识图谱等。知乎专栏:R语言数据挖掘邮箱:huang.tian-yuan@qq.com.欢迎合作交流。

前文:

HopeR:R语言金融基础:tidyquant获取数据(标普500与纳斯达克)


tidyquant除了能够获得大市的信息以外,还能够对个股行情进行采集。以苹果公司为例:

library(tidyquant)
aapl_prices  <- tq_get("AAPL", get = "stock.prices", from = " 1990-01-01")
aapl_prices 

# A tibble: 7,391 x 7
   date        open  high   low close   volume adjusted
   <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
 1 1990-01-02  1.26  1.34  1.25  1.33 45799600    0.122
 2 1990-01-03  1.36  1.36  1.34  1.34 51998800    0.123
 3 1990-01-04  1.37  1.38  1.33  1.34 55378400    0.123
 4 1990-01-05  1.35  1.37  1.32  1.35 30828000    0.124
 5 1990-01-08  1.34  1.36  1.32  1.36 25393200    0.124
 6 1990-01-09  1.36  1.36  1.32  1.34 21534800    0.123
 7 1990-01-10  1.34  1.34  1.28  1.29 49929600    0.118
 8 1990-01-11  1.29  1.29  1.23  1.23 52763200    0.113
 9 1990-01-12  1.22  1.24  1.21  1.23 42974400    0.113
10 1990-01-15  1.23  1.28  1.22  1.22 40434800    0.112
# ... with 7,381 more rows

只要输入股票代码APPL,并且注明需要获取的是“stock.prices”股价信息,即可获得自1990年1月1日以来苹果股票价格的变化(可自定义调整)。我们可以看到结果里面有日期、开盘价、最高价、最低价、收盘价、成交量和矫正收盘指数(adjusted)。很多指标都很好理解,但是对于adjusted我们需要进一步解释,参考:

Adjusted Closing Price vs. Closing Price​budgeting.thenest.com图标

原文如下:

If you're a new investor, you may have heard the terms "closing price" and "adjusted closing price." These two terms refer to slightly different ways of valuing stocks. The closing price of a stock is the price of that stock at the close of the trading day. The adjusted closing price is a more complex analysis that uses the closing price as a starting point, but it takes into account factors such as dividends, stock splits and new stock offerings to determine a value. The adjusted closing price represents a more accurate reflection of a stock's value, since distributions and new offerings can alter the closing price.

也就是说,矫正收盘价是考虑了股息、分股和增股之后,股票收盘时候价格的变化。tidyquant也支持查询股息和分股信息:

# 查看股息
aapl_divs <- tq_get("AAPL", get = "dividends", from = "1990-01-01")
aapl_divs

# A tibble: 51 x 2
   date       dividends
   <date>         <dbl>
 1 1990-02-16      0.11
 2 1990-05-21      0.11
 3 1990-08-20      0.11
 4 1990-11-16      0.12
 5 1991-02-15      0.12
 6 1991-05-20      0.12
 7 1991-08-19      0.12
 8 1991-11-18      0.12
 9 1992-02-14      0.12
10 1992-06-01      0.12
# ... with 41 more rows

# 查看分股
aapl_splits <- tq_get("AAPL", get = "splits", from = "1990-01-01")
aapl_splits

# A tibble: 3 x 2
  date       splits
  <date>      <dbl>
1 2000-06-21      2
2 2005-02-28      2
3 2014-06-09      7

也就是说,自1990年以来,苹果公司在2000、2005和2014年进行了分股,以增加其流动性。与此同时,苹果在这个时间段内进行了51次分红。如果对分红(dividend)和分股(split)概念不够熟悉,可参考:

Stock Dividend​www.investopedia.com图标Stock Split Definition​www.investopedia.com图标

不过对公共数据的请求有时候要依赖网络,如果失败了,请再试一次。这个工具对国外网络的稳定性具有很强的依赖性,主要是从雅虎金融网址直接抓取的数据。

原文作者:;如有侵权,请联系管理员删除。

请登录后发表评论 登录
全部评论

注册时间:2018-05-16

  • 博文量
    4259
  • 访问量
    39892