作者:黄天元,复旦大学博士在读,目前研究涉及文本挖掘、社交网络分析和机器学习等。希望与大家分享学习经验,推广并加深R语言在业界的应用。
邮箱:huang.tian-yuan@qq.com
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我们需要进一步解释
原文如下:
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次分红。
不过对公共数据的请求有时候要依赖网络,如果失败了,请再试一次。这个工具对国外网络的稳定性具有很强的依赖性,主要是从雅虎金融网址直接抓取的数据。
——————————————
往期精彩: